@@ -60,29 +60,21 @@ def test_treat_age():
60
60
TODAY = datetime .today ()
61
61
62
62
63
- # Test scenarios:
64
- # -file with "ShimSetting" field
65
- # -file with no "ShimSetting", in "foo" dir, should return "foo"
66
- # -file with no "ShimSetting", in "fmap" dir, acq-CatchThis, should return
67
- # "CatchThis"
68
- # -file with no "ShimSetting", in "fmap" dir, acq-fMRI, should return "func"
69
63
A_SHIM = ['{0:.4f}' .format (random ()) for i in range (SHIM_LENGTH )]
70
- @pytest .mark .parametrize (
71
- "fname, content, expected_return" , [
72
- (op .join ('foo' , 'bar.json' ), {SHIM_KEY : A_SHIM }, A_SHIM ),
73
- (op .join ('dont_catch_this' , 'foo' , 'bar.json' ), {}, 'foo' ),
74
- (op .join ('dont_catch_this' , 'fmap' , 'bar_acq-CatchThis.json' ), {}, 'CatchThis' ),
75
- (op .join ('dont_catch_this' , 'fmap' , 'bar_acq-fMRI.json' ), {}, 'func' ),
76
- ]
77
- )
78
- def test_get_shim_setting (tmpdir , fname , content , expected_return ):
64
+ def test_get_shim_setting (tmpdir ):
79
65
""" Tests for get_shim_setting """
80
- json_name = op .join (str (tmpdir ), fname )
81
- json_dir = op .dirname (json_name )
66
+ json_dir = op .join (str (tmpdir ), 'foo' )
82
67
if not op .exists (json_dir ):
83
68
os .makedirs (json_dir )
84
- save_json (json_name , content )
85
- assert get_shim_setting (json_name ) == expected_return
69
+ json_name = op .join (json_dir , 'sub-foo.json' )
70
+ # 1) file with no "ShimSetting", should return None
71
+ save_json (json_name , {})
72
+ with pytest .raises (KeyError ):
73
+ assert get_shim_setting (json_name )
74
+
75
+ # -file with "ShimSetting" field
76
+ save_json (json_name , {SHIM_KEY : A_SHIM })
77
+ assert get_shim_setting (json_name ) == A_SHIM
86
78
87
79
88
80
def test_get_key_info_for_fmap_assignment (tmpdir , monkeypatch ):
@@ -135,6 +127,26 @@ def mock_nibabel_load(file):
135
127
)
136
128
assert key_info == [KeyInfoForForce ]
137
129
130
+ # 5) matching_parameter = 'AcquisitionLabel'
131
+ for d in ['fmap' , 'func' , 'dwi' , 'anat' ]:
132
+ os .makedirs (op .join (str (tmpdir ), d ))
133
+ for (dirname , fname , expected_key_info ) in [
134
+ ('fmap' , 'sub-foo_acq-fmri_epi.json' , 'func' ),
135
+ ('fmap' , 'sub-foo_acq-bold_epi.json' , 'func' ),
136
+ ('fmap' , 'sub-foo_acq-func_epi.json' , 'func' ),
137
+ ('fmap' , 'sub-foo_acq-diff_epi.json' , 'dwi' ),
138
+ ('fmap' , 'sub-foo_acq-anat_epi.json' , 'anat' ),
139
+ ('fmap' , 'sub-foo_acq-struct_epi.json' , 'anat' ),
140
+ ('func' , 'sub-foo_bold.json' , 'func' ),
141
+ ('dwi' , 'sub-foo_dwi.json' , 'dwi' ),
142
+ ('anat' , 'sub-foo_T1w.json' , 'anat' ),
143
+ ]:
144
+ json_name = op .join (str (tmpdir ), dirname , fname )
145
+ save_json (json_name , {SHIM_KEY : A_SHIM })
146
+ assert [expected_key_info ] == get_key_info_for_fmap_assignment (
147
+ json_name , matching_parameter = 'AcquisitionLabel'
148
+ )
149
+
138
150
# Finally: invalid matching_parameters:
139
151
with pytest .raises (ValueError ):
140
152
assert get_key_info_for_fmap_assignment (
@@ -207,6 +219,7 @@ def create_dummy_pepolar_bids_session(session_path):
207
219
# 1) Simulate the file structure for a session:
208
220
209
221
# Generate some random ShimSettings:
222
+ anat_shims = ['{0:.4f}' .format (random ()) for i in range (SHIM_LENGTH )]
210
223
dwi_shims = ['{0:.4f}' .format (random ()) for i in range (SHIM_LENGTH )]
211
224
func_shims_A = ['{0:.4f}' .format (random ()) for i in range (SHIM_LENGTH )]
212
225
func_shims_B = ['{0:.4f}' .format (random ()) for i in range (SHIM_LENGTH )]
@@ -215,7 +228,7 @@ def create_dummy_pepolar_bids_session(session_path):
215
228
# -anat:
216
229
anat_struct = {
217
230
'{p}_{m}.{e}' .format (p = prefix , m = mod , e = ext ): dummy_content
218
- for ext , dummy_content in zip (['nii.gz' , 'json' ], ['' , {}])
231
+ for ext , dummy_content in zip (['nii.gz' , 'json' ], ['' , {'ShimSetting' : anat_shims }])
219
232
for mod in ['T1w' , 'T2w' ]
220
233
}
221
234
# -dwi:
@@ -675,11 +688,13 @@ def test_find_fmap_groups(tmpdir, simulation_function):
675
688
# B) same, with no ShimSetting
676
689
# C) magnitude/phase, with ShimSetting
677
690
@pytest .mark .parametrize (
678
- "simulation_function" , [create_dummy_pepolar_bids_session ,
679
- create_dummy_no_shim_settings_bids_session ,
680
- create_dummy_magnitude_phase_bids_session ]
691
+ "simulation_function, match_param" , [
692
+ (create_dummy_pepolar_bids_session , 'Shims' ),
693
+ (create_dummy_no_shim_settings_bids_session , 'AcquisitionLabel' ),
694
+ (create_dummy_magnitude_phase_bids_session , 'Shims' )
695
+ ]
681
696
)
682
- def test_find_compatible_fmaps_for_run (tmpdir , simulation_function ):
697
+ def test_find_compatible_fmaps_for_run (tmpdir , simulation_function , match_param ):
683
698
"""
684
699
Test find_compatible_fmaps_for_run.
685
700
@@ -688,6 +703,8 @@ def test_find_compatible_fmaps_for_run(tmpdir, simulation_function):
688
703
tmpdir
689
704
simulation_function : function
690
705
function to create the directory tree and expected results
706
+ match_param : str
707
+ matching_parameter for assigning fmaps
691
708
"""
692
709
folder = op .join (str (tmpdir ), 'sub-foo' )
693
710
_ , _ , expected_fmap_groups , expected_compatible_fmaps = simulation_function (folder )
@@ -696,7 +713,7 @@ def test_find_compatible_fmaps_for_run(tmpdir, simulation_function):
696
713
compatible_fmaps = find_compatible_fmaps_for_run (
697
714
json_file ,
698
715
expected_fmap_groups ,
699
- matching_parameters = 'Shims'
716
+ matching_parameters = match_param
700
717
)
701
718
assert compatible_fmaps == expected_compatible_fmaps [json_file ]
702
719
@@ -709,28 +726,42 @@ def test_find_compatible_fmaps_for_run(tmpdir, simulation_function):
709
726
# B) same, with no ShimSetting
710
727
# C) magnitude/phase, with ShimSetting
711
728
@pytest .mark .parametrize (
712
- "folder, expected_prefix, simulation_function" , [
713
- (folder , expected_prefix , sim_func )
729
+ "folder, expected_prefix, simulation_function, match_param " , [
730
+ (folder , expected_prefix , sim_func , mp )
714
731
for folder , expected_prefix in zip (['no_sessions/sub-1' , 'sessions/sub-1/ses-pre' ], ['' , 'ses-pre' ])
715
- for sim_func in [create_dummy_pepolar_bids_session ,
716
- create_dummy_no_shim_settings_bids_session ,
717
- create_dummy_magnitude_phase_bids_session ]
732
+ for sim_func , mp in [
733
+ (create_dummy_pepolar_bids_session , 'Shims' ),
734
+ (create_dummy_no_shim_settings_bids_session , 'AcquisitionLabel' ),
735
+ (create_dummy_magnitude_phase_bids_session , 'Shims' )
736
+ ]
718
737
]
719
738
)
720
- def test_find_compatible_fmaps_for_session (tmpdir , folder , expected_prefix , simulation_function ):
739
+ def test_find_compatible_fmaps_for_session (
740
+ tmpdir ,
741
+ folder ,
742
+ expected_prefix ,
743
+ simulation_function ,
744
+ match_param
745
+ ):
721
746
"""
722
747
Test find_compatible_fmaps_for_session.
723
748
724
749
Parameters:
725
750
----------
726
751
tmpdir
752
+ folder : str or os.path
753
+ path to BIDS study to be simulated, relative to tmpdir
754
+ expected_prefix : str
755
+ expected start of the "IntendedFor" elements
727
756
simulation_function : function
728
757
function to create the directory tree and expected results
758
+ match_param : str
759
+ matching_parameter for assigning fmaps
729
760
"""
730
761
session_folder = op .join (str (tmpdir ), folder )
731
762
_ , _ , _ , expected_compatible_fmaps = simulation_function (session_folder )
732
763
733
- compatible_fmaps = find_compatible_fmaps_for_session (session_folder , matching_parameters = 'Shims' )
764
+ compatible_fmaps = find_compatible_fmaps_for_session (session_folder , matching_parameters = match_param )
734
765
735
766
assert compatible_fmaps == expected_compatible_fmaps
736
767
@@ -785,15 +816,23 @@ def test_select_fmap_from_compatible_groups(tmpdir, folder, expected_prefix, sim
785
816
# B) same, with no ShimSetting
786
817
# C) magnitude/phase, with ShimSetting
787
818
@pytest .mark .parametrize (
788
- "folder, expected_prefix, simulation_function" , [
789
- (folder , expected_prefix , sim_func )
819
+ "folder, expected_prefix, simulation_function, match_param " , [
820
+ (folder , expected_prefix , sim_func , mp )
790
821
for folder , expected_prefix in zip (['no_sessions/sub-1' , 'sessions/sub-1/ses-pre' ], ['' , 'ses-pre' ])
791
- for sim_func in [create_dummy_pepolar_bids_session ,
792
- create_dummy_no_shim_settings_bids_session ,
793
- create_dummy_magnitude_phase_bids_session ]
822
+ for sim_func , mp in [
823
+ (create_dummy_pepolar_bids_session , 'Shims' ),
824
+ (create_dummy_no_shim_settings_bids_session , 'AcquisitionLabel' ),
825
+ (create_dummy_magnitude_phase_bids_session , 'Shims' )
826
+ ]
794
827
]
795
828
)
796
- def test_populate_intended_for (tmpdir , folder , expected_prefix , simulation_function ):
829
+ def test_populate_intended_for (
830
+ tmpdir ,
831
+ folder ,
832
+ expected_prefix ,
833
+ simulation_function ,
834
+ match_param
835
+ ):
797
836
"""
798
837
Test populate_intended_for.
799
838
Parameters:
@@ -805,11 +844,13 @@ def test_populate_intended_for(tmpdir, folder, expected_prefix, simulation_funct
805
844
expected start of the "IntendedFor" elements
806
845
simulation_function : function
807
846
function to create the directory tree and expected results
847
+ match_param : str
848
+ matching_parameter for assigning fmaps
808
849
"""
809
850
810
851
session_folder = op .join (str (tmpdir ), folder )
811
852
session_struct , expected_result , _ , _ = simulation_function (session_folder )
812
- populate_intended_for (session_folder , matching_parameters = 'Shims' , criterion = 'First' )
853
+ populate_intended_for (session_folder , matching_parameters = match_param , criterion = 'First' )
813
854
814
855
# Now, loop through the jsons in the fmap folder and make sure it matches
815
856
# the expected result:
0 commit comments