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

numpy-svn@scip... numpy-svn@scip...
Mon Apr 14 11:07:27 CDT 2008


Author: pierregm
Date: 2008-04-14 11:07:22 -0500 (Mon, 14 Apr 2008)
New Revision: 5032

Added:
   trunk/numpy/ma/.RData
   trunk/numpy/ma/.Rhistory
Modified:
   trunk/numpy/ma/
   trunk/numpy/ma/core.py
   trunk/numpy/ma/extras.py
   trunk/numpy/ma/mrecords.py
   trunk/numpy/ma/tests/test_mrecords.py
Log:
core:
fix_invalid : use isfinite & skip setting a mask is there's no invalid data
_update_from: force the default hardmask to False (instead of relying on class default)

extras:
cleanup

mrecords:
modified to meet new standards (import numpy as np)
__array_finalize__ : skip the call to _update_from and directly update __dict__
__setmask__        : allow an update from a valid fieldmask
mask               : as recognizable property


Property changes on: trunk/numpy/ma
___________________________________________________________________
Name: svn:ignore
   + core_tmp.py
ma_old.py


Added: trunk/numpy/ma/.RData
===================================================================
(Binary files differ)


Property changes on: trunk/numpy/ma/.RData
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/numpy/ma/.Rhistory
===================================================================
--- trunk/numpy/ma/.Rhistory	2008-04-13 21:26:04 UTC (rev 5031)
+++ trunk/numpy/ma/.Rhistory	2008-04-14 16:07:22 UTC (rev 5032)
@@ -0,0 +1,41 @@
+winter <- c(NA,NA,4,2,16,26,5,1,5,1,2,3,1)
+spring <- c(  4,  3,  5,  3,  2,  7,  3,  1,  1,  2,  3,  5,  3)
+cov(winter,spring)
+cov(winter,spring,use="complte.obs")
+cov(winter,spring,use="complete.obs")
+cor(winter,spring,use="complete.obs")
+cov2cor(winter,spring,use="complete.obs")
+help(cov2cor)
+swiss
+cor(winter,spring,use="complete.obs")
+ccf(winter,spring)
+ccf(winter,spring,na.action=pass)
+ccf(winter,spring,na.action=na.pass)
+ccf(winter,na.action=na.pass)
+acf(winter,na.action=na.pass)
+cov(winter,spring,use="complete.obs")
+cov(winter,use="complete.obs")
+cov(winter,winter,use="complete.obs")
+winter <- c(NA,NA,4,2,16,26,5,1,5,1,2,3,1)
+spring <- c(  4,  3,  5,  3,  2,  7,  3,  1,  1,  2,  3,  5,  3)
+summer <- c( 3,  2,  5,  6, 18,  4,  9,  1,  1, NA,  1,  1, NA)
+fall <- c(NA,  6, 11,  4, 17,NA,  6,  1,  1,  2,  5,  1,  1]
+fall <- c(NA,  6, 11,  4, 17,NA,  6,  1,  1,  2,  5,  1,  1)
+cov(winter,summer,use="complete.obs")
+cov(winter,fall,use="complete.obs")
+anova(winter,spring)
+fit<-lm(winter~spring)
+anova(fit)
+winter
+win<-c(0,0,4,2,16,26,5,1,5,1,2,3,1)
+spring
+fit<-lm(win~spring)
+anova(fit)
+fit<-lm(spring~win)
+anova(fit)
+xx
+x
+x<-c(1,2,3,4,5,6,7,8,9,10)
+y<-c(10,9,8,7,6,5,4,4,4,4)
+fit<-lm(x~y)
+anova(fit)

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-04-13 21:26:04 UTC (rev 5031)
+++ trunk/numpy/ma/core.py	2008-04-14 16:07:22 UTC (rev 5032)
@@ -330,7 +330,10 @@
 
     """
     a = masked_array(a, copy=copy, subok=True)
-    invalid = (numpy.isnan(a._data) | numpy.isinf(a._data))
+    #invalid = (numpy.isnan(a._data) | numpy.isinf(a._data))
+    invalid = numpy.logical_not(numpy.isfinite(a._data))
+    if not invalid.any():
+        return a
     a._mask |= invalid
     if fill_value is None:
         fill_value = a.fill_value
@@ -1224,7 +1227,7 @@
     def _update_from(self, obj):
         """Copies some attributes of obj to self.
         """
-        self._hardmask = getattr(obj, '_hardmask', self._defaulthardmask)
+        self._hardmask = getattr(obj, '_hardmask', False)
         self._sharedmask = getattr(obj, '_sharedmask', False)
         if obj is not None:
             self._baseclass = getattr(obj, '_baseclass', type(obj))

Modified: trunk/numpy/ma/extras.py
===================================================================
--- trunk/numpy/ma/extras.py	2008-04-13 21:26:04 UTC (rev 5031)
+++ trunk/numpy/ma/extras.py	2008-04-14 16:07:22 UTC (rev 5032)
@@ -824,6 +824,3 @@
     return result
 
 ################################################################################
-testmathworks = fix_invalid([1.165 , 0.6268, 0.0751, 0.3516, -0.6965, 
-                                        numpy.nan])
-expand_dims(testmathworks.mean(0),0)
\ No newline at end of file

Modified: trunk/numpy/ma/mrecords.py
===================================================================
--- trunk/numpy/ma/mrecords.py	2008-04-13 21:26:04 UTC (rev 5031)
+++ trunk/numpy/ma/mrecords.py	2008-04-14 16:07:22 UTC (rev 5032)
@@ -18,25 +18,22 @@
 import sys
 import types
 
-import numpy
-from numpy import bool_, complex_, float_, int_, str_, object_, dtype
-from numpy import array as narray
-import numpy.core.numeric as numeric
+import numpy as np
+from numpy import bool_, complex_, float_, int_, str_, object_, dtype, \
+    chararray, ndarray, recarray, record, array as narray
 import numpy.core.numerictypes as ntypes
-from numpy.core.defchararray import chararray
-from numpy.core.records import find_duplicate, format_parser, record, recarray
-from numpy.core.records import fromarrays as recfromarrays
-from numpy.core.records import fromrecords as recfromrecords
+from numpy.core.records import find_duplicate, format_parser
+from numpy.core.records import fromarrays as recfromarrays, \
+    fromrecords as recfromrecords
 
-ndarray = numeric.ndarray
-_byteorderconv = numpy.core.records._byteorderconv
+_byteorderconv = np.core.records._byteorderconv
 _typestr = ntypes._typestr
 
-import numpy.ma
+import numpy.ma as ma
 from numpy.ma import MAError, MaskedArray, masked, nomask, masked_array,\
-    make_mask, mask_or, getdata, getmask, getmaskarray, filled
-from numpy.ma.core import default_fill_value, masked_print_option
-_check_fill_value = numpy.ma.core._check_fill_value
+    make_mask, mask_or, getdata, getmask, getmaskarray, filled, \
+    default_fill_value, masked_print_option
+_check_fill_value = ma.core._check_fill_value
 
 import warnings
 
@@ -53,7 +50,7 @@
 
     formats = ''
     for obj in data:
-        obj = numeric.asarray(obj)
+        obj = np.asarray(obj)
 #        if not isinstance(obj, ndarray):
 ##        if not isinstance(obj, ndarray):
 #            raise ValueError, "item in the array list must be an ndarray."
@@ -96,7 +93,7 @@
 
 def _get_fieldmask(self):
     mdescr = [(n,'|b1') for n in self.dtype.names]
-    fdmask = numpy.empty(self.shape, dtype=mdescr)
+    fdmask = np.empty(self.shape, dtype=mdescr)
     fdmask.flat = tuple([False]*len(mdescr))
     return fdmask
 
@@ -130,17 +127,17 @@
 #        self = self.view(cls)
         #
         mdtype = [(k,'|b1') for (k,_) in self.dtype.descr]
-        if mask is nomask or not numpy.size(mask):
+        if mask is nomask or not np.size(mask):
             if not keep_mask:
                 self._fieldmask = tuple([False]*len(mdtype))
         else:
-            mask = narray(mask, copy=copy)
+            mask = np.array(mask, copy=copy)
             if mask.shape != self.shape:
                 (nd, nm) = (self.size, mask.size)
                 if nm == 1:
-                    mask = numpy.resize(mask, self.shape)
+                    mask = np.resize(mask, self.shape)
                 elif nm == nd:
-                    mask = numpy.reshape(mask, self.shape)
+                    mask = np.reshape(mask, self.shape)
                 else:
                     msg = "Mask and data not compatible: data size is %i, "+\
                           "mask size is %i."
@@ -153,8 +150,8 @@
                 if mask.dtype == mdtype:
                     _fieldmask = mask
                 else:
-                    _fieldmask = narray([tuple([m]*len(mdtype)) for m in mask],
-                                        dtype=mdtype)
+                    _fieldmask = np.array([tuple([m]*len(mdtype)) for m in mask],
+                                          dtype=mdtype)
                 self._fieldmask = _fieldmask
         return self
     #......................................................
@@ -165,21 +162,25 @@
             mdescr = [(n,'|b1') for (n,_) in self.dtype.descr]
             _mask = getattr(obj, '_mask', nomask)
             if _mask is nomask:
-                _fieldmask = numpy.empty(self.shape, dtype=mdescr).view(recarray)
+                _fieldmask = np.empty(self.shape, dtype=mdescr).view(recarray)
                 _fieldmask.flat = tuple([False]*len(mdescr))
             else:
                 _fieldmask = narray([tuple([m]*len(mdescr)) for m in _mask],
                                     dtype=mdescr).view(recarray)
         # Update some of the attributes
+        if obj is not None:
+            _baseclass = getattr(obj,'_baseclass',type(obj))
+        else:
+            _baseclass = recarray
         attrdict = dict(_fieldmask=_fieldmask,
                         _hardmask=getattr(obj,'_hardmask',False),
-                        _fill_value=getattr(obj,'_fill_value',None))
+                        _fill_value=getattr(obj,'_fill_value',None),
+                        _sharedmask=getattr(obj,'_sharedmask',False),
+                        _baseclass=_baseclass)
         self.__dict__.update(attrdict)
         # Finalize as a regular maskedarray .....
-        # Note: we can't call MaskedArray.__array_finalize__, it chokes pickling
-        self._update_from(obj)
         # Update special attributes ...
-        self._basedict = getattr(obj, '_basedict', getattr(obj, '__dict__', None))
+        self._basedict = getattr(obj, '_basedict', getattr(obj,'__dict__',None))
         if self._basedict is not None:
             self.__dict__.update(self._basedict)
         return
@@ -193,6 +194,12 @@
         "Sets the mask and update the fieldmask."
         names = self.dtype.names
         fmask = self.__dict__['_fieldmask']
+        #
+        if isinstance(mask,ndarray) and mask.dtype.names == names:
+            for n in names:
+                fmask[n] = mask[n].astype(bool)
+#            self.__dict__['_fieldmask'] = fmask.view(recarray)
+            return
         newmask = make_mask(mask, copy=False)
         if names is not None:
             if self._hardmask:
@@ -213,7 +220,7 @@
             return self._fieldmask.view((bool_, len(self.dtype))).all(1)
         else:
             return self._fieldmask.view((bool_, len(self.dtype))).all()
-    _mask = property(fget=_getmask, fset=_setmask)
+    mask = _mask = property(fget=_getmask, fset=_setmask)
     #......................................................
     def get_fill_value(self):
         """Return the filling value.
