[Numpy-svn] r4758 - in branches/maskedarray/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Jan 28 16:46:23 CST 2008


Author: pierregm
Date: 2008-01-28 16:45:54 -0600 (Mon, 28 Jan 2008)
New Revision: 4758

Modified:
   branches/maskedarray/numpy/ma/core.py
   branches/maskedarray/numpy/ma/tests/test_core.py
Log:
maskedarray.core:
* fixed compress (third time the charm?)
* fixed a pb w/ get_fill_value when fill_value is None: now recognized records.

Modified: branches/maskedarray/numpy/ma/core.py
===================================================================
--- branches/maskedarray/numpy/ma/core.py	2008-01-26 06:53:45 UTC (rev 4757)
+++ branches/maskedarray/numpy/ma/core.py	2008-01-28 22:45:54 UTC (rev 4758)
@@ -1497,7 +1497,7 @@
 
         """
         if self._fill_value is None:
-            self._fill_value = default_fill_value(self)
+            self._fill_value = _check_fill_value(None, self.dtype)
         return self._fill_value
 
     def set_fill_value(self, value=None):
@@ -1569,6 +1569,32 @@
 #        if not self._shrinkmask:
 #            data._mask = numpy.zeros(data.shape, dtype=MaskType)
         return data
+    
+    
+    def compress(self, condition, axis=None, out=None):
+        """Return a where condition is True.
+        If condition is a MaskedArray, missing values are considered as False.
+        
+        Returns
+        -------
+        A MaskedArray object.
+        
+        Notes
+        -----
+        Please note the difference with compressed() ! 
+        The output of compress has a mask, the output of compressed does not.
+    
+        """
+        # Get the basic components
+        (_data, _mask) = (self._data, self._mask)
+        # Force the condition to a regular ndarray (forget the missing values...)
+        condition = narray(condition, copy=False, subok=False)
+        #
+        _new = _data.compress(condition, axis=axis, out=out).view(type(self))
+        _new._update_from(self)
+        if _mask is not nomask:
+            _new._mask = _mask.compress(condition, axis=axis)
+        return _new
 
     #............................................
     def __str__(self):
@@ -2449,7 +2475,6 @@
     T = property(fget=lambda self:self.transpose())
     swapaxes = _arraymethod('swapaxes')
     clip = _arraymethod('clip', onmask=False)
-    compress = _arraymethod('compress')
     copy = _arraymethod('copy')
     squeeze = _arraymethod('squeeze')
     #--------------------------------------------
@@ -2756,6 +2781,7 @@
 swapaxes = _frommethod('swapaxes')
 take = _frommethod('take')
 var = _frommethod('var')
+compress = _frommethod('compress')
 
 #..............................................................................
 def power(a, b, third=None):
@@ -3100,36 +3126,6 @@
     m = make_mask(mask_or(m, getmask(indices)), copy=0, shrink=True)
     return masked_array(d, mask=m)
 
-def compress(a, condition, axis=None, out=None):
-    """Return a where condition is True.
-    If condition is a MaskedArray, missing values are considered as False.
-    
-    Returns
-    -------
-    A MaskedArray object.
-    
-    Notes
-    -----
-    Please note the difference with compressed() ! 
-    The output of compress has a mask, the output of compressed does not.
-
-    """
-    # Get the basic components
-    (_data, _mask) = (getdata(a), getmask(a))
-    # Get the type of output
-    if isinstance(a, MaskedArray):
-        _view = type(a)
-    else:
-        _view = MaskedArray
-    # Force the condition to a regular ndarray (forget the missing values...)
-    condition = numpy.array(condition, copy=False, subok=False)
-    #
-    _new = ndarray.compress(_data, condition, axis=axis, out=out).view(_view)
-    _new._update_from(a)
-    if _mask is not nomask:
-        _new._mask = _mask.compress(condition, axis=axis)
-    return _new
-
 def round_(a, decimals=0, out=None):
     """Return a copy of a, rounded to 'decimals' places.
 

Modified: branches/maskedarray/numpy/ma/tests/test_core.py
===================================================================
--- branches/maskedarray/numpy/ma/tests/test_core.py	2008-01-26 06:53:45 UTC (rev 4757)
+++ branches/maskedarray/numpy/ma/tests/test_core.py	2008-01-28 22:45:54 UTC (rev 4758)
@@ -1417,6 +1417,22 @@
         b = a.compress(x >= 2, axis=1)    
         assert_equal(b._data, [[10,30],[40,60]])
         assert_equal(b._mask, [[0,1],[1,0]])
+    #
+    def test_empty(self):
+        "Tests empty/like"
+        datatype = [('a',int_),('b',float_),('c','|S8')]
+        a = masked_array([(1,1.1,'1.1'),(2,2.2,'2.2'),(3,3.3,'3.3')],
+                         dtype=datatype)
+        assert_equal(len(a.fill_value), len(datatype))
+        #
+        b = empty_like(a)
+        assert_equal(b.shape, a.shape)
+        assert_equal(b.fill_value, a.fill_value)
+        #
+        b = empty(len(a), dtype=datatype)
+        assert_equal(b.shape, a.shape)
+        assert_equal(b.fill_value, a.fill_value)
+        print "test_empty passed!"
 
 
 #..............................................................................



More information about the Numpy-svn mailing list