-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_generate_submit_script.py
More file actions
174 lines (152 loc) · 5.56 KB
/
test_generate_submit_script.py
File metadata and controls
174 lines (152 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import subprocess
from pathlib import Path
import pytest
from babs.generate_submit_script import generate_submit_script
from babs.utils import (
read_yaml,
)
input_datasets_prep = [
{
'name': 'bids',
'path_in_babs': 'inputs/data/BIDS',
'unzipped_path_containing_subject_dirs': 'inputs/data/BIDS',
'is_zipped': False,
},
]
input_datasets_fmriprep_ingressed_anat = [
{
'name': 'freesurfer',
'path_in_babs': 'inputs/data',
'unzipped_path_containing_subject_dirs': 'inputs/data/freesurfer',
'is_zipped': True,
},
{
'name': 'bids',
'path_in_babs': 'inputs/data/BIDS',
'unzipped_path_containing_subject_dirs': 'inputs/data/BIDS',
'is_zipped': False,
},
]
input_datasets_xcpd = [
{
'name': 'fmriprep',
'path_in_babs': 'inputs/data',
'unzipped_path_containing_subject_dirs': 'inputs/data/fmriprep',
'is_zipped': True,
},
]
input_datasets_qsirecon = [
{
'name': 'qsiprep',
'path_in_babs': 'inputs/data',
'unzipped_path_containing_subject_dirs': 'inputs/data/qsiprep',
'is_zipped': True,
},
]
input_datasets_qsirecon_ingressed_anat_zipped = [
{
'name': 'freesurfer',
'path_in_babs': 'inputs/data',
'unzipped_path_containing_subject_dirs': 'inputs/data/freesurfer',
'is_zipped': True,
},
{
'name': 'qsiprep',
'path_in_babs': 'inputs/data',
'unzipped_path_containing_subject_dirs': 'inputs/data/qsiprep',
'is_zipped': True,
},
]
# Get the path to the notebooks directory
NOTEBOOKS_DIR = Path(__file__).parent.parent / 'notebooks'
# match the inputs with their corresponding yaml files in notebooks/
testing_pairs = [
(input_ds, config, level)
for input_ds, config in [
(input_datasets_prep, 'eg_toybidsapp-0-0-7_rawBIDS-walkthrough.yaml'),
(input_datasets_prep, 'eg_aslprep-0-7-5.yaml'),
(input_datasets_prep, 'eg_fmriprep-24-1-1_anatonly.yaml'),
(input_datasets_prep, 'eg_fmriprep-24-1-1_regular.yaml'),
(
input_datasets_fmriprep_ingressed_anat,
'eg_fmriprep-24-1-1_ingressed-fs.yaml',
),
(input_datasets_prep, 'eg_qsiprep-1-0-0_regular.yaml'),
(input_datasets_xcpd, 'eg_xcpd-0-10-6_linc.yaml'),
(input_datasets_qsirecon, 'eg_qsirecon-1-0-1_custom_spec.yaml'),
(input_datasets_qsirecon_ingressed_anat_zipped, 'eg_qsirecon-1-0-1_hsvs.yaml'),
]
for level in ['subject', 'session']
]
@pytest.mark.parametrize(('input_datasets', 'config_file', 'processing_level'), testing_pairs)
def test_generate_submit_script(input_datasets, config_file, processing_level, tmp_path):
"""Test that the bidsapp runscript is generated correctly."""
config_path = NOTEBOOKS_DIR / config_file
container_name = config_file.split('_')[1]
config = read_yaml(config_path)
script_content = generate_submit_script(
queue_system='slurm',
cluster_resources_config=config['cluster_resources'],
script_preamble=config['script_preamble'],
job_scratch_directory=config['job_compute_space'],
input_datasets=input_datasets,
processing_level=processing_level,
container_name=container_name,
zip_foldernames=config['zip_foldernames'],
)
out_fn = tmp_path / f'participant_job_{config_path.name}_{processing_level}.sh'
with open(out_fn, 'w') as f:
f.write(script_content)
passed, status = run_shellcheck(str(out_fn))
if not passed:
print(script_content)
assert passed, status
def run_shellcheck(script_path):
"""Run shellcheck on a shell script string and return the result.
Parameters
----------
script_path : str
The path to the shell script to check
Returns
-------
tuple
(bool, str) where bool indicates success (True) or failure (False),
and str contains shellcheck output
"""
try:
# Run shellcheck on the temporary file
result = subprocess.run(['shellcheck', script_path], capture_output=True, text=True)
return result.returncode == 0, result.stdout
except subprocess.CalledProcessError as e:
return False, e.output
except Exception as e:
return False, str(e)
def test_generate_submit_script_pipeline(tmp_path):
"""Test submit script generation for pipeline configuration."""
# Use same pattern as single-app tests: read from existing YAML config
config_path = NOTEBOOKS_DIR / 'eg_nordic-fmriprep_pipeline.yaml'
config = read_yaml(config_path)
script_content = generate_submit_script(
queue_system='slurm',
cluster_resources_config=config['cluster_resources'],
script_preamble=config['script_preamble'],
job_scratch_directory=config['job_compute_space'],
input_datasets=input_datasets_prep,
processing_level='subject',
container_name='pipeline', # placeholder
zip_foldernames=config['zip_foldernames'],
run_script_relpath='code/pipeline_zip.sh',
container_images=[
'containers/.datalad/environments/nordic-0-0-1/image',
'containers/.datalad/environments/fmriprep-25.0.0/image',
],
datalad_run_message='nordic-fmriprep pipeline',
)
# Write script to file and run shellcheck (same as single-app tests)
out_fn = tmp_path / 'participant_job.sh'
with open(out_fn, 'w') as f:
f.write(script_content)
passed, status = run_shellcheck(str(out_fn))
if not passed:
print(script_content)
assert passed, status