[Numpy-svn] r5490 - in branches/1.1.x/numpy/ma: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Jul 21 14:49:53 EDT 2008


Author: pierregm
Date: 2008-07-21 13:49:45 -0500 (Mon, 21 Jul 2008)
New Revision: 5490

Modified:
   branches/1.1.x/numpy/ma/core.py
   branches/1.1.x/numpy/ma/tests/test_core.py
   branches/1.1.x/numpy/ma/testutils.py
Log:
testutils :
* backport from 1.2

core:
* fixed the reshape function/method to accept an 'order' optional parameter

Modified: branches/1.1.x/numpy/ma/core.py
===================================================================
--- branches/1.1.x/numpy/ma/core.py	2008-07-21 16:17:51 UTC (rev 5489)
+++ branches/1.1.x/numpy/ma/core.py	2008-07-21 18:49:45 UTC (rev 5490)
@@ -1867,24 +1867,36 @@
     #
     repeat = _arraymethod('repeat')
     #
-    def reshape (self, *s):
-        """Reshape the array to shape s.
+    def reshape (self, shape, order='C'):
+        """
+    Returns a masked array containing the data of a, but with a new shape.
+    The result is a view to the original array; if this is not possible,
+    a ValueError is raised.
 
-        Returns
-        -------
-        A new masked array.
+    Parameters
+    ----------
+    shape : shape tuple or int
+       The new shape should be compatible with the original shape. If an
+       integer, then the result will be a 1D array of that length.
+    order : {'C', 'F'}, optional
+        Determines whether the array data should be viewed as in C
+        (row-major) order or FORTRAN (column-major) order.
 
-        Notes
-        -----
-        If you want to modify the shape in place, please use
-        ``a.shape = s``
+    Returns
+    -------
+    reshaped_array : array
+        A new view to the array.
 
+    Notes
+    -----
+    If you want to modify the shape in place, please use ``a.shape = s``
+
         """
-        result = self._data.reshape(*s).view(type(self))
+        result = self._data.reshape(shape, order=order).view(type(self))
         result._update_from(self)
         mask = self._mask
         if mask is not nomask:
-            result._mask = mask.reshape(*s)
+            result._mask = mask.reshape(shape, order=order)
         return result
     #
     def resize(self, newshape, refcheck=True, order=False):
@@ -3408,13 +3420,13 @@
     except AttributeError:
         return narray(a, copy=False).transpose(axes).view(MaskedArray)
 
-def reshape(a, new_shape):
+def reshape(a, new_shape, order='C'):
     """Change 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)
+        return a.reshape(new_shape, order=order)
     except AttributeError:
-        return narray(a, copy=False).reshape(new_shape).view(MaskedArray)
+        return narray(a, copy=False).reshape(new_shape, order=order).view(MaskedArray)
 
 def resize(x, new_shape):
     """Return a new array with the specified shape.

Modified: branches/1.1.x/numpy/ma/tests/test_core.py
===================================================================
--- branches/1.1.x/numpy/ma/tests/test_core.py	2008-07-21 16:17:51 UTC (rev 5489)
+++ branches/1.1.x/numpy/ma/tests/test_core.py	2008-07-21 18:49:45 UTC (rev 5490)
@@ -865,6 +865,23 @@
         assert_equal(xs.dtype, '|S3')
 
 
+    def test_reshape(self):
+        a = arange(10)
+        a[0] = masked
+        # Try the default
+        b = a.reshape((5,2))
+        assert_equal(b.shape, (5,2))
+        assert(b.flags['C'])
+        # Try w/ order
+        b = a.reshape((5,2), order='F')
+        assert_equal(b.shape, (5,2))
+        assert(b.flags['F'])
+        #
+        c = np.reshape(a, (2,5))
+        assert(isinstance(c, MaskedArray))
+        assert_equal(c.shape, (2,5))
+        assert(c[0,0] is masked)
+        assert(c.flags['C'])
 
 #...............................................................................
 

Modified: branches/1.1.x/numpy/ma/testutils.py
===================================================================
--- branches/1.1.x/numpy/ma/testutils.py	2008-07-21 16:17:51 UTC (rev 5489)
+++ branches/1.1.x/numpy/ma/testutils.py	2008-07-21 18:49:45 UTC (rev 5490)
@@ -104,10 +104,18 @@
         raise ValueError(msg)
     actual = np.array(actual, copy=False, subok=True)
     desired = np.array(desired, copy=False, subok=True)
-    if actual.dtype.char in "OSV" and desired.dtype.char in "OSV":
+    if actual_dtype.char == "S" and desired_dtype.char == "S":
         return _assert_equal_on_sequences(actual.tolist(),
                                           desired.tolist(),
                                           err_msg='')
+    elif actual_dtype.char in "OV" and desired_dtype.char in "OV":
+        if (actual_dtype != desired_dtype) and actual_dtype:
+            msg = build_err_msg([actual_dtype, desired_dtype],
+                                err_msg, header='', names=('actual', 'desired'))
+            raise ValueError(msg)
+        return _assert_equal_on_sequences(actual.tolist(),
+                                          desired.tolist(),
+                                          err_msg='')
     return assert_array_equal(actual, desired, err_msg)
 
 




More information about the Numpy-svn mailing list