Skip to content

Commit 8b76c7e

Browse files
committed
Merge pull request #436 from grlee77/shuffle_fix
MRG: make parrec tests robust to a bug in numpy 1.7 Add travis-ci test for numpy 1.7. Closes #435
2 parents e1aea51 + a158cd3 commit 8b76c7e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ matrix:
4545
- python: 2.7
4646
env:
4747
- DEPENDS="numpy==1.5.1 pydicom==0.9.7"
48+
# test against numpy 1.7
49+
- python: 2.7
50+
env:
51+
- DEPENDS="numpy==1.7.1"
4852
# pydicom 1.0 (currently unreleased)
4953
- python: 2.7
5054
env:

nibabel/tests/test_parrec.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@
148148
]
149149

150150

151+
def _shuffle(arr):
152+
"""Return a copy of the array with entries shuffled.
153+
154+
Needed to avoid a bug in np.random.shuffle for numpy 1.7.
155+
see: numpy/numpy#4286
156+
"""
157+
return arr[np.argsort(np.random.randn(len(arr)))]
158+
159+
151160
def test_top_level_load():
152161
# Test PARREC images can be loaded from nib.load
153162
img = top_load(EG_PAR)
@@ -285,7 +294,7 @@ def test_sorting_dual_echo_T1():
285294
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
286295

287296
# should get the correct order even if we randomly shuffle the order
288-
np.random.shuffle(t1_hdr.image_defs)
297+
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
289298

290299
sorted_indices = t1_hdr.get_sorted_slice_indices()
291300
sorted_echos = t1_hdr.image_defs['echo number'][sorted_indices]
@@ -316,7 +325,7 @@ def test_sorting_multiple_echos_and_contrasts():
316325
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
317326

318327
# should get the correct order even if we randomly shuffle the order
319-
np.random.shuffle(t1_hdr.image_defs)
328+
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
320329

321330
sorted_indices = t1_hdr.get_sorted_slice_indices()
322331
sorted_slices = t1_hdr.image_defs['slice number'][sorted_indices]
@@ -358,7 +367,7 @@ def test_sorting_multiecho_ASL():
358367
asl_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
359368

360369
# should get the correct order even if we randomly shuffle the order
361-
np.random.shuffle(asl_hdr.image_defs)
370+
asl_hdr.image_defs = _shuffle(asl_hdr.image_defs)
362371

363372
sorted_indices = asl_hdr.get_sorted_slice_indices()
364373
sorted_slices = asl_hdr.image_defs['slice number'][sorted_indices]
@@ -486,7 +495,7 @@ def test_diffusion_parameters_strict_sort():
486495
dti_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)
487496

488497
# should get the correct order even if we randomly shuffle the order
489-
np.random.shuffle(dti_hdr.image_defs)
498+
dti_hdr.image_defs = _shuffle(dti_hdr.image_defs)
490499

491500
assert_equal(dti_hdr.get_data_shape(), (80, 80, 10, 8))
492501
assert_equal(dti_hdr.general_info['diffusion'], 1)

0 commit comments

Comments
 (0)