[Numpy-discussion] Accessing a Numeric 'c' array from numarray]

Todd Miller jmiller at stsci.edu
Sat Jan 18 08:27:04 EST 2003


Francesc Alted wrote:

>Hi,
>
>I'm trying to make a C array from a Numeric "c" (Character) typecode array
>using the high level call:
>
>NA_InputArray(PyObject *numarray, NumarrayType t, int requires)
>
Unified handling of character arrays and numeric arrays doesn't exist 
yet in numarray.  There is no C-API for the chararray module because we 
haven't needed one.  But CharArrays are NDArrays and have attributes 
stored in PyArrayObjects just like numarrays.

>with no success.
>
>As I have been able to access all the other types (i.e.
>'1','b','s','i','l','f','d') successfully, perhaps character type is not
>supported?
>
>In the NumarrayType enum, there is no tChar, but I've tried tUInt8 and tAny
>as the value for NumarrayType parameter, but both choices issues the same
>error:
>
>Traceback (most recent call last):
>  File "table-tree2.py", line 77, in ?
>    h5file.createArray('/columns', 'name', array(names), "Name column")
>  File "/home/falted/PyTables/pytables-0.3/tables/File.py", line 400, in 
>createArray
>    setattr(group, name, object)
>  File "/home/falted/PyTables/pytables-0.3/tables/Group.py", line 355, in 
>__setattr__
>    value._f_putObjectInTree(name, self)
>  File "/home/falted/PyTables/pytables-0.3/tables/Leaf.py", line 71, in 
>_f_putObjectInTree
>    self.create()
>  File "/home/falted/PyTables/pytables-0.3/tables/Array.py", line 83, in 
>create
>    self.createArray(self.object, self.title)
>  File "/home/falted/PyTables/pytables-0.3/src/hdf5Extension.pyx", line 913, 
>in createArray
>    array = NA_InputArray(arr, numfmt2[arr.typecode()], C_ARRAY)
>libnumarray.error: getShape: sequence object nested more than MAXDIM deep.
>
NA_InputArray was intended to accept non-numeric sequences.  It could 
report this better...

>although I was passing only a Numeric 'c' with a rather small shape (10,16).
>
>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 
PyArrayObject.

CharArray has some extra stripping and padding semantics; these are lazy 
and hence absent without extra care in C.  RawCharArray has none.

CharArrays are really arrays of fixed length strings of bytes.  The 
string length is defined by the array itemsize.

>Thanks,
>
>  
>
Todd








More information about the NumPy-Discussion mailing list