Skip to content

Commit e8ab18f

Browse files
committed
TEST Test relaxed FreeSurfer hack bounds
1 parent 0a3b0b8 commit e8ab18f

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

nibabel/tests/test_nifti1.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,28 @@ def test_freesurfer_large_vector_hack(self):
261261
hdr.set_data_shape((too_big-1, 1, 1))
262262
assert_equal(hdr.get_data_shape(), (too_big-1, 1, 1))
263263
# The freesurfer case
264+
full_shape = (too_big, 1, 1, 1, 1, 1, 1)
265+
for dim in range(3, 7):
266+
expected_dim = np.array([dim, -1, 1, 1, 1, 1, 1, 1])
267+
with suppress_warnings():
268+
hdr.set_data_shape(full_shape[:dim])
269+
assert_equal(hdr.get_data_shape(), full_shape[:dim])
270+
assert_array_equal(hdr['dim'], expected_dim)
271+
assert_equal(hdr['glmin'], too_big)
272+
# Allow the fourth dimension to vary
264273
with suppress_warnings():
265-
hdr.set_data_shape((too_big, 1, 1))
266-
assert_equal(hdr.get_data_shape(), (too_big, 1, 1))
267-
assert_array_equal(hdr['dim'][:4], [3, -1, 1, 1])
268-
assert_equal(hdr['glmin'], too_big)
269-
# This only works for the case of a 3D with -1, 1, 1
274+
hdr.set_data_shape((too_big, 1, 1, 4))
275+
assert_equal(hdr.get_data_shape(), (too_big, 1, 1, 4))
276+
assert_array_equal(hdr['dim'][:5], np.array([4, -1, 1, 1, 4]))
277+
# This only works when the first 3 dimensions are -1, 1, 1
270278
assert_raises(HeaderDataError, hdr.set_data_shape, (too_big,))
271279
assert_raises(HeaderDataError, hdr.set_data_shape, (too_big,1))
272280
assert_raises(HeaderDataError, hdr.set_data_shape, (too_big,1,2))
273281
assert_raises(HeaderDataError, hdr.set_data_shape, (too_big,2,1))
274282
assert_raises(HeaderDataError, hdr.set_data_shape, (1, too_big))
275283
assert_raises(HeaderDataError, hdr.set_data_shape, (1, too_big, 1))
276284
assert_raises(HeaderDataError, hdr.set_data_shape, (1, 1, too_big))
285+
assert_raises(HeaderDataError, hdr.set_data_shape, (1, 1, 1, too_big))
277286
# Outside range of glmin raises error
278287
far_too_big = int(np.iinfo(glmin).max) + 1
279288
with suppress_warnings():
@@ -295,9 +304,22 @@ def test_freesurfer_large_vector_hack(self):
295304
def test_freesurfer_ico7_hack(self):
296305
HC = self.header_class
297306
hdr = HC()
307+
full_shape = (163842, 1, 1, 1, 1, 1, 1)
298308
# Test that using ico7 shape automatically uses factored dimensions
299-
hdr.set_data_shape((163842, 1, 1))
300-
assert_array_equal(hdr._structarr['dim'][1:4], np.array([27307, 1, 6]))
309+
for dim in range(3, 7):
310+
expected_dim = np.array([dim, 27307, 1, 6, 1, 1, 1, 1])
311+
hdr.set_data_shape(full_shape[:dim])
312+
assert_equal(hdr.get_data_shape(), full_shape[:dim])
313+
assert_array_equal(hdr._structarr['dim'], expected_dim)
314+
# Only works on dimensions >= 3
315+
assert_raises(HeaderDataError, hdr.set_data_shape, full_shape[:1])
316+
assert_raises(HeaderDataError, hdr.set_data_shape, full_shape[:2])
317+
# Bad shapes
318+
assert_raises(HeaderDataError, hdr.set_data_shape, (163842, 2, 1))
319+
assert_raises(HeaderDataError, hdr.set_data_shape, (163842, 1, 2))
320+
assert_raises(HeaderDataError, hdr.set_data_shape, (1, 163842, 1))
321+
assert_raises(HeaderDataError, hdr.set_data_shape, (1, 1, 163842))
322+
assert_raises(HeaderDataError, hdr.set_data_shape, (1, 1, 1, 163842))
301323
# Test consistency of data in .mgh and mri_convert produced .nii
302324
nitest_path = os.path.join(get_nibabel_data(), 'nitest-freesurfer')
303325
mgh = mghload(os.path.join(nitest_path, 'fsaverage', 'surf',

0 commit comments

Comments
 (0)