[Numpy-discussion] numpy records in C

Erin Sheldon erin.sheldon@gmail....
Tue Jul 20 19:51:33 CDT 2010

On Mon, Jul 19, 2010 at 4:28 PM, Caius Howcroft
<caius.howcroft@gmail.com> wrote:
> Hi all
> I'm playing with writing some C code to speed up an inner loop in my
> python code. This loop operates on a numpy record, e.g. soemthing like
> this:
> a = numpy.zeros((10,), dtype=[("myfvalue" ,"float"), ("myc", "int8"),
> ("anotheri", "uint64")])
> which is then passed into c code like so:
> myCFunc(a, "blah")
> I was wondering if someone had an example of how to access particular
> columns of "a" in the C func. Clearly, this is going to involve  a
> PyArray_Descr.fields somewhere, but an example would really help make
> things clearer for me.
> Thanks
> Regards
> Caius

Hi Caius -

Indeed, the info is in the descr.

I attached a header-only C++ class I use to wrap records.  The details
of what you want to do are in the "set_field_info" and "set_sizes"
methods, where the info is extracted from the descr.  Its rather
involved, so I won't write it out in detail here.

The accessor methods called "ptr" retrieve a pointer to row/column you
are interested in.

Note the accessor methods are not type safe, you still need to know what
type you need to cast the pointer to.  This can be determined at runtime
from the type code, which you can get via the typecode() method.  I've
considered adding "get" methods that are type safe and will cast to
the type you want, but that will take more effort.

I hope this helps, and please let me know if you find any bugs.

Erin Sheldon
Brookhaven National Laboratory

P.S. The file NumpyRecords.h is part of
the esutil package:  http://code.google.com/p/esutil/

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NumpyRecords.h
Type: application/octet-stream
Size: 10917 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100720/345cc821/attachment.obj 

More information about the NumPy-Discussion mailing list