-
Notifications
You must be signed in to change notification settings - Fork 2
/
reg.sh
executable file
·393 lines (338 loc) · 12.1 KB
/
reg.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
#!/bin/bash
ppath=$(realpath "$BASH_SOURCE")
cdir=$(dirname "$ppath")
pn=$(basename "$ppath")
. "$cdir"/functions
commandline="$pn $*"
: ${PINCRAM_ARCH:="bash"}
: ${PINCRAM_USE_LIB:="mirtk"}
usage() {
msg "reg.sh should not be called directly"
}
finish () {
savewd=1
if [[ $savewd -eq 1 ]] ; then
mv "$td" "$wd"
else
rm -rf "$td"
fi
exit
}
cmpdofs () {
# Is dof1 better than dof2?
local img1=$1 ; shift
local img2=$1 ; shift
local dof1=$1 ; shift
local dof2=$1 ; shift
nmi1=$( mirtk evaluate-similarity $img1 $img2 -csv -dofin $dof1 -threads $par | cut -d , -f 9 | tail -n 1 )
nmi2=$( mirtk evaluate-similarity $img1 $img2 -csv -dofin $dof2 -threads $par | cut -d , -f 9 | tail -n 1 )
echo $nmi1 '>' $nmi2 | bc
}
set -e # Terminate script at first error
level=$LEVEL
case "$PINCRAM_ARCH" in
bash-single | bash)
idx=$PARALLEL_SEQ
wd=$PWD
chunkn=1
;;
pbs)
idx=$PBS_ARRAY_INDEX
wd=$PBS_O_WORKDIR
jobid=$PBS_JOBID
if [[ -n "$PINCRAM_CHUNKSIZE" ]]
then chunkn="$PINCRAM_CHUNKSIZE"
else
chunkn=$[$[$level-3]**2]
fi
;;
esac
td=$(tempdir)
trap finish EXIT
cd $td
if [[ $idx -gt 0 ]]
then
split -a 4 -l $chunkn -d $wd/job.conf
idx0=$(printf '%04g' $[$idx-1])
cp x$idx0 thischunk
rm x????
else
echo $@ >thischunk
fi
cat thischunk | sort -R | while read params
do
(( loopc += 1 ))
set -- $(echo $params)
while [ $# -gt 0 ]
do
case "$1" in
-tgt) tgt=$(realpath "$2"); shift;;
-tdm) tdm=$(realpath "$2"); shift;;
-src) src=$(realpath "$2"); shift;;
-srctr) srctr=$(realpath "$2"); shift;;
-msk) msk=$(realpath "$2"); shift;;
-masktr) masktr=$(realpath "$2"); shift;;
-alt) alt=$(realpath "$2"); shift;;
-alttr) alttr=$(realpath "$2"); shift;;
-dofin) dofin=$(realpath "$2"); shift;;
-dofout) dofout=$(realpath "$2"); shift;;
-spn) spn=$(realpath "$2"); shift;;
-tpn) tpn=$(realpath "$2"); shift;;
-tmargin) tmargin=$(realpath "$2"); shift;;
-lev) lev="$2"; shift;;
-par) par="$2"; shift;;
--) shift; break;;
-*)
fatal "Parameter error" ;;
*) break;;
esac
shift
done
if [[ -e "$masktr" ]] ; then
msg "Result file $masktr exists"
continue
fi
case "$PINCRAM_USE_LIB" in
greedy)
if [[ $lev == 0 ]] ; then
mirtk compose-dofs "$spn" "$tpn" dof-pre.dof -scale 1 -1
convert-dof dof-pre.dof dof-pre.mat -output-format flirt -target "$tgt" -source "$src"
greedy -d 3 \
-a \
-dof 6 \
-threads $par \
-ia dof-pre.mat \
-i "$tgt" "$src" \
-o transform.mat \
-m NCC 5x5x5 \
-n 100x0x0
greedy -d 3 \
-threads $par \
-rf "$tgt" \
-ri LINEAR \
-rm "$msk" $masktr \
-rm "$src" $srctr \
-rm "$alt" $alttr \
-r transform.mat
mv transform.mat "$dofout"
fi
if [[ $lev == 1 ]] ; then
greedy -d 3 \
-a \
-dof 6 \
-threads $par \
-ia "$dofin" \
-i "$tdm" "$msk" \
-gm "$tmargin" \
-o pre-l1.mat \
-m NCC 5x5x5 \
-n 100x50x0
greedy -d 3 \
-a \
-dof 12 \
-threads $par \
-ia pre-l1.mat \
-i "$tgt" "$src" \
-o transform.mat \
-gm "$tmargin" \
-m NCC 3x3x3 \
-n 40x20x5
greedy -d 3 \
-threads $par \
-rf "$tgt" \
-ri LINEAR \
-rm "$msk" $masktr \
-rm "$src" $srctr \
-rm "$alt" $alttr \
-r transform.mat
mv transform.mat "$dofout"
fi
if [[ $lev == 2 ]] ; then
greedy -d 3 \
-a \
-dof 12 \
-threads $par \
-ia "$dofin" \
-i "$tdm" "$msk" \
-o pre-l2.mat \
-gm "$tmargin" \
-m NCC 3x3x3 \
-n 100x50x30
greedy -d 3 \
-threads $par \
-i "$tgt" "$src" \
-it pre-l2.mat \
-o transform.nii.gz \
-gm "$tmargin" \
-m NCC 2x2x2 \
-s 1.2vox 0.25vox \
-n 100x50x30
greedy -d 3 \
-threads $par \
-rf "$tgt" \
-ri LINEAR \
-rm "$msk" $masktr \
-rm "$src" $srctr \
-rm "$alt" $alttr \
-r transform.nii.gz
mv transform.nii.gz "$dofout"
fi;;
mirtk)
if [[ $lev == 0 ]] ; then
mirtk compose-dofs "$spn" "$tpn" dof-pre.dof -scale 1 -1
mirtk register "$tgt" "$src" \
-model Rigid+Affine \
-dofout dof-reg.dof \
-dofin dof-pre.dof \
-levels 4 4 \
-bg 0 \
-threads $par
cp dof-reg.dof dofout-m-$lev.dof
fi
if [[ $lev == 1 ]] ; then
mirtk register "$tgt" "$src" \
-model Rigid+Affine \
-dofout dofout-m-$lev.dof \
-dofin "$dofin" \
-mask "$tmargin" \
-bg -1 \
-levels 3 3 \
-threads $par
fi
if [[ $lev == 2 ]] ; then
mirtk register "$tgt" "$src" \
-model SVFFD \
-par "Bending energy weight" 1e-4 \
-dofout dofout-m-$lev.dof \
-dofin "$dofin" \
-mask "$tmargin" \
-bg -1 \
-levels 1 1 \
-threads $par
fi
mirtk transform-image "$msk" $masktr -interp "Linear" -Sp -1 -dofin dofout-m-$lev.dof -target "$tgt" || fatal "Failure at masktr"
mirtk transform-image "$src" $srctr -interp "Linear" -Sp -1 -dofin dofout-m-$lev.dof -target "$tgt" || fatal "Failure at srctr"
mirtk transform-image "$alt" $alttr -interp "Linear" -Sp -1 -dofin dofout-m-$lev.dof -target "$tgt" || fatal "Failure at alttr"
mv dofout-m-$lev.dof "$dofout"
;;
irtk)
if [[ $lev == 0 ]] ; then
cat >lev0.reg << EOF
#
# Registration parameters
#
No. of resolution levels = 1
No. of bins = 64
Epsilon = 0.0001
Padding value = -1
Source padding value = -1
Similarity measure = NMI
Interpolation mode = Linear
#
# Registration parameters for resolution level 1
#
Resolution level = 1
Target blurring (in mm) = 2
Target resolution (in mm) = 5 5 5
Source blurring (in mm) = 2
Source resolution (in mm) = 5 5 5
No. of iterations = 40
Minimum length of steps = 0.01
Maximum length of steps = 2
EOF
dofcombine "$spn" "$tpn" pre1.dof.gz -invert2
echo areg2 "$tgt" "$src" -dofin pre1.dof.gz -dofout pre2.dof.gz -parin lev0.reg
areg2 "$tgt" "$src" -dofin pre1.dof.gz -dofout pre2.dof.gz -parin lev0.reg
# nmi1=$( evaluation "$tgt" "$src" | grep NMI | cut -d : -f 2 )
nmi2=$( evaluation "$tgt" "$src" -dofin pre1.dof.gz | grep NMI | cut -d : -f 2 )
nmi3=$( evaluation "$tgt" "$src" -dofin pre2.dof.gz | grep NMI | cut -d : -f 2 )
cp pre2.dof.gz dofout.dof.gz
better=$( echo $nmi3 '>' $nmi2 | bc ) ; [[ $better -eq 0 ]] && cp pre1.dof.gz dofout.dof.gz
fi
if [[ $lev == 1 ]] ; then
cat >lev1.reg << EOF
#
# Registration parameters
#
No. of resolution levels = 2
No. of bins = 64
Epsilon = 0.0001
Padding value = 0
Source padding value = 0
Similarity measure = NMI
Interpolation mode = Linear
#
# Registration parameters for resolution level 1
#
Resolution level = 1
Target blurring (in mm) = 0
Target resolution (in mm) = 0 0 0
Source blurring (in mm) = 0
Source resolution (in mm) = 0 0 0
No. of iterations = 40
Minimum length of steps = 0.01
Maximum length of steps = 1
#
# Registration parameters for resolution level 2
#
Resolution level = 2
Target blurring (in mm) = 1.5
Target resolution (in mm) = 3 3 3
Source blurring (in mm) = 1.5
Source resolution (in mm) = 3 3 3
No. of iterations = 40
Minimum length of steps = 0.01
Maximum length of steps = 1
EOF
echo areg2 "$tgt" "$src" -dofin "$dofin" -dofout dofout.dof.gz -parin lev1.reg -mask $tmargin
areg2 "$tgt" "$src" -dofin "$dofin" -dofout dofout.dof.gz -parin lev1.reg -mask $tmargin
fi
if [[ $lev == 2 ]] ; then
cat >lev2.reg << EOF
#
# Non-rigid registration parameters
#
Lambda1 = 0.0001
Lambda2 = 1
Lambda3 = 1
Control point spacing in X = 6
Control point spacing in Y = 6
Control point spacing in Z = 6
Subdivision = True
MFFDMode = True
#
# Registration parameters
#
No. of resolution levels = 1
No. of bins = 128
Epsilon = 0.0001
Padding value = 0
Source padding value = 0
Similarity measure = NMI
Interpolation mode = Linear
#
# Skip resolution level 1
#
Resolution level = 1
Target blurring (in mm) = 0
Target resolution (in mm) = 0 0 0
Source blurring (in mm) = 0
Source resolution (in mm) = 0 0 0
No. of iterations = 40
Minimum length of steps = 0.01
Maximum length of steps = 2
EOF
echo nreg2 "$tgt" "$src" -dofin "$dofin" -dofout dofout.dof.gz -parin lev2.reg -mask $tmargin
nreg2 "$tgt" "$src" -dofin "$dofin" -dofout dofout.dof.gz -parin lev2.reg -mask $tmargin
fi
transformation "$msk" $masktr -linear -Sp -1 -dofin dofout.dof.gz -target "$tgt" || fatal "Failure at masktr"
transformation "$src" $srctr -linear -Sp -1 -dofin dofout.dof.gz -target "$tgt" || fatal "Failure at srctr"
transformation "$alt" $alttr -linear -Sp -1 -dofin dofout.dof.gz -target "$tgt" || fatal "Failure at alttr"
mv dofout.dof.gz $dofout
;;
*)
fatal "Not implemented: $PINCRAM_USE_LIB"
;;
esac
done >reg-l$level-i$idx.log 2>&1
exit 0