diff options
author | pierregm <pierregm@localhost> | 2008-07-21 18:51:47 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-07-21 18:51:47 +0000 |
commit | 70e9b4906838c7e8a44d4e58c57517df68671555 (patch) | |
tree | af2a12d8ea77b7c7c4d746606d5d370963a740f7 | |
parent | b8ecdad75b831eec62fb02e76d80a45f7ec9aa58 (diff) | |
download | numpy-70e9b4906838c7e8a44d4e58c57517df68671555.tar.gz |
core:
* fixed the reshape function/method to accept an 'order' optional parameter
-rw-r--r-- | numpy/ma/core.py | 51 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 80 | ||||
-rw-r--r-- | numpy/ma/tests/test_mrecords.py | 2 | ||||
-rw-r--r-- | numpy/ma/tests/test_old_ma.py | 44 |
4 files changed, 107 insertions, 70 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index f1e652c5c..0e6696ad5 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2038,24 +2038,36 @@ masked_%(name)s(data = %(data)s, # 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): @@ -3066,8 +3078,10 @@ masked_%(name)s(data = %(data)s, return result #........................ def tostring(self, fill_value=None, order='C'): - """Return a copy of array data as a Python string containing the raw - bytes in the array. + """ + Return a copy of array data as a Python string containing the raw bytes + in the array. + The array is filled beforehand. Parameters ---------- @@ -3081,6 +3095,11 @@ masked_%(name)s(data = %(data)s, "Any" -- Current order of array. None -- Same as "Any" + Warnings + -------- + As for :meth:`ndarray.tostring`, information about the shape, dtype..., + but also fill_value will be lost. + """ return self.filled(fill_value).tostring(order=order) #........................ @@ -3601,13 +3620,13 @@ def transpose(a, axes=None): 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. diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index cb27e1d90..d84fbf7a3 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -60,7 +60,7 @@ class TestMaskedArray(TestCase): x = masked_array(0, mask=False) assert_equal(str(x), '0') x = array(0, mask=1) - assert(x.filled().dtype is x.data.dtype) + assert(x.filled().dtype is x._data.dtype) def test_basic1d(self): @@ -753,7 +753,7 @@ class TestMaskedArrayAttributes(TestCase): xs[[1,4]] = [10,40] assert_equal(xh._data, [0,10,2,3,4]) assert_equal(xs._data, [0,10,2,3,40]) - #assert_equal(xh.mask.ctypes.data, m.ctypes.data) + #assert_equal(xh.mask.ctypes._data, m.ctypes._data) assert_equal(xs.mask, [0,0,0,1,0]) assert(xh._hardmask) assert(not xs._hardmask) @@ -761,7 +761,7 @@ class TestMaskedArrayAttributes(TestCase): xs[1:4] = [10,20,30] assert_equal(xh._data, [0,10,20,3,4]) assert_equal(xs._data, [0,10,20,30,40]) - #assert_equal(xh.mask.ctypes.data, m.ctypes.data) + #assert_equal(xh.mask.ctypes._data, m.ctypes._data) assert_equal(xs.mask, nomask) xh[0] = masked xs[0] = masked @@ -804,7 +804,7 @@ class TestMaskedArrayAttributes(TestCase): m = make_mask(n) xh = array(d, mask = m, hard_mask=True) xh[4:5] = 999 - #assert_equal(xh.mask.ctypes.data, m.ctypes.data) + #assert_equal(xh.mask.ctypes._data, m.ctypes._data) xh[0:1] = 999 assert_equal(xh._data,[999,1,2,3,4]) @@ -1047,9 +1047,9 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): # warnings.simplefilter('ignore', DeprecationWarning) (x, _, xm) = self.floatdata - id1 = x.raw_data().ctypes.data + id1 = x.raw_data().ctypes._data x += 1. - assert (id1 == x.raw_data().ctypes.data) + assert (id1 == x.raw_data().ctypes._data) assert_equal(x, y+1.) warnings.simplefilter('default', DeprecationWarning) @@ -1195,20 +1195,20 @@ class TestMaskedArrayMethods(TestCase): "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()) + 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()) + 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 test_allany(self): @@ -1325,8 +1325,8 @@ class TestMaskedArrayMethods(TestCase): mx = array(x,mask=m) 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)) + assert_equal(clipped._data,x.clip(2,8)) + assert_equal(clipped._data,mx._data.clip(2,8)) def test_compress(self): @@ -1724,14 +1724,14 @@ class TestMaskArrayMathMethod(TestCase): "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)) + assert_equal(mXcp._data,mX.filled(0).cumsum(0)) mXcp = mX.cumsum(1) - assert_equal(mXcp.data,mX.filled(0).cumsum(1)) + assert_equal(mXcp._data,mX.filled(0).cumsum(1)) # mXcp = mX.cumprod(0) - assert_equal(mXcp.data,mX.filled(1).cumprod(0)) + assert_equal(mXcp._data,mX.filled(1).cumprod(0)) mXcp = mX.cumprod(1) - assert_equal(mXcp.data,mX.filled(1).cumprod(1)) + assert_equal(mXcp._data,mX.filled(1).cumprod(1)) def test_cumsumprod_with_output(self): @@ -2143,6 +2143,24 @@ class TestMaskedArrayFunctions(TestCase): assert_equal(store, array([999999, 31, 12, 999999])) + 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']) + #------------------------------------------------------------------------------ class TestMaskedFields(TestCase): @@ -2183,13 +2201,13 @@ class TestMaskedFields(TestCase): base[0] = (pi, pi, 'pi') assert_equal(base_a.dtype, int) - assert_equal(base_a.data, [3,2,3,4,5]) + assert_equal(base_a._data, [3,2,3,4,5]) assert_equal(base_b.dtype, float) - assert_equal(base_b.data, [pi, 2.2, 3.3, 4.4, 5.5]) + assert_equal(base_b._data, [pi, 2.2, 3.3, 4.4, 5.5]) assert_equal(base_c.dtype, '|S8') - assert_equal(base_c.data, ['pi','two','three','four','five']) + assert_equal(base_c._data, ['pi','two','three','four','five']) def test_set_record_slice(self): base = self.data['base'] @@ -2197,13 +2215,13 @@ class TestMaskedFields(TestCase): base[:3] = (pi, pi, 'pi') assert_equal(base_a.dtype, int) - assert_equal(base_a.data, [3,3,3,4,5]) + assert_equal(base_a._data, [3,3,3,4,5]) assert_equal(base_b.dtype, float) - assert_equal(base_b.data, [pi, pi, pi, 4.4, 5.5]) + assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5]) assert_equal(base_c.dtype, '|S8') - assert_equal(base_c.data, ['pi','pi','pi','four','five']) + assert_equal(base_c._data, ['pi','pi','pi','four','five']) def test_mask_element(self): "Check record access" @@ -2213,7 +2231,7 @@ class TestMaskedFields(TestCase): # for n in ('a','b','c'): assert_equal(base[n].mask, [1,1,0,0,1]) - assert_equal(base[n].data, base.data[n]) + assert_equal(base[n]._data, base._data[n]) ############################################################################### #------------------------------------------------------------------------------ diff --git a/numpy/ma/tests/test_mrecords.py b/numpy/ma/tests/test_mrecords.py index abc061503..901448c31 100644 --- a/numpy/ma/tests/test_mrecords.py +++ b/numpy/ma/tests/test_mrecords.py @@ -359,7 +359,7 @@ class TestMRecordsImport(TestCase): ddtype = [('a',int),('b',float),('c','|S8')] mrec = fromarrays([_a,_b,_c], dtype=ddtype, fill_value=(99999,99999.,'N/A')) - nrec = recfromarrays((_a.data,_b.data,_c.data), dtype=ddtype) + nrec = recfromarrays((_a._data,_b._data,_c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype) def test_fromarrays(self): diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index d7940764e..50b25fe04 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -254,8 +254,8 @@ class TestMa(TestCase): x1 = numpy.arange(5) y1 = array(x1, mask=m) - self.failUnless( y1.data is not x1) - self.failUnless( allequal(x1,y1.data)) + self.failUnless( y1._data is not x1) + self.failUnless( allequal(x1,y1._data)) self.failUnless( y1.mask is m) y1a = array(y1, copy=0) @@ -603,27 +603,27 @@ class TestMa(TestCase): self.failUnless((-xm).mask) self.failUnless(maximum(xm, xm).mask) self.failUnless(minimum(xm, xm).mask) - self.failUnless(xm.filled().dtype is xm.data.dtype) + self.failUnless(xm.filled().dtype is xm._data.dtype) x = array(0, mask=0) - self.failUnless(x.filled() == x.data) + self.failUnless(x.filled() == x._data) self.failUnlessEqual(str(xm), str(masked_print_option)) def test_testArrayMethods(self): a = array([1,3,2]) b = array([1,3,2], mask=[1,0,1]) - self.failUnless(eq(a.any(), a.data.any())) - self.failUnless(eq(a.all(), a.data.all())) - self.failUnless(eq(a.argmax(), a.data.argmax())) - self.failUnless(eq(a.argmin(), a.data.argmin())) - self.failUnless(eq(a.choose(0,1,2,3,4), a.data.choose(0,1,2,3,4))) - self.failUnless(eq(a.compress([1,0,1]), a.data.compress([1,0,1]))) - self.failUnless(eq(a.conj(), a.data.conj())) - self.failUnless(eq(a.conjugate(), a.data.conjugate())) + self.failUnless(eq(a.any(), a._data.any())) + self.failUnless(eq(a.all(), a._data.all())) + self.failUnless(eq(a.argmax(), a._data.argmax())) + self.failUnless(eq(a.argmin(), a._data.argmin())) + self.failUnless(eq(a.choose(0,1,2,3,4), a._data.choose(0,1,2,3,4))) + self.failUnless(eq(a.compress([1,0,1]), a._data.compress([1,0,1]))) + self.failUnless(eq(a.conj(), a._data.conj())) + self.failUnless(eq(a.conjugate(), a._data.conjugate())) m = array([[1,2],[3,4]]) - self.failUnless(eq(m.diagonal(), m.data.diagonal())) - self.failUnless(eq(a.sum(), a.data.sum())) - self.failUnless(eq(a.take([1,2]), a.data.take([1,2]))) - self.failUnless(eq(m.transpose(), m.data.transpose())) + self.failUnless(eq(m.diagonal(), m._data.diagonal())) + self.failUnless(eq(a.sum(), a._data.sum())) + self.failUnless(eq(a.take([1,2]), a._data.take([1,2]))) + self.failUnless(eq(m.transpose(), m._data.transpose())) def test_testArrayAttributes(self): a = array([1,3,2]) @@ -756,8 +756,8 @@ class TestArrayMethods(TestCase): (x,X,XX,m,mx,mX,mXX,) = self.d clipped = mx.clip(2,8) self.failUnless(eq(clipped.mask,mx.mask)) - self.failUnless(eq(clipped.data,x.clip(2,8))) - self.failUnless(eq(clipped.data,mx.data.clip(2,8))) + self.failUnless(eq(clipped._data,x.clip(2,8))) + self.failUnless(eq(clipped._data,mx._data.clip(2,8))) def test_ptp(self): (x,X,XX,m,mx,mX,mXX,) = self.d @@ -783,16 +783,16 @@ class TestArrayMethods(TestCase): def test_cumprod(self): (x,X,XX,m,mx,mX,mXX,) = self.d mXcp = mX.cumprod(0) - self.failUnless(eq(mXcp.data,mX.filled(1).cumprod(0))) + self.failUnless(eq(mXcp._data,mX.filled(1).cumprod(0))) mXcp = mX.cumprod(1) - self.failUnless(eq(mXcp.data,mX.filled(1).cumprod(1))) + self.failUnless(eq(mXcp._data,mX.filled(1).cumprod(1))) def test_cumsum(self): (x,X,XX,m,mx,mX,mXX,) = self.d mXcp = mX.cumsum(0) - self.failUnless(eq(mXcp.data,mX.filled(0).cumsum(0))) + self.failUnless(eq(mXcp._data,mX.filled(0).cumsum(0))) mXcp = mX.cumsum(1) - self.failUnless(eq(mXcp.data,mX.filled(0).cumsum(1))) + self.failUnless(eq(mXcp._data,mX.filled(0).cumsum(1))) def test_varstd(self): (x,X,XX,m,mx,mX,mXX,) = self.d |