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

numpy-svn@scip... numpy-svn@scip...
Tue Jan 22 04:27:03 CST 2008


Author: stefan
Date: 2008-01-22 04:26:31 -0600 (Tue, 22 Jan 2008)
New Revision: 4742

Modified:
   branches/maskedarray/numpy/ma/core.py
   branches/maskedarray/numpy/ma/mrecords.py
   branches/maskedarray/numpy/ma/tests/test_core.py
Log:
Merge patch fixing mask dimensions on concatenation.


Modified: branches/maskedarray/numpy/ma/core.py
===================================================================
--- branches/maskedarray/numpy/ma/core.py	2008-01-22 06:31:01 UTC (rev 4741)
+++ branches/maskedarray/numpy/ma/core.py	2008-01-22 10:26:31 UTC (rev 4742)
@@ -178,6 +178,26 @@
     else:
         raise TypeError, 'Unsuitable type for calculating minimum.'
 
+
+def _check_fill_value(fill_value, dtype):
+    descr = numpy.dtype(dtype).descr
+    if fill_value is None:
+        if len(descr) > 1:
+            fill_value = [default_fill_value(numeric.dtype(d[1]))
+                          for d in descr]
+        else:
+            fill_value = default_fill_value(dtype)
+    else:
+        fval = numpy.resize(narray(fill_value,copy=False,dtype=object_),
+                            len(descr))
+        if len(descr) > 1:
+            fill_value = [numpy.asarray(f).astype(d[1]).item()
+                          for (f,d) in zip(fval, descr)]
+        else:
+            fill_value = narray(fval, copy=False, dtype=dtype).item()
+    return fill_value
+
+
 def set_fill_value(a, fill_value):
     """Set the filling value of a, if a is a masked array.  Otherwise,
     do nothing.
@@ -187,7 +207,7 @@
 
     """
     if isinstance(a, MaskedArray):
-        a.set_fill_value(fill_value)
+        a._fill_value = _check_fill_value(fill_value, a.dtype)
     return
 
 def get_fill_value(a):
@@ -212,6 +232,7 @@
         return t1
     return None
 
+
 #####--------------------------------------------------------------------------
 def filled(a, value = None):
     """Return a as an array with masked data replaced by value.  If
@@ -402,7 +423,7 @@
         #
         m = getmask(a)
         d1 = get_data(a)
-        #        
+        #
         if self.domain is not None:
             dm = narray(self.domain(d1), copy=False)
             m = numpy.logical_or(m, dm)
@@ -1090,8 +1111,8 @@
             masked values cannot be unmasked.
         shrink : {True, boolean}
             Whether to force compression of an empty mask.
-        
 
+
     """
 
     __array_priority__ = 15
@@ -1100,8 +1121,8 @@
     _baseclass =  numeric.ndarray
 
     def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
-                subok=True, ndmin=0, fill_value=None, 
-                keep_mask=True, hard_mask=False, flag=None,shrink=True, 
+                subok=True, ndmin=0, fill_value=None,
+                keep_mask=True, hard_mask=False, flag=None,shrink=True,
                 **options):
         """Create a new masked array from scratch.
 
@@ -1162,11 +1183,7 @@
                     _data._sharedmask = False
 
         # Update fill_value.......
-        if fill_value is None:
-            _data._fill_value = getattr(data, '_fill_value',
-                                        default_fill_value(_data))
-        else:
-            _data._fill_value = fill_value
+        _data._fill_value = _check_fill_value(fill_value, _data.dtype)
         # Process extra options ..
         _data._hardmask = hard_mask
         _data._baseclass = _baseclass
@@ -1210,7 +1227,7 @@
         if context is not None:
             result._mask = result._mask.copy()
             (func, args, _) = context
-            m = reduce(mask_or, [getmask(arg) for arg in args])
+            m = reduce(mask_or, [getmaskarray(arg) for arg in args])
             # Get the domain mask................
             domain = ufunc_domain.get(func, None)
             if domain is not None:
@@ -1266,6 +1283,10 @@
             dout = dout.view(type(self))
             # Inherit attributes from self
             dout._update_from(self)
+            # Check the fill_value ....
+            if isinstance(indx, basestring):
+                fvindx = list(self.dtype.names).index(indx)
+                dout._fill_value = self._fill_value[fvindx]
             # Update the mask if needed
             if m is not nomask:
                 if isinstance(indx, basestring):
@@ -1459,9 +1480,7 @@
         If value is None, use a default based on the data type.
 
         """
-        if value is None:
-            value = default_fill_value(self)
-        self._fill_value = value
+        self._fill_value = _check_fill_value(value,self.dtype)
 
     fill_value = property(fget=get_fill_value, fset=set_fill_value,
                           doc="Filling value.")
