[Scipy-svn] r2412 - in trunk/Lib/sandbox/maskedarray: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Thu Dec 14 20:00:00 EST 2006
Author: pierregm
Date: 2006-12-14 18:59:51 -0600 (Thu, 14 Dec 2006)
New Revision: 2412
Added:
trunk/Lib/sandbox/maskedarray/.project
trunk/Lib/sandbox/maskedarray/src/
trunk/Lib/sandbox/maskedarray/tests/test_core.py
trunk/Lib/sandbox/maskedarray/tests/test_extras.py
Modified:
trunk/Lib/sandbox/maskedarray/CHANGELOG
trunk/Lib/sandbox/maskedarray/__init__.py
trunk/Lib/sandbox/maskedarray/core.py
trunk/Lib/sandbox/maskedarray/extras.py
trunk/Lib/sandbox/maskedarray/setup.py
trunk/Lib/sandbox/maskedarray/testutils.py
Log:
see changelog
Added: trunk/Lib/sandbox/maskedarray/.project
===================================================================
--- trunk/Lib/sandbox/maskedarray/.project 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/.project 2006-12-15 00:59:51 UTC (rev 2412)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>scipy_svn_maskedarray</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.python.pydev.PyDevBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.python.pydev.pythonNature</nature>
+ </natures>
+</projectDescription>
Modified: trunk/Lib/sandbox/maskedarray/CHANGELOG
===================================================================
--- trunk/Lib/sandbox/maskedarray/CHANGELOG 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/CHANGELOG 2006-12-15 00:59:51 UTC (rev 2412)
@@ -1,18 +1,28 @@
-#2006-12-09: - Code reorganization: define 2 modules, core and extras
-#2006-11-25: - Disable copy by default
-# - Added keep_mask flag (to save mask when creating a ma from a ma)
-# - Fixed functions: empty_like
-# - Fixed methods: .any and .all
-# - New functions: masked_all, masked_all_like
-# - New methods: .squeeze
-#2006-11-20: - fixed make_mask
-# - fixed nonzero method
-#2006-11-16: - fixed .T
-#2006-11-12: - add max, min as function (not only method...)
-# - repr returns a name like masked_xxx, where xxx is the subclass
-#2006-10-31: - make sure that make_mask returns a pure ndarray.
-#2006-10-30: - When converted to a float, a masked singleton is transformed to nan
-# instead of raising an exception.
+#2006-12-13 : - moved 'average' to 'extras'
+# : Core
+# : - Fixed make_mask (forced filling to True)
+# : - Fixed ndim
+# : - Fixed error message in __new__ when wrong sizes
+# : - Fixed the reshape function.
+# : Extras
+# : - masked_all: set default dtype to float_
+# : - _fromnxfunctions: make sure that list are recognized
+# : - added notmasked_edges, notmasked_contiguous
+#2006-12-09 : - Code reorganization: define 2 modules, core and extras
+#2006-11-25 : - Disable copy by default
+# - Added keep_mask flag (to save mask when creating a ma from a ma)
+# - Fixed functions: empty_like
+# - Fixed methods: .any and .all
+# - New functions: masked_all, masked_all_like
+# - New methods: .squeeze
+#2006-11-20 : - fixed make_mask
+# - fixed nonzero method
+#2006-11-16 : - fixed .T
+#2006-11-12 : - add max, min as function (not only method...)
+# - repr returns a name like masked_xxx, where xxx is the subclass
+#2006-10-31 : - make sure that make_mask returns a pure ndarray.
+#2006-10-30 : - When converted to a float, a masked singleton is transformed to nan
+# instead of raising an exception.
#21: Use __get__ method in _arraymethods, _arithmethods, _compamethods
#18: Updated put to match the definition of numpy 1.0, deleted putmask, changed resize
#2: prevent an extra kword being sent to make_mask_none
Modified: trunk/Lib/sandbox/maskedarray/__init__.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/__init__.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/__init__.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -4,12 +4,12 @@
:author: Pierre GF Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
-:version: $Id: __init__.py 38 2006-12-09 23:01:14Z backtopop $
+:version: $Id$
"""
-__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
-__revision__ = "$Revision: 38 $"
-__date__ = '$Date: 2006-12-09 18:01:14 -0500 (Sat, 09 Dec 2006) $'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
import core
reload(core)
Property changes on: trunk/Lib/sandbox/maskedarray/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Modified: trunk/Lib/sandbox/maskedarray/core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/core.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/core.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -12,12 +12,12 @@
:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
-:version: $Id: core.py 40 2006-12-10 19:50:35Z backtopop $
+:version: $Id$
"""
-__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
-__revision__ = "$Revision: 40 $"
-__date__ = '$Date: 2006-12-10 14:50:35 -0500 (Sun, 10 Dec 2006) $'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
__all__ = ['MAError', 'MaskType', 'MaskedArray',
'bool_', 'complex_', 'float_', 'int_', 'object_',
@@ -25,7 +25,7 @@
'amax', 'amin', 'anom', 'anomalies', 'any', 'arange',
'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2',
'arctanh', 'argmax', 'argmin', 'argsort', 'around',
- 'array', 'asarray', 'average',
+ 'array', 'asarray',
'bitwise_and', 'bitwise_or', 'bitwise_xor',
'ceil', 'choose', 'compressed', 'concatenate', 'conjugate',
'cos', 'cosh', 'count',
@@ -582,6 +582,7 @@
if m is nomask:
return nomask
elif isinstance(m, ndarray):
+ m = filled(m, True)
if m.dtype.type is MaskType:
if copy:
result = numeric.array(m, dtype=MaskType, copy=copy)
@@ -591,7 +592,7 @@
result = numeric.array(m, dtype=MaskType)
else:
result = numeric.array(filled(m, True), dtype=MaskType)
-
+ # Bas les masques !
if flag and not result.any():
return nomask
else:
@@ -856,7 +857,7 @@
else:
msg = "Mask and data not compatible: data size is %i, "+\
"mask size is %i."
- raise MAError, msg % (nm, nd)
+ raise MAError, msg % (nd, nm)
elif (_mask.shape != _data.shape):
_mask = _mask.reshape(_data.shape).copy()
#....
@@ -1412,6 +1413,12 @@
size = property(fget=_get_size,
doc="Size (number of elements) of the array.")
#
+ def _get_ndim(self):
+ "Returns the number of dimensions."
+ return self._data.ndim
+ ndim = property(fget=_get_ndim,
+ doc="Number of dimensions of the array.")
+ #
def reshape (self, *s):
"""Reshapes the array to shape s.
Returns a new masked array.
@@ -2302,7 +2309,6 @@
ptp = _frommethod('ptp')
ravel = _frommethod('ravel')
repeat = _frommethod('repeat')
-reshape = _frommethod('reshape')
std = _frommethod('std')
sum = _frommethod('sum')
swapaxes = _frommethod('swapaxes')
@@ -2310,6 +2316,27 @@
var = _frommethod('var')
#..............................................................................
+def power(a, b, third=None):
+ """Computes a**b elementwise.
+ Masked values are set to 1."""
+ if third is not None:
+ raise MAError, "3-argument power not supported."
+ ma = getmask(a)
+ mb = getmask(b)
+ m = mask_or(ma, mb)
+ fa = filled(a, 1)
+ fb = filled(b, 1)
+ if fb.dtype.char in typecodes["Integer"]:
+ return masked_array(umath.power(fa, fb), m)
+ md = make_mask((fa < 0), flag=1)
+ m = mask_or(m, md)
+ if m is nomask:
+ return masked_array(umath.power(fa, fb))
+ else:
+ fa[m] = 1
+ return masked_array(umath.power(fa, fb), m)
+
+#..............................................................................
def argsort(a, axis=None, kind='quicksort', fill_value=None):
"""Returns an array of indices that sort 'a' along the specified axis.
Masked values are filled beforehand to `fill_value`.
@@ -2462,34 +2489,42 @@
d = umath.right_shift(filled(a, 0), n)
return masked_array(d, mask=m)
#......................................
-def put(x, indices, values, mode='raise'):
- """sets storage-indexed locations to corresponding values.
+def put(a, indices, values, mode='raise'):
+ """Sets storage-indexed locations to corresponding values.
Values and indices are filled if necessary."""
# We can't use 'frommethod', the order of arguments is different
try:
- return x.put(indices, values, mode=mode)
+ return a.put(indices, values, mode=mode)
except AttributeError:
- return fromnumeric.asarray(x).put(indices, values, mode=mode)
+ return fromnumeric.asarray(a).put(indices, values, mode=mode)
-def putmask(x, mask, values): #, mode='raise'):
- """`putmask(x, mask, v)` results in `x = v` for all places where `mask` is true.
+def putmask(a, mask, values): #, mode='raise'):
+ """`putmask(a, mask, v)` results in `a = v` for all places where `mask` is true.
If `v` is shorter than `mask`, it will be repeated as necessary.
In particular `v` can be a scalar or length 1 array."""
# We can't use 'frommethod', the order of arguments is different
try:
- return x.putmask(values, mask)
+ return a.putmask(values, mask)
except AttributeError:
- return fromnumeric.asarray(x).putmask(values, mask)
+ return fromnumeric.asarray(a).putmask(values, mask)
-def transpose(x,axes=None):
+def transpose(a,axes=None):
"""Returns a view of the array with dimensions permuted according to axes.
If `axes` is None (default), returns array with dimensions reversed.
"""
#We can't use 'frommethod', as 'transpose' doesn't take keywords
try:
- return x.transpose(axes)
+ return a.transpose(axes)
except AttributeError:
- return fromnumeric.asarray(x).transpose(axes)
+ return fromnumeric.asarray(a).transpose(axes)
+
+def reshape(a, new_shape):
+ """Changes the shape of the array `a` to `new_shape`."""
+ #We can't use 'frommethod', it whine about some parameters. Dmmit.
+ try:
+ return a.reshape(new_shape)
+ except AttributeError:
+ return fromnumeric.asarray(a).reshape(new_shape)
def resize(x, new_shape):
"""resize(a,new_shape) returns a new array with the specified shape.
@@ -2686,123 +2721,6 @@
d = umath.less_equal(umath.absolute(x-y), atol + rtol * umath.absolute(y))
return fromnumeric.alltrue(fromnumeric.ravel(d))
-def average (a, axis=None, weights=None, returned = 0):
- """average(a, axis=None weights=None, returned=False)
-
- Averages the array over the given axis. If the axis is None, averages
- over all dimensions of the array. Equivalent to a.mean(axis)
-
- If an integer axis is given, this equals:
- a.sum(axis) * 1.0 / size(a, axis)
-
- If axis is None, this equals:
- a.sum(axis) * 1.0 / a.size
-
- If weights are given, result is:
- sum(a * weights,axis) / sum(weights,axis),
- where the weights must have a's shape or be 1D with length the
- size of a in the given axis. Integer weights are converted to
- Float. Not specifying weights is equivalent to specifying
- weights that are all 1.
-
- If 'returned' is True, return a tuple: the result and the sum of
- the weights or count of values. The shape of these two results
- will be the same.
-
- Returns masked values instead of ZeroDivisionError if appropriate.
-
- """
- a = asarray(a)
- mask = a.mask
- ash = a.shape
- if ash == ():
- ash = (1,)
- if axis is None:
- if mask is nomask:
- if weights is None:
- n = a.sum(axis=None)
- d = float(a.size)
- else:
- w = filled(weights, 0.0).ravel()
- n = umath.add.reduce(a._data.ravel() * w)
- d = umath.add.reduce(w)
- del w
- else:
- if weights is None:
- n = a.filled(0).sum(axis=None)
- d = umath.add.reduce((-mask).ravel().astype(int_))
- else:
- w = array(filled(weights, 0.0), float, mask=mask).ravel()
- n = add.reduce(a.ravel() * w)
- d = add.reduce(w)
- del w
- else:
- if mask is nomask:
- if weights is None:
- d = ash[axis] * 1.0
- n = add.reduce(a._data, axis)
- else:
- w = filled(weights, 0.0)
- wsh = w.shape
- if wsh == ():
- wsh = (1,)
- if wsh == ash:
- w = numeric.array(w, float_, copy=0)
- n = add.reduce(a*w, axis)
- d = add.reduce(w, axis)
- del w
- elif wsh == (ash[axis],):
- ni = ash[axis]
- r = [None]*len(ash)
- r[axis] = slice(None, None, 1)
- w = eval ("w["+ repr(tuple(r)) + "] * ones(ash, float)")
- n = add.reduce(a*w, axis)
- d = add.reduce(w, axis)
- del w, r
- else:
- raise ValueError, 'average: weights wrong shape.'
- else:
- if weights is None:
- n = add.reduce(a, axis)
- d = umath.add.reduce((-mask), axis=axis, dtype=float_)
- else:
- w = filled(weights, 0.0)
- wsh = w.shape
- if wsh == ():
- wsh = (1,)
- if wsh == ash:
- w = array(w, float, mask=mask, copy=0)
- n = add.reduce(a*w, axis)
- d = add.reduce(w, axis)
- elif wsh == (ash[axis],):
- ni = ash[axis]
- r = [None]*len(ash)
- r[axis] = slice(None, None, 1)
- w = eval ("w["+ repr(tuple(r)) + "] * masked_array(ones(ash, float), mask)")
- n = add.reduce(a*w, axis)
- d = add.reduce(w, axis)
- else:
- raise ValueError, 'average: weights wrong shape.'
- del w
- if n is masked or d is masked:
- return masked
- result = n/d
- del n
-
- if isinstance(result, MaskedArray):
- if ((axis is None) or (axis==0 and a.ndim == 1)) and \
- (result._mask is nomask):
- result = result._data
- if returned:
- if not isinstance(d, MaskedArray):
- d = masked_array(d)
- if isinstance(d, ndarray) and (not d.shape == result.shape):
- d = ones(result.shape, float) * d
- if returned:
- return result, d
- else:
- return result
-
#..............................................................................
def asarray(a, dtype=None):
"""asarray(data, dtype) = array(data, dtype, copy=0)
Property changes on: trunk/Lib/sandbox/maskedarray/core.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Modified: trunk/Lib/sandbox/maskedarray/extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/extras.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/extras.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -4,26 +4,32 @@
:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
-:version: $Id: extras.py 38 2006-12-09 23:01:14Z backtopop $
+:version: $Id$
"""
-__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
-__revision__ = "$Revision: 38 $"
-__date__ = '$Date: 2006-12-09 18:01:14 -0500 (Sat, 09 Dec 2006) $'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
__all__ = ['apply_along_axis', 'atleast_1d', 'atleast_2d', 'atleast_3d',
+ 'average',
'vstack', 'hstack', 'dstack', 'row_stack', 'column_stack',
'count_masked',
'masked_all', 'masked_all_like', 'mr_',
+ 'notmasked_edges', 'notmasked_contiguous',
'stdu', 'varu',
]
+from itertools import groupby
+
import core
reload(core)
from core import *
from core import _arraymethod
import numpy
+from numpy import float_
+import numpy.core.umath as umath
import numpy.core.numeric as numeric
from numpy.core.numeric import ndarray
from numpy.core.numeric import array as nxarray
@@ -47,7 +53,7 @@
m = getmaskarray(arr)
return m.sum(axis)
-def masked_all(shape, dtype):
+def masked_all(shape, dtype=float_):
"""Returns an empty masked array of the given shape and dtype,
where all the data are masked."""
a = empty(shape, dtype)
@@ -120,12 +126,12 @@
if len(args)==1:
x = args[0]
if isinstance(x,ndarray):
- _d = func.__call__(nxasarray(x), **params)
- _m = func.__call__(getmaskarray(x), **params)
+ _d = func(nxasarray(x), **params)
+ _m = func(getmaskarray(x), **params)
return masked_array(_d, mask=_m)
- elif isinstance(x, tuple):
- _d = func.__call__(tuple([nxasarray(a) for a in x]), **params)
- _m = func.__call__(tuple([getmaskarray(a) for a in x]), **params)
+ elif isinstance(x, tuple) or isinstance(x, list):
+ _d = func(tuple([nxasarray(a) for a in x]), **params)
+ _m = func(tuple([getmaskarray(a) for a in x]), **params)
return masked_array(_d, mask=_m)
else:
arrays = []
@@ -134,8 +140,8 @@
arrays.append(args.pop(0))
res = []
for x in arrays:
- _d = func.__call__(nxasarray(x), *args, **params)
- _m = func.__call__(getmaskarray(x), *args, **params)
+ _d = func(nxasarray(x), *args, **params)
+ _m = func(getmaskarray(x), *args, **params)
res.append(masked_array(_d, mask=_m))
return res
@@ -229,6 +235,125 @@
else:
return outarr.astype(max(dtypes))
+
+def average (a, axis=None, weights=None, returned = 0):
+ """average(a, axis=None weights=None, returned=False)
+
+ Averages the array over the given axis. If the axis is None, averages
+ over all dimensions of the array. Equivalent to a.mean(axis)
+
+ If an integer axis is given, this equals:
+ a.sum(axis) * 1.0 / size(a, axis)
+
+ If axis is None, this equals:
+ a.sum(axis) * 1.0 / a.size
+
+ If weights are given, result is:
+ sum(a * weights,axis) / sum(weights,axis),
+ where the weights must have a's shape or be 1D with length the
+ size of a in the given axis. Integer weights are converted to
+ Float. Not specifying weights is equivalent to specifying
+ weights that are all 1.
+
+ If 'returned' is True, return a tuple: the result and the sum of
+ the weights or count of values. The shape of these two results
+ will be the same.
+
+ Returns masked values instead of ZeroDivisionError if appropriate.
+
+ """
+ a = asarray(a)
+ mask = a.mask
+ ash = a.shape
+ if ash == ():
+ ash = (1,)
+ if axis is None:
+ if mask is nomask:
+ if weights is None:
+ n = a.sum(axis=None)
+ d = float(a.size)
+ else:
+ w = filled(weights, 0.0).ravel()
+ n = umath.add.reduce(a._data.ravel() * w)
+ d = umath.add.reduce(w)
+ del w
+ else:
+ if weights is None:
+ n = a.filled(0).sum(axis=None)
+ d = umath.add.reduce((-mask).ravel().astype(int_))
+ else:
+ w = array(filled(weights, 0.0), float, mask=mask).ravel()
+ n = add.reduce(a.ravel() * w)
+ d = add.reduce(w)
+ del w
+ else:
+ if mask is nomask:
+ if weights is None:
+ d = ash[axis] * 1.0
+ n = add.reduce(a._data, axis)
+ else:
+ w = filled(weights, 0.0)
+ wsh = w.shape
+ if wsh == ():
+ wsh = (1,)
+ if wsh == ash:
+ w = numeric.array(w, float_, copy=0)
+ n = add.reduce(a*w, axis)
+ d = add.reduce(w, axis)
+ del w
+ elif wsh == (ash[axis],):
+ ni = ash[axis]
+ r = [None]*len(ash)
+ r[axis] = slice(None, None, 1)
+ w = eval ("w["+ repr(tuple(r)) + "] * ones(ash, float)")
+ n = add.reduce(a*w, axis)
+ d = add.reduce(w, axis)
+ del w, r
+ else:
+ raise ValueError, 'average: weights wrong shape.'
+ else:
+ if weights is None:
+ n = add.reduce(a, axis)
+ d = umath.add.reduce((-mask), axis=axis, dtype=float_)
+ else:
+ w = filled(weights, 0.0)
+ wsh = w.shape
+ if wsh == ():
+ wsh = (1,)
+ if wsh == ash:
+ w = array(w, float, mask=mask, copy=0)
+ n = add.reduce(a*w, axis)
+ d = add.reduce(w, axis)
+ elif wsh == (ash[axis],):
+ ni = ash[axis]
+ r = [None]*len(ash)
+ r[axis] = slice(None, None, 1)
+ w = eval ("w["+ repr(tuple(r)) + "] * masked_array(ones(ash, float), mask)")
+ n = add.reduce(a*w, axis)
+ d = add.reduce(w, axis)
+ else:
+ raise ValueError, 'average: weights wrong shape.'
+ del w
+ if n is masked or d is masked:
+ return masked
+ result = n/d
+ del n
+
+ if isinstance(result, MaskedArray):
+ if ((axis is None) or (axis==0 and a.ndim == 1)) and \
+ (result._mask is nomask):
+ result = result._data
+ if returned:
+ if not isinstance(d, MaskedArray):
+ d = masked_array(d)
+ if isinstance(d, ndarray) and (not d.shape == result.shape):
+ d = ones(result.shape, float) * d
+ if returned:
+ return result, d
+ else:
+ return result
+
+
#####--------------------------------------------------------------------------
#---- --- Concatenation helpers ---
#####--------------------------------------------------------------------------
@@ -301,3 +426,76 @@
mconcatenator.__init__(self, 0)
mr_ = mr_class()
+
+#####--------------------------------------------------------------------------
+#---- ---
+#####--------------------------------------------------------------------------
+
+def flatnotmasked_edges(a):
+ """Finds the indices of the first and last not masked values in a 1D masked array.
+ If all values are masked, returns None.
+ """
+ m = getmask(a)
+ if m is nomask or not numpy.any(m):
+ return [0,-1]
+ unmasked = numeric.flatnonzero(~m)
+ if len(unmasked) > 0:
+ return unmasked[[0,-1]]
+ else:
+ return None
+
+def notmasked_edges(a, axis=None):
+ """Finds the indices of the first and last not masked values along the given
+ axis in a masked array.
+ If all values are masked, returns None.
+ Otherwise, returns a list of 2 tuples, corresponding to the indices of the
+ first and last unmasked values respectively.
+ """
+ a = asarray(a)
+ if axis is None or a.ndim == 1:
+ return flatnotmasked_edges(a)
+ m = getmask(a)
+ idx = array(numpy.indices(a.shape), mask=nxasarray([m]*a.ndim))
+ return [tuple([idx[i].min(axis).compressed() for i in range(a.ndim)]),
+ tuple([idx[i].max(axis).compressed() for i in range(a.ndim)]),]
+
+def flatnotmasked_contiguous(a):
+ """Finds contiguous unmasked data in a flattened masked array.
+ Returns a sorted sequence of tuples (size,(start index, end index)).
+ """
+ m = getmask(a)
+ if m is nomask:
+ return (a.size, [0,-1])
+ unmasked = numeric.flatnonzero(~m)
+ if len(unmasked) == 0:
+ return None
+ result = []
+ for k, group in groupby(enumerate(unmasked), lambda (i,x):i-x):
+ tmp = numpy.fromiter((g[1] for g in group), int_)
+ result.append((tmp.size, tuple(tmp[[0,-1]])))
+ result.sort()
+ return result
+
+def notmasked_contiguous(a, axis=None):
+ """Finds contiguous unmasked data in a masked array along the given axis.
+ Returns a sorted sequence of tuples (size,(start index, end index)).
+ Note: Only accepts 2D arrays at most.
+ """
+ a = asarray(a)
+ nd = a.ndim
+ if nd > 2:
+ raise NotImplementedError,"Currently limited to atmost 2D array."
+ if axis is None or nd == 1:
+ return flatnotmasked_contiguous(a)
+ #
+ result = []
+ #
+ other = (axis+1)%2
+ idx = [0,0]
+ idx[axis] = slice(None,None)
+ #
+ for i in range(a.shape[other]):
+ idx[other] = i
+ result.append( flatnotmasked_contiguous(a[idx]) )
+ return result
+
Property changes on: trunk/Lib/sandbox/maskedarray/extras.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Modified: trunk/Lib/sandbox/maskedarray/setup.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/setup.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/setup.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -1,8 +1,8 @@
#!/usr/bin/env python
-__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
-__revision__ = "$Revision: 37 $"
-__date__ = '$Date: 2006-12-08 14:30:29 -0500 (Fri, 08 Dec 2006) $'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
import os
Property changes on: trunk/Lib/sandbox/maskedarray/setup.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Added: trunk/Lib/sandbox/maskedarray/tests/test_core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_core.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/tests/test_core.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -0,0 +1,1058 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for MaskedArray.
+Adapted from the original test_ma by Pierre Gerard-Marchant
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id$
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
+__version__ = '1.0'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
+
+import types
+
+import numpy as N
+import numpy.core.fromnumeric as fromnumeric
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+
+import maskedarray.testutils
+reload(maskedarray.testutils)
+from maskedarray.testutils import *
+
+import maskedarray.core
+reload(maskedarray.core)
+from maskedarray.core import *
+
+pi = N.pi
+
+#..............................................................................
+class test_ma(NumpyTestCase):
+ "Base test class for MaskedArrays."
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+ self.setUp()
+
+ def setUp (self):
+ "Base data definition."
+ x = N.array([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
+ y = N.array([5.,0.,3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
+ a10 = 10.
+ m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
+ m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 ,0, 1]
+ xm = masked_array(x, mask=m1)
+ ym = masked_array(y, mask=m2)
+ z = N.array([-.5, 0., .5, .8])
+ zm = masked_array(z, mask=[0,1,0,0])
+ xf = N.where(m1, 1.e+20, x)
+ xm.set_fill_value(1.e+20)
+ self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
+ #........................
+ def check_testBasic1d(self):
+ "Test of basic array creation and properties in 1 dimension."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert(not isMaskedArray(x))
+ assert(isMaskedArray(xm))
+ assert((xm-ym).filled(0).any())
+ fail_if_equal(xm.mask.astype(int_), ym.mask.astype(int_))
+ s = x.shape
+ assert_equal(N.shape(xm), s)
+ assert_equal(xm.shape, s)
+ assert_equal(xm.dtype, x.dtype)
+ assert_equal(zm.dtype, z.dtype)
+ assert_equal(xm.size , reduce(lambda x,y:x*y, s))
+ assert_equal(count(xm) , len(m1) - reduce(lambda x,y:x+y, m1))
+ assert_array_equal(xm, xf)
+ assert_array_equal(filled(xm, 1.e20), xf)
+ assert_array_equal(x, xm)
+ #........................
+ def check_testBasic2d(self):
+ "Test of basic array creation and properties in 2 dimensions."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ for s in [(4,3), (6,2)]:
+ x.shape = s
+ y.shape = s
+ xm.shape = s
+ ym.shape = s
+ xf.shape = s
+
+ assert(not isMaskedArray(x))
+ assert(isMaskedArray(xm))
+ assert_equal(shape(xm), s)
+ assert_equal(xm.shape, s)
+ assert_equal( xm.size , reduce(lambda x,y:x*y, s))
+ assert_equal( count(xm) , len(m1) - reduce(lambda x,y:x+y, m1))
+ assert_equal(xm, xf)
+ assert_equal(filled(xm, 1.e20), xf)
+ assert_equal(x, xm)
+ #........................
+ def check_testArithmetic (self):
+ "Test of basic arithmetic."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ a2d = array([[1,2],[0,4]])
+ a2dm = masked_array(a2d, [[0,0],[1,0]])
+ assert_equal(a2d * a2d, a2d * a2dm)
+ assert_equal(a2d + a2d, a2d + a2dm)
+ assert_equal(a2d - a2d, a2d - a2dm)
+ for s in [(12,), (4,3), (2,6)]:
+ x = x.reshape(s)
+ y = y.reshape(s)
+ xm = xm.reshape(s)
+ ym = ym.reshape(s)
+ xf = xf.reshape(s)
+ assert_equal(-x, -xm)
+ assert_equal(x + y, xm + ym)
+ assert_equal(x - y, xm - ym)
+ assert_equal(x * y, xm * ym)
+ assert_equal(x / y, xm / ym)
+ assert_equal(a10 + y, a10 + ym)
+ assert_equal(a10 - y, a10 - ym)
+ assert_equal(a10 * y, a10 * ym)
+ assert_equal(a10 / y, a10 / ym)
+ assert_equal(x + a10, xm + a10)
+ assert_equal(x - a10, xm - a10)
+ assert_equal(x * a10, xm * a10)
+ assert_equal(x / a10, xm / a10)
+ assert_equal(x**2, xm**2)
+ assert_equal(abs(x)**2.5, abs(xm) **2.5)
+ assert_equal(x**y, xm**ym)
+ assert_equal(N.add(x,y), add(xm, ym))
+ assert_equal(N.subtract(x,y), subtract(xm, ym))
+ assert_equal(N.multiply(x,y), multiply(xm, ym))
+ assert_equal(N.divide(x,y), divide(xm, ym))
+ #........................
+ def check_testMixedArithmetic(self):
+ "Tests mixed arithmetics."
+ na = N.array([1])
+ ma = array([1])
+ self.failUnless(isinstance(na + ma, MaskedArray))
+ self.failUnless(isinstance(ma + na, MaskedArray))
+ #.........................
+ def check_testUfuncs1 (self):
+ "Test various functions such as sin, cos."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert_equal(N.cos(x), cos(xm))
+ assert_equal(N.cosh(x), cosh(xm))
+ assert_equal(N.sin(x), sin(xm))
+ assert_equal(N.sinh(x), sinh(xm))
+ assert_equal(N.tan(x), tan(xm))
+ assert_equal(N.tanh(x), tanh(xm))
+ assert_equal(N.sqrt(abs(x)), sqrt(xm))
+ assert_equal(N.log(abs(x)), log(xm))
+ assert_equal(N.log10(abs(x)), log10(xm))
+ assert_equal(N.exp(x), exp(xm))
+ assert_equal(N.arcsin(z), arcsin(zm))
+ assert_equal(N.arccos(z), arccos(zm))
+ assert_equal(N.arctan(z), arctan(zm))
+ assert_equal(N.arctan2(x, y), arctan2(xm, ym))
+ assert_equal(N.absolute(x), absolute(xm))
+ assert_equal(N.equal(x,y), equal(xm, ym))
+ assert_equal(N.not_equal(x,y), not_equal(xm, ym))
+ assert_equal(N.less(x,y), less(xm, ym))
+ assert_equal(N.greater(x,y), greater(xm, ym))
+ assert_equal(N.less_equal(x,y), less_equal(xm, ym))
+ assert_equal(N.greater_equal(x,y), greater_equal(xm, ym))
+ assert_equal(N.conjugate(x), conjugate(xm))
+ assert_equal(N.concatenate((x,y)), concatenate((xm,ym)))
+ assert_equal(N.concatenate((x,y)), concatenate((x,y)))
+ assert_equal(N.concatenate((x,y)), concatenate((xm,y)))
+ assert_equal(N.concatenate((x,y,x)), concatenate((x,ym,x)))
+ #........................
+ def check_xtestCount (self):
+ "Tests count"
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ assert( isinstance(count(ott), types.IntType))
+ assert_equal(3, count(ott))
+ assert_equal(1, count(1))
+ assert_equal(0, array(1,mask=[1]))
+ ott = ott.reshape((2,2))
+ assert isMaskedArray(count(ott,0))
+ assert isinstance(count(ott), types.IntType)
+ assert_equal(3, count(ott))
+ assert getmask(count(ott,0)) is nomask
+ assert_equal([1,2],count(ott,0))
+ #........................
+ def check_testMinMax (self):
+ "Tests minimum and maximum."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ xr = N.ravel(x) #max doesn't work if shaped
+ xmr = ravel(xm)
+ assert_equal(max(xr), maximum(xmr)) #true because of careful selection of data
+ assert_equal(min(xr), minimum(xmr)) #true because of careful selection of data
+ #
+ assert_equal(minimum([1,2,3],[4,0,9]), [1,0,3])
+ assert_equal(maximum([1,2,3],[4,0,9]), [4,2,9])
+ x = arange(5)
+ y = arange(5) - 2
+ x[3] = masked
+ y[0] = masked
+ assert_equal(minimum(x,y), where(less(x,y), x, y))
+ assert_equal(maximum(x,y), where(greater(x,y), x, y))
+ assert minimum(x) == 0
+ assert maximum(x) == 4
+ #........................
+ def check_testAddSumProd (self):
+ "Tests add, sum, product."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert_equal(N.add.reduce(x), add.reduce(x))
+ assert_equal(N.add.accumulate(x), add.accumulate(x))
+ assert_equal(4, sum(array(4),axis=0))
+ assert_equal(4, sum(array(4), axis=0))
+ assert_equal(N.sum(x,axis=0), sum(x,axis=0))
+ assert_equal(N.sum(filled(xm,0),axis=0), sum(xm,axis=0))
+ assert_equal(N.sum(x,0), sum(x,0))
+ assert_equal(N.product(x,axis=0), product(x,axis=0))
+ assert_equal(N.product(x,0), product(x,0))
+ assert_equal(N.product(filled(xm,1),axis=0), product(xm,axis=0))
+ s = (3,4)
+ x.shape = y.shape = xm.shape = ym.shape = s
+ if len(s) > 1:
+ assert_equal(N.concatenate((x,y),1), concatenate((xm,ym),1))
+ assert_equal(N.add.reduce(x,1), add.reduce(x,1))
+ assert_equal(N.sum(x,1), sum(x,1))
+ assert_equal(N.product(x,1), product(x,1))
+ #.........................
+ def check_concat(self):
+ "Tests concatenations."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ s = (3,4)
+ x.shape = y.shape = xm.shape = ym.shape = s
+ assert_equal(xm.mask, N.reshape(m1, s))
+ assert_equal(ym.mask, N.reshape(m2, s))
+ xmym = concatenate((xm,ym),1)
+ assert_equal(N.concatenate((x,y),1), xmym)
+ assert_equal(N.concatenate((xm.mask,ym.mask),1), xmym._mask)
+ #........................
+ def check_testCI(self):
+ "Tests conversions and indexing"
+ x1 = N.array([1,2,4,3])
+ x2 = array(x1, mask=[1,0,0,0])
+ x3 = array(x1, mask=[0,1,0,1])
+ x4 = array(x1)
+ # test conversion to strings
+ junk, garbage = str(x2), repr(x2)
+ assert_equal(N.sort(x1),sort(x2, fill_value=0))
+ # tests of indexing
+ assert type(x2[1]) is type(x1[1])
+ assert x1[1] == x2[1]
+ assert x2[0] is masked
+ assert_equal(x1[2],x2[2])
+ assert_equal(x1[2:5],x2[2:5])
+ assert_equal(x1[:],x2[:])
+ assert_equal(x1[1:], x3[1:])
+ x1[2] = 9
+ x2[2] = 9
+ assert_equal(x1,x2)
+ x1[1:3] = 99
+ x2[1:3] = 99
+ assert_equal(x1,x2)
+ x2[1] = masked
+ assert_equal(x1,x2)
+ x2[1:3] = masked
+ assert_equal(x1,x2)
+ x2[:] = x1
+ x2[1] = masked
+ assert allequal(getmask(x2),array([0,1,0,0]))
+ x3[:] = masked_array([1,2,3,4],[0,1,1,0])
+ assert allequal(getmask(x3), array([0,1,1,0]))
+ x4[:] = masked_array([1,2,3,4],[0,1,1,0])
+ assert allequal(getmask(x4), array([0,1,1,0]))
+ assert allequal(x4, array([1,2,3,4]))
+ x1 = N.arange(5)*1.0
+ x2 = masked_values(x1, 3.0)
+ assert_equal(x1,x2)
+ assert allequal(array([0,0,0,1,0],MaskType), x2.mask)
+#FIXME: Well, eh, fill_value is now a property assert_equal(3.0, x2.fill_value())
+ assert_equal(3.0, x2.fill_value)
+ x1 = array([1,'hello',2,3],object)
+ x2 = N.array([1,'hello',2,3],object)
+ s1 = x1[1]
+ s2 = x2[1]
+ assert_equal(type(s2), str)
+ assert_equal(type(s1), str)
+ assert_equal(s1, s2)
+ assert x1[1:1].shape == (0,)
+ #........................
+ def check_testCopySize(self):
+ "Tests of some subtle points of copying and sizing."
+ n = [0,0,1,0,0]
+ m = make_mask(n)
+ m2 = make_mask(m)
+ assert(m is m2)
+ m3 = make_mask(m, copy=1)
+ assert(m is not m3)
+
+ x1 = N.arange(5)
+ y1 = array(x1, mask=m)
+ assert( y1._data is x1)
+ assert( allequal(x1,y1.raw_data()))
+ assert( y1.mask is m)
+
+ y1a = array(y1)
+ assert( y1a.raw_data() is y1.raw_data())
+ assert( y1a.mask is y1.mask)
+
+ y2 = array(x1, mask=m)
+ assert( y2.raw_data() is x1)
+ assert( y2.mask is m)
+ assert( y2[2] is masked)
+ y2[2] = 9
+ assert( y2[2] is not masked)
+ assert( y2.mask is not m)
+ assert( allequal(y2.mask, 0))
+
+ y3 = array(x1*1.0, mask=m)
+ assert(filled(y3).dtype is (x1*1.0).dtype)
+
+ x4 = arange(4)
+ x4[2] = masked
+ y4 = resize(x4, (8,))
+ assert_equal(concatenate([x4,x4]), y4)
+ assert_equal(getmask(y4),[0,0,1,0,0,0,1,0])
+ y5 = repeat(x4, (2,2,2,2), axis=0)
+ assert_equal(y5, [0,0,1,1,2,2,3,3])
+ y6 = repeat(x4, 2, axis=0)
+ assert_equal(y5, y6)
+ y7 = x4.repeat((2,2,2,2), axis=0)
+ assert_equal(y5,y7)
+ y8 = x4.repeat(2,0)
+ assert_equal(y5,y8)
+
+ y9 = x4.copy()
+ assert_equal(y9._data, x4._data)
+ assert_equal(y9._mask, x4._mask)
+
+ #........................
+ def check_testOddFeatures_1(self):
+ "Test of other odd features"
+ x = arange(20)
+ x = x.reshape(4,5)
+ x.flat[5] = 12
+ assert x[1,0] == 12
+ z = x + 10j * x
+ assert_equal(z.real, x)
+ assert_equal(z.imag, 10*x)
+ assert_equal((z*conjugate(z)).real, 101*x*x)
+ z.imag[...] = 0.0
+
+ x = arange(10)
+ x[3] = masked
+ assert str(x[3]) == str(masked)
+ c = x >= 8
+ assert count(where(c,masked,masked)) == 0
+ assert shape(where(c,masked,masked)) == c.shape
+ #
+ z = where(c , x, masked)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is masked
+ assert z[7] is masked
+ assert z[8] is not masked
+ assert z[9] is not masked
+ assert_equal(x,z)
+ #
+ z = where(c , masked, x)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is not masked
+ assert z[7] is not masked
+ assert z[8] is masked
+ assert z[9] is masked
+ #
+ z = masked_where(c, x)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is not masked
+ assert z[7] is not masked
+ assert z[8] is masked
+ assert z[9] is masked
+ assert_equal(x,z)
+ #
+ #........................
+ def check_testOddFeatures_2(self):
+ "Tests some more features."
+ x = array([1.,2.,3.,4.,5.])
+ c = array([1,1,1,0,0])
+ x[2] = masked
+ z = where(c, x, -x)
+ assert_equal(z, [1.,2.,0., -4., -5])
+ c[0] = masked
+ z = where(c, x, -x)
+ assert_equal(z, [1.,2.,0., -4., -5])
+ assert z[0] is masked
+ assert z[1] is not masked
+ assert z[2] is masked
+ #
+ x = arange(6)
+ x[5] = masked
+ y = arange(6)*10
+ y[2] = masked
+ c = array([1,1,1,0,0,0], mask=[1,0,0,0,0,0])
+ cm = c.filled(1)
+ z = where(c,x,y)
+ zm = where(cm,x,y)
+ assert_equal(z, zm)
+ assert getmask(zm) is nomask
+ assert_equal(zm, [0,1,2,30,40,50])
+ z = where(c, masked, 1)
+ assert_equal(z, [99,99,99,1,1,1])
+ z = where(c, 1, masked)
+ assert_equal(z, [99, 1, 1, 99, 99, 99])
+ #........................
+ def check_testOddFeatures_3(self):
+ """Tests some generic features."""
+ atest = ones((10,10,10), dtype=float_)
+ btest = zeros(atest.shape, MaskType)
+ ctest = masked_where(btest,atest)
+ assert_equal(atest,ctest)
+ #........................
+ def check_maskingfunctions(self):
+ "Tests masking functions."
+ x = array([1.,2.,3.,4.,5.])
+ x[2] = masked
+ assert_equal(masked_where(greater(x, 2), x), masked_greater(x,2))
+ assert_equal(masked_where(greater_equal(x, 2), x), masked_greater_equal(x,2))
+ assert_equal(masked_where(less(x, 2), x), masked_less(x,2))
+ assert_equal(masked_where(less_equal(x, 2), x), masked_less_equal(x,2))
+ assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x,2))
+ assert_equal(masked_where(equal(x, 2), x), masked_equal(x,2))
+ assert_equal(masked_where(not_equal(x,2), x), masked_not_equal(x,2))
+ assert_equal(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4])
+ assert_equal(masked_outside(range(5), 1, 3),[199,1,2,3,199])
+ assert_equal(masked_inside(array(range(5), mask=[1,0,0,0,0]), 1, 3).mask, [1,1,1,1,0])
+ assert_equal(masked_outside(array(range(5), mask=[0,1,0,0,0]), 1, 3).mask, [1,1,0,0,1])
+ assert_equal(masked_equal(array(range(5), mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,0])
+ assert_equal(masked_not_equal(array([2,2,1,2,1], mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,1])
+ assert_equal(masked_where([1,1,0,0,0], [1,2,3,4,5]), [99,99,3,4,5])
+ #........................
+ def check_testTakeTransposeInnerOuter(self):
+ "Test of take, transpose, inner, outer products"
+ x = arange(24)
+ y = N.arange(24)
+ x[5:6] = masked
+ x = x.reshape(2,3,4)
+ y = y.reshape(2,3,4)
+ assert_equal(N.transpose(y,(2,0,1)), transpose(x,(2,0,1)))
+ assert_equal(N.take(y, (2,0,1), 1), take(x, (2,0,1), 1))
+ assert_equal(N.inner(filled(x,0),filled(y,0)),
+ inner(x, y))
+ assert_equal(N.outer(filled(x,0),filled(y,0)),
+ outer(x, y))
+ y = array(['abc', 1, 'def', 2, 3], object)
+ y[2] = masked
+ t = take(y,[0,3,4])
+ assert t[0] == 'abc'
+ assert t[1] == 2
+ assert t[2] == 3
+ #........................
+ def check_testInplace(self):
+ """Test of inplace operations and rich comparisons"""
+ y = arange(10)
+
+ x = arange(10)
+ xm = arange(10)
+ xm[2] = masked
+ x += 1
+ assert_equal(x, y+1)
+ xm += 1
+ assert_equal(xm, y+1)
+
+ x = arange(10)
+ xm = arange(10)
+ xm[2] = masked
+ x -= 1
+ assert_equal(x, y-1)
+ xm -= 1
+ assert_equal(xm, y-1)
+
+ x = arange(10)*1.0
+ xm = arange(10)*1.0
+ xm[2] = masked
+ x *= 2.0
+ assert_equal(x, y*2)
+ xm *= 2.0
+ assert_equal(xm, y*2)
+
+ x = arange(10)*2
+ xm = arange(10)*2
+ xm[2] = masked
+ x /= 2
+ assert_equal(x, y)
+ xm /= 2
+ assert_equal(xm, y)
+
+ x = arange(10)*1.0
+ xm = arange(10)*1.0
+ xm[2] = masked
+ x /= 2.0
+ assert_equal(x, y/2.0)
+ xm /= arange(10)
+ assert_equal(xm, ones((10,)))
+
+ x = arange(10).astype(float_)
+ xm = arange(10)
+ xm[2] = masked
+ id1 = id(x.raw_data())
+ x += 1.
+ assert id1 == id(x.raw_data())
+ assert_equal(x, y+1.)
+
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x += a
+ xm += a
+ assert_equal(x,y+a)
+ assert_equal(xm,y+a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x -= a
+ xm -= a
+ assert_equal(x,y-a)
+ assert_equal(xm,y-a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x *= a
+ xm *= a
+ assert_equal(x,y*a)
+ assert_equal(xm,y*a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x /= a
+ xm /= a
+ assert_equal(x,y/a)
+ assert_equal(xm,y/a)
+ assert_equal(xm.mask, mask_or(mask_or(m,a.mask), (a==0)))
+ #........................
+ def check_testPickle(self):
+ "Test of pickling"
+ import pickle
+ x = arange(12)
+ x[4:10:2] = masked
+ x = x.reshape(4,3)
+ s = pickle.dumps(x)
+ y = pickle.loads(s)
+ assert_equal(x,y)
+ #.......................
+ def check_testMasked(self):
+ "Test of masked element"
+ x = arange(6)
+ x[1] = masked
+ assert(str(masked) == '--')
+ assert(x[1] is masked)
+ assert_equal(filled(x[1], 0), 0)
+ # don't know why these should raise an exception...
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, masked)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, 2)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, xx)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, xx, masked)
+ #........................
+ def check_testToPython(self):
+ "Tests some communication issues with Python."
+ assert_equal(1, int(array(1)))
+ assert_equal(1.0, float(array(1)))
+ assert_equal(1, int(array([[[1]]])))
+ assert_equal(1.0, float(array([[1]])))
+ self.failUnlessRaises(ValueError, float, array([1,1]))
+ assert N.isnan(float(array([1],mask=[1])))
+#TODO: Check how bool works...
+#TODO: self.failUnless(bool(array([0,1])))
+#TODO: self.failUnless(bool(array([0,0],mask=[0,1])))
+#TODO: self.failIf(bool(array([0,0])))
+#TODO: self.failIf(bool(array([0,0],mask=[0,0])))
+ #..........................
+ def check_testScalarArithmetic(self):
+ "Tests some scalar arithmetics on MaskedArrays."
+ xm = array(0, mask=1)
+ assert((1/array(0)).mask)
+ assert((1 + xm).mask)
+ assert((-xm).mask)
+ assert((-xm).mask)
+ assert(maximum(xm, xm).mask)
+ assert(minimum(xm, xm).mask)
+ assert(xm.filled().dtype is xm.data.dtype)
+ x = array(0, mask=0)
+ assert(x.filled() is x.data)
+ assert_equal(str(xm), str(masked_print_option))
+ #........................
+ def check_testArrayMethods(self):
+ "Tests some MaskedArray methods."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a.any(), a.data.any())
+ assert_equal(a.all(), a.data.all())
+ assert_equal(a.argmax(), a.data.argmax())
+ assert_equal(a.argmin(), a.data.argmin())
+ assert_equal(a.choose(0,1,2,3,4), a.data.choose(0,1,2,3,4))
+ assert_equal(a.compress([1,0,1]), a.data.compress([1,0,1]))
+ assert_equal(a.conj(), a.data.conj())
+ assert_equal(a.conjugate(), a.data.conjugate())
+ #
+ m = array([[1,2],[3,4]])
+ assert_equal(m.diagonal(), m.data.diagonal())
+ assert_equal(a.sum(), a.data.sum())
+ assert_equal(a.take([1,2]), a.data.take([1,2]))
+ assert_equal(m.transpose(), m.data.transpose())
+ #........................
+ def check_testArrayAttributes(self):
+ "Tests some basic array attributes."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a.ndim, 1)
+ assert_equal(b.ndim, 1)
+ assert_equal(a.size, 3)
+ assert_equal(b.size, 3)
+ assert_equal(a.shape, (3,))
+ assert_equal(b.shape, (3,))
+ #........................
+ def check_testSingleElementSubscript(self):
+ "Tests single element subscripts of Maskedarrays."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a[0].shape, ())
+ assert_equal(b[0].shape, ())
+ assert_equal(b[1].shape, ())
+ #........................
+ def check_maskcreation(self):
+ "Tests how masks are initialized at the creation of Maskedarrays."
+ data = arange(24, dtype=float_)
+ data[[3,6,15]] = masked
+ dma_1 = MaskedArray(data)
+ assert_equal(dma_1.mask, data.mask)
+ dma_2 = MaskedArray(dma_1)
+ assert_equal(dma_2.mask, dma_1.mask)
+ dma_3 = MaskedArray(dma_1, mask=[1,0,0,0]*6)
+ fail_if_equal(dma_3.mask, dma_1.mask)
+
+ def check_testAverage1(self):
+ "Test of average."
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ assert_equal(2.0, average(ott,axis=0))
+ assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
+ result, wts = average(ott, weights=[1.,1.,2.,1.], returned=1)
+ assert_equal(2.0, result)
+ assert(wts == 4.0)
+ ott[:] = masked
+ assert(average(ott,axis=0) is masked)
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ ott = ott.reshape(2,2)
+ ott[:,1] = masked
+ assert_equal(average(ott,axis=0), [2.0, 0.0])
+ assert(average(ott,axis=1)[0] is masked)
+ assert_equal([2.,0.], average(ott, axis=0))
+ result, wts = average(ott, axis=0, returned=1)
+ assert_equal(wts, [1., 0.])
+
+ def check_testAverage2(self):
+ "More tests of average."
+ w1 = [0,1,1,1,1,0]
+ w2 = [[0,1,1,1,1,0],[1,0,0,0,0,1]]
+ x = arange(6)
+ assert_equal(average(x, axis=0), 2.5)
+ assert_equal(average(x, axis=0, weights=w1), 2.5)
+ y = array([arange(6), 2.0*arange(6)])
+ assert_equal(average(y, None), N.add.reduce(N.arange(6))*3./12.)
+ assert_equal(average(y, axis=0), N.arange(6) * 3./2.)
+ assert_equal(average(y, axis=1), [average(x,axis=0), average(x,axis=0) * 2.0])
+ assert_equal(average(y, None, weights=w2), 20./6.)
+ assert_equal(average(y, axis=0, weights=w2), [0.,1.,2.,3.,4.,10.])
+ assert_equal(average(y, axis=1), [average(x,axis=0), average(x,axis=0) * 2.0])
+ m1 = zeros(6)
+ m2 = [0,0,1,1,0,0]
+ m3 = [[0,0,1,1,0,0],[0,1,1,1,1,0]]
+ m4 = ones(6)
+ m5 = [0, 1, 1, 1, 1, 1]
+ assert_equal(average(masked_array(x, m1),axis=0), 2.5)
+ assert_equal(average(masked_array(x, m2),axis=0), 2.5)
+ assert(average(masked_array(x, m4),axis=0) is masked)
+ assert_equal(average(masked_array(x, m5),axis=0), 0.0)
+ assert_equal(count(average(masked_array(x, m4),axis=0)), 0)
+ z = masked_array(y, m3)
+ assert_equal(average(z, None), 20./6.)
+ assert_equal(average(z, axis=0), [0.,1.,99.,99.,4.0, 7.5])
+ assert_equal(average(z, axis=1), [2.5, 5.0])
+ assert_equal(average(z,axis=0, weights=w2), [0.,1., 99., 99., 4.0, 10.0])
+
+ def check_testAverage3(self):
+ "Yet more tests of average!"
+ a = arange(6)
+ b = arange(6) * 3
+ r1, w1 = average([[a,b],[b,a]], axis=1, returned=1)
+ assert_equal(shape(r1) , shape(w1))
+ assert_equal(r1.shape , w1.shape)
+ r2, w2 = average(ones((2,2,3)), axis=0, weights=[3,1], returned=1)
+ assert_equal(shape(w2) , shape(r2))
+ r2, w2 = average(ones((2,2,3)), returned=1)
+ assert_equal(shape(w2) , shape(r2))
+ r2, w2 = average(ones((2,2,3)), weights=ones((2,2,3)), returned=1)
+ assert_equal(shape(w2), shape(r2))
+ a2d = array([[1,2],[0,4]], float)
+ a2dm = masked_array(a2d, [[0,0],[1,0]])
+ a2da = average(a2d, axis=0)
+ assert_equal(a2da, [0.5, 3.0])
+ a2dma = average(a2dm, axis=0)
+ assert_equal(a2dma, [1.0, 3.0])
+ a2dma = average(a2dm, axis=None)
+ assert_equal(a2dma, 7./3.)
+ a2dma = average(a2dm, axis=1)
+ assert_equal(a2dma, [1.5, 4.0])
+
+ def check_backwards(self):
+ "Tests backward compatibility with numpy.core.ma"
+ import numpy.core.ma as nma
+ x = nma.arange(5)
+ x[2] = nma.masked
+ X = masked_array(x, mask=x._mask)
+ assert_equal(X._mask, x.mask)
+ assert_equal(X._data, x._data)
+ X = masked_array(x)
+ assert_equal(X._data, x._data)
+ assert_equal(X._mask, x.mask)
+ assert_equal(getmask(x), [0,0,1,0,0])
+
+#..............................................................................
+
+class test_ufuncs(NumpyTestCase):
+ "Test class for the application of ufuncs on MaskedArrays."
+ def setUp(self):
+ "Base data definition."
+ self.d = (array([1.0, 0, -1, pi/2]*2, mask=[0,1]+[0]*6),
+ array([1.0, 0, -1, pi/2]*2, mask=[1,0]+[0]*6),)
+
+ def check_testUfuncRegression(self):
+ "Tests new ufuncs on MaskedArrays."
+ for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
+ 'sin', 'cos', 'tan',
+ 'arcsin', 'arccos', 'arctan',
+ 'sinh', 'cosh', 'tanh',
+ 'arcsinh',
+ 'arccosh',
+ 'arctanh',
+ 'absolute', 'fabs', 'negative',
+ # 'nonzero', 'around',
+ 'floor', 'ceil',
+ # 'sometrue', 'alltrue',
+ 'logical_not',
+ 'add', 'subtract', 'multiply',
+ 'divide', 'true_divide', 'floor_divide',
+ 'remainder', 'fmod', 'hypot', 'arctan2',
+ 'equal', 'not_equal', 'less_equal', 'greater_equal',
+ 'less', 'greater',
+ 'logical_and', 'logical_or', 'logical_xor',
+ ]:
+ #print f
+ try:
+ uf = getattr(umath, f)
+ except AttributeError:
+ uf = getattr(fromnumeric, f)
+ mf = getattr(maskedarray, f)
+ args = self.d[:uf.nin]
+ ur = uf(*args)
+ mr = mf(*args)
+ assert_equal(ur.filled(0), mr.filled(0), f)
+ assert_mask_equal(ur.mask, mr.mask)
+ #........................
+ def test_reduce(self):
+ "Tests reduce on MaskedArrays."
+ a = self.d[0]
+ assert(not alltrue(a,axis=0))
+ assert(sometrue(a,axis=0))
+ assert_equal(sum(a[:3],axis=0), 0)
+ assert_equal(product(a,axis=0), 0)
+ #........................
+ def test_minmax(self):
+ "Tests extrema on MaskedArrays."
+ a = arange(1,13).reshape(3,4)
+ amask = masked_where(a < 5,a)
+ assert_equal(amask.max(), a.max())
+ assert_equal(amask.min(), 5)
+ assert_equal(amask.max(0), a.max(0))
+ assert_equal(amask.min(0), [5,6,7,8])
+ assert(amask.max(1)[0].mask)
+ assert(amask.min(1)[0].mask)
+
+#..............................................................................
+
+class test_array_methods(NumpyTestCase):
+ "Test class for miscellaneous MaskedArrays methods."
+ def setUp(self):
+ "Base data definition."
+ x = N.array([ 8.375, 7.545, 8.828, 8.5 , 1.757, 5.928,
+ 8.43 , 7.78 , 9.865, 5.878, 8.979, 4.732,
+ 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
+ 6.04 , 9.63 , 7.712, 3.382, 4.489, 6.479,
+ 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
+ 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
+ X = x.reshape(6,6)
+ XX = x.reshape(3,2,2,3)
+
+ m = N.array([0, 1, 0, 1, 0, 0,
+ 1, 0, 1, 1, 0, 1,
+ 0, 0, 0, 1, 0, 1,
+ 0, 0, 0, 1, 1, 1,
+ 1, 0, 0, 1, 0, 0,
+ 0, 0, 1, 0, 1, 0])
+ mx = array(data=x,mask=m)
+ mX = array(data=X,mask=m.reshape(X.shape))
+ mXX = array(data=XX,mask=m.reshape(XX.shape))
+
+ m2 = N.array([1, 1, 0, 1, 0, 0,
+ 1, 1, 1, 1, 0, 1,
+ 0, 0, 1, 1, 0, 1,
+ 0, 0, 0, 1, 1, 1,
+ 1, 0, 0, 1, 1, 0,
+ 0, 0, 1, 0, 1, 1])
+ m2x = array(data=x,mask=m2)
+ m2X = array(data=X,mask=m2.reshape(X.shape))
+ m2XX = array(data=XX,mask=m2.reshape(XX.shape))
+ self.d = (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX)
+
+ #------------------------------------------------------
+ def test_trace(self):
+ "Tests trace on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXdiag = mX.diagonal()
+ assert_equal(mX.trace(), mX.diagonal().compressed().sum())
+ assert_almost_equal(mX.trace(),
+ X.trace() - sum(mXdiag.mask*X.diagonal(),axis=0))
+
+ def test_clip(self):
+ "Tests clip on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ clipped = mx.clip(2,8)
+ assert_equal(clipped.mask,mx.mask)
+ assert_equal(clipped.data,x.clip(2,8))
+ assert_equal(clipped.data,mx.data.clip(2,8))
+
+ def test_ptp(self):
+ "Tests ptp on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ (n,m) = X.shape
+ assert_equal(mx.ptp(),mx.compressed().ptp())
+ rows = N.zeros(n,N.float_)
+ cols = N.zeros(m,N.float_)
+ for k in range(m):
+ cols[k] = mX[:,k].compressed().ptp()
+ for k in range(n):
+ rows[k] = mX[k].compressed().ptp()
+ assert_equal(mX.ptp(0),cols)
+ assert_equal(mX.ptp(1),rows)
+
+ def test_swapaxes(self):
+ "Tests swapaxes on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXswapped = mX.swapaxes(0,1)
+ assert_equal(mXswapped[-1],mX[:,-1])
+ mXXswapped = mXX.swapaxes(0,2)
+ assert_equal(mXXswapped.shape,(2,2,3,3))
+
+ def test_cumsumprod(self):
+ "Tests cumsum & cumprod on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXcp = mX.cumsum(0)
+ assert_equal(mXcp.data,mX.filled(0).cumsum(0))
+ mXcp = mX.cumsum(1)
+ assert_equal(mXcp.data,mX.filled(0).cumsum(1))
+ #
+ mXcp = mX.cumprod(0)
+ assert_equal(mXcp.data,mX.filled(1).cumprod(0))
+ mXcp = mX.cumprod(1)
+ assert_equal(mXcp.data,mX.filled(1).cumprod(1))
+
+ def test_varstd(self):
+ "Tests var & std on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ assert_almost_equal(mX.var(axis=None),mX.compressed().var())
+ assert_almost_equal(mX.std(axis=None),mX.compressed().std())
+ assert_equal(mXX.var(axis=3).shape,XX.var(axis=3).shape)
+ assert_equal(mX.var().shape,X.var().shape)
+ (mXvar0,mXvar1) = (mX.var(axis=0), mX.var(axis=1))
+ for k in range(6):
+ assert_almost_equal(mXvar1[k],mX[k].compressed().var())
+ assert_almost_equal(mXvar0[k],mX[:,k].compressed().var())
+ assert_almost_equal(N.sqrt(mXvar0[k]), mX[:,k].compressed().std())
+
+ def test_argmin(self):
+ "Tests argmin & argmax on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ #
+ assert_equal(mx.argmin(),35)
+ assert_equal(mX.argmin(),35)
+ assert_equal(m2x.argmin(),4)
+ assert_equal(m2X.argmin(),4)
+ assert_equal(mx.argmax(),28)
+ assert_equal(mX.argmax(),28)
+ assert_equal(m2x.argmax(),31)
+ assert_equal(m2X.argmax(),31)
+ #
+ assert_equal(mX.argmin(0), [2,2,2,5,0,5])
+ assert_equal(m2X.argmin(0), [2,2,4,5,0,4])
+ assert_equal(mX.argmax(0), [0,5,0,5,4,0])
+ assert_equal(m2X.argmax(0), [5,5,0,5,1,0])
+ #
+ assert_equal(mX.argmin(1), [4,1,0,0,5,5,])
+ assert_equal(m2X.argmin(1), [4,4,0,0,5,3])
+ assert_equal(mX.argmax(1), [2,4,1,1,4,1])
+ assert_equal(m2X.argmax(1), [2,4,1,1,1,1])
+
+ def check_put(self):
+ "Tests put."
+ d = arange(5)
+ n = [0,0,0,1,1]
+ m = make_mask(n)
+ x = array(d, mask = m)
+ assert( x[3] is masked)
+ assert( x[4] is masked)
+ x[[1,4]] = [10,40]
+ assert( x.mask is not m)
+ assert( x[3] is masked)
+ assert( x[4] is not masked)
+ assert_equal(x, [0,10,2,-1,40])
+ #
+ x = masked_array(arange(10), mask=[1,0,0,0,0]*2)
+ i = [0,2,4,6]
+ x.put(i, [6,4,2,0])
+ assert_equal(x, asarray([6,1,4,3,2,5,0,7,8,9,]))
+ assert_equal(x.mask, [0,0,0,0,0,1,0,0,0,0])
+ x.put(i, masked_array([0,2,4,6],[1,0,1,0]))
+ assert_array_equal(x, [0,1,2,3,4,5,6,7,8,9,])
+ assert_equal(x.mask, [1,0,0,0,1,1,0,0,0,0])
+ #
+ x = masked_array(arange(10), mask=[1,0,0,0,0]*2)
+ put(x, i, [6,4,2,0])
+ assert_equal(x, asarray([6,1,4,3,2,5,0,7,8,9,]))
+ assert_equal(x.mask, [0,0,0,0,0,1,0,0,0,0])
+ put(x, i, masked_array([0,2,4,6],[1,0,1,0]))
+ assert_array_equal(x, [0,1,2,3,4,5,6,7,8,9,])
+ assert_equal(x.mask, [1,0,0,0,1,1,0,0,0,0])
+
+ def check_take(self):
+ "Tests take"
+ x = masked_array([10,20,30,40],[0,1,0,1])
+ assert_equal(x.take([0,0,3]), masked_array([10, 10, 40], [0,0,1]) )
+ assert_equal(x.take([0,0,3]), x[[0,0,3]])
+ assert_equal(x.take([[0,1],[0,1]]),
+ masked_array([[10,20],[10,20]], [[0,1],[0,1]]) )
+ #
+ x = array([[10,20,30],[40,50,60]], mask=[[0,0,1],[1,0,0,]])
+ assert_equal(x.take([0,2], axis=1),
+ array([[10,30],[40,60]], mask=[[0,1],[1,0]]))
+ assert_equal(take(x, [0,2], axis=1),
+ array([[10,30],[40,60]], mask=[[0,1],[1,0]]))
+ #........................
+ def check_anyall(self):
+ """Checks the any/all methods/functions."""
+ x = N.array([[ 0.13, 0.26, 0.90],
+ [ 0.28, 0.33, 0.63],
+ [ 0.31, 0.87, 0.70]])
+ m = N.array([[ True, False, False],
+ [False, False, False],
+ [True, True, False]], dtype=N.bool_)
+ mx = masked_array(x, mask=m)
+ xbig = N.array([[False, False, True],
+ [False, False, True],
+ [False, True, True]], dtype=N.bool_)
+ mxbig = (mx > 0.5)
+ mxsmall = (mx < 0.5)
+ #
+ assert (mxbig.all()==False)
+ assert (mxbig.any()==True)
+ assert_equal(mxbig.all(0),[False, False, True])
+ assert_equal(mxbig.all(1), [False, False, True])
+ assert_equal(mxbig.any(0),[False, False, True])
+ assert_equal(mxbig.any(1), [True, True, True])
+ #
+ assert (mxsmall.all()==False)
+ assert (mxsmall.any()==True)
+ assert_equal(mxsmall.all(0), [True, True, False])
+ assert_equal(mxsmall.all(1), [False, False, False])
+ assert_equal(mxsmall.any(0), [True, True, False])
+ assert_equal(mxsmall.any(1), [True, True, False])
+ #
+ X = N.matrix(x)
+ mX = masked_array(X, mask=m)
+ mXbig = (mX > 0.5)
+ mXsmall = (mX < 0.5)
+ #
+ assert (mXbig.all()==False)
+ assert (mXbig.any()==True)
+ assert_equal(mXbig.all(0), N.matrix([False, False, True]))
+ assert_equal(mXbig.all(1), N.matrix([False, False, True]).T)
+ assert_equal(mXbig.any(0), N.matrix([False, False, True]))
+ assert_equal(mXbig.any(1), N.matrix([ True, True, True]).T)
+ #
+ assert (mXsmall.all()==False)
+ assert (mXsmall.any()==True)
+ assert_equal(mXsmall.all(0), N.matrix([True, True, False]))
+ assert_equal(mXsmall.all(1), N.matrix([False, False, False]).T)
+ assert_equal(mXsmall.any(0), N.matrix([True, True, False]))
+ assert_equal(mXsmall.any(1), N.matrix([True, True, False]).T)
+
+ def check_keepmask(self):
+ "Tests the keep mask flag"
+ x = masked_array([1,2,3], mask=[1,0,0])
+ mx = masked_array(x)
+ assert_equal(mx.mask, x.mask)
+ mx = masked_array(x, mask=[0,1,0], keep_mask=False)
+ assert_equal(mx.mask, [0,1,0])
+ mx = masked_array(x, mask=[0,1,0], keep_mask=True)
+ assert_equal(mx.mask, [1,1,0])
+ #We default to true
+ mx = masked_array(x, mask=[0,1,0])
+ assert_equal(mx.mask, [1,1,0])
+#..............................................................................
+
+#..............................................................................
+
+class test_subclassing(NumpyTestCase):
+ """Test suite for masked subclasses of ndarray."""
+
+ class SubArray(N.ndarray):
+ """Defines a generic N.ndarray subclass, that stores some metadata
+ in the dictionary `info`."""
+ def __new__(cls,arr,info={}):
+ x = N.array(arr).view(cls)
+ x.info = info
+ return x
+ def __array_finalize__(self, obj):
+ if hasattr(obj,'info'):
+ self.info = obj.info
+ return
+
+ def check_subclassing(self):
+ "Tests whether the subclass is kept."
+ x = N.arange(5)
+ m = [0,0,1,0,0]
+ xsub = test_subclassing.SubArray(x)
+ xmsub = masked_array(xsub, mask=m)
+ assert isinstance(xmsub, MaskedArray)
+ assert_equal(xmsub._data, xsub)
+ assert isinstance(xmsub._data, test_subclassing.SubArray)
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
\ No newline at end of file
Property changes on: trunk/Lib/sandbox/maskedarray/tests/test_core.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Added: trunk/Lib/sandbox/maskedarray/tests/test_extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_extras.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/tests/test_extras.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -0,0 +1,105 @@
+# pylint: disable-msg=W0611, W0612, W0511
+"""Tests suite for MaskedArray.
+Adapted from the original test_ma by Pierre Gerard-Marchant
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id$
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
+__version__ = '1.0'
+__revision__ = "$Revision$"
+__date__ = '$Date$'
+
+import numpy as N
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+
+import maskedarray.testutils
+reload(maskedarray.testutils)
+from maskedarray.testutils import *
+
+import maskedarray.core
+reload(maskedarray.core)
+from maskedarray.core import *
+import maskedarray.extras
+reload(maskedarray.extras)
+from maskedarray.extras import *
+
+
+class test_concatenator(NumpyTestCase):
+ "Tests for mr_, the equivalent of r_ for masked arrays."
+ def check_1d(self):
+ "Tests mr_ on 1D arrays."
+ assert_array_equal(mr_[1,2,3,4,5,6],array([1,2,3,4,5,6]))
+ b = ones(5)
+ m = [1,0,0,0,0]
+ d = masked_array(b,mask=m)
+ c = mr_[d,0,0,d]
+ assert(isinstance(c,MaskedArray) or isinstance(c,core.MaskedArray))
+ assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1])
+ assert_array_equal(c.mask, mr_[m,0,0,m])
+
+ def check_2d(self):
+ "Tests mr_ on 2D arrays."
+ a_1 = rand(5,5)
+ a_2 = rand(5,5)
+ m_1 = N.round_(rand(5,5),0)
+ m_2 = N.round_(rand(5,5),0)
+ b_1 = masked_array(a_1,mask=m_1)
+ b_2 = masked_array(a_2,mask=m_2)
+ d = mr_['1',b_1,b_2] # append columns
+ assert(d.shape == (5,10))
+ assert_array_equal(d[:,:5],b_1)
+ assert_array_equal(d[:,5:],b_2)
+ assert_array_equal(d.mask, N.r_['1',m_1,m_2])
+ d = mr_[b_1,b_2]
+ assert(d.shape == (10,5))
+ assert_array_equal(d[:5,:],b_1)
+ assert_array_equal(d[5:,:],b_2)
+ assert_array_equal(d.mask, N.r_[m_1,m_2])
+
+class test_notmasked(NumpyTestCase):
+ "Tests notmasked_edges and notmasked_contiguous."
+ def check_edges(self):
+ "Tests unmasked_edges"
+ a = masked_array(N.arange(24).reshape(3,8),
+ mask=[[0,0,0,0,1,1,1,0],
+ [1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,1,0],])
+ #
+ assert_equal(notmasked_edges(a, None), [0,23])
+ #
+ tmp = notmasked_edges(a, 0)
+ assert_equal(tmp[0], (array([0,0,0,0,2,2,0]), array([0,1,2,3,4,5,7])))
+ assert_equal(tmp[1], (array([2,2,2,2,2,2,2]), array([0,1,2,3,4,5,7])))
+ #
+ tmp = notmasked_edges(a, 1)
+ assert_equal(tmp[0], (array([0,2,]), array([0,0])))
+ assert_equal(tmp[1], (array([0,2,]), array([7,7])))
+
+ def check_contiguous(self):
+ "Tests notmasked_contiguous"
+ a = masked_array(N.arange(24).reshape(3,8),
+ mask=[[0,0,0,0,1,1,1,1],
+ [1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,1,0],])
+ tmp = notmasked_contiguous(a, None)
+ assert_equal(tmp[-1], (6, (16,21)))
+ assert_equal(tmp[-2], (4, (0,3)))
+ #
+ tmp = notmasked_contiguous(a, 0)
+ assert(len(tmp[-1]) == 1)
+ assert(tmp[-2] is None)
+ assert_equal(tmp[-3],tmp[-1])
+ assert(len(tmp[0]) == 2)
+ #
+ tmp = notmasked_contiguous(a, 1)
+ assert_equal(tmp[0][-1], (4, (0,3)))
+ assert(tmp[1] is None)
+ assert_equal(tmp[2][-1], (6, (0,5)))
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
\ No newline at end of file
Property changes on: trunk/Lib/sandbox/maskedarray/tests/test_extras.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
Modified: trunk/Lib/sandbox/maskedarray/testutils.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/testutils.py 2006-12-14 16:31:20 UTC (rev 2411)
+++ trunk/Lib/sandbox/maskedarray/testutils.py 2006-12-15 00:59:51 UTC (rev 2412)
@@ -2,12 +2,12 @@
:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
-:version: $Id: testutils.py 14 2006-12-04 19:31:13Z pierregm $
+:version: $Id$
"""
-__author__ = "Pierre GF Gerard-Marchant ($Author: pierregm $)"
+__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = "1.0"
-__revision__ = "$Revision: 14 $"
-__date__ = "$Date: 2006-12-04 14:31:13 -0500 (Mon, 04 Dec 2006) $"
+__revision__ = "$Revision$"
+__date__ = "$Date$"
import numpy as N
Property changes on: trunk/Lib/sandbox/maskedarray/testutils.py
___________________________________________________________________
Name: svn:keywords
+ Date
Author
Revision
Id
More information about the Scipy-svn
mailing list