[Numpy-discussion] Accessing a Numeric 'c' array from numarray]
jmiller at stsci.edu
Sat Jan 18 11:57:03 EST 2003
Francesc Alted wrote:
>A Dissabte 18 Gener 2003 17:41, Todd Miller va escriure:
>>>I just want to access the buffer data, and the shape of this object from C
>>>(well, I'm actually using Pyrex, but I think this is not important). Is
>>>that possible by only using numarray C calls?
>>Look at Lib/chararray.py and Src/_chararraymodule.c.
>>If you can handle using a CharArray or RawCharArray, try:
>>1. call NA_updateDataPtr( array ) to refresh the data buffer pointer in
>>the PyArrayObject. Even _chararraymodule.c doesn't do this right yet.
>>2. call NA_OFFSETDATA(array) to add the byteoffset to the pointer.
>>3. shape, strides, and itemsize should be directly accessible from the
>Ok. I'll try to do that.
>>CharArray has some extra stripping and padding semantics; these are lazy
>>and hence absent without extra care in C. RawCharArray has none.
>By the way, is it safe to assume that CharArray objects are contiguous? or
Mostly no. Each fixed length element is stored as a contiguous
sequence of bytes. Anything goes for the rest, so you need to look at
the strides arrays and byteoffset.
>The same question goes for RecArray objects.
No. It's possible to select every 10th record, for instance, in a
slice. I believe the resulting decimated array would be a discontiguous
view of the original.
>Or it is always
>convenient to check with iscontiguous() method if they are or not?.
I'm not even certain the method works correctly for chararray and
I think the portion of chararray that has been written in C considers
recarray is pure python. In both cases, I think I'd just forget about
contiguity and use the strides arrays.
> In case
>these objects can be non-contiguous, I guess there's still not a function
>like NA_InputArray that works with CharArray or RecArray objects in order to
>obtain well-behaved objects. Is that true?
True. But neither recarray nor chararray really has behavedness
problems like misalignment,
byteswapping, or type conversion. I think contiguity is the only issue,
and that is solved
just by calling .copy(). You might argue that records contain
byteswapped and misaligned fields. I don't have an immediate answer to
My preference is to use strides and forget about contiguity, but you
could also make contiguous copies simply. Noone I'm aware of has yet
tried access to misbehaved records in C.
>I think it would be possible to me to include support for numarray objects
>in next release of PyTables.
More information about the NumPy-Discussion