-
Notifications
You must be signed in to change notification settings - Fork 6
/
make_fmriprep_jobscript.py
executable file
·259 lines (166 loc) · 6.56 KB
/
make_fmriprep_jobscript.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#### Script for passing fmriprep jobs to SLURM.
## Requirements
# 1. An fmriprep singularity image
# 2. The fmriprep_base.sh template
# 3. All of the required templateflow libraries installed locally (see readme)
####
####
## Imports
####
import re
import os
####
## Participant input.
####
# The participant identifiers in the BIDS directory (can be a list).
PIDS=["02"]
####
## Execution options
####
EX={}
# sbatch or sh? (sbatch if using the RACC)
EX['execute_type']='sbatch'
# Send the jobs fo excution, or just write? If set to False, then you will need to submit the job manually.
EX['execute']=False
# Number of CPUs requested from SLURM.
EX['cpus']='16'
# Memory per CPU requested from SLURM.
EX['memperCPU']='4G'
# Email to send error messages to
EX['email']='nhedger1@gmail.com'
####
## Path options
####
# To avoid confusion - use the following scheme
# L_= local location
# S_= Singularity image location
# B_= Base location.
# Here we set the local paths to mount onto the singularity image.
# All of these in this section must be existing locations
# Start with local paths.
LPATH={}
# Location of the singularity image itself (static)
LPATH['im_path']='/storage/basic/nh_leverhulme/UTILS/poldracklab_fmriprep_latest-2020-04-09-30f9d1d80eba.simg'
# Path to the templateflow cache (static)
LPATH['tflow_path']='/storage/basic/nh_leverhulme/cache/templateflow'
# Path to the jobscript template to modify (static)
LPATH['jobscript_path']='/storage/basic/nh_leverhulme/BASE/BASE2/fmriprep_base.sh'
# Path to output the job scripts (static)
LPATH['job_path']='/storage/basic/nh_leverhulme/JOBS/fmriprep'
# Directory to the BIDS directory data (The one that contains the participant subdirectories).
LPATH['data_path']='/storage/basic/nh_leverhulme/DATA/knapenprf'
# Path to freesurfer directory (static - I think...).
LPATH['fs_path']='/storage/basic/nh_leverhulme/freesurfer'
# Base temporary working directory (dynamic). This is where fmriprep will store temporary files.
LPATH['B_work_path']='/storage/basic/nh_leverhulme/TEMP'
# Path to the freesurfer liscence (For convenience, I put mine in my temporary directory.
# It makes sense to do this, since it needs to be mounted anyway).
LPATH['fsli_path']=os.path.join(LPATH['B_work_path'],'license.txt')
# Base directory to outputs (dynamic).
LPATH['B_output_path']='/storage/basic/nh_leverhulme/DATA/knapenprf'
# Now some singularity locations
SPATH={}
# Data path (dynamic)
SPATH['B_data_path']='/data'
# Temp path (dynamic)
SPATH['B_work_path']='/work'
# License path (static)
SPATH['lipath']='/license'
SPATH['fsli_path']=os.path.join(SPATH['lipath'],'license.txt')
# Freesurfer directory path (static)
SPATH['fs_path']='/fsdir'
# Base output path (dynamic)
SPATH['B_output_path']='/output'
####
## Mount options
####
MOUNTS={}
# Make the static mounts now
# Mount the templateflow directory.
# The templateflow directory is mounted.
MOUNTS['tfmount']='-B ' +'${TEMPLATEFLOW_HOST_HOME}:${SINGULARITYENV_TEMPLATEFLOW_HOME}'
# The freesurfer directory is mounted to /fsdir
MOUNTS['fsmount']= '-B ' + LPATH['fs_path']+':'+SPATH['fs_path']
# Mount the path to the lisence
MOUNTS['fslmount']= '-B ' + LPATH['B_work_path'] +':'+SPATH['lipath']
####
## Fmriprep specific options
####
FMRIPREP={}
# Set the output spaces.
FMRIPREP['output_spaces'] = ['fsaverage','MNI152NLin2009cAsym']
FMRIPREP['output_spaces']=" ".join(FMRIPREP['output_spaces'])
# Here I use a recipe that seems to work fairly well.
# Upper bound memory limit for fMRIPrep processes
FMRIPREP['mem_mb']='30000'
# Maximum number of threads per-process
FMRIPREP['ot']='8'
# Maximum number of threads across all processes
FMRIPREP['nt']='12'
# Put any optional flags here and they will get appended to the end of the call.
FMRIPREP['optionals']=['--write-graph','--ignore slicetiming', '--low-mem']
FMRIPREP['optionals']=" ".join(FMRIPREP['optionals'])
####
## Main loop
####
for PID in PIDS:
#The path to the jobfile to be written
LPATH['jobscript_current_path']=os.path.join(LPATH['job_path'],'myjob_'+PID+'.sh')
# Force unique output directory for participant.
# This will need to be made into a real location.
if not os.path.isdir(os.path.join(LPATH['B_output_path'],PID,'derivatives')):
os.mkdir(os.path.join(LPATH['B_output_path'],PID))
os.mkdir(os.path.join(LPATH['B_output_path'],PID,'derivatives'))
LPATH['output_path']=os.path.join(LPATH['B_output_path'],PID,'derivatives')
SPATH['output_path']=os.path.join(SPATH['B_output_path'],PID,'derivatives')
# Force unique working directory.
if not os.path.isdir(os.path.join(LPATH['B_work_path'],PID)):
os.mkdir(os.path.join(LPATH['B_work_path'],PID))
LPATH['work_path']=os.path.join(LPATH['B_work_path'],PID)
SPATH['work_path']=os.path.join(SPATH['B_work_path'],PID)
# Mount the data in a distinct location.
SPATH['data_path']=os.path.join(SPATH['B_data_path'],PID)
# Now add the dynamic mounts
MOUNTS['dmount']= '-B ' + LPATH['data_path']+':'+SPATH['data_path']
MOUNTS['wmount']= '-B ' + LPATH['work_path']+':'+SPATH['work_path']
MOUNTS['omount']= '-B ' + LPATH['output_path']+':'+SPATH['output_path']
# Join these mount commands altogether.
MOUNTS['mounts']=[MOUNTS['dmount'],MOUNTS['wmount'],MOUNTS['tfmount'],MOUNTS['fslmount'],MOUNTS['omount']]
MOUNTS['mounts']=" ".join(MOUNTS['mounts'])
# Make unique error file for the participant
EX['errfile']=os.path.join(LPATH['job_path'],'myjob'+PID+'.err')
RE_dict = {
'---cpus---':EX['cpus'],
'---memperCPU---':EX['memperCPU'],
'---email---':EX['email'],
'---errpath---':EX['errfile'],
'---tflow_path---':LPATH['tflow_path'],
'---mounts---':MOUNTS['mounts'],
'---imloc---':LPATH['im_path'],
'---data_base---':SPATH['data_path'],
'---outputpath---':SPATH['output_path'],
'---pid---':PID,
'---wpath---':SPATH['work_path'],
'---output_spaces---':FMRIPREP['output_spaces'],
'---mem_mb---':FMRIPREP['mem_mb'],
'---ot---':FMRIPREP['ot'],
'---nt---':FMRIPREP['nt'],
'---fsdir---':SPATH['fs_path'],
'---fsli---':SPATH['fsli_path'],
'---optionals---':FMRIPREP['optionals']
}
print(RE_dict)
jobscript = open(LPATH['jobscript_path'])
working_string = jobscript.read()
jobscript.close()
# Populate the template with the relevant information for this participant.
for e in RE_dict:
rS = re.compile(e)
working_string = re.sub(rS, RE_dict[e], working_string)
of = open(LPATH['jobscript_current_path'],'w')
of.write(working_string)
of.close()
print('Job script written to' + '' + LPATH['jobscript_current_path'])
# Execute, or just write the file.
if EX['execute']:
os.system(EX['execute_type'] + ' ' + LPATH['jobscript_current_path'])