-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathshapeupdatetotemplate.sh
executable file
·211 lines (180 loc) · 7.28 KB
/
shapeupdatetotemplate.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
#!/bin/bash
# trap keyboard interrupt (control-c)
trap control_c SIGINT
WARP=antsApplyTransforms
AVERAGE_AFFINE_PROGRAM=AverageAffineTransform # NoRigid
if ! command -v ${WARP} &> /dev/null
then
echo "antsApplyTransforms program can't be found. Please (re)define \$PATH in your environment."
exit
fi
if ! command -v ${AVERAGE_AFFINE_PROGRAM} &> /dev/null
then
echo "AverageAffineTransform* program can't be found. Please (re)define \$PATH in your environment."
exit
fi
DIM=3
TEMPLATE=template0.nii.gz
OUTPUTNAME=T_
GRADIENTSTEP=0.25
whichtemplate=0
statsmethod=1
useaff=0
USAGE="$0 -d 3 -t template0.nii.gz -o T_ -g 0.25 -s 1 -w 0 -y 0"
while getopts "d:t:o:g:w:s:y:h:" OPT
do
case $OPT in
h) #help
echo $USAGE
exit 0
;;
d) # dimensions
DIM=$OPTARG
;;
t) # name of image
TEMPLATE=$OPTARG
;;
o) # output prefix
OUTPUTNAME=$OPTARG
;;
g) # moving image
GRADIENTSTEP=$OPTARG
;;
w) # for multivar templates
whichtemplate=$OPTARG
;;
y) # affine only in update?
useaff=$OPTARG
;;
s) # median, mean, etc
statsmethod=$OPTARG
;;
\?) # getopts issues an error message
echo "$USAGE" >&2
exit 1
;;
esac
done
if [[ $useaff -eq 1 ]] ; then
AVERAGE_AFFINE_PROGRAM=AverageAffineTransformNoRigid
fi
function summarizeimageset() {
local dim=$1
shift
local output=$1
shift
local method=$1
shift
local images=( "${@}" "" )
case $method in
0) #mean
AverageImages $dim $output 0 ${images[*]}
;;
1) #mean of normalized images
AverageImages $dim $output 1 ${images[*]}
;;
2) #median
for i in "${images[@]}";
do
echo $i >> ${output}_list.txt
done
ImageSetStatistics $dim ${output}_list.txt ${output} 0
rm ${output}_list.txt
;;
3) #median+sharpen
for i in "${images[@]}";
do
echo $i >> ${output}_list.txt
done
ImageSetStatistics $dim ${output}_list.txt ${output} 0
ImageMath $dim ${output} Sharpen ${output}
rm ${output}_list.txt
;;
4) #mean+sharpen
AverageImages $dim $output 1 ${images[*]}
ImageMath $dim ${output} Sharpen ${output}
;;
esac
}
function shapeupdatetotemplate() {
echo "shapeupdatetotemplate()"
# local declaration of values
dim=$1
template=$2
templatename=$3
outputname=$4
gradientstep=-$5
whichtemplate=$6
statsmethod=$7
# debug only
# echo $dim
# echo ${template}
# echo ${templatename}
# echo ${outputname}
# echo ${outputname}*WarpedToTemplate.nii*
# echo ${gradientstep}
# We find the average warp to the template and apply its inverse to the template image
# This keeps the template shape stable over multiple iterations of template building
echo
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---voxel-wise averaging of the warped images to the current template"
date
#echo " AverageImages $dim ${template} 1 ${templatename}${whichtemplate}*WarpedToTemplate.nii.gz "
#echo " ImageSetStatistics $dim ${whichtemplate}WarpedToTemplateList.txt ${template} 0"
echo "--------------------------------------------------------------------------------------"
imagelist=(`ls ${outputname}template${whichtemplate}*WarpedToTemplate.nii.gz`)
if [[ ${#imagelist[@]} -eq 0 ]] ; then
echo ERROR shapeupdatedtotemplate - imagelist length is 0
exit 1
fi
summarizeimageset $dim $template $statsmethod ${imagelist[@]}
WARPLIST=( `ls ${outputname}*[0-9]Warp.nii.gz 2> /dev/null` )
NWARPS=${#WARPLIST[*]}
echo "number of warps = $NWARPS"
echo "$WARPLIST"
if [[ $whichtemplate -eq 0 ]];
then
if [[ $NWARPS -ne 0 ]]; then
echo "$NWARPS does not equal 0"
echo
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---voxel-wise averaging of the inverse warp fields (from subject to template)"
echo " AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 `ls ${outputname}*Warp.nii.gz | grep -v "InverseWarp"`"
date
echo "--------------------------------------------------------------------------------------"
AverageImages $dim ${templatename}${whichtemplate}warp.nii.gz 0 `ls ${outputname}*Warp.nii.gz | grep -v "InverseWarp"`
echo
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---scale the averaged inverse warp field by the gradient step"
echo " MultiplyImages $dim ${templatename}${whichtemplate}warp.nii.gz ${gradientstep} ${templatename}${whichtemplate}warp.nii.gz"
date
echo "--------------------------------------------------------------------------------------"
MultiplyImages $dim ${templatename}${whichtemplate}warp.nii.gz ${gradientstep} ${templatename}${whichtemplate}warp.nii.gz
fi
echo
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---average the affine transforms (template <-> subject)"
echo " ---transform the inverse field by the resulting average affine transform"
echo " ${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}*GenericAffine.mat"
echo " ${WARP} -d ${dim} -e vector -i ${templatename}0warp.nii.gz -o ${templatename}0warp.nii.gz -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template}"
echo "--------------------------------------------------------------------------------------"
${AVERAGE_AFFINE_PROGRAM} ${dim} ${templatename}0GenericAffine.mat ${outputname}*GenericAffine.mat
if [[ $NWARPS -ne 0 ]];
then
${WARP} -d ${dim} -e vector -i ${templatename}0warp.nii.gz -o ${templatename}0warp.nii.gz -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template}
MeasureMinMaxMean ${dim} ${templatename}0warp.nii.gz ${templatename}warplog.txt 1
fi
fi
echo "--------------------------------------------------------------------------------------"
echo " shapeupdatetotemplate---warp each template by the resulting transforms"
echo " ${WARP} -d ${dim} --float $USEFLOAT -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -r ${template}"
echo "--------------------------------------------------------------------------------------"
if [ -f "${templatename}0warp.nii.gz" ];
then
${WARP} -d ${dim} --float $USEFLOAT -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -t ${templatename}0warp.nii.gz -r ${template}
else
${WARP} -d ${dim} --float $USEFLOAT -i ${template} -o ${template} -t [ ${templatename}0GenericAffine.mat,1 ] -r ${template}
fi
}
TEMPLATENAME=$OUTPUTNAME
shapeupdatetotemplate ${DIM} ${TEMPLATE} ${TEMPLATENAME} ${OUTPUTNAME} ${GRADIENTSTEP} ${whichtemplate} $statsmethod