[Numpy-discussion] Questions about the array interface.

Chris Barker Chris.Barker at noaa.gov
Wed Apr 6 15:51:17 CDT 2005


Hi all, (but mostly Travis),

I've taken a look at:

http://numeric.scipy.org/array_interface.html)

to try and see how I would use this with wxPython. I have a few 
questions, and a little code I'd like you to look at to see if I 
understand how this works.

Here's a first stab on how I might use this for the wxPython 
DrawPointsList method. The method takes a sequence of length-2 sequences 
of numbers, and draws a point at each point described by coordinates in 
the data:

[(x,y), (x2,y2), (x3,y3), ...] (or a NX2 NumPy array of Ints)

Here's what I have:

     def DrawPointList(self, points, pens=None):
	...
	# some checking code on the pens)
         ...
         if (hasattr(points,'__array_shape__') and
                 hasattr(points,'__array_typestr__') and
                 len(points.__array_shape__) == 2 and
                 points.__array_shape__[1] == 2 and
                 points.__array_typestr__ == 'i4' and
                 ): # this means we have a compliant array
            # return the array protocol version
            return self._DrawPointArray(points.__array_data__, pens,[])
                    #This needs to be written now!
         else:
             #return the generic python sequence version
             return self._DrawPointList(points, pens, [])

Then we'll need a function (in C++):
  _DrawPointArray(points.__array_data__, pens,[])
That takes a buffer object, and does the drawing.

My questions:

1) Is this what you had in mind for how to use this?

2) As __array_strides__ is optional, I'd kind of like to have a 
__contiguous__ flag that I could just check, rather than checking for 
the existence of strides, then calculating what the strides should be, 
then checking them.

3) A number of the attributes are optional, but will always be there 
with SciPy arrays..(I assume) have you documented them anywhere?

4) a wxWidgets wxPoint is defined as such:

class WXDLLEXPORT wxPoint
{
public:
     int x, y;

etc.

As wxWidgets is using "int", I"d like to be able to use "int". If I 
define it as a 4 byte integer, I'm losing platform independence, aren't 
I? Or can I use something like sizeof(int) ?

5) Why is: __array_data__ optional? Isn't that the whole point of this?

6) Should __array_offset__ be optional? I'd rather it were required, but 
  default to zero. This way I have to check for it, then use it. Also, I 
assume it is an integer number of bytes, is that right?

7) An alternative to the above: A __simple_ flag, that means the data is 
a simple, C array of contiguous data of a single type. The most common 
use, and it would be nice to just check that flag and not have to take 
all other options into account.

Thanks,

-Chris






-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the Numpy-discussion mailing list