creating a numpy array with values to be cast to an enum?

1,808 views
Skip to first unread message

Henry Gomersall

unread,
Jul 10, 2012, 12:07:13 PM7/10/12
to cython...@googlegroups.com
I'm trying to create an 5xN array of values to pass from a function. One
of these values is actually an enumeration, so some kind of integer.

Is there some way in which I can create a numpy dtype that has the
correct integer type in order to put an enum value into it.

The idea is that I can use numpy to manage an array of structures.

Perhaps there is a better way to do this?

Cheers,

Henry

Henry Gomersall

unread,
Jul 10, 2012, 12:18:17 PM7/10/12
to cython...@googlegroups.com
On Tue, 2012-07-10 at 17:07 +0100, Henry Gomersall wrote:
>
> Is there some way in which I can create a numpy dtype that has the
> correct integer type in order to put an enum value into it.

So, the solution I have come up with is as follows:

enum_dtype = np.dtype('u'+sizeof(MY_ENUM_TYPE))

Does this seem reasonable?

Cheers,

Henry

Robert Bradshaw

unread,
Jul 11, 2012, 12:23:32 PM7/11/12
to cython...@googlegroups.com
It's possible that your enum type is signed, so you might want to use
'i' (though this could have the same issues for the top of an unsigned
enum's range (but this might not actually matter because the cast to
and from the wrong signed type should preserve the value)). It looks
like the type of int used for an enum is implementation dependent, and
may even be a function of the enumerated enum values.

If N is not large, you could just use i8, or even int with an assert in
your module that sizeof(MY_ENUM_TYPE) <= sizeof(int) (which should typically be
true unless your enum has very large values in it). This may be less
memory-efficient, but would have the advantage of being able to
specify the type at compile time.

- Robert

Henry Gomersall

unread,
Jul 11, 2012, 2:30:17 PM7/11/12
to cython...@googlegroups.com
On Wed, 2012-07-11 at 09:23 -0700, Robert Bradshaw wrote:
>
> On Tue, Jul 10, 2012 at 9:18 AM, Henry Gomersall <he...@cantab.net>
> wrote:
> > On Tue, 2012-07-10 at 17:07 +0100, Henry Gomersall wrote:
> >>
> >> Is there some way in which I can create a numpy dtype that has the
> >> correct integer type in order to put an enum value into it.
> >
> > So, the solution I have come up with is as follows:
> >
> > enum_dtype = np.dtype('u'+sizeof(MY_ENUM_TYPE))
> >
> > Does this seem reasonable?
>
> It's possible that your enum type is signed, so you might want to use
> 'i' (though this could have the same issues for the top of an unsigned
> enum's range (but this might not actually matter because the cast to
> and from the wrong signed type should preserve the value)). It looks
> like the type of int used for an enum is implementation dependent, and
> may even be a function of the enumerated enum values.
>
The signed/unsigned issue became apparent to me in that half sleep state
just before one drifts off yesterday evening. What you say makes sense.

> If N is not large, you could just use i8, or even int with an assert
> in
> your module that sizeof(MY_ENUM_TYPE) <= sizeof(int) (which should
> typically be
> true unless your enum has very large values in it). This may be less
> memory-efficient, but would have the advantage of being able to
> specify the type at compile time.

It's actually the case that I'm wrapping a lib in which the enums are
already defined. The purpose of using a numpy dtype is to create an
array of structures, in which the memory management is handled for me,
and in which I get the niceties of numpy (getting the size of the array
and so on). I then just cast the pointer to the buffer to be a struct
pointer and use that as needed.

It strikes me this must be a pretty common problem? To the point that
there could/should be a way in which C arrays of any type (including
structs) can be created trivially and presented as a python object
(which is just a presentation of the underlying buffer). I'm not saying
Numpy is the way to do this, but it does seem to possess the necessary
machinery. (If something like this is already implemented, I'd really
appreciate knowing how to use it :)

Cheers,

Henry

Robert Bradshaw

unread,
Jul 17, 2012, 6:58:17 PM7/17/12
to cython...@googlegroups.com
The difficulty is that you're trying to use an item of unknown size
while leveraging already-compiled libraries.

- Robert
Reply all
Reply to author
Forward
0 new messages