|
17 | 17 |
|
18 | 18 | import numpy as np
|
19 | 19 |
|
20 |
| -from ..arrayproxy import ArrayProxy, is_proxy |
| 20 | +from ..arrayproxy import ArrayProxy, is_proxy, reshape_dataobj |
21 | 21 | from ..nifti1 import Nifti1Header
|
22 | 22 |
|
23 | 23 | from numpy.testing import assert_array_equal, assert_array_almost_equal
|
@@ -158,6 +158,33 @@ class NP(object):
|
158 | 158 | assert_false(is_proxy(NP()))
|
159 | 159 |
|
160 | 160 |
|
| 161 | +def test_reshape_dataobj(): |
| 162 | + # Test function that reshapes using method if possible |
| 163 | + shape = (1, 2, 3, 4) |
| 164 | + hdr = FunkyHeader(shape) |
| 165 | + bio = BytesIO() |
| 166 | + prox = ArrayProxy(bio, hdr) |
| 167 | + arr = np.arange(np.prod(shape), dtype=prox.dtype).reshape(shape) |
| 168 | + bio.write(b'\x00' * prox.offset + arr.tostring(order='F')) |
| 169 | + assert_array_equal(prox, arr) |
| 170 | + assert_array_equal(reshape_dataobj(prox, (2, 3, 4)), |
| 171 | + np.reshape(arr, (2, 3, 4))) |
| 172 | + assert_equal(prox.shape, shape) |
| 173 | + assert_equal(arr.shape, shape) |
| 174 | + assert_array_equal(reshape_dataobj(arr, (2, 3, 4)), |
| 175 | + np.reshape(arr, (2, 3, 4))) |
| 176 | + assert_equal(arr.shape, shape) |
| 177 | + |
| 178 | + class ArrGiver(object): |
| 179 | + |
| 180 | + def __array__(self): |
| 181 | + return arr |
| 182 | + |
| 183 | + assert_array_equal(reshape_dataobj(ArrGiver(), (2, 3, 4)), |
| 184 | + np.reshape(arr, (2, 3, 4))) |
| 185 | + assert_equal(arr.shape, shape) |
| 186 | + |
| 187 | + |
161 | 188 | def test_get_unscaled():
|
162 | 189 | # Test fetch of raw array
|
163 | 190 | class FunkyHeader2(FunkyHeader):
|
|
0 commit comments