-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbobs_qwarpar.sh
executable file
·512 lines (425 loc) · 15.9 KB
/
bobs_qwarpar.sh
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/bin/tcsh
if( $#argv > 0 ) then
echo " "
echo "** This script is similar to @toMNI_Qwarp -- but it spawns **"
echo "** jobs in parallel (on the same system). To use it, you **"
echo "** must edit the script and set the 2 variables **"
echo " "
echo "Script to take a collection of datasets and transform them"
echo "to MNI space, then collectively re-transform them to produce"
echo "a more refined average. This script is usually used AFTER"
echo "running @toMNI_Awarp to do the affine alignments, and that"
echo "script is run AFTER skull-stripping the original volumes."
echo " "
echo "This script spawns jobs to run in parallel (on the same system)."
echo "Before using it, copy it into the data directory, and edit it"
echo "to set the 2 variables:"
echo " set numcpu = TOTAL NUMBER OF CPUS TO USE"
echo " set numjob = MAX NUMBER OF JOBS TO USE"
echo "numcpu should not exceed the number of CPUs (cores) on the system;"
echo "it is often simplest to set numjob to the same value as numcpu,"
echo "so that 1 dataset is processed in 1 core, and numcpu jobs are"
echo "run at a time."
echo " "
echo "Usage: @toMNI_Qwarpar (... and then wait a long time)"
echo " "
echo "It should be run inside the directory created by @toMNI_Awarp, and"
echo "will process the *_uni+tlrc.HEAD datasets created by that script."
exit 0
endif
##### Set these 2 variables for your system ##################################
##### Do NOT make numjob > numcpu or make numcpu > actual number of CPUs
##### Each job will get numcpu/numjob CPUs.
##### For speed, the number of input datasets (*_uni+tlrc.HEAD) should
##### be an even multiple of numjob.
set numcpu = 8
set numjob = 8
##### Set this variable to YES if you want to do the F level (finest, slowest)
set doFFF = NO
##### Only change this affine level template name if you truly grok, my brother
set basename = MNI152_1mm_uni+tlrc
################### Don't alter the script below this line! ###################
################### (unless you are Zhark or a myrmidon) ###################
##### Find the A level template
set tpath = `@FindAfniDsetPath $basename`
if( $tpath == '' ) then
echo "** @toMNI_Qwarp: Failed to find template $basename -- cannot continue"
exit 1
endif
set AAbasename = ${tpath}/${basename}
##### Create the list of 3dUnifize-d and affine MNI-ized datasets to process
set dlist = ( *_uni+tlrc.HEAD )
if( $#dlist < 9 ) then
echo "** @toMNI_Qwarp: Not enough input datasets (need at least 9) -- cannot continue"
exit 1
endif
##### A level (affine) #####
if( -f Qwarp_meanA+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanA =========="
echo " "
goto AAA_done ;
endif
echo " "
echo "========== @toMNI_Qwarp: Compute A level mean (affine) =========="
echo " "
nice 3dMean -prefix Qwarp_meanA -datum float $dlist
AAA_done:
##### B level (coarsest scale nonlinear) #####
if( -f Qwarp_meanB+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanB =========="
echo " "
goto BBB_done ;
endif
echo " "
echo "========== @toMNI_Qwarp: Compute B level (minimum patch = 101) =========="
echo " "
# make a list of those warps that have NOT been done yet at this level,
# then parcel it out into parts to be done in different jobs
set qlist = ( )
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
if( ! -e ${bbb}_uniB_WARP+tlrc.HEAD ) set qlist = ( $qlist $fred )
end
if( $#qlist == 0 ) then
echo "=== All B warps computed -- skipping to averaging step ==="
goto BBB_domean
endif
set qjob = $numjob
if( `@ tmp = ( $#qlist < $qjob ) ; echo $tmp` ) set qjob = $#qlist
@ qper = $#qlist / $qjob
@ qext = $#qlist % $qjob
@ qcpu = $numcpu / $qjob
@ qplus = $qjob + 1
set top = 0
set botlist = ( )
set toplist = ( )
foreach part ( `count -dig 1 1 $qjob` )
@ bot = $top + 1
@ top = $top + $qper
if ( $qext > 0 ) then
@ top++ ; @ qext--
endif
set botlist = ( $botlist $bot )
set toplist = ( $toplist $top )
end
echo "=== Processing $#qlist datasets in $qjob jobs"
echo "=== Bottom index list = $botlist"
echo "=== Top index list = $toplist"
# create the script to run 3dQwarp over a bunch of datasets
echo "setenv OMP_NUM_THREADS $qcpu" > BBwarp_script
echo 'echo $argv' >> BBwarp_script
echo 'foreach lucy ( $argv )' >> BBwarp_script
echo ' set bbb = `basename $lucy _uni+tlrc.HEAD`' >> BBwarp_script
echo ' nice 3dQwarp -prefix ${bbb}_uniB -blur 0 9 \' >> BBwarp_script
echo ' -minpatch 101 -nodset \' >> BBwarp_script
echo " $AAbasename" '$lucy' >> BBwarp_script
echo 'end' >> BBwarp_script
echo 'touch BBwarp_$argv[1]_Done' >> BBwarp_script
echo 'exit 0' >> BBwarp_script
# and actually do all the big work
foreach part ( `count -dig 1 1 $qjob` )
set bot = $botlist[$part]
set top = $toplist[$part]
set plist = ( $qlist[$bot-$top] )
tcsh ./BBwarp_script $plist &
sleep 9
end
# wait until the number of markers for scripts being done is complete
while(1)
sleep 60
set fdone = ( BBwarp_* )
if( $#fdone == $qplus ) break
end
# remove the script and the markers
\rm BBwarp_*
# adjust warps (remove mean warp) and then compute mean warped dataset
BBB_domean:
nice 3dNwarpAdjust -nwarp *_uniB_WARP+tlrc.HEAD -source $dlist -prefix Qwarp_meanB
BBB_done:
##### C level (finer level nonlinear) #####
if( -f Qwarp_meanC+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanC =========="
echo " "
goto CCC_done ;
endif
echo " "
echo "========== @toMNI_Qwarp: Compute C level (minimum patch = 49) =========="
echo " "
# make a list of those warps that have NOT been done yet at this level,
# then parcel it out into parts to be done in different jobs
set qlist = ( )
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
if( ! -e ${bbb}_uniC_WARP+tlrc.HEAD ) set qlist = ( $qlist $fred )
end
if( $#qlist == 0 ) then
echo "=== All C warps computed -- skipping to averaging step ==="
goto CCC_domean
endif
set qjob = $numjob
if( `@ tmp = ( $#qlist < $qjob ) ; echo $tmp` ) set qjob = $#qlist
@ qper = $#qlist / $qjob
@ qext = $#qlist % $qjob
@ qcpu = $numcpu / $qjob
@ qplus = $qjob + 1
set top = 0
set botlist = ( )
set toplist = ( )
foreach part ( `count -dig 1 1 $qjob` )
@ bot = $top + 1
@ top = $top + $qper
if ( $qext > 0 ) then
@ top++ ; @ qext--
endif
set botlist = ( $botlist $bot )
set toplist = ( $toplist $top )
end
echo "=== Processing $#qlist datasets in $qjob jobs"
echo "=== Bottom index list = $botlist"
echo "=== Top index list = $toplist"
echo "setenv OMP_NUM_THREADS $qcpu" > CCwarp_script
echo 'foreach fred ( $argv )' >> CCwarp_script
echo ' set bbb = `basename $fred _uni+tlrc.HEAD`' >> CCwarp_script
echo ' nice 3dQwarp -prefix ${bbb}_uniC -blur 1 6 \' >> CCwarp_script
echo ' -inilev 2 -minpatch 49 -nodset \' >> CCwarp_script
echo ' -iniwarp ${bbb}_uniB_WARP+tlrc.HEAD \' >> CCwarp_script
echo ' Qwarp_meanB+tlrc.HEAD $fred' >> CCwarp_script
echo 'end' >> CCwarp_script
echo 'touch CCwarp_$argv[1]_Done' >> CCwarp_script
echo 'exit 0' >> CCwarp_script
foreach part ( `count -dig 1 1 $qjob` )
tcsh ./CCwarp_script $qlist[$botlist[$part]-$toplist[$part]] &
sleep 9
end
while(1)
sleep 60
set fdone = ( CCwarp_* )
if( $#fdone == $qplus ) break
end
\rm CCwarp_*
CCC_domean:
nice 3dNwarpAdjust -nwarp *_uniC_WARP+tlrc.HEAD -source $dlist -prefix Qwarp_meanC
CCC_done:
##### D level (even finer nonlinear) #####
if( -f Qwarp_meanD+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanD =========="
echo " "
goto DDD_done ;
endif
echo " "
echo "========== @toMNI_Qwarp: Compute D level (minimum patch = 23) =========="
echo " "
# make a list of those warps that have NOT been done yet at this level,
# then parcel it out into parts to be done in different jobs
set qlist = ( )
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
if( ! -e ${bbb}_uniD_WARP+tlrc.HEAD ) set qlist = ( $qlist $fred )
end
if( $#qlist == 0 ) then
echo "=== All D warps computed -- skipping to averaging step ==="
goto DDD_domean
endif
set qjob = $numjob
if( `@ tmp = ( $#qlist < $qjob ) ; echo $tmp` ) set qjob = $#qlist
@ qper = $#qlist / $qjob
@ qext = $#qlist % $qjob
@ qcpu = $numcpu / $qjob
@ qplus = $qjob + 1
set top = 0
set botlist = ( )
set toplist = ( )
foreach part ( `count -dig 1 1 $qjob` )
@ bot = $top + 1
@ top = $top + $qper
if ( $qext > 0 ) then
@ top++ ; @ qext--
endif
set botlist = ( $botlist $bot )
set toplist = ( $toplist $top )
end
echo "=== Processing $#qlist datasets in $qjob jobs"
echo "=== Bottom index list = $botlist"
echo "=== Top index list = $toplist"
echo "setenv OMP_NUM_THREADS $qcpu" > DDwarp_script
echo 'foreach fred ( $argv )' >> DDwarp_script
echo ' set bbb = `basename $fred _uni+tlrc.HEAD`' >> DDwarp_script
echo ' nice 3dQwarp -prefix ${bbb}_uniD -blur 0 4 \' >> DDwarp_script
echo ' -inilev 5 -minpatch 23 -nodset \' >> DDwarp_script
echo ' -iniwarp ${bbb}_uniC_WARP+tlrc.HEAD \' >> DDwarp_script
echo ' Qwarp_meanC+tlrc.HEAD $fred' >> DDwarp_script
echo 'end' >> DDwarp_script
echo 'touch DDwarp_$argv[1]_Done' >> DDwarp_script
echo 'exit 0' >> DDwarp_script
foreach part ( `count -dig 1 1 $qjob` )
tcsh ./DDwarp_script $qlist[$botlist[$part]-$toplist[$part]] &
sleep 9
end
while(1)
sleep 60
set fdone = ( DDwarp_* )
if( $#fdone == $qplus ) break
end
\rm DDwarp_*
DDD_domean:
nice 3dNwarpAdjust -nwarp *_uniD_WARP+tlrc.HEAD -source $dlist -prefix Qwarp_meanD
DDD_done:
##### E level (even finer nonlinear) #####
if( -f Qwarp_meanE+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanE =========="
echo " "
goto EEE_done ;
endif
# make a list of those warps that have NOT been done yet at this level,
# then parcel it out into parts to be done in different jobs
set qlist = ( )
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
if( ! -e ${bbb}_uniE_WARP+tlrc.HEAD ) set qlist = ( $qlist $fred )
end
if( $#qlist == 0 ) then
echo "=== All E warps computed -- skipping to averaging step ==="
goto EEE_domean
endif
set qjob = $numjob
if( `@ tmp = ( $#qlist < $qjob ) ; echo $tmp` ) set qjob = $#qlist
@ qper = $#qlist / $qjob
@ qext = $#qlist % $qjob
@ qcpu = $numcpu / $qjob
@ qplus = $qjob + 1
set top = 0
set botlist = ( )
set toplist = ( )
foreach part ( `count -dig 1 1 $qjob` )
@ bot = $top + 1
@ top = $top + $qper
if ( $qext > 0 ) then
@ top++ ; @ qext--
endif
set botlist = ( $botlist $bot )
set toplist = ( $toplist $top )
end
echo "=== Processing $#qlist datasets in $qjob jobs"
echo "=== Bottom index list = $botlist"
echo "=== Top index list = $toplist"
echo " "
echo "========== @toMNI_Qwarp: Compute E level (minimum patch = 13) =========="
echo " "
echo "setenv OMP_NUM_THREADS $qcpu" > EEwarp_script
echo 'foreach fred ( $argv )' >> EEwarp_script
echo ' set bbb = `basename $fred _uni+tlrc.HEAD`' >> EEwarp_script
echo ' nice 3dQwarp -prefix ${bbb}_uniE -blur 0 -2 \' >> EEwarp_script
echo ' -inilev 7 -minpatch 13 -nodset \' >> EEwarp_script
echo ' -iniwarp ${bbb}_uniD_WARP+tlrc.HEAD \' >> EEwarp_script
echo ' Qwarp_meanD+tlrc.HEAD $fred' >> EEwarp_script
echo 'end' >> EEwarp_script
echo 'touch EEwarp_$argv[1]_Done' >> EEwarp_script
echo 'exit 0' >> EEwarp_script
foreach part ( `count -dig 1 1 $qjob` )
tcsh ./EEwarp_script $qlist[$botlist[$part]-$toplist[$part]] &
sleep 9
end
while(1)
sleep 60
set fdone = ( EEwarp_* )
if( $#fdone == $qplus ) break
end
\rm EEwarp_*
EEE_domean:
nice 3dNwarpAdjust -nwarp *_uniE_WARP+tlrc.HEAD -source $dlist -prefix Qwarp_meanE
if( $doFFF != 'YES' )then
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
3dNwarpApply -nwarp ${bbb}_uniE_WARP+tlrc.HEAD \
-source $fred -prefix ${bbb}_uniE
end
endif
EEE_done:
##### F level (optional) #####
if( $doFFF != 'YES' ) goto FFF_done
if( -f Qwarp_meanF+tlrc.HEAD ) then
echo " "
echo "========== @toMNI_Qwarp: skipping Qwarp_meanF =========="
echo " "
goto FFF_done ;
endif
echo " "
echo "========== @toMNI_Qwarp: Compute F level (minimum patch = 9) =========="
echo " "
set qlist = ( )
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
if( ! -e ${bbb}_uniF_WARP+tlrc.HEAD ) set qlist = ( $qlist $fred )
end
if( $#qlist == 0 ) then
echo "=== All F warps computed -- skipping to averaging step ==="
goto FFF_domean
endif
set qjob = $numjob
if( `@ tmp = ( $#qlist < $qjob ) ; echo $tmp` ) set qjob = $#qlist
@ qper = $#qlist / $qjob
@ qext = $#qlist % $qjob
@ qcpu = $numcpu / $qjob
@ qplus = $qjob + 1
set top = 0
set botlist = ( )
set toplist = ( )
foreach part ( `count -dig 1 1 $qjob` )
@ bot = $top + 1
@ top = $top + $qper
if ( $qext > 0 ) then
@ top++ ; @ qext--
endif
set botlist = ( $botlist $bot )
set toplist = ( $toplist $top )
end
echo "=== Processing $#qlist datasets in $qjob jobs"
echo "=== Bottom index list = $botlist"
echo "=== Top index list = $toplist"
echo "setenv OMP_NUM_THREADS $qcpu" > FFwarp_script
echo 'foreach fred ( $argv )' >> FFwarp_script
echo ' set bbb = `basename $fred _uni+tlrc.HEAD`' >> FFwarp_script
echo ' nice 3dQwarp -prefix ${bbb}_uniF -blur 0 -2 \' >> FFwarp_script
echo ' -inilev 9 -minpatch 9 -nodset \' >> FFwarp_script
echo ' -iniwarp ${bbb}_uniE_WARP+tlrc.HEAD \' >> FFwarp_script
echo ' Qwarp_meanE+tlrc.HEAD $fred' >> FFwarp_script
echo 'end' >> FFwarp_script
echo 'touch FFwarp_$argv[1]_Done' >> FFwarp_script
echo 'exit 0' >> FFwarp_script
foreach part ( `count -dig 1 1 $qjob` )
tcsh ./FFwarp_script $qlist[$botlist[$part]-$toplist[$part]] &
sleep 9
end
while(1)
sleep 60
set fdone = ( FFwarp_* )
if( $#fdone == $qplus ) break
end
\rm FFwarp_*
nice 3dNwarpAdjust -nwarp *_uniF_WARP+tlrc.HEAD -source $dlist -prefix Qwarp_meanF
foreach fred ( $dlist )
set bbb = `basename $fred _uni+tlrc.HEAD`
3dNwarpApply -nwarp ${bbb}_uniF_WARP+tlrc.HEAD \
-source $fred -prefix ${bbb}_uniF
end
FFF_done:
################ finished ################
echo " "
echo "========== @toMNI_Qwarpar: compressing output BRIK files =========="
echo " "
which pigz >& /dev/null
if ( $status == 0 ) then
set GZIP = pigz
else
set GZIP = gzip
endif
# nice $GZIP -9v *.BRIK
echo " "
echo "========== @toMNI_Qwarpar: free at last =========="
echo " "
exit 0