Defining custom types

Travis Oliphant oliphant at ee.byu.edu
Thu Oct 26 18:19:11 CDT 2006


Jonathan Wang wrote:

> I'm trying to write a Numpy extension that will encapsulate mxDateTime 
> as a native Numpy type. I've decided to use a type inherited from 
> Numpy's scalar double. However, I'm running into all sorts of 
> problems. I'm using numpy 1.0b5; I realize this is somewhat out of date.
>
Cool.   The ability to create your own data-types (and define ufuncs for 
them) is a feature that I'd like to see explored.   But, it has not 
received a lot of attention and so you may find bugs along the way.  
We'll try to fix them quickly as they arise (and there will be bug fix 
releases for 1.0). 

But, what do you mean "inheriting" from NumPy's double for your scalar 
data-type.  This has significant implications.  To define a new 
data-type object (that doesn't build from the VOID data-type),  you need 
to flesh out the PyArray_Descr * structure and this can only be done in 
C.   Perhaps you are borrowing most entries in the structure builtin 
double type and then filling in a few differently like setitem and 
getitem?   Is that accurate?

> For all the examples below, assume that I've created a 1x1 array, 
> mxArr, with my custom type.
>
> The interface used by Array_FromPyScalar does not conform with the 
> documentation's claim that a negative return value indicates an error.


You must be talking about a different function.  Array_FromPyScalar is 
an internal function and not a C-API call.  It also returns a PyObject * 
not an integer. So, which function are you actually referring to?

> The return code from setitem is not checked. Instead, the code depends 
> on a Python error being set. 

This may be true, but how is it a problem?

>
> I seem to be able to load values into the array, but I can't extract 
> anything out of the array, even to print it. In gdb I've verified that 
> loading DateTime.now() correctly puts a float representation of the 
> date into my array. However, if I try to get the value out, I get an 
> error:
> >>> mxArr[0] = DateTime.now()
> >>> mxArr[0]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.4/site-packages/numpy/core/numeric.py", line 
> 391, in array_repr
>     ', ', "array(")
>   File "/usr/lib/python2.4/site-packages/numpy/core/arrayprint.py", 
> line 204, in array2string
>     separator, prefix)
>   File "/usr/lib/python2.4/site-packages/numpy/core/arrayprint.py", 
> line 160, in _array2string
>     format = _floatFormat(data, precision, suppress_small)
>   File "/usr/lib/python2.4/site-packages/numpy/core/arrayprint.py", 
> line 281, in _floatFormat
>     non_zero = _uf.absolute(data.compress(_uf.not_equal(data, 0)))
> TypeError: bad operand type for abs()
>
> I'm not sure why it's trying to call abs() on my object to print it.

Because that's the implication of inheriting from a double.  It's just 
part of the code that tries to format your values into an array (notice 
the _floatFormat).  I actually borrowed this code from numarray so I 
can't speak to exactly what it's doing without more study.

> I have a separate PyNumberMethods attached to my object type, copied 
> from the float scalar type, and nb_absolute is set to 0. When I break 
> at the various functions I've registered, the last thing Numpy tries 
> to do is cast my custom data type to an object type (which it does so 
> successfully) via _broadcast_cast.

Don't confuse the Python object  associated when an element of the array 
is extracted and the data-type of the array. Also don't confuse the 
PyNumberMethods of the scalar object with the ufuncs.  Defining 
PyNumberMethods won't usually give you the ability to calculate ufuncs.

Perhaps you just want to construct an "object" array of mxDateTime's.   
What is the reason you want to define an mxDateTime data-type?

-Travis


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list