Skip to content

Commit bda7cc5

Browse files
committed
Cover the case key_info is a string; modify unit tests
1 parent bf9a3e1 commit bda7cc5

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

heudiconv/bids.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,12 @@ def find_compatible_fmaps_for_run(json_file, fmap_groups, matching_parameters=['
680680
compatible = False
681681
for param in matching_parameters:
682682
fm_info = get_key_info_for_fmap_assignment(fm_group[0], param)
683-
# allow for tiny differences between the affines etc
684-
compatible = all(np.allclose(x, y) for x, y in zip(json_info[param], fm_info))
683+
# for the case in which key_info is a list of strings:
684+
if type(json_info[param][0]):
685+
compatible = json_info[param] == fm_info
686+
else:
687+
# allow for tiny differences between the affines etc
688+
compatible = all(np.allclose(x, y) for x, y in zip(json_info[param], fm_info))
685689
if not compatible:
686690
continue # don't bother checking more params
687691
if compatible:
172 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-1.99887669 0.0670080110 0.00144872535 109.236588
2+
0.0650372952 1.94962955 -0.441264838 -71.7261658
3+
0.0161963794 0.440969884 1.95071352 -64.1231308
4+
0 0 0 1
5+
5 5 5 1

heudiconv/tests/test_bids.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
from glob import glob
1515

1616
import nibabel
17+
from numpy import testing as np_testing
1718

1819
from heudiconv.utils import (
1920
load_json,
2021
save_json,
2122
create_tree,
23+
remove_suffix,
2224
)
25+
2326
from heudiconv.bids import (
2427
maybe_na,
2528
treat_age,
@@ -35,6 +38,10 @@
3538
KeyInfoForForce,
3639
)
3740

41+
from .utils import (
42+
TESTS_DATA_PATH,
43+
)
44+
3845
import pytest
3946

4047
def test_maybe_na():
@@ -60,7 +67,7 @@ def test_treat_age():
6067
TODAY = datetime.today()
6168

6269

63-
A_SHIM = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
70+
A_SHIM = [random() for i in range(SHIM_LENGTH)]
6471
def test_get_shim_setting(tmpdir):
6572
""" Tests for get_shim_setting """
6673
json_dir = op.join(str(tmpdir), 'foo')
@@ -77,38 +84,24 @@ def test_get_shim_setting(tmpdir):
7784
assert get_shim_setting(json_name) == A_SHIM
7885

7986

80-
def test_get_key_info_for_fmap_assignment(tmpdir, monkeypatch):
87+
def test_get_key_info_for_fmap_assignment(tmpdir):
8188
"""
8289
Test get_key_info_for_fmap_assignment
8390
"""
8491

85-
# Stuff needed to mock reading of a NIfTI file header:
86-
87-
# affines (qforms/sforms) are 4x4 matrices
88-
MY_AFFINE = [[random() for i in range(4)] for j in range(4)]
89-
# dims are arrays with 8 elements with the first one indicating the number
90-
# of dims in the image; remaining elements are 1:
91-
MY_DIM = [4] + [round(256 * random()) for i in range(4)] + [1] * 3
92-
# We use namedtuples so that we can use the .dot notation, to mock
93-
# nibabel headers:
94-
MyHeader = namedtuple('MyHeader', 'affine dim')
95-
MY_HEADER = MyHeader(MY_AFFINE, MY_DIM)
96-
MyMockNifti = namedtuple('MyMockNifti', 'header')
97-
98-
def mock_nibabel_load(file):
99-
"""
100-
Pretend we run nibabel.load, but return only a header with just a few fields
101-
"""
102-
return MyMockNifti(MY_HEADER)
103-
monkeypatch.setattr(nibabel, "load", mock_nibabel_load)
104-
105-
json_name = op.join(str(tmpdir), 'foo.json')
92+
nifti_file = op.join(TESTS_DATA_PATH, 'sample_nifti.nii.gz')
93+
# Get the expected parameters from the NIfTI header:
94+
MY_HEADER = nibabel.ni1.np.loadtxt(
95+
op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '_params.txt')
96+
)
97+
json_name = op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '.json')
10698

10799
# 1) Call for a non-existing file should give an error:
108100
with pytest.raises(FileNotFoundError):
109101
assert get_key_info_for_fmap_assignment('foo.json')
110102

111103
# 2) matching_parameters = 'Shims'
104+
json_name = op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '.json')
112105
save_json(json_name, {SHIM_KEY: A_SHIM}) # otherwise get_key_info_for_fmap_assignment will give an error
113106
key_info = get_key_info_for_fmap_assignment(
114107
json_name, matching_parameter='Shims'
@@ -119,7 +112,8 @@ def mock_nibabel_load(file):
119112
key_info = get_key_info_for_fmap_assignment(
120113
json_name, matching_parameter='ImagingVolume'
121114
)
122-
assert key_info == [MY_AFFINE, MY_DIM[1:3]]
115+
np_testing.assert_almost_equal(key_info[0], MY_HEADER[:4], decimal=6)
116+
np_testing.assert_almost_equal(key_info[1], MY_HEADER[4][:3], decimal=6)
123117

124118
# 4) matching_parameters = 'Force'
125119
key_info = get_key_info_for_fmap_assignment(
@@ -219,10 +213,10 @@ def create_dummy_pepolar_bids_session(session_path):
219213
# 1) Simulate the file structure for a session:
220214

221215
# Generate some random ShimSettings:
222-
anat_shims = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
223-
dwi_shims = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
224-
func_shims_A = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
225-
func_shims_B = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
216+
anat_shims = [random() for i in range(SHIM_LENGTH)]
217+
dwi_shims = [random() for i in range(SHIM_LENGTH)]
218+
func_shims_A = [random() for i in range(SHIM_LENGTH)]
219+
func_shims_B = [random() for i in range(SHIM_LENGTH)]
226220

227221
# Dict with the file structure for the session:
228222
# -anat:
@@ -246,7 +240,7 @@ def create_dummy_pepolar_bids_session(session_path):
246240
'{p}_acq-A_bold.json'.format(p=prefix): {'ShimSetting': func_shims_A},
247241
'{p}_acq-B_bold.json'.format(p=prefix): {'ShimSetting': func_shims_B},
248242
'{p}_acq-unmatched_bold.json'.format(p=prefix): {
249-
'ShimSetting': ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
243+
'ShimSetting': [random() for i in range(SHIM_LENGTH)]
250244
},
251245
})
252246
# -fmap:
@@ -538,9 +532,9 @@ def create_dummy_magnitude_phase_bids_session(session_path):
538532
# 1) Simulate the file structure for a session:
539533

540534
# Generate some random ShimSettings:
541-
dwi_shims = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
542-
func_shims_A = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
543-
func_shims_B = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
535+
dwi_shims = [random() for i in range(SHIM_LENGTH)]
536+
func_shims_A = [random() for i in range(SHIM_LENGTH)]
537+
func_shims_B = [random() for i in range(SHIM_LENGTH)]
544538

545539
# Dict with the file structure for the session:
546540
# -dwi:
@@ -558,7 +552,7 @@ def create_dummy_magnitude_phase_bids_session(session_path):
558552
'{p}_acq-A_bold.json'.format(p=prefix): {'ShimSetting': func_shims_A},
559553
'{p}_acq-B_bold.json'.format(p=prefix): {'ShimSetting': func_shims_B},
560554
'{p}_acq-unmatched_bold.json'.format(p=prefix): {
561-
'ShimSetting': ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
555+
'ShimSetting': [random() for i in range(SHIM_LENGTH)]
562556
},
563557
})
564558
# -fmap:

0 commit comments

Comments
 (0)