diff options
author | pierregm <pierregm@localhost> | 2009-12-18 22:13:33 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2009-12-18 22:13:33 +0000 |
commit | f8f753b33fd77211227d5e5dad2d0b9c2e3dedad (patch) | |
tree | 41f1ac7ec2af8e6b713976292a068c72ffa7f875 | |
parent | 2e28be893dd2a8180505683f3aa0cc72ebe0bc47 (diff) | |
download | numpy-f8f753b33fd77211227d5e5dad2d0b9c2e3dedad.tar.gz |
* Force _fill_value to a 0d array (so that field values can be propagated, bug #1332)
* Make .fill_value return a scalar (int, float, void...) and not a 0d array
-rw-r--r-- | numpy/ma/core.py | 49 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 17 |
2 files changed, 45 insertions, 21 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 83bff9516..25050f651 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -68,7 +68,6 @@ __all__ = ['MAError', 'MaskError', 'MaskType', 'MaskedArray', 'zeros'] import cPickle -import operator import numpy as np from numpy import ndarray, amax, amin, iscomplexobj, bool_ @@ -108,12 +107,12 @@ def get_object_signature(obj): try: sig = formatargspec(*getargspec(obj)) except TypeError, errmsg: - msg = "Unable to retrieve the signature of %s '%s'\n"\ - "(Initial error message: %s)" + sig = '' +# msg = "Unable to retrieve the signature of %s '%s'\n"\ +# "(Initial error message: %s)" # warnings.warn(msg % (type(obj), # getattr(obj, '__name__', '???'), # errmsg)) - sig = '' return sig @@ -411,10 +410,10 @@ def _check_fill_value(fill_value, ndtype): else: # In case we want to convert 1e+20 to int... try: - fill_value = np.array(fill_value, copy=False, dtype=ndtype).item() + fill_value = np.array(fill_value, copy=False, dtype=ndtype)#.item() except OverflowError: fill_value = default_fill_value(ndtype) - return fill_value + return np.array(fill_value) def set_fill_value(a, fill_value): @@ -478,7 +477,7 @@ def set_fill_value(a, fill_value): """ if isinstance(a, MaskedArray): - a._fill_value = _check_fill_value(fill_value, a.dtype) + a.set_fill_value(fill_value) return def get_fill_value(a): @@ -3259,23 +3258,21 @@ class MaskedArray(ndarray): """ warnings.warn('Use .data instead.', DeprecationWarning) return self._data - #............................................ - def _get_flat(self): - """Return a flat iterator. - """ + + def _get_flat(self): + "Return a flat iterator." return MaskedIterator(self) # def _set_flat (self, value): - """Set a flattened version of self to value. - - """ + "Set a flattened version of self to value." y = self.ravel() y[:] = value # flat = property(fget=_get_flat, fset=_set_flat, doc="Flat version of the array.") - #............................................ + + def get_fill_value(self): """ Return the filling value of the masked array. @@ -3302,7 +3299,7 @@ class MaskedArray(ndarray): """ if self._fill_value is None: self._fill_value = _check_fill_value(None, self.dtype) - return self._fill_value + return self._fill_value[()] def set_fill_value(self, value=None): """ @@ -3334,7 +3331,14 @@ class MaskedArray(ndarray): 1e+20 """ - self._fill_value = _check_fill_value(value, self.dtype) + target = _check_fill_value(value, self.dtype) + _fill_value = self._fill_value + if _fill_value is None: + # Create the attribute if it was undefined + self._fill_value = target + else: + # Don't overwrite the attribute, just fill it (for propagation) + _fill_value[()] = target fill_value = property(fget=get_fill_value, fset=set_fill_value, doc="Filling value.") @@ -5400,7 +5404,7 @@ class MaskedArray(ndarray): - a binary string for the mask. """ - (ver, shp, typ, isf, raw, msk, flv) = state + (_, shp, typ, isf, raw, msk, flv) = state ndarray.__setstate__(self, (shp, typ, isf, raw)) self._mask.__setstate__((shp, make_mask_descr(typ), isf, msk)) self.fill_value = flv @@ -5525,7 +5529,14 @@ class mvoid(MaskedArray): def tolist(self): """ - Transforms the object into a list + Transforms the mvoid object into a tuple. + + Masked fields are replaced by None. + + Returns + ------- + returned_tuple + Tuple of fields """ _mask = self._mask if _mask is nomask: diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 6b21ae015..ba3a23040 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1297,13 +1297,13 @@ class TestFillingValues(TestCase): fval = _check_fill_value([-999, -999.9, "???"], ndtype) self.failUnless(isinstance(fval, ndarray)) assert_equal(fval.item(), [-999, -999.9, "???"]) - # A check on Non should output the defaults + # A check on None should output the defaults fval = _check_fill_value(None, ndtype) self.failUnless(isinstance(fval, ndarray)) assert_equal(fval.item(), [default_fill_value(0), default_fill_value(0.), default_fill_value("0")]) - #.....Using a flexible type as fill_value should work + #.....Using a structured type as fill_value should work fill_val = np.array((-999, -999.9, "???"), dtype=ndtype) fval = _check_fill_value(fill_val, ndtype) self.failUnless(isinstance(fval, ndarray)) @@ -1378,6 +1378,7 @@ class TestFillingValues(TestCase): x.fill_value = 999 assert_equal(np.asarray(x.fill_value).dtype, float) assert_equal(x.fill_value, 999.) + assert_equal(x._fill_value, np.array(999.)) def test_fillvalue_exotic_dtype(self): @@ -1429,6 +1430,18 @@ class TestFillingValues(TestCase): assert_equal(test[1][1], maximum_fill_value(a['B']['BB'])) assert_equal(test[1], maximum_fill_value(a['B'])) + def test_fillvalue_individual_fields(self): + "Test setting fill_value on individual fields" + ndtype = [('a', int), ('b', int)] + a = array(zip([1, 2, 3], [4, 5, 6]), + fill_value=(-999, -999), dtype=ndtype) + f = a._fill_value + aa = a['a'] + aa.set_fill_value(10) + assert_equal(aa._fill_value, np.array(10)) + assert_equal(tuple(a.fill_value), (10, -999)) + a.fill_value['b'] = -10 + assert_equal(tuple(a.fill_value), (10, -10)) #------------------------------------------------------------------------------ |