Nov-26-2019, 04:59 PM
I am using Anaconda Spyder 3.3.6 (Python 3.7) on CentOS 7. I would like to pass 3D numpy arrays to a C function that processes the input and returns a numpy array as output. The C code is in a module, surfaceModules, which has the following form.
The same erratic behavior also happens with this code.
#include <stdio.h> #include <stdlib.h> #include "/home/peter/anaconda3/include/python3.7m/Python.h" #include "numpy/arrayobject.h" #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION static PyObject * squeezeSurfaces(PyObject* self, PyObject* args) { PyArrayObject *matin, *matout; double ***cin, ***cout, dfactor; int i,j,k,n,m,l, dims[3], ifactor; // Parse tuples separately since args will differ between C fcns if (!PyArg_ParseTuple(args, "O!id", &PyArray_Type, &matin, &ifactor, &dfactor)) return NULL; if (NULL == matin) return NULL; return PyArray_Return(matin); } static PyMethodDef surfaceMethods[] = { {"squeezeSurfaces", squeezeSurfaces, METH_VARARGS, "Squeezes surfaces along normal vectors"}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef surfaceModules = { PyModuleDef_HEAD_INIT, "surfaceModules", "Surface function Module", -1, surfaceMethods }; PyMODINIT_FUNC PyInit_surfaceModules(void){ import_array(); return PyModule_Create(&surfaceModules); }The following code
import surfaceModules as sm import numpy as NP x=NP.zeros(27) for i in range(0,27): x[i]=i x=x.reshape(3,3,3) jfac=2 xfac=1.5 y=sm.squeezeSurfaces(x, jfac, xfac)sometimes produces this
In [1]: import surfaceModules as sm import numpy as NP x=NP.zeros(27) for i in range(0,27): x[i]=i x=x.reshape(3,3,3) jfac=2 xfac=1.5 y=sm.squeezeSurfaces(x, jfac, xfac) In [1]: y Out[2]: array([[[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]], [[ 9., 10., 11.], [12., 13., 14.], [15., 16., 17.]], [[18., 19., 20.], [21., 22., 23.], [24., 25., 26.]]])which is what I want. However, sometimes, it produces this.
In [4]: import surfaceModules as sm import numpy as NP x=NP.zeros(27) for i in range(0,27): x[i]=i x=x.reshape(3,3,3) jfac=2 xfac=1.5 y=sm.squeezeSurfaces(x, jfac, xfac) In [1]: y Traceback (most recent call last): File "<ipython-input-1-9063a9f0e032>", line 1, in <module> y NameError: name 'y' is not definedNote In [1]: y instead of In [5]: y. Seems to reset the Spyder kernel. But seems erratic and unpredictable. I would not want this happening when it is part of larger code.
The same erratic behavior also happens with this code.
import surfaceModules as sm import numpy as NP x=NP.zeros(27) jfac=2 xfac=1.5 y=sm.squeezeSurfaces(x, jfac, xfac)