[Numpy-svn] r8094 - in trunk/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Feb 8 00:21:33 CST 2010


Author: pierregm
Date: 2010-02-08 00:21:33 -0600 (Mon, 08 Feb 2010)
New Revision: 8094

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
* Make sure _fill_value is never None for structured masked arrays

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2010-02-07 22:26:04 UTC (rev 8093)
+++ trunk/numpy/ma/core.py	2010-02-08 06:21:33 UTC (rev 8094)
@@ -2792,6 +2792,10 @@
             except AttributeError:
                 # When _mask.shape is not writable (because it's a void)
                 pass
+        # Finalize the fill_value for structured arrays
+        if self.dtype.names:
+            if self._fill_value is None:
+                self._fill_value = _check_fill_value(None, self.dtype)
         return
 
 
@@ -3569,7 +3573,8 @@
         elif n <= 1:
             return _print_templates['short'] % parameters
         return _print_templates['long'] % parameters
-    #............................................
+
+
     def __eq__(self, other):
         "Check whether other equals self elementwise"
         if self is masked:
@@ -5432,7 +5437,9 @@
     #
     def __new__(self, data, mask=nomask, dtype=None, fill_value=None):
         dtype = dtype or data.dtype
-        _data = ndarray.__new__(self, (), dtype=dtype, buffer=data.data)
+        _data = ndarray((), dtype=dtype)
+        _data[()] = data
+        _data = _data.view(self)
         if mask is not nomask:
             try:
                 # Mask is already a 0D array
@@ -5463,17 +5470,29 @@
             return self._data.__str__()
         m = tuple(m)
         if (not any(m)):
-            return self._data.__repr__()
+            return self._data.__str__()
         r = self._data.tolist()
         p = masked_print_option
         if not p.enabled():
             p = 'N/A'
         else:
             p = str(p)
-        r = [(str(_), p)[_m] for (_, _m) in zip(self._data.tolist(), tuple(m))]
+        r = [(str(_), p)[_m] for (_, _m) in zip(r, m)]
         return "(%s)" % ", ".join(r)
 
-    __repr__ = __str__
+    def __repr__(self):
+        m = self._mask
+        if (m is nomask):
+            return self._data.__repr__()
+        m = tuple(m)
+        if not any(m):
+            return self._data.__repr__()
+        p = masked_print_option
+        if not p.enabled():
+            return self.filled(self.fill_value).__repr__()
+        p = str(p)
+        r = [(str(_), p)[_m] for (_, _m) in zip(r, m)]
+        return "(%s)" % ", ".join(r)
 
     def __iter__(self):
         "Defines an iterator for mvoid"

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2010-02-07 22:26:04 UTC (rev 8093)
+++ trunk/numpy/ma/tests/test_core.py	2010-02-08 06:21:33 UTC (rev 8094)
@@ -503,7 +503,7 @@
     def test_filled_w_mvoid(self):
         "Test filled w/ mvoid"
         ndtype = [('a', int), ('b', float)]
-        a = mvoid(np.array((1, 2)), mask=[(0, 1)], dtype=ndtype)
+        a = mvoid((1, 2.), mask=[(0, 1)], dtype=ndtype)
         # Filled using default
         test = a.filled()
         assert_equal(tuple(test), (1, default_fill_value(1.)))
@@ -1457,6 +1457,19 @@
         assert_equal(tt._fill_value, np.array(10))
         assert_equal(tuple(t.fill_value), (10, default_fill_value(0)))
 
+    def test_fillvalue_implicit_structured_array(self):
+        "Check that fill_value is always defined for structured arrays"
+        ndtype = ('b', float)
+        adtype = ('a', float)
+        a = array([(1.,), (2.,)], mask=[(False,), (False,)],
+                  fill_value=(np.nan,), dtype=np.dtype([adtype]))
+        b = empty(a.shape, dtype=[adtype, ndtype])
+        b['a'] = a['a']
+        b['a'].set_fill_value(a['a'].fill_value)
+        f = b._fill_value[()]
+        assert(np.isnan(f[0]))
+        assert_equal(f[-1], default_fill_value(1.))
+
 #------------------------------------------------------------------------------
 
 class TestUfuncs(TestCase):



More information about the Numpy-svn mailing list