Skip to content

Commit 880bdaa

Browse files
committed
RF - allow lists and arrays for shape
Shape was assumed a tuple.
1 parent bffecea commit 880bdaa

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

nibabel/nifti1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def set_data_shape(self, shape):
674674
'''
675675
# Apply freesurfer hack for vector
676676
hdr = self._structarr
677+
shape = tuple(shape)
677678
if (len(shape) == 3 and shape[1:] == (1, 1) and
678679
shape[0] > np.iinfo(hdr['dim'].dtype.base).max): # Freesurfer case
679680
try:

nibabel/tests/test_analyze.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ def test_shapes(self):
234234
assert_equal(hdr.get_data_shape(), shape)
235235
shape = (mx+1,)
236236
assert_raises(HeaderDataError, hdr.set_data_shape, shape)
237+
# Lists or tuples or arrays will work for setting shape
238+
shape = (2, 3, 4)
239+
for constructor in (list, tuple, np.array):
240+
hdr.set_data_shape(constructor(shape))
241+
assert_equal(hdr.get_data_shape(), shape)
237242

238243
def test_read_write_data(self):
239244
# Check reading and writing of data

nibabel/tests/test_nifti1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ def test_freesurfer_hack(self):
206206
hdr.set_data_shape((-1,1,1))
207207
hdr['glmin'] = 0
208208
assert_raises(HeaderDataError, hdr.get_data_shape)
209+
# Lists or tuples or arrays will work for setting shape
210+
for shape in ((too_big-1, 1, 1), (too_big, 1, 1)):
211+
for constructor in (list, tuple, np.array):
212+
hdr.set_data_shape(constructor(shape))
213+
assert_equal(hdr.get_data_shape(), shape)
209214

210215

211216
class TestNifti1SingleHeader(TestNifti1PairHeader):

0 commit comments

Comments
 (0)