[Numpy-svn] r3003 - in trunk/numpy: core core/tests lib numarray oldnumeric

numpy-svn at scipy.org numpy-svn at scipy.org
Sun Aug 13 02:15:59 CDT 2006


Author: oliphant
Date: 2006-08-13 02:15:52 -0500 (Sun, 13 Aug 2006)
New Revision: 3003

Modified:
   trunk/numpy/core/ma.py
   trunk/numpy/core/tests/test_ma.py
   trunk/numpy/lib/function_base.py
   trunk/numpy/numarray/functions.py
   trunk/numpy/oldnumeric/functions.py
   trunk/numpy/oldnumeric/ma.py
   trunk/numpy/oldnumeric/misc.py
Log:
Change default axis argument of average and several masked array functions. Move old behaviors to compatibility layer.

Modified: trunk/numpy/core/ma.py
===================================================================
--- trunk/numpy/core/ma.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/core/ma.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -448,7 +448,7 @@
     """returns the indices of the elements of a which are not zero
     and not masked
     """
-    return asarray(filled(a, 0).nonzero())
+    return numeric.asarray(filled(a, 0).nonzero())
 
 around = masked_unary_operation(fromnumeric.round_)
 floor = masked_unary_operation(umath.floor)
@@ -1532,7 +1532,7 @@
     result.set_fill_value(get_fill_value(a))
     return result
 
