summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2008-08-03 23:45:49 +0000
committerpierregm <pierregm@localhost>2008-08-03 23:45:49 +0000
commitc3de90a4f53d23513f4295646610fe204b2cfc47 (patch)
treec0e1246c9c465fb4ee03a714bb53deaaa12e6565 /numpy
parent9156bdce373ed8f7d6e9813f5261bc6b2f473309 (diff)
downloadnumpy-c3de90a4f53d23513f4295646610fe204b2cfc47.tar.gz
core:
* renamed _basedict to _optinfo * prevented the back propagation of _optinfo in _update_from : the __dict__ is copied only if the underlying object is not a MaskedArray * fixed getmaskarray for flexible dtype * fixed __setitem__ when filling fields (the mask might still be nomask) * _arraymethod now stores the name of the method in both self._name and self.__name__
Diffstat (limited to 'numpy')
-rw-r--r--numpy/ma/core.py24
-rw-r--r--numpy/ma/tests/test_core.py29
2 files changed, 37 insertions, 16 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index b282861eb..b4f08171a 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -79,6 +79,8 @@ nomask = MaskType(0)
np.seterr(all='ignore')
+
+
def doc_note(note):
return "\nNotes\n-----\n%s" % note
@@ -765,7 +767,7 @@ def getmaskarray(arr):
"""
mask = getmask(arr)
if mask is nomask:
- mask = make_mask_none(np.shape(arr), getdata(arr).dtype.names)
+ mask = make_mask_none(np.shape(arr), getdata(arr).dtype)
return mask
def is_mask(m):
@@ -1102,7 +1104,7 @@ class _arraymethod(object):
"""
def __init__(self, funcname, onmask=True):
- self._name = funcname
+ self._name = self.__name__ = funcname
self._onmask = onmask
self.obj = None
self.__doc__ = self.getdoc()
@@ -1226,7 +1228,7 @@ class MaskedArray(ndarray):
# Process data............
_data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin)
_baseclass = getattr(data, '_baseclass', type(_data))
- _basedict = getattr(data, '_basedict', getattr(data, '__dict__', {}))
+ _optinfo = {}
# Careful, cls might not always be MaskedArray...
if not isinstance(data, cls) or not subok:
_data = _data.view(cls)
@@ -1312,7 +1314,7 @@ class MaskedArray(ndarray):
# Process extra options ..
_data._hardmask = hard_mask
_data._baseclass = _baseclass
- _data._basedict = _basedict
+ _data._optinfo = _data._basedict = _optinfo
return _data
#
def _update_from(self, obj):
@@ -1323,16 +1325,20 @@ class MaskedArray(ndarray):
else:
_baseclass = ndarray
# We need to copy the _basedict to avoid backward propagation
- _basedict = {}
- _basedict.update(getattr(obj, '_basedict', getattr(obj, '__dict__',{})))
+ _optinfo = {}
+ _optinfo.update(getattr(obj, '_optinfo', {}))
+ _optinfo.update(getattr(obj, '_basedict',{}))
+ if not isinstance(obj, MaskedArray):
+ _optinfo.update(getattr(obj, '__dict__', {}))
_dict = dict(_fill_value=getattr(obj, '_fill_value', None),
_hardmask=getattr(obj, '_hardmask', False),
_sharedmask=getattr(obj, '_sharedmask', False),
_isfield=getattr(obj, '_isfield', False),
_baseclass=getattr(obj,'_baseclass', _baseclass),
- _basedict=_basedict,)
+ _optinfo=_optinfo,
+ _basedict=_optinfo)
self.__dict__.update(_dict)
- self.__dict__.update(_basedict)
+ self.__dict__.update(_optinfo)
return
#........................
def __array_finalize__(self,obj):
@@ -1461,6 +1467,8 @@ class MaskedArray(ndarray):
# raise IndexError, msg
if isinstance(indx, basestring):
ndarray.__setitem__(self._data, indx, value)
+ if self._mask is nomask:
+ self._mask = make_mask_none(self.shape, self.dtype)
ndarray.__setitem__(self._mask, indx, getmask(value))
return
#........................................
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index b352c2970..e581e274e 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -437,14 +437,14 @@ class TestMaskedArray(TestCase):
np.array([(1, '1', 1.)], dtype=flexi.dtype))
- def test_basedict_propagation(self):
- "Checks that basedict isn't back-propagated"
+ def test_optinfo_propagation(self):
+ "Checks that _optinfo dictionary isn't back-propagated"
x = array([1,2,3,], dtype=float)
- x._basedict['info'] = '???'
+ x._optinfo['info'] = '???'
y = x.copy()
- assert_equal(y._basedict['info'],'???')
- y._basedict['info'] = '!!!'
- assert_equal(x._basedict['info'], '???')
+ assert_equal(y._optinfo['info'],'???')
+ y._optinfo['info'] = '!!!'
+ assert_equal(x._optinfo['info'], '???')
#------------------------------------------------------------------------------
@@ -918,7 +918,7 @@ class TestFillingValues(TestCase):
# We had a tailored comment to make sure special attributes are properly
# dealt with
a = array(['3', '4', '5'])
- a._basedict.update({'comment':"updated!"})
+ a._optinfo.update({'comment':"updated!"})
#
b = array(a, dtype=int)
assert_equal(b._data, [3,4,5])
@@ -931,7 +931,7 @@ class TestFillingValues(TestCase):
b = a.astype(int)
assert_equal(b._data, [3,4,5])
assert_equal(b.fill_value, default_fill_value(0))
- assert_equal(b._basedict['comment'], "updated!")
+ assert_equal(b._optinfo['comment'], "updated!")
#
b = a.astype([('a','|S3')])
assert_equal(b['a']._data, a._data)
@@ -2268,6 +2268,19 @@ class TestMaskedFields(TestCase):
for n in ('a','b','c'):
assert_equal(base[n].mask, [1,1,0,0,1])
assert_equal(base[n]._data, base._data[n])
+ #
+ def test_getmaskarray(self):
+ "Test getmaskarray on flexible dtype"
+ ndtype = [('a', int), ('b', float)]
+ test = empty(3, dtype=ndtype)
+ assert_equal(getmaskarray(test),
+ np.array([(0, 0) , (0, 0), (0, 0)],
+ dtype=[('a', '|b1'), ('b', '|b1')]))
+ test[:] = masked
+ assert_equal(getmaskarray(test),
+ np.array([(1, 1) , (1, 1), (1, 1)],
+ dtype=[('a', '|b1'), ('b', '|b1')]))
+
###############################################################################
#------------------------------------------------------------------------------