@@ -222,7 +229,7 @@
         if self._fill_value is None:
             ddtype = self.dtype
             fillval = _check_fill_value(None, ddtype)
-            self._fill_value = narray(tuple(fillval), dtype=ddtype)
+            self._fill_value = np.array(tuple(fillval), dtype=ddtype)
         return self._fill_value
 
     def set_fill_value(self, value=None):
@@ -233,7 +240,7 @@
         """
         ddtype = self.dtype
         fillval = _check_fill_value(value, ddtype)
-        self._fill_value = narray(tuple(fillval), dtype=ddtype)
+        self._fill_value = np.array(tuple(fillval), dtype=ddtype)
 
     fill_value = property(fget=get_fill_value, fset=set_fill_value,
                           doc="Filling value.")
@@ -271,7 +278,7 @@
 
     def __setattr__(self, attr, val):
         "Sets the attribute attr to the value val."
-        newattr = attr not in self.__dict__
+#        newattr = attr not in self.__dict__
         try:
             # Is attr a generic attribute ?
             ret = object.__setattr__(self, attr, val)
@@ -282,17 +289,20 @@
                 exctype, value = sys.exc_info()[:2]
                 raise exctype, value
         else:
+            if attr in ['_mask','fieldmask']:
+                self.__setmask__(val)
+                return
             # Get the list of names ......
             _names = self.dtype.names
             if _names is None:
                 _names = []
             else:
                 _names = list(_names)
-            _names += ['_mask','mask']
             # Check the attribute
-            if attr not in _names:
+            self_dict = self.__dict__
+            if attr not in _names+list(self_dict):
                 return ret
-            if newattr:         # We just added this one
+            if attr not in self_dict:         # We just added this one
                 try:            #  or this setattr worked on an internal
                                 #  attribute.
                     object.__delattr__(self, attr)
@@ -300,7 +310,7 @@
                     return ret
         # Case #1.: Basic field ............
         base_fmask = self._fieldmask
-        _names = self.dtype.names
+        _names = self.dtype.names or []
         if attr in _names:
             if val is masked:
                 fval = self.fill_value[attr]
@@ -313,17 +323,6 @@
             self._data.__setattr__(attr, fval)
             base_fmask.__setattr__(attr, mval)
             return
-        elif attr == '_mask':
-            #FIXME: We should check for self._harmask over there.
-#            if self._hardmask:
-#                val = make_mask(val)
-#                if val is not nomask:
-##                    mval = getmaskarray(val)
-#                    for k in _names:
-#                        m = mask_or(val, base_fmask.__getattr__(k))
-#                        base_fmask.__setattr__(k, m)
-            self.__setmask__(val)
-            return
     #............................................
     def __getitem__(self, indx):
         """Returns all the fields sharing the same fieldname base.
