Exemplo n.º 1
0
static PyObject *somap(PyObject *self, PyObject *args)
{
    int ninputs, nsteps, nrows, ncols, depth, width, height, i, j, k;
    PyArrayObject *inputs_obj, *spread_obj;
    PyObject *somap_obj;
    double **inputs, **spread, ***somap;
    time_t t0, t1;
    npy_intp dims[3];

    if (!PyArg_ParseTuple(args, "O!O!iii", &PyArray_Type, &inputs_obj, &PyArray_Type, &spread_obj, &nrows, &ncols, &nsteps))
	RETURN_ERROR("need five arguments: inputs, spread, nrows, ncols, nsteps");

    if (!PyArray_Check(inputs_obj))
	RETURN_ERROR("inputs needs to be NumPy array");

    if (PyArray_NDIM(inputs_obj) != 2)
	RETURN_ERROR("inputs needs to be NumPy array with ndim 2");

    if (!PyArray_Check(spread_obj))
	RETURN_ERROR("spread needs to be NumPy array");

    if (PyArray_NDIM(spread_obj) != 2)
	RETURN_ERROR("spread needs to be NumPy array with ndim 2");

    if (PyArray_TYPE(inputs_obj) != NPY_DOUBLE)
	RETURN_ERROR("inputs needs to be array of doubles");

    if (PyArray_TYPE(spread_obj) != NPY_DOUBLE)
	RETURN_ERROR("spread needs to be array of doubles");

    ninputs = PyArray_DIM(inputs_obj, 0);
    depth = PyArray_DIM(inputs_obj, 1);

    width = PyArray_DIM(spread_obj, 0);
    height = PyArray_DIM(spread_obj, 1);

    //if (PyArray_AsCArray((PyObject**) &inputs_obj, (void *) &inputs, PyArray_DIMS(inputs_obj), PyArray_NDIM(inputs_obj), PyArray_DescrFromType(NPY_DOUBLE)) < 0)
    if (!(inputs = copyArray2(inputs_obj)))
	RETURN_ERROR("getting inputs as C array");

    //if (PyArray_AsCArray((PyObject**) &spread_obj, (void *) &spread, PyArray_DIMS(spread_obj), PyArray_NDIM(spread_obj), PyArray_DescrFromType(NPY_DOUBLE)) < 0)
    if (!(spread = copyArray2(spread_obj)))
    {
    	//PyArray_Free((PyObject*) inputs_obj, (void *) inputs);
        freeArray2(inputs, ninputs);
	RETURN_ERROR("getting spread as C array");
    }

    t0 = time(NULL);

    somap = selfOrganisingMap(inputs, ninputs, depth, nrows, ncols, spread, width, height, nsteps);

    t1 = time(NULL);

    printf("Time for just somap = %ld\n", t1-t0);

    //somap_obj = PyArray_NewFromDescr(&PyArray_Type, PyArray_DescrFromType(NPY_DOUBLE), PyArray_NDIM(inputs_obj), PyArray_DIMS(inputs_obj), NULL, (void *) somap, 0, NULL);
    // below does not work because data not contiguous
    //somap_obj = PyArray_SimpleNewFromData(PyArray_NDIM(inputs_obj), PyArray_DIMS(inputs_obj), NPY_DOUBLE, (void *) somap);
    dims[0] = nrows;
    dims[1] = ncols;
    dims[2] = depth;
    somap_obj = PyArray_SimpleNew(3, dims, NPY_DOUBLE);
    for (i = 0; i < nrows; i++)
      for (j = 0; j < ncols; j++)
        for (k = 0; k < depth; k++)
          *((double *) PyArray_GETPTR3(somap_obj, i, j, k)) = somap[i][j][k];

    //PyArray_Free((PyObject*) inputs_obj, (void *) inputs);
    //PyArray_Free((PyObject*) spread_obj, (void *) spread);
    freeArray3(somap, nrows, ncols);
    freeArray2(inputs, ninputs);
    freeArray2(spread, width);

    return somap_obj;
}
Exemplo n.º 2
0
void* PyWrArray_GetPtr3(PyObject* obj, size_t i, size_t j, size_t k)
{
	return PyArray_GETPTR3(obj, (npy_intp)i, (npy_intp)j, (npy_intp)k);
}
Exemplo n.º 3
0
static PyObject* gist_extract(PyObject *self, PyObject *args)
{
	int nblocks=4;
	int n_scale=3;
	int orientations_per_scale[50]={8,8,4};
	PyArrayObject *image, *descriptor;

	if (!PyArg_ParseTuple(args, "O", &image))
	{
		return NULL;
	}

	if (PyArray_TYPE(image) != NPY_UINT8) {
		PyErr_SetString(PyExc_TypeError, "type of image must be uint8");
		return NULL;
	}

	if (PyArray_NDIM(image) != 3) {
		PyErr_SetString(PyExc_TypeError, "dimensions of image must be 3.");
		return NULL;
	}

	npy_intp *dims_image = PyArray_DIMS(image);


	const int w = (int) *(dims_image+1);
	const int h = (int) *(dims_image);

	// Read image to color_image_t structure
	color_image_t *im=color_image_new(w,h);

	for (int y=0, i=0 ; y<h ; ++y) {
		for (int x=0 ; x<w ; ++x, ++i) {
			im->c1[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 0);
			im->c2[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 1);
			im->c3[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 2);
		}
	}

	// Extract descriptor
	float *desc=color_gist_scaletab(im,nblocks,n_scale,orientations_per_scale);

	int descsize=0;
	/* compute descriptor size */
	for(int i=0;i<n_scale;i++)
		descsize+=nblocks*nblocks*orientations_per_scale[i];

	descsize*=3; /* color */


	// Allocate output
	npy_intp dim_desc[1] = {descsize};
	descriptor = (PyArrayObject *) PyArray_SimpleNew(1, dim_desc, NPY_FLOAT);

	// Set val
	for (int i=0 ; i<descsize ; ++i) {
		*(float *)PyArray_GETPTR1(descriptor, i) = desc[i];
	}

	// Release memory
	color_image_delete(im);
	free(desc);

	return PyArray_Return(descriptor);
}
Exemplo n.º 4
0
static PyObject *fourier_sphere(PyObject *self, PyObject *args)
{
  /*
    Computes Fourier Transform of an ellipsoid:

      fourier_sphere((Nx,Ny,Nz), (Dx, Dy, Dz), pcount) -> array

      Nx, Ny, Nz - defines the shape of an output array
      Dx, Dy, Dz - defines the diameters of the ellipsoid in index units
      pcount - defines periodicity parameter of the algorithm, the higher
        pcount is, the more accurate is the result but the more time it
	takes to compute the result. pcount should be around 1000.

    References:
      http://dx.doi.org/10.3247/SL2Math07.002
   */
  npy_intp dims[] = {0, 0, 0};
  double r[] = {1.0, 1.0, 1.0};
  double a, b;
  double ir, jr, kr;
  double sn, cs;
  int i,j,k,n1,n2,n3,nx,ny,nz;
  int n1_end, n2_end, n3_end;
  int dn1, dn2, dn3;
  int total, count, pcount;
  PyObject* result = NULL;
  double eps, inveps;
  clock_t start_clock = clock();
  double eta;
  PyObject* write_func = NULL;
  int verbose;
  if (!PyArg_ParseTuple(args, "(iii)(ddd)dO", &nx, &ny, &nz, r, r+1, r+2, &eps, &write_func))
    return NULL;
  if (write_func == Py_None)
    verbose = 0;
  else if (PyCallable_Check(write_func))
    verbose = 1;
  else
    {
      PyErr_SetString(PyExc_TypeError,"eighth argument must be None or callable object");
      return NULL;
    }
  dims[0] = nx;
  dims[1] = ny;
  dims[2] = nz;
  dn1 = nx;
  dn2 = ny;
  dn3 = nz;

#define min2(a,b) ((a>b)?(b):(a))
#define min3(a,b,c) min2((a), min2((b),(c)))
#define CALL_WRITE(ARGS) \
  if (verbose && PyObject_CallFunctionObjArgs ARGS == NULL) return NULL;

  r[0] /= dims[0];
  r[1] /= dims[1];
  r[2] /= dims[2];

  if (eps>1.0) // eps specifies peridicity count
    {
      eps = 2e-4 * pow(eps,-2.0/3.0) / min3(r[0]*r[0],r[1]*r[1],r[2]*r[2]);
      CALL_WRITE((write_func,PyString_FromString("fourier_sphere: using eps = %.5e\n\n"), PyFloat_FromDouble(eps), NULL));
    }
  result = PyArray_SimpleNew(3, dims, PyArray_FLOAT64);
  r[0] *= M_PI;
  r[1] *= M_PI;
  r[2] *= M_PI;
  total = (dn1/2+1) * (dn2/2+1) * (dn3/2+1);
  count = 0;
  pcount = 0;
  inveps = 3.0/eps;
  nx = 1 + sqrt(inveps)/(dn1*r[0]);
  nx *= dn1;

  for (i=0; i<dn1/2+1; ++i)
    {
      for (j=0; j<dn2/2+1; ++j)
	{
	  for (k=0; k<dn3/2+1; ++k)
	    {
	      a = 0.0;
	      pcount = 0;
	      n1_end = i + nx;
	      for (n1=i-nx; n1<=n1_end; n1 += dn1)
		{
		  ir = n1*r[0];
		  ir *= ir;
		  if (ir>inveps) continue;
		  ny = 1 + sqrt(inveps-ir)/(dn2*r[1]);
		  ny *= dn2;
		  n2_end = j + ny;
		  for (n2=j-ny; n2<=n2_end; n2 += dn2)
		    {
		      jr = n2*r[1];
		      jr = jr*jr + ir;
		      if (jr>inveps) continue;
		      nz = (1 + sqrt(inveps-jr)/(dn3*r[2]));
		      nz *= dn3;
		      n3_end = k + nz;
		      for (n3=k-nz; n3<=n3_end; n3 += dn3)
			{
			  kr = n3*r[2];
			  kr = kr*kr + jr;
			  if (kr>inveps) continue;
			  if (kr==0.0)
			    a += 1.0;
			  else
			    {
			      b = sqrt(kr);
			      sincos(b, &sn, &cs);
			      a += 3.0 * (sn/b - cs)/kr;
			    }
			  pcount++;
			}
		    }
		}
	      *((npy_float64*)PyArray_GETPTR3(result, i, j, k)) = a;
	      if (i)
		*((npy_float64*)PyArray_GETPTR3(result, dn1-i, j, k)) = a;
	      if (j)
		*((npy_float64*)PyArray_GETPTR3(result, i, dn2-j, k)) = a;
	      if (k)
		*((npy_float64*)PyArray_GETPTR3(result, i, j, dn3-k)) = a;
	      if (i && j)
		*((npy_float64*)PyArray_GETPTR3(result, dn1-i, dn2-j, k)) = a;
	      if (i && k)
		*((npy_float64*)PyArray_GETPTR3(result, dn1-i, j, dn3-k)) = a;
	      if (j && k)
		*((npy_float64*)PyArray_GETPTR3(result, i, dn2-j, dn3-k)) = a;
	      if (i && j && k)
		*((npy_float64*)PyArray_GETPTR3(result, dn1-i, dn2-j, dn3-k)) = a;
	      count ++;
	    }
	  eta = (clock() - start_clock) * (total/(count+0.0)-1.0) / CLOCKS_PER_SEC;
	  CALL_WRITE((write_func,  
		      PyString_FromString("\rfourier_sphere: %6.2f%% done (%d), ETA:%4.1fs"),
		      PyFloat_FromDouble((count*100.0)/total),
		      PyInt_FromLong(pcount),
		      PyFloat_FromDouble(eta),
		      NULL));
	}
    }
  *((npy_float64*)PyArray_GETPTR3(result, 0, 0, 0)) = 1.0; // normalize sum(sphere) to 1.0
  CALL_WRITE((write_func, PyString_FromString("\n"), NULL));
  return Py_BuildValue("N", result);
}
Exemplo n.º 5
0
static PyObject *div_unit_grad(PyObject *self, PyObject *args)
{
  PyObject* f = NULL;
  npy_intp Nx, Ny, Nz;
  int i, j, k, im1, im2, ip1, jm1, jm2, jp1, km1, km2, kp1;
  npy_float64* f_data_dp = NULL;
  npy_float64* r_data_dp = NULL;
  npy_float32* f_data_sp = NULL;
  npy_float32* r_data_sp = NULL;
  double hx, hy, hz;
  double hx2, hy2, hz2;
  PyArrayObject* r = NULL;
  double fip, fim, fjp, fjm, fkp, fkm, fijk;
  double fimkm, fipkm, fjmkm, fjpkm, fimjm, fipjm, fimkp, fjmkp, fimjp;
  double aim, bjm, ckm, aijk, bijk, cijk;
  double Dxpf, Dxmf, Dypf, Dymf, Dzpf, Dzmf;
  double Dxma, Dymb, Dzmc;
  if (!PyArg_ParseTuple(args, "O(ddd)", &f, &hx, &hy, &hz))
    return NULL;
  hx2 = 2*hx;  hy2 = 2*hy;  hz2 = 2*hz;
  if (!PyArray_Check(f))
    {
      PyErr_SetString(PyExc_TypeError,"first argument must be array");
      return NULL;
    }
  if (PyArray_NDIM(f) != 3)
    {
      PyErr_SetString(PyExc_TypeError,"array argument must have rank 3");
      return NULL;
    }
  Nx = PyArray_DIM(f, 0);
  Ny = PyArray_DIM(f, 1);
  Nz = PyArray_DIM(f, 2);
  r = (PyArrayObject*)PyArray_SimpleNew(3, PyArray_DIMS(f), PyArray_TYPE(f));

  if (PyArray_TYPE(f) == PyArray_FLOAT32)
    {
      f_data_sp = (npy_float32*)PyArray_DATA(f);
      r_data_sp = (npy_float32*)PyArray_DATA(r);
      for (i=0; i<Nx; ++i)
	{
	  im1 = (i?i-1:0);
	  im2 = (im1?im1-1:0);
      	  ip1 = (i+1==Nx?i:i+1);
	  for (j=0; j<Ny; ++j)
	    {
	      jm1 = (j?j-1:0);
	      jm2 = (jm1?jm1-1:0);
	      jp1 = (j+1==Ny?j:j+1);
	      for (k=0; k<Nz; ++k)
		{
		  km1 = (k?k-1:0);
		  km2 = (km1?km1-1:0);
		  kp1 = (k+1==Nz?k:k+1);

		  fimjm = *((npy_float32*)PyArray_GETPTR3(f, im1, jm1, k));
		  fim = *((npy_float32*)PyArray_GETPTR3(f, im1, j, k));
		  fimkm = *((npy_float32*)PyArray_GETPTR3(f, im1, j, km1));
		  fimkp = *((npy_float32*)PyArray_GETPTR3(f, im1, j, kp1));
		  fimjp = *((npy_float32*)PyArray_GETPTR3(f, im1, jp1, k));

		  fjmkm = *((npy_float32*)PyArray_GETPTR3(f, i, jm1, km1));
		  fjm = *((npy_float32*)PyArray_GETPTR3(f, i, jm1, k));
		  fjmkp = *((npy_float32*)PyArray_GETPTR3(f, i, jm1, kp1));

		  fkm = *((npy_float32*)PyArray_GETPTR3(f, i, j, km1));
		  fijk = *((npy_float32*)PyArray_GETPTR3(f, i, j, k));
		  fkp = *((npy_float32*)PyArray_GETPTR3(f, i, j, kp1));

		  fjpkm = *((npy_float32*)PyArray_GETPTR3(f, i, jp1, km1));
		  fjp = *((npy_float32*)PyArray_GETPTR3(f, i, jp1, k));

		  fipjm = *((npy_float32*)PyArray_GETPTR3(f, ip1, jm1, k));
		  fipkm = *((npy_float32*)PyArray_GETPTR3(f, ip1, j, km1));
		  fip = *((npy_float32*)PyArray_GETPTR3(f, ip1, j, k));

		  Dxpf = (fip - fijk) / hx;
		  Dxmf = (fijk - fim) / hx;
		  Dypf = (fjp - fijk) / hy;
		  Dymf = (fijk - fjm) / hy;
		  Dzpf = (fkp - fijk) / hz;
		  Dzmf = (fijk - fkm) / hz;
		  aijk = hypot3(Dxpf, m(Dypf, Dymf), m(Dzpf, Dzmf));
		  bijk = hypot3(Dypf, m(Dxpf, Dxmf), m(Dzpf, Dzmf));
		  cijk = hypot3(Dzpf, m(Dypf, Dymf), m(Dxpf, Dxmf));

		  aijk = (aijk>FLOAT32_EPS?Dxpf / aijk:0.0);
		  bijk = (bijk>FLOAT32_EPS?Dypf / bijk: 0.0);
		  cijk = (cijk>FLOAT32_EPS?Dzpf / cijk:0.0); 
		  

		  Dxpf = (fijk - fim) / hx;
		  Dypf = (fimjp - fim) / hy;
		  Dymf = (fim - fimjm) / hy;
		  Dzpf = (fimkp - fim) / hz;
		  Dzmf = (fim - fimkm) / hz;
		  aim = hypot3(Dxpf, m(Dypf, Dymf), m(Dzpf, Dzmf));

		  aim = (aim>FLOAT32_EPS?Dxpf/aim:0.0); 


		  Dxpf = (fipjm - fjm) / hx;
		  Dxmf = (fjm - fimjm) / hx;
		  Dypf = (fijk - fjm) / hy;
		  Dzpf = (fjmkp - fjm) / hz;
		  Dzmf = (fjm - fjmkm) / hz;
		  bjm = hypot3(Dypf, m(Dxpf, Dxmf), m(Dzpf, Dzmf));

		  bjm = (bjm>FLOAT32_EPS?Dypf/bjm:0.0);
		  

		  Dxpf = (fipkm - fkm) / hx;
		  Dxmf = (fjm - fimkm) / hx;
		  Dypf = (fjpkm - fkm) / hy;
		  Dymf = (fkm - fjmkm) / hy;
		  Dzpf = (fijk - fkm) / hz;
		  ckm = hypot3(Dzpf, m(Dypf, Dymf), m(Dxpf, Dxmf));

		  ckm = (ckm>FLOAT32_EPS?Dzpf/ckm:0.0); 

		  Dxma = (aijk - aim) / hx;
		  Dymb = (bijk - bjm) / hy;
		  Dzmc = (cijk - ckm) / hz;
		  
		  //*((npy_float32*)PyArray_GETPTR3(r, i, j, k)) = Dxma/hx + Dymb/hy + Dzmc/hz;
		  *((npy_float32*)PyArray_GETPTR3(r, i, j, k)) = Dxma + Dymb + Dzmc;
		}
	    }
	}      
    }
  else if (PyArray_TYPE(f) == PyArray_FLOAT64)
    {
      f_data_dp = (npy_float64*)PyArray_DATA(f);
      r_data_dp = (npy_float64*)PyArray_DATA(r);
      for (i=0; i<Nx; ++i)
	{
	  im1 = (i?i-1:0);
	  im2 = (im1?im1-1:0);
      	  ip1 = (i+1==Nx?i:i+1);
	  for (j=0; j<Ny; ++j)
	    {
	      jm1 = (j?j-1:0);
	      jm2 = (jm1?jm1-1:0);
	      jp1 = (j+1==Ny?j:j+1);
	      for (k=0; k<Nz; ++k)
		{
		  km1 = (k?k-1:0);
		  km2 = (km1?km1-1:0);
		  kp1 = (k+1==Nz?k:k+1);

		  fimjm = *((npy_float64*)PyArray_GETPTR3(f, im1, jm1, k));
		  fim = *((npy_float64*)PyArray_GETPTR3(f, im1, j, k));
		  fimkm = *((npy_float64*)PyArray_GETPTR3(f, im1, j, km1));
		  fimkp = *((npy_float64*)PyArray_GETPTR3(f, im1, j, kp1));
		  fimjp = *((npy_float64*)PyArray_GETPTR3(f, im1, jp1, k));

		  fjmkm = *((npy_float64*)PyArray_GETPTR3(f, i, jm1, km1));
		  fjm = *((npy_float64*)PyArray_GETPTR3(f, i, jm1, k));
		  fjmkp = *((npy_float64*)PyArray_GETPTR3(f, i, jm1, kp1));

		  fkm = *((npy_float64*)PyArray_GETPTR3(f, i, j, km1));
		  fijk = *((npy_float64*)PyArray_GETPTR3(f, i, j, k));
		  fkp = *((npy_float64*)PyArray_GETPTR3(f, i, j, kp1));

		  fjpkm = *((npy_float64*)PyArray_GETPTR3(f, i, jp1, km1));
		  fjp = *((npy_float64*)PyArray_GETPTR3(f, i, jp1, k));

		  fipjm = *((npy_float64*)PyArray_GETPTR3(f, ip1, jm1, k));
		  fipkm = *((npy_float64*)PyArray_GETPTR3(f, ip1, j, km1));
		  fip = *((npy_float64*)PyArray_GETPTR3(f, ip1, j, k));

		  Dxpf = (fip - fijk) / hx;
		  Dxmf = (fijk - fim) / hx;
		  Dypf = (fjp - fijk) / hy;
		  Dymf = (fijk - fjm) / hy;
		  Dzpf = (fkp - fijk) / hz;
		  Dzmf = (fijk - fkm) / hz;
		  aijk = hypot3(Dxpf, m(Dypf, Dymf), m(Dzpf, Dzmf));
		  aijk = (aijk>FLOAT64_EPS?Dxpf / aijk:0.0);
		  bijk = hypot3(Dypf, m(Dxpf, Dxmf), m(Dzpf, Dzmf));
		  bijk = (bijk>FLOAT64_EPS?Dypf / bijk: 0.0);
		  cijk = hypot3(Dzpf, m(Dypf, Dymf), m(Dxpf, Dxmf));
		  cijk = (cijk>FLOAT64_EPS?Dzpf/cijk:0.0);

		  Dxpf = (fijk - fim) / hx;
		  Dypf = (fimjp - fim) / hy;
		  Dymf = (fim - fimjm) / hy;
		  Dzpf = (fimkp - fim) / hz;
		  Dzmf = (fim - fimkm) / hz;
		  aim = hypot3(Dxpf, m(Dypf, Dymf), m(Dzpf, Dzmf));
		  aim = (aim>FLOAT64_EPS?Dxpf/aim:0.0); 

		  Dxpf = (fipjm - fjm) / hx;
		  Dxmf = (fjm - fimjm) / hx;
		  Dypf = (fijk - fjm) / hy;
		  Dzpf = (fjmkp - fjm) / hz;
		  Dzmf = (fjm - fjmkm) / hz;
		  bjm = hypot3(Dypf, m(Dxpf, Dxmf), m(Dzpf, Dzmf));
		  bjm = (bjm>FLOAT64_EPS?Dypf/bjm:0.0);
		  


		  Dxpf = (fipkm - fkm) / hx;
		  Dxmf = (fjm - fimkm) / hx;
		  Dypf = (fjpkm - fkm) / hy;
		  Dymf = (fkm - fjmkm) / hy;
		  Dzpf = (fijk - fkm) / hz;
		  ckm = hypot3(Dzpf, m(Dypf, Dymf), m(Dxpf, Dxmf));
		  ckm = (ckm>FLOAT64_EPS?Dzpf/ckm:0.0); 
		  
		  Dxma = (aijk - aim) / hx;
		  Dymb = (bijk - bjm) / hy;
		  Dzmc = (cijk - ckm) / hz;

		  //*((npy_float64*)PyArray_GETPTR3(r, i, j, k)) = Dxma/hx + Dymb/hy + Dzmc/hz;
		  *((npy_float64*)PyArray_GETPTR3(r, i, j, k)) = Dxma + Dymb + Dzmc;
		}
	    }
	}
    }
  else
    {
      PyErr_SetString(PyExc_TypeError,"array argument type must be float64");
      return NULL;
    }
  return Py_BuildValue("N", r);
}
Exemplo n.º 6
0
 BaseType& at(int p0, int p1, int p2) {
     assert(p0 < this->dim(0));
     assert(p1 < this->dim(1));
     assert(p2 < this->dim(2));
     return *static_cast<BaseType*>(PyArray_GETPTR3(this->array_, p0, p1, p2));
 }