[Numpy-svn] r6300 - in trunk/numpy/ma: . tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Wed Jan 7 17:34:56 EST 2009
Author: pierregm
Date: 2009-01-07 16:34:51 -0600 (Wed, 07 Jan 2009)
New Revision: 6300
Modified:
trunk/numpy/ma/core.py
trunk/numpy/ma/tests/test_core.py
Log:
* Renamed `torecords` to `toflex`, keeping `torecords` as an alias
* Introduced `fromflex`, to reconstruct a masked_array from the output of `toflex` (can?\226?\128?\153t `use fromrecords` as it would clash with `numpy.ma.mrecords.fromrecords`)
* Fixed a bug in MaskedBinaryOperation (#979) (wrong array broadcasting)
Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py 2009-01-07 18:14:12 UTC (rev 6299)
+++ trunk/numpy/ma/core.py 2009-01-07 22:34:51 UTC (rev 6300)
@@ -34,7 +34,7 @@
'default_fill_value', 'diag', 'diagonal', 'divide', 'dump', 'dumps',
'empty', 'empty_like', 'equal', 'exp', 'expand_dims',
'fabs', 'flatten_mask', 'fmod', 'filled', 'floor', 'floor_divide',
- 'fix_invalid', 'frombuffer', 'fromfunction',
+ 'fix_invalid', 'frombuffer', 'fromflex', 'fromfunction',
'getdata','getmask', 'getmaskarray', 'greater', 'greater_equal',
'harden_mask', 'hypot',
'identity', 'ids', 'indices', 'inner', 'innerproduct',
@@ -623,8 +623,8 @@
# Transforms to a (subclass of) MaskedArray if we don't have a scalar
if result.shape:
result = result.view(get_masked_subclass(a, b))
- result._mask = make_mask_none(result.shape)
- result._mask.flat = m
+ if m.any():
+ result._mask = mask_or(getmaskarray(a), getmaskarray(b))
if isinstance(a, MaskedArray):
result._update_from(a)
if isinstance(b, MaskedArray):
@@ -3603,7 +3603,7 @@
def tofile(self, fid, sep="", format="%s"):
raise NotImplementedError("Not implemented yet, sorry...")
- def torecords(self):
+ def toflex(self):
"""
Transforms a MaskedArray into a flexible-type array with two fields:
* the ``_data`` field stores the ``_data`` part of the array;
@@ -3648,6 +3648,7 @@
record['_data'] = self._data
record['_mask'] = self._mask
return record
+ torecords = toflex
#--------------------------------------------
# Pickling
def __getstate__(self):
@@ -4613,6 +4614,15 @@
raise NotImplementedError("Not yet implemented. Sorry")
+def fromflex(fxarray):
+ """
+ Rebuilds a masked_array from a flexible-type array output by the '.torecord'
+ array
+ """
+ return masked_array(fxarray['_data'], mask=fxarray['_mask'])
+
+
+
class _convert2ma:
"""Convert functions from numpy to numpy.ma.
Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py 2009-01-07 18:14:12 UTC (rev 6299)
+++ trunk/numpy/ma/tests/test_core.py 2009-01-07 22:34:51 UTC (rev 6300)
@@ -549,6 +549,7 @@
assert_equal(np.multiply(x,y), multiply(xm, ym))
assert_equal(np.divide(x,y), divide(xm, ym))
+
def test_divide_on_different_shapes(self):
x = arange(6, dtype=float)
x.shape = (2,3)
@@ -567,6 +568,7 @@
assert_equal(z, [[-1.,-1.,-1.], [3.,4.,5.]])
assert_equal(z.mask, [[1,1,1],[0,0,0]])
+
def test_mixed_arithmetic(self):
"Tests mixed arithmetics."
na = np.array([1])
@@ -581,6 +583,7 @@
assert_equal(getmaskarray(a/2), [0,0,0])
assert_equal(getmaskarray(2/a), [1,0,1])
+
def test_masked_singleton_arithmetic(self):
"Tests some scalar arithmetics on MaskedArrays."
# Masked singleton should remain masked no matter what
@@ -591,6 +594,7 @@
self.failUnless(maximum(xm, xm).mask)
self.failUnless(minimum(xm, xm).mask)
+
def test_arithmetic_with_masked_singleton(self):
"Checks that there's no collapsing to masked"
x = masked_array([1,2])
@@ -603,6 +607,7 @@
assert_equal(y.shape, x.shape)
assert_equal(y._mask, [True, True])
+
def test_arithmetic_with_masked_singleton_on_1d_singleton(self):
"Check that we're not losing the shape of a singleton"
x = masked_array([1, ])
@@ -610,6 +615,7 @@
assert_equal(y.shape, x.shape)
assert_equal(y.mask, [True, ])
+
def test_scalar_arithmetic(self):
x = array(0, mask=0)
assert_equal(x.filled().ctypes.data, x.ctypes.data)
@@ -618,6 +624,7 @@
assert_equal(xm.shape,(2,))
assert_equal(xm.mask,[1,1])
+
def test_basic_ufuncs (self):
"Test various functions such as sin, cos."
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
@@ -659,6 +666,7 @@
assert getmask(count(ott,0)) is nomask
assert_equal([1,2],count(ott,0))
+
def test_minmax_func (self):
"Tests minimum and maximum."
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
@@ -682,6 +690,7 @@
x[-1,-1] = masked
assert_equal(maximum(x), 2)
+
def test_minimummaximum_func(self):
a = np.ones((2,2))
aminimum = minimum(a,a)
@@ -700,6 +709,7 @@
self.failUnless(isinstance(amaximum, MaskedArray))
assert_equal(amaximum, np.maximum.outer(a,a))
+
def test_minmax_funcs_with_output(self):
"Tests the min/max functions with explicit outputs"
mask = np.random.rand(12).round()
@@ -745,7 +755,8 @@
self.failUnless(x.min() is masked)
self.failUnless(x.max() is masked)
self.failUnless(x.ptp() is masked)
- #........................
+
+
def test_addsumprod (self):
"Tests add, sum, product."
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
@@ -767,6 +778,44 @@
assert_equal(np.sum(x,1), sum(x,1))
assert_equal(np.product(x,1), product(x,1))
+
+ def test_binops_d2D(self):
+ "Test binary operations on 2D data"
+ a = array([[1.], [2.], [3.]], mask=[[False], [True], [True]])
+ b = array([[2., 3.], [4., 5.], [6., 7.]])
+ #
+ test = a * b
+ control = array([[2., 3.], [2., 2.], [3., 3.]],
+ mask=[[0, 0], [1, 1], [1, 1]])
+ assert_equal(test, control)
+ assert_equal(test.data, control.data)
+ assert_equal(test.mask, control.mask)
+ #
+ test = b * a
+ control = array([[2., 3.], [4., 5.], [6., 7.]],
+ mask=[[0, 0], [1, 1], [1, 1]])
+ assert_equal(test, control)
+ assert_equal(test.data, control.data)
+ assert_equal(test.mask, control.mask)
+ #
+ a = array([[1.], [2.], [3.]])
+ b = array([[2., 3.], [4., 5.], [6., 7.]],
+ mask=[[0, 0], [0, 0], [0, 1]])
+ test = a * b
+ control = array([[2, 3], [8, 10], [18, 3]],
+ mask=[[0, 0], [0, 0], [0, 1]])
+ assert_equal(test, control)
+ assert_equal(test.data, control.data)
+ assert_equal(test.mask, control.mask)
+ #
+ test = b * a
+ control = array([[2, 3], [8, 10], [18, 7]],
+ mask=[[0, 0], [0, 0], [0, 1]])
+ assert_equal(test, control)
+ assert_equal(test.data, control.data)
+ assert_equal(test.mask, control.mask)
+
+
def test_mod(self):
"Tests mod"
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
@@ -779,7 +828,6 @@
assert_equal(test.mask, mask_or(mask_or(xm.mask, ym.mask), (ym == 0)))
-
def test_TakeTransposeInnerOuter(self):
"Test of take, transpose, inner, outer products"
x = arange(24)
@@ -1983,15 +2031,15 @@
assert_equal(x.tolist(), [(1,1.1,'one'),(2,2.2,'two'),(None,None,None)])
- def test_torecords(self):
+ def test_toflex(self):
"Test the conversion to records"
data = arange(10)
- record = data.torecords()
+ record = data.toflex()
assert_equal(record['_data'], data._data)
assert_equal(record['_mask'], data._mask)
#
data[[0,1,2,-1]] = masked
- record = data.torecords()
+ record = data.toflex()
assert_equal(record['_data'], data._data)
assert_equal(record['_mask'], data._mask)
#
@@ -2001,7 +2049,7 @@
np.random.rand(10))],
dtype=ndtype)
data[[0,1,2,-1]] = masked
- record = data.torecords()
+ record = data.toflex()
assert_equal(record['_data'], data._data)
assert_equal(record['_mask'], data._mask)
#
@@ -2011,10 +2059,29 @@
np.random.rand(10))],
dtype=ndtype)
data[[0,1,2,-1]] = masked
- record = data.torecords()
+ record = data.toflex()
assert_equal_records(record['_data'], data._data)
assert_equal_records(record['_mask'], data._mask)
+
+ def test_fromflex(self):
+ "Test the reconstruction of a masked_array from a record"
+ a = array([1, 2, 3])
+ test = fromflex(a.toflex())
+ assert_equal(test, a)
+ assert_equal(test.mask, a.mask)
+ #
+ a = array([1, 2, 3], mask=[0, 0, 1])
+ test = fromflex(a.toflex())
+ assert_equal(test, a)
+ assert_equal(test.mask, a.mask)
+ #
+ a = array([(1, 1.), (2, 2.), (3, 3.)], mask=[(1, 0), (0, 0), (0, 1)],
+ dtype=[('A', int), ('B', float)])
+ test = fromflex(a.toflex())
+ assert_equal(test, a)
+ assert_equal(test.data, a.data)
+
#------------------------------------------------------------------------------
More information about the Numpy-svn
mailing list