[Numpy-discussion] python user defined type

Nicolas Rougier Nicolas.Rougier@loria...
Fri Jul 11 02:24:05 CDT 2008

My Unit class is supposed to represent a neuron that can be linked to
any other unit. The neuron itself is merely a (float) potential that can
vary along time under the influence of other units and learning. I
gather these units into groups which are in fact 2D matrix of units.
Currently, I implemented the Unit and Group and I "talk" with numpy
through an attribute of groups which represent all available potentials.
Finally my group is like a simple 2d matrix of float but I need to have
an underlying object to perform computation on each Unit at each time

Currently I'm able to write something like:

>>> group = Unit()*[2,2]
>>> group.potentials = numpy.zeros([2,2])
>>> print group.potentials
[[ 0.  0.]
 [ 0.  0.]]
>>> group[0,0].potential = 1
[[ 1.  0.]
 [ 0.  0.]]


On Thu, 2008-07-10 at 16:30 -0700, Christopher Barker wrote:
> Nicolas Rougier wrote:
> > Concerning the dtype argument during array creation, I thought it was
> > here for somehow controlling the type of array elements. For example, if
> > I use a "regular" numpy array (let's say a float array), I cannot set an
> > item to a string value (it raises a ValueError: setting an array element
> > with a sequence).
> Yes, but numpy is designed primarily for numeric types: ints, floats, 
> etc. It can also be used for custom types that are essentially like C 
> structs (see recarray). The key is that a dtype desribes a data type in 
> terms of bytes and that they represent -- It can not be a python type.
> The only way to use arbitrary python types is a object array, which 
> you've discovered, but then numpy doesn't know any thing about the 
> objects, other than that they are python objects.
> > So what would be the best way to use numpy arrays with "foreign" types
> > (or is it possible at all) ?  I've designed the "real" Unit in C++ and
> > exported it to python (via boost and shared pointers) and I would like
> > to create array of such Units
> If your type is a C++ class, then it may be possible, with some C 
> hackary to get numpy to understand it, but you're getting beyong my 
> depth here -- also I doubt that you'd get the full features like array 
> math and all anyway -- that's all set up for basic numeric types.
> Maybe others will have some idea, but I think you're pushing what numpy 
> is capable of.
> > (in fact, I also created an array-like
> > class but I would prefer to use directly the real array interface to
> > benefit from the great work of numpy instead of re-inventing the
> > wheel). 
> What operations do you expect to perform with these arrays of Units?
> -Chris

More information about the Numpy-discussion mailing list