[Numpy-discussion] Optionally using Numeric in another compiled extension package.

Chris Barker Chris.Barker at noaa.gov
Wed Jan 15 10:21:02 EST 2003

Hi folks,

I use Numeric an wxPython together a lot (of course I do, I use Numeric
for everything!).

Unfortunately, since wxPython is not Numeric aware, you lose some real
potential performance advantages. For example, I'm now working on
expanding the extensions to graphics device contexts (DCs) so that you
can draw a whole bunch of objects with a single Python call. The idea is
that the looping can be done in C++, rather than Python, saving a lot of
overhead of the loop itself, as well as the Python-wxWindows translation

For drawing thousands of points, the speed-up is substantial. It's less
substantial on more complex objects (rectangles give a factor of two
improvement for ~1000 objects), due to the longer time it takes to draw
the object itself, rather than make the call. 

Anyway, at the moment, Robin Dunn has the wrappers set up so that you
can pass in a NumPy array (or, indeed, and sequence) rather than a list
or tuple of coordinates, but it is faster to use a list than a NumPy
array, because for arrays, it uses the generic PySequence_GetItem call.
If we used the NumPy API directly, it should be faster than using a
list, not slower! THis is how a representative section of the code looks

bool      isFastSeq  = PyList_Check(pyPoints) ||
                // Get the point coordinants
                if (isFastSeq) {
                    obj = PySequence_Fast_GET_ITEM(pyPoints, i);
                else {
                    obj = PySequence_GetItem(pyPoints, i);


So you can see that if a NumPy array is passed in, PySequence_GetItem
will be used.

What I would like to do is have an isNumPyArray check, and then access
the NumPy array directly in that case.

The tricky part is that Robin does not want to have wxPython require
Numeric. (Oh how I dream of the day that NumArray becomes part of the
standard library!)
How can I check if an Object is a NumPy array (and then use it as such),
without including Numeric during compilation?

I know one option is to have condition compilation, with a NumPy and
non-Numpy version, but Robin is managing a whole lot of different
version as it is, and I don't think he wants to deal with twice as many!

Anyone have any ideas?

By the way, you can substitute NumArray for NumPy in this, as it is the
wave of the future, and particularly if it would be easier.


Christopher Barker, Ph.D.
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