[Numpy-discussion] Optionally using Numeric in another compiled

gvermeul at grenoble.cnrs.fr gvermeul at grenoble.cnrs.fr
Wed Jan 15 13:50:05 EST 2003


> Gerard Vermeulen wrote:
> > I just want to point out that PyQwt plots NumPy arrays. I have played
> > a little bit with the Scipy-wxWindows interface, but it is no match
> > for PyQwt (I display x-y data with 16000 points).
> 
> Thanks for the tip, I'll check it out. I think what you have there is
> that the plotting is all done at the C++ level, expecting some kind of
> sequence of data points. That's exactly what I want to adress with
> wxPython: being able to pass in a whole sequence and have the looping
> done at the C++ level.
>
Yes, I am using PyArray_ContiguousFromObject() to convert any sequence
into a NumPy array before copying the data into Qwt's double arrays. 
>
> Have you ever tested whether it's fster or slower to plot data passed in
> as a list vs. a NumPy array?
>
I did not test it, but there is certainly more overhead if you pass
a list or a tuple into PyArray_ContiguousFromObject() than a NumPy array
> 
> How do you access the data in the passed in sequence? Do you use:
> PySequence_GetItem ?
> 
No, see above. The code looks like (in "sip" language, sip is a sort of
swig, but more specialized to C++ and Qt):

    void setData(double *, double *, int);
%MemberCode
    PyObject *xSeq, *ySeq;
    $C *ptr;
    if (sipParseArgs(&sipArgsParsed, sipArgs, "mOO",
                     sipThisObj, sipClass_$C, &ptr, &xSeq, &ySeq)) {
        PyArrayObject *x = (PyArrayObject *)
            PyArray_ContiguousFromObject(xSeq, PyArray_DOUBLE, 1, 0);
        if (!(x))
            return 0;
        PyArrayObject *y = (PyArrayObject *)
            PyArray_ContiguousFromObject(ySeq, PyArray_DOUBLE, 1, 0);
        if (!(y))
            return 0;
        int size;
        Py_BEGIN_ALLOW_THREADS
        size = (x->dimensions[0] < y->dimensions[0]) ?
            x->dimensions[0] : y->dimensions[0];
        ptr->setData((double*)(x->data), (double*)(y->data), size);
        Py_END_ALLOW_THREADS

        Py_DECREF(x);
        Py_DECREF(y);

        Py_INCREF(Py_None);
        return Py_None;
    }
%End

The setData calls copy the data.
>
> thanks for the tip. Qwt (and PyQwt) look very nice, I may have to
> reconsider using PyQT!
> 

Gerard

>
> -Chris
> 
> 
> 
>  
> > Take a look at http://gerard.vermeulen.free.fr
> > 
> > PyQwt is an addon for PyQt (a Python wrapper for Qt) that knows nothing
> > about NumPy
> > 
> > Maybe it is possible to make a NumPy plot add-on for wxWindows, too.
> > 
> > Gerard
> > 
> > On Wed, Jan 15, 2003 at 09:22:20AM -0800, Chris Barker wrote:
> > > 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
> > > step.
> > >
> > > 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
> > > now:
> > >
> > >
> > > bool      isFastSeq  = PyList_Check(pyPoints) ||
> > > PyTuple_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.
> > >
> > > -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
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.NET email is sponsored by: A Thawte Code Signing Certificate
> > > is essential in establishing user confidence by providing assurance of
> > > authenticity and code integrity. Download our Free Code Signing guide:
> > > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0028en
> > > _______________________________________________
> > > Numpy-discussion mailing list
> > > Numpy-discussion at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/numpy-discussion
> 
> -- 
> 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
> 


-------------------------------------------------------------
This message was sent using HTTPS service from CNRS Grenoble.
         --->   https://grenoble.cnrs.fr   <---         






More information about the NumPy-Discussion mailing list