[SciPy-User] Pb with Extending ndimage in C

moscardi eric.moscardi@sophia.inria...
Thu Sep 9 11:06:20 CDT 2010


Hi scipy-users,

I try to use the geometric_transform function from scipy.ndimage with a mapping function written in C in order to accelerate execution time of my code.

For that, I try to use the tutorial " Extending ndimage in C"  (http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html#ndimage-ccallbacks) but I have any difficulties with it.

The given example (which implement a simple shift function) doesn't work : 

###########################################################################
static int
_shift_function(int *output_coordinates, double* input_coordinates,
                int output_rank, int input_rank, void *callback_data)
{
  int ii;
  /* get the shift from the callback data pointer: */
  double shift = *(double*)callback_data;
  /* calculate the coordinates: */
  for(ii = 0; ii < irank; ii++)
    icoor[ii] = ocoor[ii] - shift;
  /* return OK status: */
  return 1;
}
###########################################################################


*****************************************************************************
gcc example.c --shared -lpython -o example.so
Compile error :

example.c: In function ‘_shift_function’:
example.c:12: error: ‘irank’ undeclared (first use in this function)
example.c:12: error: (Each undeclared identifier is reported only once
example.c:12: error: for each function it appears in.)
example.c:13: error: ‘icoor’ undeclared (first use in this function)
example.c:13: error: ‘ocoor’ undeclared (first use in this function)
*****************************************************************************

So I replace :

icoor
ocoor
irank

by :
input_coordinates
output_coordinates
input_rank

So my code is :

###########################################################################
static int 
_shift_function(int *output_coordinates, double* input_coordinates,
                int output_rank, int input_rank, void *callback_data)
{
    int ii; 
    /* get the shift from the callback data pointer: */ 
    double shift = *(double*)callback_data;
    /* calculate the coordinates: */ 
    for(ii = 0; ii < input_rank; ii++)
        input_coordinates[ii] = output_coordinates[ii] - shift; 
    /* return OK status: */ 
    return 1;
}
###########################################################################

I run this code, but the result is not the same as the example : 

>>> a = np.arange(12.).reshape((4, 3))
>>> fnc= example.shift_function(0.5)
>>> print ndimage.geometric_transform(a, fnc)

array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

instead of :

[[ 0.      0.      0.    ]
 [ 0.      1.3625  2.7375]
 [ 0.      4.8125  6.1875]
 [ 0.      8.2625  9.6375]]

Could someone please suggest what I need to get this working?

Thanks,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100909/3a950b5c/attachment.html 


More information about the SciPy-User mailing list