15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SciPy Reference Guide, Release 0.8.dev<br />

The value of the shift is obtained and then assigned to a dynamically allocated memory location. Both this data pointer<br />

and the function pointer are then wrapped in a PyCObject, which is returned. Additionally, a pointer to a destructor<br />

function is given, that will free the memory we allocated for the shift value when the PyCObject is destroyed. This<br />

destructor is very simple:<br />

static void<br />

_destructor(void* cobject, void *cdata)<br />

{<br />

if (cdata)<br />

free(cdata);<br />

}<br />

To use these functions, an extension module is built:<br />

static PyMethodDef methods[] = {<br />

{"shift_function", (PyCFunction)py_shift_function, METH_VARARGS, ""},<br />

{NULL, NULL, 0, NULL}<br />

};<br />

void<br />

initexample(void)<br />

{<br />

Py_InitModule("example", methods);<br />

}<br />

This extension can then be used in Python, for example:<br />

>>> import example<br />

>>> array = arange(12, shape=(4,3), type = Float64)<br />

>>> fnc = example.shift_function(0.5)<br />

>>> print geometric_transform(array, fnc)<br />

[[ 0. 0. 0. ]<br />

[ 0. 1.3625 2.7375]<br />

[ 0. 4.8125 6.1875]<br />

[ 0. 8.2625 9.6375]]<br />

C callback functions for use with ndimage functions must all be written according to this scheme. The next section<br />

lists the ndimage functions that acccept a C callback function and gives the prototype of the callback function.<br />

1.10.10 Functions that support C callback functions<br />

The ndimage functions that support C callback functions are described here. Obviously, the prototype of the function<br />

that is provided to these functions must match exactly that what they expect. Therefore we give here the prototypes<br />

of the callback functions. All these callback functions accept a void callback_data pointer that must be<br />

wrapped in a PyCObject using the Python PyCObject_FromVoidPtrAndDesc function, which can also accept<br />

a pointer to a destructor function to free any memory allocated for callback_data. If callback_data is not needed,<br />

PyCObject_FromVoidPtr may be used instead. The callback functions must return an integer error status that is<br />

equal to zero if something went wrong, or 1 otherwise. If an error occurs, you should normally set the python error<br />

status with an informative message before returning, otherwise, a default error message is set by the calling function.<br />

The function generic_filter (see Generic filter functions) accepts a callback function with the following prototype:<br />

The calling function iterates over the elements of the input and output arrays, calling the callback function<br />

at each element. The elements within the footprint of the filter at the current element are passed through<br />

1.10. Multi-dimensional image processing (ndimage) 79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!