8
8
from datetime import (datetime ,
9
9
timedelta ,
10
10
)
11
- from collections import (namedtuple ,
12
- OrderedDict ,
11
+ from collections import (OrderedDict ,
13
12
)
14
13
from glob import glob
15
14
16
15
import nibabel
16
+ from numpy import testing as np_testing
17
17
18
18
from heudiconv .utils import (
19
19
load_json ,
20
20
save_json ,
21
21
create_tree ,
22
+ remove_suffix ,
22
23
)
24
+
23
25
from heudiconv .bids import (
24
26
maybe_na ,
25
27
treat_age ,
35
37
KeyInfoForForce ,
36
38
)
37
39
40
+ from .utils import (
41
+ TESTS_DATA_PATH ,
42
+ )
43
+
38
44
import pytest
39
45
40
46
def test_maybe_na ():
@@ -60,7 +66,7 @@ def test_treat_age():
60
66
TODAY = datetime .today ()
61
67
62
68
63
- A_SHIM = ['{0:.4f}' . format ( random () ) for i in range (SHIM_LENGTH )]
69
+ A_SHIM = [random () for i in range (SHIM_LENGTH )]
64
70
def test_get_shim_setting (tmpdir ):
65
71
""" Tests for get_shim_setting """
66
72
json_dir = op .join (str (tmpdir ), 'foo' )
@@ -77,38 +83,24 @@ def test_get_shim_setting(tmpdir):
77
83
assert get_shim_setting (json_name ) == A_SHIM
78
84
79
85
80
- def test_get_key_info_for_fmap_assignment (tmpdir , monkeypatch ):
86
+ def test_get_key_info_for_fmap_assignment (tmpdir ):
81
87
"""
82
88
Test get_key_info_for_fmap_assignment
83
89
"""
84
90
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' )
106
97
107
98
# 1) Call for a non-existing file should give an error:
108
99
with pytest .raises (FileNotFoundError ):
109
100
assert get_key_info_for_fmap_assignment ('foo.json' )
110
101
111
102
# 2) matching_parameters = 'Shims'
103
+ json_name = op .join (TESTS_DATA_PATH , remove_suffix (nifti_file , '.nii.gz' ) + '.json' )
112
104
save_json (json_name , {SHIM_KEY : A_SHIM }) # otherwise get_key_info_for_fmap_assignment will give an error
113
105
key_info = get_key_info_for_fmap_assignment (
114
106
json_name , matching_parameter = 'Shims'
@@ -119,7 +111,8 @@ def mock_nibabel_load(file):
119
111
key_info = get_key_info_for_fmap_assignment (
120
112
json_name , matching_parameter = 'ImagingVolume'
121
113
)
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 )
123
116
124
117
# 4) matching_parameters = 'Force'
125
118
key_info = get_key_info_for_fmap_assignment (
@@ -219,10 +212,10 @@ def create_dummy_pepolar_bids_session(session_path):
219
212
# 1) Simulate the file structure for a session:
220
213
221
214
# 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 )]
226
219
227
220
# Dict with the file structure for the session:
228
221
# -anat:
@@ -246,7 +239,7 @@ def create_dummy_pepolar_bids_session(session_path):
246
239
'{p}_acq-A_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_A },
247
240
'{p}_acq-B_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_B },
248
241
'{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 )]
250
243
},
251
244
})
252
245
# -fmap:
@@ -538,9 +531,9 @@ def create_dummy_magnitude_phase_bids_session(session_path):
538
531
# 1) Simulate the file structure for a session:
539
532
540
533
# 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 )]
544
537
545
538
# Dict with the file structure for the session:
546
539
# -dwi:
@@ -558,7 +551,7 @@ def create_dummy_magnitude_phase_bids_session(session_path):
558
551
'{p}_acq-A_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_A },
559
552
'{p}_acq-B_bold.json' .format (p = prefix ): {'ShimSetting' : func_shims_B },
560
553
'{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 )]
562
555
},
563
556
})
564
557
# -fmap:
0 commit comments