-def repeat(a, repeats, axis=0):
+def repeat(a, repeats, axis=None):
     """repeat elements of a repeats times along axis
        repeats is a sequence of length a.shape[axis]
        telling how many times to repeat each element.
@@ -1603,8 +1603,8 @@
 sum = add.reduce
 product = multiply.reduce
 
-def average (a, axis=0, weights=None, returned = 0):
-    """average(a, axis=0, weights=None)
+def average (a, axis=None, weights=None, returned = 0):
+    """average(a, axis=None, weights=None)
        Computes average along indicated axis.
        If axis is None, average over the entire array
        Inputs can be integer or floating types; result is of type float.
@@ -1858,7 +1858,7 @@
                             mask=numeric.swapaxes(m, axis1, axis2),)
 
 
-def take (a, indices, axis=0, out=None, mode='raise'):
+def take (a, indices, axis=None, out=None, mode='raise'):
     "returns selection of items from a."
     m = getmask(a)
     # d = masked_array(a).raw_data()
@@ -2168,14 +2168,14 @@
 array.conj = array.conjugate = _m(conjugate)
 array.copy = _m(not_implemented)
 
-def _cumprod(self, axis=0, dtype=None, out=None):
+def _cumprod(self, axis=None, dtype=None, out=None):
     m = self.mask
     if m is not nomask:
         m = umath.logical_or.accumulate(self.mask, axis)
     return MaskedArray(data = self.filled(1).cumprod(axis, dtype), mask=m)
 array.cumprod = _m(_cumprod)
 
-def _cumsum(self, axis=0, dtype=None, out=None):
+def _cumsum(self, axis=None, dtype=None, out=None):
     m = self.mask
     if m is not nomask:
         m = umath.logical_or.accumulate(self.mask, axis)
@@ -2214,7 +2214,7 @@
 array.nonzero = _m(nonzero)
 array.prod = _m(product)
 
-def _ptp(a,axis=0,out=None):
+def _ptp(a,axis=None,out=None):
     return a.max(axis,out)-a.min(axis)
 array.ptp = _m(_ptp)
 array.repeat = _m(repeat)
@@ -2244,15 +2244,15 @@
 array.trace = _m(trace)
 array.transpose = _m(transpose)
 
-def _var(self,axis=0,dtype=None, out=None):
+def _var(self,axis=None,dtype=None, out=None):
     if axis is None:
-        return asarray(self.compressed()).var()
+        return numeric.asarray(self.compressed()).var()
     a = self.swapaxes(axis,0)
     a = a - a.mean(axis=0)
     a *= a
     a /= a.count(axis=0)
     return a.swapaxes(0,axis).sum(axis)
-def _std(self,axis=0,dtype=None, out=None):
+def _std(self,axis=None, dtype=None, out=None):
     return (self.var(axis,dtype))**0.5
 array.var = _m(_var)
 array.std = _m(_std)

Modified: trunk/numpy/core/tests/test_ma.py
===================================================================
--- trunk/numpy/core/tests/test_ma.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/core/tests/test_ma.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -267,9 +267,9 @@
         y4 = resize(x4, (8,))
         self.failUnless( eq(concatenate([x4,x4]), y4))
         self.failUnless( eq(getmask(y4),[0,0,1,0,0,0,1,0]))
-        y5 = repeat(x4, (2,2,2,2))
+        y5 = repeat(x4, (2,2,2,2), axis=0)
         self.failUnless( eq(y5, [0,0,1,1,2,2,3,3]))
-        y6 = repeat(x4, 2)
+        y6 = repeat(x4, 2, axis=0)
         self.failUnless( eq(y5, y6))
 
     def check_testPut(self):
@@ -523,10 +523,10 @@
         ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
         ott=ott.reshape(2,2)
         ott[:,1] = masked
-        self.failUnless(eq(average(ott), [2.0, 0.0]))
+        self.failUnless(eq(average(ott,axis=0), [2.0, 0.0]))
         self.failUnless(average(ott,axis=1)[0] is masked)
-        self.failUnless(eq([2.,0.], average(ott)))
-        result, wts = average(ott, returned=1)
+        self.failUnless(eq([2.,0.], average(ott, axis=0)))
+        result, wts = average(ott, axis=0, returned=1)
         self.failUnless(eq(wts, [1., 0.]))
 
     def check_testAverage2(self):
@@ -534,8 +534,8 @@
         w1 = [0,1,1,1,1,0]
         w2 = [[0,1,1,1,1,0],[1,0,0,0,0,1]]
         x=arange(6)
-        self.failUnless(allclose(average(x), 2.5))
-        self.failUnless(allclose(average(x, weights=w1), 2.5))
+        self.failUnless(allclose(average(x, axis=0), 2.5))
+        self.failUnless(allclose(average(x, axis=0, weights=w1), 2.5))
         y=array([arange(6), 2.0*arange(6)])
         self.failUnless(allclose(average(y, None), numpy.add.reduce(numpy.arange(6))*3./12.))
         self.failUnless(allclose(average(y, axis=0), numpy.arange(6) * 3./2.))
@@ -557,7 +557,7 @@
         self.failUnless(allclose(average(z, None), 20./6.))
         self.failUnless(allclose(average(z, axis=0), [0.,1.,99.,99.,4.0, 7.5]))
         self.failUnless(allclose(average(z, axis=1), [2.5, 5.0]))
-        self.failUnless(allclose( average(z,weights=w2), [0.,1., 99., 99., 4.0, 10.0]))
+        self.failUnless(allclose( average(z,axis=0, weights=w2), [0.,1., 99., 99., 4.0, 10.0]))
 
         a = arange(6)
         b = arange(6) * 3
@@ -572,9 +572,9 @@
         self.failUnless(shape(w2) == shape(r2))
         a2d = array([[1,2],[0,4]], float)
         a2dm = masked_array(a2d, [[0,0],[1,0]])
-        a2da = average(a2d)
+        a2da = average(a2d, axis=0)
         self.failUnless(eq (a2da, [0.5, 3.0]))
-        a2dma = average(a2dm)
+        a2dma = average(a2dm, axis=0)
         self.failUnless(eq( a2dma, [1.0, 3.0]))
         a2dma = average(a2dm, axis=None)
         self.failUnless(eq(a2dma, 7./3.))

Modified: trunk/numpy/lib/function_base.py
===================================================================
--- trunk/numpy/lib/function_base.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/lib/function_base.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -1,4 +1,3 @@
-
 __all__ = ['logspace', 'linspace',
            'select', 'piecewise', 'trim_zeros',
            'copy', 'iterable', #'base_repr', 'binary_repr',
@@ -100,8 +99,8 @@
     else:
         return n, bins
 
-def average(a, axis=0, weights=None, returned=False):
-    """average(a, axis=0, weights=None, returned=False)
+def average(a, axis=None, weights=None, returned=False):
+    """average(a, axis=None weights=None, returned=False)
 
     Average the array over the given axis.  If the axis is None, average
     over all dimensions of the array.  Equivalent to a.mean(axis), but

Modified: trunk/numpy/numarray/functions.py
===================================================================
--- trunk/numpy/numarray/functions.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/numarray/functions.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -42,7 +42,7 @@
 
 from numpy import dot as matrixmultiply, dot, vdot, ravel, concatenate, all,\
      allclose, any, around, argsort, array_equal, array_equiv,\
-     array_str, array_repr, average, CLIP, RAISE, WRAP, clip, concatenate, \
+     array_str, array_repr, CLIP, RAISE, WRAP, clip, concatenate, \
      diagonal, e, pi, fromfunction, indices, inner as innerproduct, nonzero, \
      outer as outerproduct, kron as kroneckerproduct, lexsort, putmask, rank, \
      resize, searchsorted, shape, size, sort, swapaxes, trace, transpose
@@ -453,3 +453,6 @@
 
 def togglebyteorder(self):
     self.dtype=self.dtype.newbyteorder()
+
+def average(a, axis=0, weights=None, returned=0):
+    return N.average(a, axis, weights, returned)

Modified: trunk/numpy/oldnumeric/functions.py
===================================================================
--- trunk/numpy/oldnumeric/functions.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/oldnumeric/functions.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -9,7 +9,8 @@
            'cumsum', 'cumproduct', 'compress',
            'ones', 'empty', 'identity', 'zeros', 'array', 'asarray',
            'nonzero', 'reshape', 'arange', 'fromstring', 'ravel', 'trace',
-           'indices', 'where','sarray','cross_product', 'argmax', 'argmin']
+           'indices', 'where','sarray','cross_product', 'argmax', 'argmin',
+           'average']
 
 def take(a, indicies, axis=0):
     return N.take(a, indicies, axis)
@@ -115,3 +116,6 @@
 
 def cross_product(a, b, axis1=-1, axis2=-1):
     return N.cross(a, b, axis1, axis2)
+
+def average(a, axis=0, weights=None, returned=False):
+    return N.average(a, axis, weights, returned)

Modified: trunk/numpy/oldnumeric/ma.py
===================================================================
--- trunk/numpy/oldnumeric/ma.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/oldnumeric/ma.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -2,4 +2,14 @@
 #  instead of None
 
 from numpy.core.ma import *
+import numpy.core.ma as nca
 
+def repeat(a, repeats, axis=0):
+    return nca.repeat(a, repeats, axis)
+
+def average(a, axis=0, weights=None, returned=0):
+    return nca.average(a, axis, weights, returned)
+
+def take(a, indices, axis=0):
+    return nca.average(a, indices, axis=0)
+

Modified: trunk/numpy/oldnumeric/misc.py
===================================================================
--- trunk/numpy/oldnumeric/misc.py	2006-08-12 22:00:55 UTC (rev 3002)
+++ trunk/numpy/oldnumeric/misc.py	2006-08-13 07:15:52 UTC (rev 3003)
@@ -7,7 +7,7 @@
            'pi', 'math', 'concatenate',
            'around', 'vdot', 'transpose', 'array2string', 'diagonal',
            'searchsorted', 'put', 'fromfunction', 'copy', 'resize',
-           'array_repr', 'e', 'StringIO', 'pickle', 'average',
+           'array_repr', 'e', 'StringIO', 'pickle',
            'argsort', 'convolve', 'loads', 'cross_correlate',
            'Pickler', 'dot', 'outerproduct', 'innerproduct']
 
@@ -22,7 +22,7 @@
 from numpy import sort, clip, putmask, rank, sign, shape, allclose, size,\
      choose, swapaxes, array_str, array_repr, e, pi, \
      fromfunction, resize, around, concatenate, vdot, transpose, \
-     diagonal, searchsorted, put, average, argsort, convolve, dot, \
+     diagonal, searchsorted, put, argsort, convolve, dot, \
      outer as outerproduct, inner as innerproduct, correlate as cross_correlate
 
 from array_printer import array2string



More information about the Numpy-svn mailing list