Problem with using PyArray_AsCArray

I am trying to pass a multi-dimensional ndarray to C as a multi-dimensional C array for the purposes of passing it to mathematica. I am using PyArray_AsCArray but getting an error. ###################################################### Python Code: import Image from scipy.misc import fromimage img = Image.open("../APLS/image709_enhanced_2.tif") nimg = fromimage(img) ... mathlink_PutIntegerArray(nimg) ###################################################################### C Wrapper Code: static PyObject * mathlink_PutIntegerArray(mathlink_Link *self, PyObject *args) { npy_intp dims[3]; /* PyArray_AsCArray is for ndim <= 3 */ PyObject *o1; double *d1; long result; i=PyArray_AsCArray(&o1, (void *)&d1, dims, PyArray_NDIM(o1), PyArray_DescrFromType(PyArray_TYPE(o1))); ... } Program received signal SIGSEGV, Segmentation fault. 0x00002aaaaefa3d2e in PyArray_AsCArray (op=0x7fffffffdf68, ptr=0x7fffffffdf60, dims=0x7fffffffdf40, nd=3, typedescr=<value optimized out>) at numpy/core/src/multiarray/multiarraymodule.c:218 218 ptr3[i][j] = ap->data + i*ap->strides[0] + j*ap->strides[1] ################################################################################# I am able to read i,j,ap->data, ap->strides[0], ap->strides[1] so the error seems to be in the assignment to ptr3[i][j] This happens on the first instance i=0 j=0 ################################################################################# PyArray_AsCArray code is below. NPY_NO_EXPORT int PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd, PyArray_Descr* typedescr) { PyArrayObject *ap; npy_intp n, m, i, j; char **ptr2; char ***ptr3; if ((nd < 1) || (nd > 3)) { PyErr_SetString(PyExc_ValueError, "C arrays of only 1-3 dimensions available"); Py_XDECREF(typedescr); return -1; } if ((ap = (PyArrayObject*)PyArray_FromAny(*op, typedescr, nd, nd, CARRAY, NULL)) == NULL) { return -1; } switch(nd) { case 1: *((char **)ptr) = ap->data; break; case 2: n = ap->dimensions[0]; ptr2 = (char **)_pya_malloc(n * sizeof(char *)); if (!ptr2) { goto fail; } for (i = 0; i < n; i++) { ptr2[i] = ap->data + i*ap->strides[0]; } *((char ***)ptr) = ptr2; break; case 3: n = ap->dimensions[0]; m = ap->dimensions[1]; ptr3 = (char ***)_pya_malloc(n*(m+1) * sizeof(char *)); if (!ptr3) { goto fail; } for (i = 0; i < n; i++) { ptr3[i] = ptr3[n + (m-1)*i]; for (j = 0; j < m; j++) { ptr3[i][j] = ap->data + i*ap->strides[0] + j*ap->strides[1]; } } *((char ****)ptr) = ptr3; } memcpy(dims, ap->dimensions, nd*sizeof(npy_intp)); *op = (PyObject *)ap; return 0; fail: PyErr_SetString(PyExc_MemoryError, "no memory"); return -1; }

On 12/9/11 11:25 AM, Ng, Enrico wrote:
I am trying to pass a multi-dimensional ndarray to C as a multi-dimensional C array for the purposes of passing it to mathematica. I am using PyArray_AsCArray but getting an error.
I understand that SWIG, Boost, et. al are perhaps too heavyweight for this one use, but you might want to give Cython a try. It makes it really easy to grab a numpy array (and tst it for compliance), and then you can do whatever you want with the data pointer: http://wiki.cython.org/tutorials/numpy http://wiki.cython.org/WrappingNumpy (this one is marked as depricated, but may be what you want in your case, as you do want a raw C array) Some wore googling and browsing of the Cython list will likely yield examples similar to yours. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

I actually figured it out. I went one level down in the array and it took it. -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Chris.Barker Sent: Friday, December 09, 2011 3:33 PM To: Discussion of Numerical Python Subject: EXTERNAL: Re: [Numpy-discussion] Problem with using PyArray_AsCArray On 12/9/11 11:25 AM, Ng, Enrico wrote:
I am trying to pass a multi-dimensional ndarray to C as a multi-dimensional C array for the purposes of passing it to mathematica. I am using PyArray_AsCArray but getting an error.
I understand that SWIG, Boost, et. al are perhaps too heavyweight for this one use, but you might want to give Cython a try. It makes it really easy to grab a numpy array (and tst it for compliance), and then you can do whatever you want with the data pointer: http://wiki.cython.org/tutorials/numpy http://wiki.cython.org/WrappingNumpy (this one is marked as depricated, but may be what you want in your case, as you do want a raw C array) Some wore googling and browsing of the Cython list will likely yield examples similar to yours. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Chris.Barker
-
Ng, Enrico