[Numpy-discussion] Questions about records

Russell E Owen owen at astro.washington.edu
Fri Apr 2 09:59:06 CST 2004


I've been trying to generate a 2-dimensional numarray.records array 
and am rather puzzled about some failures:

I tried a pair of 2x2 arrays for the buffer (one per field). I 
thought the record would get its shape from those, but instead of a 
2x2 record I get a 2-length record, each of whose elements is a pair 
of 2-length arrays.

>>>  arr1 = num.arange(4, shape=(2,2), type=num.Float64)
>>>  arr2 = num.arange(4, shape=(2,2), type=num.Float64) + 10
>>>  a = rec.array([arr1, arr2], names="a,b")
>>>  a
array(
[(array([ 0.,  1.]), array([ 10.,  11.])),
(array([ 2.,  3.]), array([ 12.,  13.]))],
formats=['(2,)Float64', '(2,)Float64'],
shape=2,
names=['a', 'b'])

So I tried passing in 4-length arrays while specifying the shape 
explicitly, but it failed. Is this a bug?:

>>>  arr1 = num.arange(4, type=num.Float64)
>>>  arr2 = num.arange(4, type=num.Float64) + 10
>>>  a = rec.array([arr1, arr2], shape=(2,2), names="a, b")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/local/lib/python2.3/site-packages/numarray/records.py", 
line 384, in array
     byteorder=byteorder, aligned=aligned)
   File "/usr/local/lib/python2.3/site-packages/numarray/records.py", 
line 177, in fromarrays
     raise ValueError, "array has different lengths"
ValueError: array has different lengths

Generating a 4-length record and reshaping it does seem to work, 
though there seems to be a bug in __str__ which I'll report:

>>>  arr1 = num.arange(4, type=num.Float64)
>>>  arr2 = num.arange(4, type=num.Float64) + 10
>>>  a = rec.array([arr1, arr2], names="a, b")
>>>  a.setshape((2,2))
>>>  a
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/local/lib/python2.3/site-packages/numarray/records.py", 
line 718, in __repr__
     outlist.append(Record.__str__(i))
TypeError: unbound method __str__() must be called with Record 
instance as first argument (got RecArray instance instead)
>>>  a[1,1]
<numarray.records.Record instance at 0x15d2ad0>
>>>  str(a[1,1])
'(1.0, 11.0)'

I see the str problem again if I don't specify any buffer:

>>>  import numarray as num
>>>  import numarray.records as rec
>>>  a = rec.array(formats="Float64,Float64", names="a,b", shape=(2,2))
>>>  a
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/local/lib/python2.3/site-packages/numarray/records.py", 
line 718, in __repr__
     outlist.append(Record.__str__(i))
TypeError: unbound method __str__() must be called with Record 
instance as first argument (got RecArray instance instead)

So...comments? Should I report the shape issues as a bug?

-- Russell




More information about the Numpy-discussion mailing list