random numpy wisdom – record arrays

> Is it possible to create a numpy array from an array of a C structure like
> this?
> struct RateInfo
> {
> unsigned int ctm;
> double open;
> double low;
> double high;
> double close;
> double vol;
> };

Sure. On the numpy side, you would make an record array with the
appropriate dtype and size.

In [1]: from numpy import *

In [2]: dt = dtype([(‘ctm’, uint), (‘open’, double), (‘low’, double),
(‘high’, double), (‘close’, double), (‘vol’, double)])

In [3]: a = empty(10, dtype=dt)

On the C side, you would iterate through your C array and your numpy
array and just assign elements from the one to the other. If you have
a contiguous C array, you could also just use memcpy().

This is probably reliable because all of your struct members take up
multiples of 4 bytes and most C compilers will pack those without any
space between them. If you were mixing, say, chars and doubles, the C
compiler may try to align the doubles on a 4-byte boundary (or
possibly another boundary, but 4-bytes is common). In that case, you
will have to figure out how your C compiler is packing the member and
emulate that in your dtype. Each of the tuples in the constructor can
have a third element which represents the byte offset of that member
from the beginning of the struct.

In [4]: dt2 = dtype([(‘ctm’, uint, 0), (‘open’, double, 4), (‘low’,
double, 12), (‘high’, double, 20), (‘close’, double, 28), (‘vol’,
double, 36)])

Robert Kern


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: