[Scipy-svn] r2999 - in trunk/Lib/sandbox/maskedarray: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Mon May 14 20:18:19 EDT 2007
Author: pierregm
Date: 2007-05-14 19:17:29 -0500 (Mon, 14 May 2007)
New Revision: 2999
Modified:
trunk/Lib/sandbox/maskedarray/core.py
trunk/Lib/sandbox/maskedarray/extras.py
trunk/Lib/sandbox/maskedarray/mstats.py
trunk/Lib/sandbox/maskedarray/tests/test_mstats.py
Log:
maskedarray
extras.dot : added a 'strict' argument to (de)activate the propagation of masked values
mstats : added cov
core.argosrt : make sure that the filled array is a pure ndarray
Modified: trunk/Lib/sandbox/maskedarray/core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/core.py 2007-05-14 22:11:48 UTC (rev 2998)
+++ trunk/Lib/sandbox/maskedarray/core.py 2007-05-15 00:17:29 UTC (rev 2999)
@@ -66,13 +66,10 @@
import numpy.core.umath as umath
import numpy.core.fromnumeric as fromnumeric
-from numpy.core.numeric import ndarray
-from numpy.core.fromnumeric import amax, amin
-import numpy.core.numerictypes as ntypes
-from numpy.core.numerictypes import bool_, typecodes
-from numpy.core.multiarray import dtype
import numpy.core.numeric as numeric
-from numpy.lib.shape_base import expand_dims as n_expand_dims
+import numpy.core.numerictypes as ntypes
+from numpy import bool_, dtype, typecodes, amax, amin, ndarray
+from numpy import expand_dims as n_expand_dims
import warnings
@@ -1713,7 +1710,7 @@
"""
if fill_value is None:
fill_value = default_fill_value(self)
- d = self.filled(fill_value)
+ d = self.filled(fill_value).view(ndarray)
return d.argsort(axis=axis, kind=kind, order=order)
#........................
def argmin(self, axis=None, fill_value=None):
Modified: trunk/Lib/sandbox/maskedarray/extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/extras.py 2007-05-14 22:11:48 UTC (rev 2998)
+++ trunk/Lib/sandbox/maskedarray/extras.py 2007-05-15 00:17:29 UTC (rev 2999)
@@ -426,22 +426,22 @@
return mask_rowcols(a, 1)
-def dot(a,b):
+def dot(a,b, strict=False):
"""Returns the dot product of two 2D masked arrays a and b.
Like the generic numpy equivalent the product sum is over
the last dimension of a and the second-to-last dimension of b.
- Masked values are propagated: if a masked value appears in a row or column,
- the whole row or column is considered masked.
+ If strict is True, masked values are propagated: if a masked value appears
+ in a row or column, the whole row or column is considered masked.
NB: The first argument is not conjugated.
"""
#TODO: Works only with 2D arrays. There should be a way to get it to run with higher dimension
- if (a.ndim == 2) and (b.ndim == 2):
+ if strict and (a.ndim == 2) and (b.ndim == 2):
a = mask_rows(a)
b = mask_cols(b)
#
- d = numpy.dot(a.filled(0), b.filled(0))
+ d = numpy.dot(filled(a, 0), filled(b, 0))
#
am = (~getmaskarray(a))
bm = (~getmaskarray(b))
@@ -496,6 +496,9 @@
r_mask = dm
return masked_array(r_data, mask=r_mask)
+
+
+
#####--------------------------------------------------------------------------
#---- --- Concatenation helpers ---
#####--------------------------------------------------------------------------
Modified: trunk/Lib/sandbox/maskedarray/mstats.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/mstats.py 2007-05-14 22:11:48 UTC (rev 2998)
+++ trunk/Lib/sandbox/maskedarray/mstats.py 2007-05-15 00:17:29 UTC (rev 2999)
@@ -15,12 +15,13 @@
import numpy
from numpy import bool_, float_, int_
from numpy import array as narray
-from numpy.core import numeric as numeric
+import numpy.core.numeric as numeric
+from numpy.core.numeric import concatenate
import maskedarray as MA
from maskedarray.core import masked, nomask, MaskedArray
from maskedarray.core import masked_array as marray
-from maskedarray.extras import apply_along_axis
+from maskedarray.extras import apply_along_axis, dot
def _quantiles_1D(data,m,p):
@@ -149,8 +150,53 @@
# return med
return apply_along_axis(_median1d, 0, data)
+def cov(x, y=None, rowvar=True, bias=False, strict=False):
+ """
+ Estimate the covariance matrix.
+ If x is a vector, return the variance. For matrices, returns the covariance
+ matrix.
+ If y is given, it is treated as an additional (set of) variable(s).
+
+ Normalization is by (N-1) where N is the number of observations (unbiased
+ estimate). If bias is True then normalization is by N.
+
+ If rowvar is non-zero (default), then each row is a variable with observations
+ in the columns, otherwise each column is a variable and the observations are
+ in the rows.
+
+ If strict is True, masked values are propagated: if a masked value appears in
+ a row or column, the whole row or column is considered masked.
+ """
+ X = narray(x, ndmin=2, subok=True, dtype=float)
+ if X.shape[0] == 1:
+ rowvar = True
+ if rowvar:
+ axis = 0
+ tup = (slice(None),None)
+ else:
+ axis = 1
+ tup = (None, slice(None))
+ #
+ if y is not None:
+ y = narray(y, copy=False, ndmin=2, subok=True, dtype=float)
+ X = concatenate((X,y),axis)
+ #
+ X -= X.mean(axis=1-axis)[tup]
+ n = X.count(1-axis)
+ #
+ if bias:
+ fact = n*1.0
+ else:
+ fact = n-1.0
+ #
+ if not rowvar:
+ return (dot(X.T, X.conj(), strict=False) / fact).squeeze()
+ else:
+ return (dot(X, X.T.conj(), strict=False) / fact).squeeze()
+
+
################################################################################
if __name__ == '__main__':
from maskedarray.testutils import assert_almost_equal, assert_equal
Modified: trunk/Lib/sandbox/maskedarray/tests/test_mstats.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_mstats.py 2007-05-14 22:11:48 UTC (rev 2998)
+++ trunk/Lib/sandbox/maskedarray/tests/test_mstats.py 2007-05-15 00:17:29 UTC (rev 2999)
@@ -18,7 +18,7 @@
import maskedarray.testutils
from maskedarray.testutils import *
-from maskedarray.mstats import mquantiles, mmedian
+from maskedarray.mstats import mquantiles, mmedian, cov
#..............................................................................
class test_quantiles(NumpyTestCase):
@@ -110,6 +110,20 @@
x[x%5==0] = masked
assert_equal(mmedian(x), [[12,10],[8,9],[16,17]])
+class test_misc(NumpyTestCase):
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+
+ def check_cov(self):
+ "Tests the cov function."
+ x = masked_array([[1,2,3],[4,5,6]], mask=[[1,0,0],[0,0,0]])
+ c = cov(x[0])
+ assert_equal(c, (x[0].anom()**2).sum())
+ c = cov(x[1])
+ assert_equal(c, (x[1].anom()**2).sum()/2.)
+ c = cov(x)
+ assert_equal(c[1,0], (x[0].anom()*x[1].anom()).sum())
+
###############################################################################
#------------------------------------------------------------------------------
if __name__ == "__main__":
More information about the Scipy-svn
mailing list