Skip to content

Commit

Permalink
Merge pull request #1234 from nipy/debug
Browse files Browse the repository at this point in the history
TEST: Only use stable argsorts in PARREC tests
  • Loading branch information
effigies authored Jun 27, 2023
2 parents b7022e0 + c385a53 commit 064e80d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
22 changes: 8 additions & 14 deletions nibabel/tests/test_parrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,6 @@
]


def _shuffle(arr):
"""Return a copy of the array with entries shuffled.
Needed to avoid a bug in np.random.shuffle for numpy 1.7.
see: numpy/numpy#4286
"""
return arr[np.argsort(np.random.randn(len(arr)))]


def test_top_level_load():
# Test PARREC images can be loaded from nib.load
img = top_load(EG_PAR)
Expand Down Expand Up @@ -332,7 +323,7 @@ def test_sorting_dual_echo_T1():
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)

# should get the correct order even if we randomly shuffle the order
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
np.random.shuffle(t1_hdr.image_defs)

sorted_indices = t1_hdr.get_sorted_slice_indices()
sorted_echos = t1_hdr.image_defs['echo number'][sorted_indices]
Expand Down Expand Up @@ -363,7 +354,7 @@ def test_sorting_multiple_echos_and_contrasts():
t1_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)

# should get the correct order even if we randomly shuffle the order
t1_hdr.image_defs = _shuffle(t1_hdr.image_defs)
np.random.shuffle(t1_hdr.image_defs)

sorted_indices = t1_hdr.get_sorted_slice_indices()
sorted_slices = t1_hdr.image_defs['slice number'][sorted_indices]
Expand Down Expand Up @@ -402,7 +393,7 @@ def test_sorting_multiecho_ASL():
asl_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)

# should get the correct order even if we randomly shuffle the order
asl_hdr.image_defs = _shuffle(asl_hdr.image_defs)
np.random.shuffle(asl_hdr.image_defs)

sorted_indices = asl_hdr.get_sorted_slice_indices()
sorted_slices = asl_hdr.image_defs['slice number'][sorted_indices]
Expand Down Expand Up @@ -524,7 +515,7 @@ def test_diffusion_parameters_strict_sort():
dti_hdr = PARRECHeader.from_fileobj(fobj, strict_sort=True)

# should get the correct order even if we randomly shuffle the order
dti_hdr.image_defs = _shuffle(dti_hdr.image_defs)
np.random.shuffle(dti_hdr.image_defs)

assert dti_hdr.get_data_shape() == (80, 80, 10, 8)
assert dti_hdr.general_info['diffusion'] == 1
Expand All @@ -533,7 +524,10 @@ def test_diffusion_parameters_strict_sort():
# DTI_PAR_BVECS gives bvecs copied from first slice each vol in DTI.PAR
# Permute to match bvec directions to acquisition directions
# note that bval sorting occurs prior to bvec sorting
assert_almost_equal(bvecs, DTI_PAR_BVECS[np.ix_(np.argsort(DTI_PAR_BVALS), [2, 0, 1])])
assert_almost_equal(
bvecs,
DTI_PAR_BVECS[np.ix_(np.argsort(DTI_PAR_BVALS, kind='stable'), [2, 0, 1])],
)
# Check q vectors
assert_almost_equal(dti_hdr.get_q_vectors(), bvals[:, None] * bvecs)

Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def test_parrec2nii_with_data():
assert_almost_equal(np.loadtxt('DTI.bvals'), np.sort(DTI_PAR_BVALS))
img = load('DTI.nii')
data_sorted = img.get_fdata()
assert_almost_equal(data[..., np.argsort(DTI_PAR_BVALS)], data_sorted)
assert_almost_equal(data[..., np.argsort(DTI_PAR_BVALS, kind='stable')], data_sorted)
del img

# Writes .ordering.csv if requested
Expand Down

0 comments on commit 064e80d

Please sign in to comment.