@@ -369,7 +368,7 @@
                 m[n][i:j] = mval
         else:
             mindx = getmaskarray(self)[i:j]
-            dval = numeric.asarray(value)
+            dval = np.asarray(value)
             valmask = getmask(value)
             if valmask is nomask:
                 for n in names:
@@ -410,7 +409,7 @@
                 return ndarray.view(self, obj)
         except TypeError:
             pass
-        dtype = numeric.dtype(obj)
+        dtype = np.dtype(obj)
         if dtype.fields is None:
             return self.__array__().view(dtype)
         return ndarray.view(self, obj)
@@ -426,23 +425,22 @@
         _localdict = self.__dict__
         d = self._data
         fm = _localdict['_fieldmask']
-        if not numeric.asarray(fm, dtype=bool_).any():
+        if not np.asarray(fm, dtype=bool_).any():
             return d
         #
         if fill_value is None:
             value = _check_fill_value(_localdict['_fill_value'],self.dtype)
         else:
             value = fill_value
-            if numeric.size(value) == 1:
+            if np.size(value) == 1:
                 value = [value,] * len(self.dtype)
         #
         if self is masked:
-            result = numeric.asanyarray(value)
+            result = np.asanyarray(value)
         else:
             result = d.copy()
             for (n, v) in zip(d.dtype.names, value):
