10
10
from datetime import (datetime ,
11
11
timedelta ,
12
12
)
13
- from collections import (namedtuple ,
14
- OrderedDict ,
13
+ from collections import (OrderedDict ,
15
14
)
16
15
from glob import glob
17
16
18
17
import nibabel
18
+ from numpy import testing as np_testing
19
19
20
20
from heudiconv .utils import (
21
21
load_json ,
22
22
save_json ,
23
23
create_tree ,
24
+ remove_suffix ,
24
25
)
26
+
25
27
from heudiconv .bids import (
26
28
maybe_na ,
27
29
treat_age ,
38
40
BIDSFile ,
39
41
)
40
42
43
+ from .utils import (
44
+ TESTS_DATA_PATH ,
45
+ )
46
+
41
47
import pytest
42
48
43
49
def test_maybe_na ():
@@ -63,7 +69,7 @@ def test_treat_age():
63
69
TODAY = datetime .today ()
64
70
65
71
66
- A_SHIM = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
72
+ A_SHIM = [random () for i in range (SHIM_LENGTH )]
67
73
def test_get_shim_setting (tmpdir ):
68
74
""" Tests for get_shim_setting """
69
75
json_dir = op .join (str (tmpdir ), 'foo' )
@@ -80,38 +86,24 @@ def test_get_shim_setting(tmpdir):
80
86
assert get_shim_setting (json_name ) == A_SHIM
81
87
82
88
83
- def test_get_key_info_for_fmap_assignment (tmpdir , monkeypatch ):
89
+ def test_get_key_info_for_fmap_assignment (tmpdir ):
84
90
"""
85
91
Test get_key_info_for_fmap_assignment
86
92
"""
87
93
88
- # Stuff needed to mock reading of a NIfTI file header:
89
-
90
- # affines (qforms/sforms) are 4x4 matrices
91
- MY_AFFINE = [[random () for i in range (4 )] for j in range (4 )]
92
- # dims are arrays with 8 elements with the first one indicating the number
93
- # of dims in the image; remaining elements are 1:
94
- MY_DIM = [4 ] + [round (256 * random ()) for i in range (4 )] + [1 ] * 3
95
- # We use namedtuples so that we can use the .dot notation, to mock
96
- # nibabel headers:
97
- MyHeader = namedtuple ('MyHeader' , 'affine dim' )
98
- MY_HEADER = MyHeader (MY_AFFINE , MY_DIM )
99
- MyMockNifti = namedtuple ('MyMockNifti' , 'header' )
100
-
101
- def mock_nibabel_load (file ):
102
- """
103
- Pretend we run nibabel.load, but return only a header with just a few fields
104
- """
105
- return MyMockNifti (MY_HEADER )
106
- monkeypatch .setattr (nibabel , "load" , mock_nibabel_load )
107
-
108
- json_name = op .join (str (tmpdir ), 'foo.json' )
94
+ nifti_file = op .join (TESTS_DATA_PATH , 'sample_nifti.nii.gz' )
95
+ # Get the expected parameters from the NIfTI header:
96
+ MY_HEADER = nibabel .ni1 .np .loadtxt (
97
+ op .join (TESTS_DATA_PATH , remove_suffix (nifti_file , '.nii.gz' ) + '_params.txt' )
98
+ )
99
+ json_name = op .join (TESTS_DATA_PATH , remove_suffix (nifti_file , '.nii.gz' ) + '.json' )
109
100
110
101
# 1) Call for a non-existing file should give an error:
111
102
with pytest .raises (FileNotFoundError ):
112
103
assert get_key_info_for_fmap_assignment ('foo.json' )
113
104
114
105
# 2) matching_parameters = 'Shims'
106
+ json_name = op .join (TESTS_DATA_PATH , remove_suffix (nifti_file , '.nii.gz' ) + '.json' )
115
107
save_json (json_name , {SHIM_KEY : A_SHIM }) # otherwise get_key_info_for_fmap_assignment will give an error
116
108
key_info = get_key_info_for_fmap_assignment (
117
109
json_name , matching_parameter = 'Shims'
@@ -122,7 +114,8 @@ def mock_nibabel_load(file):
122
114
key_info = get_key_info_for_fmap_assignment (
123
115
json_name , matching_parameter = 'ImagingVolume'
124
116
)
125
- assert key_info == [MY_AFFINE , MY_DIM [1 :3 ]]
117
+ np_testing .assert_almost_equal (key_info [0 ], MY_HEADER [:4 ], decimal = 6 )
118
+ np_testing .assert_almost_equal (key_info [1 ], MY_HEADER [4 ][:3 ], decimal = 6 )
126
119
127
120
# 4) matching_parameters = 'Force'
128
121
key_info = get_key_info_for_fmap_assignment (
@@ -222,10 +215,10 @@ def create_dummy_pepolar_bids_session(session_path):
222
215
# 1) Simulate the file structure for a session:
223
216
224
217
# Generate some random ShimSettings:
225
- anat_shims = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
226
- dwi_shims = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
227
- func_shims_A = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
228
- func_shims_B = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
218
+ anat_shims = [random () for i in range (SHIM_LENGTH )]
219
+ dwi_shims = [random () for i in range (SHIM_LENGTH )]
220
+ func_shims_A = [random () for i in range (SHIM_LENGTH )]
221
+ func_shims_B = [random () for i in range (SHIM_LENGTH )]
229
222
230
223
# Dict with the file structure for the session:
231
224
# -anat:
@@ -249,7 +242,7 @@ def create_dummy_pepolar_bids_session(session_path):
249
242
'{p}_acq-A_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_A },
250
243
'{p}_acq-B_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_B },
251
244
'{p}_acq-unmatched_bold.json' .format (p = prefix ): {
252
- 'ShimSetting' : ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
245
+ 'ShimSetting' : [random () for i in range (SHIM_LENGTH )]
253
246
},
254
247
})
255
248
# -fmap:
@@ -541,9 +534,9 @@ def create_dummy_magnitude_phase_bids_session(session_path):
541
534
# 1) Simulate the file structure for a session:
542
535
543
536
# Generate some random ShimSettings:
544
- dwi_shims = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
545
- func_shims_A = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
546
- func_shims_B = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
537
+ dwi_shims = [random () for i in range (SHIM_LENGTH )]
538
+ func_shims_A = [random () for i in range (SHIM_LENGTH )]
539
+ func_shims_B = [random () for i in range (SHIM_LENGTH )]
547
540
548
541
# Dict with the file structure for the session:
549
542
# -dwi:
@@ -561,7 +554,7 @@ def create_dummy_magnitude_phase_bids_session(session_path):
561
554
'{p}_acq-A_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_A },
562
555
'{p}_acq-B_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_B },
563
556
'{p}_acq-unmatched_bold.json' .format (p = prefix ): {
564
- 'ShimSetting' : ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
557
+ 'ShimSetting' : [random () for i in range (SHIM_LENGTH )]
565
558
},
566
559
})
567
560
# -fmap:
0 commit comments