99
1010sys .path .append ('..' )
1111sys .path .append ('../babs' )
12- from babs .utils import ( read_yaml ) # noqa
13- from babs .cli import ( # noqa
12+ from babs .utils import read_yaml # noqa
13+ from babs .cli import ( # noqa
1414 babs_init_main ,
15- babs_check_setup_main )
16- from get_data import ( # noqa
15+ babs_check_setup_main ,
16+ )
17+ from get_data import ( # noqa
1718 get_input_data ,
1819 container_ds_path ,
1920 where_now ,
2324 INFO_2ND_INPUT_DATA ,
2425 LIST_WHICH_BIDSAPP ,
2526 TOYBIDSAPP_VERSION_DASH ,
26- TEMPLATEFLOW_HOME
27+ TEMPLATEFLOW_HOME ,
2728)
2829
30+
2931@pytest .mark .order (index = 1 )
3032@pytest .mark .parametrize (
31- 'which_bidsapp, which_input, type_session, if_input_local, if_two_input' ,
33+ ( 'which_bidsapp' , ' which_input' , ' type_session' , ' if_input_local' , ' if_two_input') ,
3234 # test toybidsapp: BIDS/zipped x single/multi-ses:
3335 # the input data will also be remote by default:
34- [('toybidsapp' , 'BIDS' , 'single-ses' , False , False ),
35- ('toybidsapp' , 'BIDS' , 'multi-ses' , False , False ),
36- ('toybidsapp' , 'fmriprep' , 'single-ses' , False , False ),
37- ('toybidsapp' , 'fmriprep' , 'multi-ses' , False , False ),
38- # test if input is local:
39- ('toybidsapp' , 'BIDS' , 'single-ses' , True , False ),
40- # test fmriprep: single/multi-ses
41- ('fmriprep' , 'BIDS' , 'single-ses' , False , False ),
42- ('fmriprep' , 'BIDS' , 'multi-ses' , False , False ),
43- # test qsiprep multi-ses: remove sessions without dMRI
44- ('qsiprep' , 'BIDS' , 'multi-ses' , False , False ),
45- # test 2 input datasets (2nd one will be zipped fmriprep derivatives):
46- ('fmriprep' , 'BIDS' , 'single-ses' , False , True ),
47- ('fmriprep' , 'BIDS' , 'multi-ses' , False , True ),
48- ])
49- def test_babs_init (which_bidsapp , which_input , type_session , if_input_local , if_two_input ,
50- tmp_path , tmp_path_factory ,
51- container_ds_path , if_circleci ,
52- ):
36+ [
37+ ('toybidsapp' , 'BIDS' , 'single-ses' , False , False ),
38+ ('toybidsapp' , 'BIDS' , 'multi-ses' , False , False ),
39+ ('toybidsapp' , 'fmriprep' , 'single-ses' , False , False ),
40+ ('toybidsapp' , 'fmriprep' , 'multi-ses' , False , False ),
41+ # test if input is local:
42+ ('toybidsapp' , 'BIDS' , 'single-ses' , True , False ),
43+ # test fmriprep: single/multi-ses
44+ ('fmriprep' , 'BIDS' , 'single-ses' , False , False ),
45+ ('fmriprep' , 'BIDS' , 'multi-ses' , False , False ),
46+ # test qsiprep multi-ses: remove sessions without dMRI
47+ ('qsiprep' , 'BIDS' , 'multi-ses' , False , False ),
48+ # test 2 input datasets (2nd one will be zipped fmriprep derivatives):
49+ ('fmriprep' , 'BIDS' , 'single-ses' , False , True ),
50+ ('fmriprep' , 'BIDS' , 'multi-ses' , False , True ),
51+ ],
52+ )
53+ def test_babs_init (
54+ which_bidsapp ,
55+ which_input ,
56+ type_session ,
57+ if_input_local ,
58+ if_two_input ,
59+ tmp_path ,
60+ tmp_path_factory ,
61+ container_ds_path_param ,
62+ if_circleci_param ,
63+ ):
5364 """
5465 This is to test `babs-init` in different cases.
5566
@@ -70,9 +81,9 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
7081 whether to use two input datasets
7182 tmp_path: fixture from pytest
7283 tmp_path_factory: fixture from pytest
73- container_ds_path : fixture; str
84+ container_ds_path_param : fixture; str
7485 Path to the container datalad dataset
75- if_circleci : fixture; bool
86+ if_circleci_param : fixture; bool
7687 Whether currently in CircleCI
7788
7889 TODO: add `type_system` and to test out Slurm version!
@@ -85,25 +96,27 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
8596 input_ds_cli = [[which_input , path_in ]]
8697 if if_two_input :
8798 # get another input dataset: qsiprep derivatives
88- assert INFO_2ND_INPUT_DATA ['which_input' ] != which_input # avoid repeated input ds name
99+ assert INFO_2ND_INPUT_DATA ['which_input' ] != which_input # avoid repeated input ds name
89100 path_in_2nd = get_input_data (
90101 INFO_2ND_INPUT_DATA ['which_input' ],
91- type_session , # should be consistent with the 1st dataset
102+ type_session , # should be consistent with the 1st dataset
92103 INFO_2ND_INPUT_DATA ['if_input_local' ],
93- tmp_path_factory )
104+ tmp_path_factory ,
105+ )
94106 input_ds_cli .append ([INFO_2ND_INPUT_DATA ['which_input' ], path_in_2nd ])
95107
96108 # Container dataset - has been set up by fixture `prep_container_ds_toybidsapp()`
97- assert op .exists (container_ds_path )
98- assert op .exists (op .join (container_ds_path , '.datalad/config' ))
109+ assert op .exists (container_ds_path_param )
110+ assert op .exists (op .join (container_ds_path_param , '.datalad/config' ))
99111
100112 # Preparation of freesurfer: for fmriprep and qsiprep:
101113 # check if `--fs-license-file` is included in YAML file:
102- container_config_yaml_filename = \
103- get_container_config_yaml_filename (which_bidsapp , which_input , if_two_input ,
104- type_system = 'slurm' ) # TODO: also test slurm!
105- container_config_yaml_file = op .join (op .dirname (__location__ ), 'notebooks' ,
106- container_config_yaml_filename )
114+ container_config_yaml_filename = get_container_config_yaml_filename (
115+ which_bidsapp , which_input , if_two_input , type_system = 'slurm'
116+ ) # TODO: also test slurm!
117+ container_config_yaml_file = op .join (
118+ op .dirname (__location__ ), 'notebooks' , container_config_yaml_filename
119+ )
107120 assert op .exists (container_config_yaml_file )
108121 container_config_yaml = read_yaml (container_config_yaml_file )
109122
@@ -117,11 +130,11 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
117130
118131 # Preparation of env variable `TEMPLATEFLOW_HOME`:
119132 os .environ ['TEMPLATEFLOW_HOME' ] = TEMPLATEFLOW_HOME
120- assert os .getenv ('TEMPLATEFLOW_HOME' ) is not None # assert env var has been set
133+ assert os .getenv ('TEMPLATEFLOW_HOME' ) is not None # assert env var has been set
121134 # as env var has been set up, expect that BABS will generate necessary cmd for templateflow
122135
123136 # Get the cli of `babs-init`:
124- where_project = tmp_path .absolute ().as_posix () # turn into a string
137+ where_project = tmp_path .absolute ().as_posix () # turn into a string
125138 project_name = 'my_babs_project'
126139 project_root = op .join (where_project , project_name )
127140 container_name = which_bidsapp + '-' + TOYBIDSAPP_VERSION_DASH
@@ -131,28 +144,25 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
131144 project_name = project_name ,
132145 input = input_ds_cli ,
133146 list_sub_file = None ,
134- container_ds = container_ds_path ,
147+ container_ds = container_ds_path_param ,
135148 container_name = container_name ,
136149 container_config_yaml_file = container_config_yaml_file ,
137150 type_session = type_session ,
138151 type_system = 'slurm' ,
139- keep_if_failed = False
152+ keep_if_failed = False ,
140153 )
141154
142155 # run `babs-init`:
143- with mock .patch .object (
144- argparse .ArgumentParser , 'parse_args' , return_value = babs_init_opts ):
156+ with mock .patch .object (argparse .ArgumentParser , 'parse_args' , return_value = babs_init_opts ):
145157 babs_init_main ()
146158
147159 # ================== ASSERT ============================
148160 # Assert by running `babs-check-setup`
149- babs_check_setup_opts = argparse .Namespace (
150- project_root = project_root ,
151- job_test = False
152- )
161+ babs_check_setup_opts = argparse .Namespace (project_root = project_root , job_test = False )
153162 # Run `babs-check-setup`:
154163 with mock .patch .object (
155- argparse .ArgumentParser , 'parse_args' , return_value = babs_check_setup_opts ):
164+ argparse .ArgumentParser , 'parse_args' , return_value = babs_check_setup_opts
165+ ):
156166 babs_check_setup_main ()
157167
158168 # Check if necessary commands in ``<container_name>-0-0-0_zip.sh`:
@@ -169,11 +179,10 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
169179 lines_bash_container_zip = file_bash_container_zip .readlines ()
170180 file_bash_container_zip .close ()
171181 # check:
172- if_bind_templateflow = False # `singularity run -B` to bind a path to container
182+ if_bind_templateflow = False # `singularity run -B` to bind a path to container
173183 if_bind_freesurfer = False
174- str_bind_freesurfer = '-B ' + str_fs_license_file \
175- + ':/SGLR/FREESURFER_HOME/license.txt'
176- print (str_bind_freesurfer ) # FOR DEBUGGING
184+ str_bind_freesurfer = '-B ' + str_fs_license_file + ':/SGLR/FREESURFER_HOME/license.txt'
185+ print (str_bind_freesurfer ) # FOR DEBUGGING
177186
178187 if_set_singu_templateflow = False # `singularity run --env` to set env var within container
179188 if_generate_bidsfilterfile = False
@@ -183,8 +192,7 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
183192 for line in lines_bash_container_zip :
184193 if '--env TEMPLATEFLOW_HOME=/SGLR/TEMPLATEFLOW_HOME' in line :
185194 if_set_singu_templateflow = True
186- if all (ele in line for ele in ['-B' ,
187- TEMPLATEFLOW_HOME + ':/SGLR/TEMPLATEFLOW_HOME' ]):
195+ if all (ele in line for ele in ['-B' , TEMPLATEFLOW_HOME + ':/SGLR/TEMPLATEFLOW_HOME' ]):
188196 # e.g., `-B /test/templateflow_home:/SGLR/TEMPLATEFLOW_HOME \`
189197 if_bind_templateflow = True
190198 if str_bind_freesurfer in line :
@@ -197,40 +205,65 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
197205 if_flag_fs_license = True
198206 # assert they are found:
199207 # 1) TemplateFlow: should be found in all cases:
200- assert if_bind_templateflow , \
201- "Env variable 'TEMPLATEFLOW_HOME' has been set," \
202- + " but Templateflow home path did not get bound in 'singularity run'" \
203- + " with `-B` in '" + container_name + "_zip.sh'."
204- assert if_set_singu_templateflow , \
205- "Env variable 'TEMPLATEFLOW_HOME' has been set," \
206- + " but env variable 'SINGULARITYENV_TEMPLATEFLOW_HOME' was not set" \
207- + " with `--env` in '" + container_name + "_zip.sh'."
208+ assert if_bind_templateflow , (
209+ "Env variable 'TEMPLATEFLOW_HOME' has been set,"
210+ " but Templateflow home path did not get bound in 'singularity run'"
211+ " with `-B` in '" + container_name + "_zip.sh'."
212+ )
213+ assert if_set_singu_templateflow , (
214+ "Env variable 'TEMPLATEFLOW_HOME' has been set,"
215+ " but env variable 'SINGULARITYENV_TEMPLATEFLOW_HOME' was not set"
216+ " with `--env` in '" + container_name + "_zip.sh'."
217+ )
208218 # 2) BIDS filter file: only when qsiprep/fmriprep & multi-ses:
209219 if (which_bidsapp in ['qsiprep' , 'fmriprep' ]) & (type_session == 'multi-ses' ):
210- assert if_generate_bidsfilterfile , \
211- "This is BIDS App '" + which_bidsapp + "' and " + type_session + ',' \
212- + ' however, filterfile to be used in `--bids-filter-file` was not generated' \
213- + " in '" + container_name + "_zip.sh'."
214- assert if_flag_bidsfilterfile , \
215- "This is BIDS App '" + which_bidsapp + "' and " + type_session + ',' \
216- + ' however, flag `--bids-filter-file` was not included in `singularity run`' \
217- + " in '" + container_name + "_zip.sh'."
220+ assert if_generate_bidsfilterfile , (
221+ "This is BIDS App '"
222+ + which_bidsapp
223+ + "' and "
224+ + type_session
225+ + ','
226+ + ' however, filterfile to be used in `--bids-filter-file` was not generated'
227+ + " in '"
228+ + container_name
229+ + "_zip.sh'."
230+ )
231+ assert if_flag_bidsfilterfile , (
232+ "This is BIDS App '"
233+ + which_bidsapp
234+ + "' and "
235+ + type_session
236+ + ','
237+ + ' however, flag `--bids-filter-file` was not included in `singularity run`'
238+ + " in '"
239+ + container_name
240+ + "_zip.sh'."
241+ )
218242 else :
219- assert (not if_generate_bidsfilterfile ) & (not if_flag_bidsfilterfile ), \
220- "This is BIDS App '" + which_bidsapp + "' and " + type_session + ',' \
221- + ' so `--bids-filter-file` should not be generated or used in `singularity run`' \
222- + " in '" + container_name + "_zip.sh'."
243+ assert (not if_generate_bidsfilterfile ) & (not if_flag_bidsfilterfile ), (
244+ "This is BIDS App '"
245+ + which_bidsapp
246+ + "' and "
247+ + type_session
248+ + ','
249+ + ' so `--bids-filter-file` should not be generated or used in `singularity run`'
250+ + " in '"
251+ + container_name
252+ + "_zip.sh'."
253+ )
223254 # 3) freesurfer license:
224255 if flag_requested_fs_license :
225- assert if_bind_freesurfer , \
226- "`--fs-license-file` was requested in container's YAML file," \
227- + " but FreeSurfer license path did not get bound in 'singularity run'" \
228- + " with `-B` in '" + container_name + "_zip.sh'."
229- assert if_flag_fs_license , \
230- "`--fs-license-file` was requested in container's YAML file," \
231- + ' but flag `' + flag_fs_license + '` was not found in the `singularity run`' \
232- + " in '" + container_name + "_zip.sh'." \
233- + " Path to YAML file: '" + container_config_yaml_file + "'."
256+ assert if_bind_freesurfer , (
257+ "`--fs-license-file` was requested in container's YAML file,"
258+ " but FreeSurfer license path did not get bound in 'singularity run'"
259+ " with `-B` in '" + container_name + "_zip.sh'."
260+ )
261+ assert if_flag_fs_license , (
262+ "`--fs-license-file` was requested in container's YAML file,"
263+ ' but flag `' + flag_fs_license + '` was not found in the `singularity run`'
264+ " in '" + container_name + "_zip.sh'."
265+ " Path to YAML file: '" + container_config_yaml_file + "'."
266+ )
234267
235268 # Check `sub_ses_final_inclu.csv`:
236269 # if qsiprep + multi-ses: one session without dMRI should not be included
@@ -244,9 +277,12 @@ def test_babs_init(which_bidsapp, which_input, type_session, if_input_local, if_
244277 if_inclu_missing_session = False
245278 if 'sub-02,ses-A' in line :
246279 if_inclu_missing_session = True
247- assert not if_inclu_missing_session , \
248- "'sub-02,ses-A' without dMRI was included in the BABS project of " \
249- + which_bidsapp + ', ' + type_session
280+ assert not if_inclu_missing_session , (
281+ "'sub-02,ses-A' without dMRI was included in the BABS project of "
282+ + which_bidsapp
283+ + ', '
284+ + type_session
285+ )
250286
251287 # Note: No need to manually remove temporary dirs; those are created by pytest's fixtures
252288 # and will be automatically removed after 3 runs of pytests. ref below:
0 commit comments