-                numpy.putmask(numeric.asarray(result[n]),
-                              numeric.asarray(fm[n]), v)
+                np.putmask(np.asarray(result[n]), np.asarray(fm[n]), v)
         return result
     #......................................................
     def harden_mask(self):
@@ -506,7 +504,7 @@
         (ver, shp, typ, isf, raw, msk, flv) = state
         ndarray.__setstate__(self, (shp, typ, isf, raw))
         mdtype = dtype([(k,bool_) for (k,_) in self.dtype.descr])
-        self._fieldmask.__setstate__((shp, mdtype, isf, msk))
+        self.__dict__['_fieldmask'].__setstate__((shp, mdtype, isf, msk))
         self.fill_value = flv
     #
     def __reduce__(self):
@@ -639,7 +637,7 @@
         mrec.fill_value = fill_value
     # Now, let's deal w/ the mask
     if mask is not nomask:
-        mask = narray(mask, copy=False)
+        mask = np.array(mask, copy=False)
         maskrecordlength = len(mask.dtype)
         if maskrecordlength:
             mrec._fieldmask.flat = mask
@@ -658,7 +656,7 @@
 on the first line. An exception is raised if the file is 3D or more.
     """
     vartypes = []
-    arr = numeric.asarray(arr)
+    arr = np.asarray(arr)
     if len(arr.shape) == 2 :
         arr = arr[0]
     elif len(arr.shape) > 2:
@@ -676,11 +674,11 @@
                 except ValueError:
                     vartypes.append(arr.dtype)
                 else:
-                    vartypes.append(complex_)
+                    vartypes.append(complex)
             else:
-                vartypes.append(float_)
+                vartypes.append(float)
         else:
-            vartypes.append(int_)
+            vartypes.append(int)
     return vartypes
 
 def openfile(fname):
@@ -741,7 +739,7 @@
     if vartypes is None:
         vartypes = _guessvartypes(_variables[0])
     else:
-        vartypes = [numeric.dtype(v) for v in vartypes]
+        vartypes = [np.dtype(v) for v in vartypes]
         if len(vartypes) != nfields:
             msg = "Attempting to %i dtypes for %i fields!"
             msg += " Reverting to default."
@@ -766,11 +764,10 @@
     _mask = mrecord._fieldmask
     if newfieldname is None or newfieldname in reserved_fields:
         newfieldname = 'f%i' % len(_data.dtype)
-    newfield = masked_array(newfield)
+    newfield = ma.array(newfield)
     # Get the new data ............
     # Create a new empty recarray
-    newdtype = numeric.dtype(_data.dtype.descr + \
-                             [(newfieldname, newfield.dtype)])
+    newdtype = np.dtype(_data.dtype.descr + [(newfieldname, newfield.dtype)])
     newdata = recarray(_data.shape, newdtype)
     # Add the exisintg field
     [newdata.setfield(_data.getfield(*f),*f)
@@ -780,7 +777,7 @@
     newdata = newdata.view(MaskedRecords)
     # Get the new mask .............
     # Create a new empty recarray
-    newmdtype = numeric.dtype([(n,bool_) for n in newdtype.names])
+    newmdtype = np.dtype([(n,bool_) for n in newdtype.names])
     newmask = recarray(_data.shape, newmdtype)
     # Add the old masks
     [newmask.setfield(_mask.getfield(*f),*f)
@@ -793,29 +790,20 @@
 
 ###############################################################################
 #
-#if 1:
-#    from numpy.ma.testutils import assert_equal
-#    if 1:
-#        ilist = [1,2,3,4,5]
-#        flist = [1.1,2.2,3.3,4.4,5.5]
-#        slist = ['one','two','three','four','five']
-#        ddtype = [('a',int_),('b',float_),('c','|S8')]
-#        mask = [0,1,0,0,1]
-#        self_base = masked_array(zip(ilist,flist,slist), mask=mask, dtype=ddtype)
-#    if 1:
-#        base = self_base.copy()
-#        mbase = base.view(mrecarray)
-#        mbase = mbase.copy()
-#        mbase.fill_value = (999999,1e20,'N/A')
-#        
-#        print mbase.a
-#        
-#        # Change the data, the mask should be conserved
-#        mbase.a._data[:] = 5
-#        assert_equal(mbase['a']._data, [5,5,5,5,5])
-#        assert_equal(mbase['a']._mask, [0,1,0,0,1])
-#        #
-#        z = mbase.a
-#        print z.tolist()
-#        z = base[:2]
-#        print z.view(mrecarray)
\ No newline at end of file
+if 1:
+    import numpy.ma as ma
+    from numpy.ma.testutils import *
+    if 1:
+        ilist = [1,2,3,4,5]
+        flist = [1.1,2.2,3.3,4.4,5.5]
+        slist = ['one','two','three','four','five']
+        ddtype = [('a',int),('b',float),('c','|S8')]
+        mask = [0,1,0,0,1]
+        self_base = ma.array(zip(ilist,flist,slist), mask=mask, dtype=ddtype)
+    if 1:
+        mbase = self_base.copy().view(mrecarray)
+        import cPickle
+        _ = cPickle.dumps(mbase)
+        mrec_ = cPickle.loads(_)
+        
+        
\ No newline at end of file

Modified: trunk/numpy/ma/tests/test_mrecords.py
===================================================================
--- trunk/numpy/ma/tests/test_mrecords.py	2008-04-13 21:26:04 UTC (rev 5031)
+++ trunk/numpy/ma/tests/test_mrecords.py	2008-04-14 16:07:22 UTC (rev 5032)
@@ -10,25 +10,19 @@
 
 import types
 
-import numpy as N
-from numpy import recarray, bool_, int_, float_
-from numpy import array as narray
-from numpy.core.records import fromrecords as recfromrecords
-from numpy.core.records import fromarrays as recfromarrays
-import numpy.core.fromnumeric  as fromnumeric_
-from numpy.testing import NumpyTest, NumpyTestCase
-from numpy.testing.utils import build_err_msg
+import numpy as np
+from numpy import recarray
+from numpy.core.records import fromrecords as recfromrecords, \
+    fromarrays as recfromarrays
 
 import numpy.ma.testutils
-from numpy.ma.testutils import assert_equal, assert_equal_records
+from numpy.ma.testutils import *
 
-import numpy.ma
-from numpy.ma import masked_array, masked, nomask, getdata, getmaskarray
+import numpy.ma as ma
+from numpy.ma import masked, nomask, getdata, getmaskarray
 
-#import numpy.ma.mrecords
-#from numpy.ma.mrecords import mrecarray, fromarrays, fromtextfile, fromrecords
-
 import numpy.ma.mrecords
+reload(numpy.ma.mrecords)
 from numpy.ma.mrecords import MaskedRecords, mrecarray,\
     fromarrays, fromtextfile, fromrecords, addfield
 
@@ -44,9 +38,9 @@
         ilist = [1,2,3,4,5]
         flist = [1.1,2.2,3.3,4.4,5.5]
         slist = ['one','two','three','four','five']
-        ddtype = [('a',int_),('b',float_),('c','|S8')]
+        ddtype = [('a',int),('b',float),('c','|S8')]
         mask = [0,1,0,0,1]
-        self.base = masked_array(zip(ilist,flist,slist), mask=mask, dtype=ddtype)
+        self.base = ma.array(zip(ilist,flist,slist), mask=mask, dtype=ddtype)
         
     def test_byview(self):
         "Test creation by view"
@@ -105,19 +99,19 @@
         # Change the elements, and the mask will follow
         mbase.a = 1
         assert_equal(mbase['a']._data, [1]*5)
-        assert_equal(getmaskarray(mbase['a']), [0]*5)
+        assert_equal(ma.getmaskarray(mbase['a']), [0]*5)
         assert_equal(mbase._mask, [False]*5)
         assert_equal(mbase._fieldmask.tolist(), 
-                     narray([(0,0,0),(0,1,1),(0,0,0),(0,0,0),(0,1,1)],
-                            dtype=bool_))
+                     np.array([(0,0,0),(0,1,1),(0,0,0),(0,0,0),(0,1,1)],
+                              dtype=bool))
         # Set a field to mask ........................
         mbase.c = masked
         assert_equal(mbase.c.mask, [1]*5)
-        assert_equal(getmaskarray(mbase['c']), [1]*5)
-        assert_equal(getdata(mbase['c']), ['N/A']*5)
+        assert_equal(ma.getmaskarray(mbase['c']), [1]*5)
+        assert_equal(ma.getdata(mbase['c']), ['N/A']*5)
         assert_equal(mbase._fieldmask.tolist(), 
-                     narray([(0,0,1),(0,1,1),(0,0,1),(0,0,1),(0,1,1)],
-                            dtype=bool_))
+                     np.array([(0,0,1),(0,1,1),(0,0,1),(0,0,1),(0,1,1)],
+                              dtype=bool))
         # Set fields by slices .......................
         mbase = base.view(mrecarray).copy()
         mbase.a[3:] = 5
@@ -132,27 +126,55 @@
         mbase = base.view(mrecarray)
         # Set the mask to True .......................
         mbase._mask = masked
-        assert_equal(getmaskarray(mbase['b']), [1]*5)
+        assert_equal(ma.getmaskarray(mbase['b']), [1]*5)
         assert_equal(mbase['a']._mask, mbase['b']._mask)
         assert_equal(mbase['a']._mask, mbase['c']._mask)
         assert_equal(mbase._fieldmask.tolist(), 
-                     narray([(1,1,1)]*5,
-                            dtype=bool_))
+                     np.array([(1,1,1)]*5, dtype=bool))
         # Delete the mask ............................
         mbase._mask = nomask
-        assert_equal(getmaskarray(mbase['c']), [0]*5)
+        assert_equal(ma.getmaskarray(mbase['c']), [0]*5)
         assert_equal(mbase._fieldmask.tolist(), 
-                     narray([(0,0,0)]*5,
-                            dtype=bool_))
+                     np.array([(0,0,0)]*5, dtype=bool))
     #
+    def test_set_mask_fromarray(self):
+        base = self.base.copy()
+        mbase = base.view(mrecarray)
+        # Sets the mask w/ an array
+        mbase._mask = [1,0,0,0,1]
+        assert_equal(mbase.a.mask, [1,0,0,0,1])
+        assert_equal(mbase.b.mask, [1,0,0,0,1])
+        assert_equal(mbase.c.mask, [1,0,0,0,1])
+        # Yay, once more !
+        mbase.mask = [0,0,0,0,1]
+        assert_equal(mbase.a.mask, [0,0,0,0,1])
+        assert_equal(mbase.b.mask, [0,0,0,0,1])
+        assert_equal(mbase.c.mask, [0,0,0,0,1])
+    #
+    def test_set_mask_fromfields(self):
+        mbase = self.base.copy().view(mrecarray)
+        #
+        nmask = np.array([(0,1,0),(0,1,0),(1,0,1),(1,0,1),(0,0,0)], 
+                         dtype=[('a',bool),('b',bool),('c',bool)])
+        mbase.mask = nmask
+        assert_equal(mbase.a.mask, [0,0,1,1,0])
+        assert_equal(mbase.b.mask, [1,1,0,0,0])
+        assert_equal(mbase.c.mask, [0,0,1,1,0])
+        # Reinitalizes and redo
+        mbase.mask = False
+        mbase.fieldmask = nmask
+        assert_equal(mbase.a.mask, [0,0,1,1,0])
+        assert_equal(mbase.b.mask, [1,1,0,0,0])
+        assert_equal(mbase.c.mask, [0,0,1,1,0])
+    #
     def test_set_elements(self):
         base = self.base.copy()
         mbase = base.view(mrecarray)
         # Set an element to mask .....................
         mbase[-2] = masked
         assert_equal(mbase._fieldmask.tolist(),
-                     narray([(0,0,0),(1,1,1),(0,0,0),(1,1,1),(1,1,1)],
-                             dtype=bool_))
+                     np.array([(0,0,0),(1,1,1),(0,0,0),(1,1,1),(1,1,1)],
+                              dtype=bool))
         assert_equal(mbase._mask, [0,1,0,1,1])
         # Set slices .................................
         mbase = base.view(mrecarray).copy()
@@ -214,23 +236,23 @@
     #
     def test_filled(self):
         "Test filling the array"
-        _a = masked_array([1,2,3],mask=[0,0,1],dtype=int_)
-        _b = masked_array([1.1,2.2,3.3],mask=[0,0,1],dtype=float_)
-        _c = masked_array(['one','two','three'],mask=[0,0,1],dtype='|S8')
-        ddtype = [('a',int_),('b',float_),('c','|S8')]
+        _a = ma.array([1,2,3],mask=[0,0,1],dtype=int)
+        _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float)
+        _c = ma.array(['one','two','three'],mask=[0,0,1],dtype='|S8')
+        ddtype = [('a',int),('b',float),('c','|S8')]
         mrec = fromarrays([_a,_b,_c], dtype=ddtype, 
                           fill_value=(99999,99999.,'N/A')) 
         mrecfilled = mrec.filled()
-        assert_equal(mrecfilled['a'], narray((1,2,99999), dtype=int_))
-        assert_equal(mrecfilled['b'], narray((1.1,2.2,99999.), dtype=float_))
-        assert_equal(mrecfilled['c'], narray(('one','two','N/A'), dtype='|S8'))
+        assert_equal(mrecfilled['a'], np.array((1,2,99999), dtype=int))
+        assert_equal(mrecfilled['b'], np.array((1.1,2.2,99999.), dtype=float))
+        assert_equal(mrecfilled['c'], np.array(('one','two','N/A'), dtype='|S8'))
     #
     def test_tolist(self):
         "Test tolist."
-        _a = masked_array([1,2,3],mask=[0,0,1],dtype=int_)
-        _b = masked_array([1.1,2.2,3.3],mask=[0,0,1],dtype=float_)
-        _c = masked_array(['one','two','three'],mask=[1,0,0],dtype='|S8')
-        ddtype = [('a',int_),('b',float_),('c','|S8')]
+        _a = ma.array([1,2,3],mask=[0,0,1],dtype=int)
+        _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float)
+        _c = ma.array(['one','two','three'],mask=[1,0,0],dtype='|S8')
+        ddtype = [('a',int),('b',float),('c','|S8')]
         mrec = fromarrays([_a,_b,_c], dtype=ddtype, 
                           fill_value=(99999,99999.,'N/A')) 
         #
@@ -246,19 +268,19 @@
 
     def setup(self):
         "Generic setup"
-        _a = masked_array([1,2,3],mask=[0,0,1],dtype=int_)
-        _b = masked_array([1.1,2.2,3.3],mask=[0,0,1],dtype=float_)
-        _c = masked_array(['one','two','three'],mask=[0,0,1],dtype='|S8')
-        ddtype = [('a',int_),('b',float_),('c','|S8')]
+        _a = ma.array([1,2,3],mask=[0,0,1],dtype=int)
+        _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float)
+        _c = ma.array(['one','two','three'],mask=[0,0,1],dtype='|S8')
+        ddtype = [('a',int),('b',float),('c','|S8')]
         mrec = fromarrays([_a,_b,_c], dtype=ddtype, 
                           fill_value=(99999,99999.,'N/A')) 
         nrec = recfromarrays((_a.data,_b.data,_c.data), dtype=ddtype)
         self.data = (mrec, nrec, ddtype)
         
     def test_fromarrays(self):
-        _a = masked_array([1,2,3],mask=[0,0,1],dtype=int_)
-        _b = masked_array([1.1,2.2,3.3],mask=[0,0,1],dtype=float_)
-        _c = masked_array(['one','two','three'],mask=[0,0,1],dtype='|S8')
+        _a = ma.array([1,2,3],mask=[0,0,1],dtype=int)
+        _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float)
+        _c = ma.array(['one','two','three'],mask=[0,0,1],dtype='|S8')
         (mrec, nrec, _) = self.data
         for (f,l) in zip(('a','b','c'),(_a,_b,_c)):
             assert_equal(getattr(mrec,f)._mask, l._mask)
@@ -281,7 +303,7 @@
             assert_equal(getattr(_mrec, field), getattr(mrec._data, field)) 
         #
         _mrec = fromrecords(nrec.tolist(), names='c1,c2,c3')
-        assert_equal(_mrec.dtype, [('c1',int_),('c2',float_),('c3','|S5')])
+        assert_equal(_mrec.dtype, [('c1',int),('c2',float),('c3','|S5')])
         for (f,n) in zip(('c1','c2','c3'), ('a','b','c')):
             assert_equal(getattr(_mrec,f), getattr(mrec._data, n))
         #
@@ -338,7 +360,7 @@
         "Tests addfield"
         (mrec, nrec, ddtype) = self.data
         (d,m) = ([100,200,300], [1,0,0])
-        mrec = addfield(mrec, masked_array(d, mask=m))
+        mrec = addfield(mrec, ma.array(d, mask=m))
         assert_equal(mrec.f3, d)
         assert_equal(mrec.f3._mask, m)
 



More information about the Numpy-svn mailing list