@@ -1804,7 +1823,7 @@
     def ids (self):
         """Return the addresses of the data and mask areas."""
         if self._mask is nomask:
-            return (self.ctypes.data, id(nomask))        
+            return (self.ctypes.data, id(nomask))
         return (self.ctypes.data, self._mask.ctypes.data)
     #............................................
     def all(self, axis=None, out=None):
@@ -2304,13 +2323,13 @@
         if result.ndim > 0:
             result._mask = mask
         return result
-    
-    def mini(self, axis=None):    
+
+    def mini(self, axis=None):
         if axis is None:
             return minimum(self)
         else:
             return minimum.reduce(self, axis)
-    
+
     #........................
     def max(self, axis=None, fill_value=None):
         """Return the maximum/a along the given axis.
@@ -2493,9 +2512,9 @@
 
 masked_array = MaskedArray
 
-def array(data, dtype=None, copy=False, order=False, 
-          mask=nomask, fill_value=None, 
-          keep_mask=True, hard_mask=False, shrink=True, subok=True, ndmin=0, 
+def array(data, dtype=None, copy=False, order=False,
+          mask=nomask, fill_value=None,
+          keep_mask=True, hard_mask=False, shrink=True, subok=True, ndmin=0,
           ):
     """array(data, dtype=None, copy=False, order=False, mask=nomask,
              fill_value=None, keep_mask=True, hard_mask=False, shrink=True,
@@ -2840,7 +2859,7 @@
     If values is not the same size of a and mask then it will repeat
     as necessary.  This gives different behavior than
     a[mask] = values.
-    
+
     Note: Using a masked array as values will NOT transform a ndarray in
           a maskedarray.
 
@@ -3207,4 +3226,3 @@
 def loads(strg):
     "Load a pickle from the current string."""
     return cPickle.loads(strg)
-

Modified: branches/maskedarray/numpy/ma/mrecords.py
===================================================================
--- branches/maskedarray/numpy/ma/mrecords.py	2008-01-22 06:31:01 UTC (rev 4741)
+++ branches/maskedarray/numpy/ma/mrecords.py	2008-01-22 10:26:31 UTC (rev 4742)
@@ -2,13 +2,8 @@
 Defines a class of record arrays supporting masked arrays.
 
 :author: Pierre Gerard-Marchant
-:contact: pierregm_at_uga_dot_edu
-:version: $Id: mrecords.py 3473 2007-10-29 15:18:13Z jarrod.millman $
 """
-__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
-__version__ = '1.0'
-__revision__ = "$Revision: 3473 $"
-__date__     = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+__author__ = "Pierre GF Gerard-Marchant"
 
 import sys
 import types
@@ -249,7 +244,7 @@
         _localdict = self.__dict__
         _data = self._data
         # We want a field ........
-        if isinstance(indx, str):
+        if isinstance(indx, basestring):
             obj = _data[indx].view(MaskedArray)
             obj._set_mask(_localdict['_fieldmask'][indx])
             # Force to nomask if the mask is empty

Modified: branches/maskedarray/numpy/ma/tests/test_core.py
===================================================================
--- branches/maskedarray/numpy/ma/tests/test_core.py	2008-01-22 06:31:01 UTC (rev 4741)
+++ branches/maskedarray/numpy/ma/tests/test_core.py	2008-01-22 10:26:31 UTC (rev 4742)
@@ -3,12 +3,8 @@
 
 :author: Pierre Gerard-Marchant
 :contact: pierregm_at_uga_dot_edu
-:version: $Id: test_core.py 3473 2007-10-29 15:18:13Z jarrod.millman $
 """
-__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
-__version__ = '1.0'
-__revision__ = "$Revision: 3473 $"
-__date__     = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+__author__ = "Pierre GF Gerard-Marchant"
 
 import types
 import warnings
@@ -32,18 +28,6 @@
 from test_old_ma import *
 restore_path()
 
-class TestNoMask(NumpyTestCase):
-    def test_no_inplace(self):
-        x = nomask
-        y = x
-        x += 1
-        assert x != y
-
-    def test_no_copy(self):
-        x = nomask
-        y = x.copy()
-        assert x is y
-
 #..............................................................................
 class TestMA(NumpyTestCase):
     "Base test class for MaskedArrays."
@@ -352,6 +336,11 @@
         assert(xm[0].ptp() is masked)
         assert(xm[0].ptp(0) is masked)
         assert(xm[0].ptp(-1) is masked)
+        #
+        x = array([1,2,3], mask=True)
+        assert(x.min() is masked)
+        assert(x.max() is masked)
+        assert(x.ptp() is masked)
     #........................
     def test_addsumprod (self):
         "Tests add, sum, product."
@@ -390,6 +379,16 @@
         xmym = concatenate((xm,ym),1)
         assert_equal(numpy.concatenate((x,y),1), xmym)
         assert_equal(numpy.concatenate((xm.mask,ym.mask),1), xmym._mask)
+        #
+        x=zeros(2)
+        y=array(ones(2),mask=[False,True])
+        z = concatenate((x,y))
+        assert_array_equal(z,[0,0,1,1])
+        assert_array_equal(z.mask,[False,False,False,True])
+        z = concatenate((y,x))
+        assert_array_equal(z,[1,1,0,0])
+        assert_array_equal(z.mask,[False,True,False,False])
+
     #........................
     def test_indexing(self):
         "Tests conversions and indexing"
@@ -774,10 +773,27 @@
         assert(isinstance(a_pickled._data,numpy.matrix))
     #
     def test_fillvalue(self):
-        "Check that we don't lose the fill_value"
+        "Having fun with the fill_value"
         data = masked_array([1,2,3],fill_value=-999)
         series = data[[0,2,1]]
         assert_equal(series._fill_value, data._fill_value)
+        #
+        mtype = [('f',float_),('s','|S3')]
+        x = array([(1,'a'),(2,'b'),(numpy.pi,'pi')], dtype=mtype)
+        x.fill_value=999
+        assert_equal(x.fill_value,[999.,'999'])
+        assert_equal(x['f'].fill_value, 999)
+        assert_equal(x['s'].fill_value, '999')
+        #
+        x.fill_value=(9,'???')
+        assert_equal(x.fill_value, (9,'???'))
+        assert_equal(x['f'].fill_value, 9)
+        assert_equal(x['s'].fill_value, '???')
+        #
+        x = array([1,2,3.1])
+        x.fill_value = 999
+        assert_equal(numpy.asarray(x.fill_value).dtype, float_)
+        assert_equal(x.fill_value, 999.)
     #
     def test_asarray(self):
         (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
@@ -793,6 +809,7 @@
         assert_equal(data_fixed._mask, [1., 0., 1.])
     #
     def test_imag_real(self):
+        "Check complex"
         xx = array([1+10j,20+2j], mask=[1,0])
         assert_equal(xx.imag,[10,2])
         assert_equal(xx.imag.filled(), [1e+20,2])
@@ -800,7 +817,30 @@
         assert_equal(xx.real,[1,20])
         assert_equal(xx.real.filled(), [1e+20,20])
         assert_equal(xx.real.dtype, xx._data.real.dtype)
+    #
+    def test_ndmin(self):
+        "Check the use of ndmin"
+        x = array([1,2,3],mask=[1,0,0], ndmin=2)
+        assert_equal(x.shape,(1,3))
+        assert_equal(x._data,[[1,2,3]])
+        assert_equal(x._mask,[[1,0,0]])
+    #
+    def test_record(self):
+        "Check record access"
+        mtype = [('f',float_),('s','|S3')]
+        x = array([(1,'a'),(2,'b'),(numpy.pi,'pi')], dtype=mtype)
+        x[1] = masked
+        #
+        (xf, xs) = (x['f'], x['s'])
+        assert_equal(xf.data, [1,2,numpy.pi])
+        assert_equal(xf.mask, [0,1,0])
+        assert_equal(xf.dtype, float_)
+        assert_equal(xs.data, ['a', 'b', 'pi'])
+        assert_equal(xs.mask, [0,1,0])
+        assert_equal(xs.dtype, '|S3')
+    #
 
+
 #...............................................................................
 
 class TestUfuncs(NumpyTestCase):
@@ -1348,37 +1388,7 @@
         putmask(mxx, mask, values)
         assert_equal(mxx, [1,2,30,4,5,60])
 
-    def test_ndmin(self):
-        x = array([1,2,3],mask=[1,0,0], ndmin=2)
-        assert_equal(x.shape,(1,3))
-        assert_equal(x._data,[[1,2,3]])
-        assert_equal(x._mask,[[1,0,0]])
 
-    def test_sumprod_masked(self):
-        x = masked_array([1,2,3], mask=True)
-        z = x.min()
-        assert(x.sum() is masked)
-        assert(x.prod() is masked)
-
-    def test_fancy_dtype(self):
-        mtype = [('f',float_),('s','|S3')]
-        x = array([(1,'a'),(2,'b'),(numpy.pi,'pi')], dtype=mtype)
-        x[1] = masked
-        x['f'] = 17
-
-    def test_concat(self):
-         x=zeros(2)
-         y=array(ones(2),mask=[False,True])
-
-         z = concatenate((x,y))
-         assert_array_equal(z,[0,0,1,1])
-         assert_array_equal(z.mask,[False,False,False,True])
-
-         z = concatenate((y,x))
-         assert_array_equal(z,[1,1,0,0])
-         assert_array_equal(z.mask,[False,True,False,False])
-
-
 #..............................................................................
 
 ###############################################################################



More information about the Numpy-svn mailing list