I'm running into a couple of small problems with mrecarray. I'm not
sure if they're bugs or a usage error.
First, the constructor throws an exception when the format string
contains nested arrays (if that is the proper term) such as "(2,2)f8".
This creates a three-element tuple in the dtype.descr list, whereas the
mrecord.py code seems to assume each descr will always have 2 elements.
========
In [1]: from numpy.ma import mrecords
In [2]: x = mrecords.mrecarray(1, formats="(2,2)f8")
---------------------------------------------------------------------------
Traceback (most recent call last)
/wonkabar/data1/scraps/<ipython console> in <module>()
/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in
__new__(cls, shape, dtype, buf, offset, strides, formats, names, titles,
byteorder, aligned, mask, hard_mask, fill_value, keep_mask, copy, **options)
121 self = recarray.__new__(cls, shape, dtype=dtype, buf=buf, offset=offset,
122 strides=strides, formats=formats,
--> 123 byteorder=byteorder, aligned=aligned,)
124 #
125 mdtype = [(k,'|b1') for (k,_) in self.dtype.descr]
/home/mdroe/usr/lib/python2.5/site-packages/numpy/core/records.py in
__new__(subtype, shape, dtype, buf, offset, strides, formats, names,
titles, byteorder, aligned)
248
249 if buf is None:
--> 250 self = ndarray.__new__(subtype, shape, (record, descr))
251 else:
252 self = ndarray.__new__(subtype, shape, (record, descr),
/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in
__array_finalize__(self, obj)
157 _fieldmask = getattr(obj, '_fieldmask', None)
158 if _fieldmask is None:
--> 159 mdescr = [(n,'|b1') for (n,_) in self.dtype.descr]
160 objmask = getattr(obj, '_mask', nomask)
161 if objmask is nomask:
: too many values to unpack
========
In my own use case, I don't care if the individual elements of the
nested array are maskable -- either the whole array being masked or not
is good enough -- but perhaps that shortcoming is why this wasn't
designed to work? I have attached a patch to mrecords.py that gets me
past this and does seem to allow the nested array to be masked as a whole.
Secondly, the "names" and "titles" kwargs seem to be ignored by the
mrecarray constructor.
========
In [3]: x = mrecords.mrecarray(1, formats="f8", names="foo")
In [4]: x[0]['foo'] = 42.0
---------------------------------------------------------------------------
Traceback (most recent call last)
/wonkabar/data1/scraps/<ipython console> in <module>()
/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/mrecords.py in
__setitem__(self, indx, value)
309 def __setitem__(self, indx, value):
310 "Sets the given record to value."
--> 311 MaskedArray.__setitem__(self, indx, value)
312 if isinstance(indx, basestring):
313 self._mask[indx] = ma.getmaskarray(value)
/home/mdroe/usr/lib/python2.5/site-packages/numpy/ma/core.py in
__setitem__(self, indx, value)
1437 # raise IndexError, msg
1438 if isinstance(indx, basestring):
-> 1439 ndarray.__setitem__(self._data, indx, value)
1440 ndarray.__setitem__(self._mask, indx, getmask(value))
1441 return
: field named foo not found.
========
The included patch delegates these kwargs onto the underlying recarray.
Cheers,
Mike
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA
Index: numpy/ma/mrecords.py
===================================================================
--- numpy/ma/mrecords.py (revision 5378)
+++ numpy/ma/mrecords.py (working copy)
@@ -119,10 +119,10 @@
**options):
#
self = recarray.__new__(cls, shape, dtype=dtype, buf=buf, offset=offset,
- strides=strides, formats=formats,
- byteorder=byteorder, aligned=aligned,)
+ strides=strides, formats=formats, names=names,
+ titles=titles, byteorder=byteorder, aligned=aligned,)
#
- mdtype = [(k,'|b1') for (k,_) in self.dtype.descr]
+ mdtype = [(k[0],'|b1') for k in self.dtype.descr]
if mask is nomask or not np.size(mask):
if not keep_mask:
self._mask = tuple([False]*len(mdtype))
@@ -156,7 +156,7 @@
# Make sure we have a _fieldmask by default ..
_fieldmask = getattr(obj, '_fieldmask', None)
if _fieldmask is None:
- mdescr = [(n,'|b1') for (n,_) in self.dtype.descr]
+ mdescr = [(n[0],'|b1') for n in self.dtype.descr]
objmask = getattr(obj, '_mask', nomask)
if objmask is nomask:
_mask = np.empty(self.shape, dtype=mdescr).view(recarray)