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