Skip to content

Commit 141f6e9

Browse files
authored
Merge pull request #34 from cbinyu/patch_dbic
BF: Cover the case key_info is a string; modify unit tests
2 parents bf9a3e1 + 606a727 commit 141f6e9

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
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: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
from datetime import (datetime,
99
timedelta,
1010
)
11-
from collections import (namedtuple,
12-
OrderedDict,
11+
from collections import (OrderedDict,
1312
)
1413
from glob import glob
1514

1615
import nibabel
16+
from numpy import testing as np_testing
1717

1818
from heudiconv.utils import (
1919
load_json,
2020
save_json,
2121
create_tree,
22+
remove_suffix,
2223
)
24+
2325
from heudiconv.bids import (
2426
maybe_na,
2527
treat_age,
@@ -35,6 +37,10 @@
3537
KeyInfoForForce,
3638
)
3739

40+
from .utils import (
41+
TESTS_DATA_PATH,
42+
)
43+
3844
import pytest
3945

4046
def test_maybe_na():
@@ -60,7 +66,7 @@ def test_treat_age():
6066
TODAY = datetime.today()
6167

6268

63-
A_SHIM = ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
69+
A_SHIM = [random() for i in range(SHIM_LENGTH)]
6470
def test_get_shim_setting(tmpdir):
6571
""" Tests for get_shim_setting """
6672
json_dir = op.join(str(tmpdir), 'foo')
@@ -77,38 +83,24 @@ def test_get_shim_setting(tmpdir):
7783
assert get_shim_setting(json_name) == A_SHIM
7884

7985

80-
def test_get_key_info_for_fmap_assignment(tmpdir, monkeypatch):
86+
def test_get_key_info_for_fmap_assignment(tmpdir):
8187
"""
8288
Test get_key_info_for_fmap_assignment
8389
"""
8490

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')
91+
nifti_file = op.join(TESTS_DATA_PATH, 'sample_nifti.nii.gz')
92+
# Get the expected parameters from the NIfTI header:
93+
MY_HEADER = nibabel.ni1.np.loadtxt(
94+
op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '_params.txt')
95+
)
96+
json_name = op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '.json')
10697

10798
# 1) Call for a non-existing file should give an error:
10899
with pytest.raises(FileNotFoundError):
109100
assert get_key_info_for_fmap_assignment('foo.json')
110101

111102
# 2) matching_parameters = 'Shims'
103+
json_name = op.join(TESTS_DATA_PATH, remove_suffix(nifti_file, '.nii.gz') + '.json')
112104
save_json(json_name, {SHIM_KEY: A_SHIM}) # otherwise get_key_info_for_fmap_assignment will give an error
113105
key_info = get_key_info_for_fmap_assignment(
114106
json_name, matching_parameter='Shims'
@@ -119,7 +111,8 @@ def mock_nibabel_load(file):
119111
key_info = get_key_info_for_fmap_assignment(
120112
json_name, matching_parameter='ImagingVolume'
121113
)
122-
assert key_info == [MY_AFFINE, MY_DIM[1:3]]
114+
np_testing.assert_almost_equal(key_info[0], MY_HEADER[:4], decimal=6)
115+
np_testing.assert_almost_equal(key_info[1], MY_HEADER[4][:3], decimal=6)
123116

124117
# 4) matching_parameters = 'Force'
125118
key_info = get_key_info_for_fmap_assignment(
@@ -219,10 +212,10 @@ def create_dummy_pepolar_bids_session(session_path):
219212
# 1) Simulate the file structure for a session:
220213

221214
# 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)]
215+
anat_shims = [random() for i in range(SHIM_LENGTH)]
216+
dwi_shims = [random() for i in range(SHIM_LENGTH)]
217+
func_shims_A = [random() for i in range(SHIM_LENGTH)]
218+
func_shims_B = [random() for i in range(SHIM_LENGTH)]
226219

227220
# Dict with the file structure for the session:
228221
# -anat:
@@ -246,7 +239,7 @@ def create_dummy_pepolar_bids_session(session_path):
246239
'{p}_acq-A_bold.json'.format(p=prefix): {'ShimSetting': func_shims_A},
247240
'{p}_acq-B_bold.json'.format(p=prefix): {'ShimSetting': func_shims_B},
248241
'{p}_acq-unmatched_bold.json'.format(p=prefix): {
249-
'ShimSetting': ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
242+
'ShimSetting': [random() for i in range(SHIM_LENGTH)]
250243
},
251244
})
252245
# -fmap:
@@ -538,9 +531,9 @@ def create_dummy_magnitude_phase_bids_session(session_path):
538531
# 1) Simulate the file structure for a session:
539532

540533
# 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)]
534+
dwi_shims = [random() for i in range(SHIM_LENGTH)]
535+
func_shims_A = [random() for i in range(SHIM_LENGTH)]
536+
func_shims_B = [random() for i in range(SHIM_LENGTH)]
544537

545538
# Dict with the file structure for the session:
546539
# -dwi:
@@ -558,7 +551,7 @@ def create_dummy_magnitude_phase_bids_session(session_path):
558551
'{p}_acq-A_bold.json'.format(p=prefix): {'ShimSetting': func_shims_A},
559552
'{p}_acq-B_bold.json'.format(p=prefix): {'ShimSetting': func_shims_B},
560553
'{p}_acq-unmatched_bold.json'.format(p=prefix): {
561-
'ShimSetting': ['{0:.4f}'.format(random()) for i in range(SHIM_LENGTH)]
554+
'ShimSetting': [random() for i in range(SHIM_LENGTH)]
562555
},
563556
})
564557
# -fmap:

0 commit comments

Comments
 (0)