[Numpy-discussion] numarray.objects incompatibility

Gary Ruben gazzar at email.com
Thu Mar 25 01:27:01 CST 2004


I thought I'd better try porting my Numeric python module to numarray before posting it to somewhere like Activestate's Python Cookbook site. By the way, if you have a suggestion about a more appropriate place to post what is a potentially useful generic module based on Numpy, can you let me know. It would be nice to have some sort of repository for useful modules.

In my module based on Numeric, I build an array of PyObjects:

def ArrayOfErr(errList, posErr=0., negErr=None):
    valArray = array([], 'O')
    for e in errList:
        if type(e) == types.TupleType:
            newErrObject = apply(Err, tuple(e))
        elif type(e) == types.FloatType or types.IntType:
            newErrObject = Err(e, posErr, negErr)
        valArray = concatenate((valArray, array([newErrObject], 'O')))
    return valArray


numarray.objects can't handle the first line.
Likewise
a = fromlist([])
fails.

Numeric doesn't seem to have a way of building a PyObject array dynamically,
so doing it the way I've shown seemed necessary to me.
Porting to numarray required building the list first as follows:

def ArrayOfErr(errList, posErr=0., negErr=None):
    valArray = []
    for e in errList:
        if type(e) == types.TupleType:
            newErrObject = apply(Err, tuple(e))
        elif type(e) == types.FloatType or types.IntType:
            newErrObject = Err(e, posErr, negErr)
        valArray.append(newErrObject)
    return fromlist(valArray)

You might like to think about supporting zero length object arrays to maintain compatibility.
By the way, I'm using object arrays to hold a Python number type which can by coerced to from a standard Python number type. I can therefore do things like add an array of my objects to a Numpy array of floats for example and the members of the standard Numpy array elements are coerced elementwise to my type producing a new Numpy object array. I wasn't sure that Numeric/numarray would handle this properly, since the documentation implies object arrays are only useful for manipulating arrays without casting types, but it does it correctly - lovely!

Gary Ruben
-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm





More information about the Numpy-discussion mailing list