forked from khanlab/gradcorrect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procGradCorrect
executable file
·190 lines (157 loc) · 4.1 KB
/
procGradCorrect
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
#!/bin/bash
#function for unwarping and generating warp files
function usage {
echo "Performs gradient unwarping and saves relevant output"
echo ""
echo "Required args:"
echo " -i input_nii"
echo " -g input_grad_coeff"
echo ""
echo "Optional args:"
echo " -u output_unwarped_nii"
echo " -c output_unwarped_intcorr_nii"
echo " -w output_warp_nii (apply with FSL applywarp --abs)"
echo " -j output_jacobian_determinant_nii (multiply for intensity correction)"
echo " -d output_grad_dev_nii (for DWI)"
echo " -s scratch_dir (default `pwd`/gradcorr)"
echo " -k (keep scratch directory , default removed afterwards)"
echo ""
echo "Grad-unwarp options"
echo " -I interporder"
echo " -N numpoints"
echo " -F fovmin (from -fovmin to +fovmin)"
}
if [ "$#" -lt 3 ]
then
usage
exit 1
fi
in_nii=
grad_coeff=
scratch_dir=`pwd`/gradcorr
unwarped_nii=
unwarped_intcorr_nii=
warp_nii=
detjac_nii=
grad_dev_nii=
keep_scratch=0
fovmin=0.2 #in metres
numpoints=150
interporder=3 #cubic
while getopts "i:g:s:kw:u:c:j:d:I:N:F:" options; do
case $options in
i ) echo " Input nii $OPTARG"
in_nii=$OPTARG;;
g ) echo " Input grad_coeff $OPTARG"
grad_coeff=$OPTARG;;
s ) echo " Using scratch dir $OPTARG"
scratch_dir=$OPTARG;;
k ) echo " Keeping scratch dir"
keep_scratch=1;;
w ) echo " Saving warp nii to $OPTARG "
warp_nii=$OPTARG;;
u ) echo " Saving unwarped nii to $OPTARG"
unwarped_nii=$OPTARG;;
c ) echo " Saving unwarped, intensity-corrected nii to $OPTARG"
unwarped_intcorr_nii=$OPTARG;;
j ) echo " Saving warp detjac nii to $OPTARG"
detjac_nii=$OPTARG;;
d ) echo " Saving warp grad_dev nii to $OPTARG"
grad_dev_nii=$OPTARG;;
I ) echo " Interp order $OPTARG"
interporder=$OPTARG;;
N ) echo " Numpoints $OPTARG"
numpoints=$OPTARG;;
F ) echo " fovmin $OPTARG"
fovmin=$OPTARG;;
* ) usage
exit 1;;
esac
done
if [ ! -n "$in_nii" -o ! -n "$grad_coeff" ]
then
usage
exit 1
fi
#CAVEAT -- works only for nii.gz, not nii
if [ ! -e $in_nii ]
then
echo "Input image $in_nii does not exist!"
exit 1
fi
if [ ! -e $grad_coeff ]
then
echo "Input grad coeff $grad_coeff does not exist!"
exit 1
fi
if [ ! -e $scratch_dir ]
then
mkdir -p $scratch_dir
fi
in=input.nii.gz
coeff=coeff.grad
unwarped=input_unwarped.nii.gz
unwarped_intcorr=input_unwarped_intcorr.nii.gz
detjac=detjac.nii.gz
warp=fullWarp_abs.nii.gz
grad_dev=grad_dev.nii.gz
grad_dev_prefix=grad_dev
ext=${in_nii##*.}
if [ "$ext" = "nii" ]
then
cp -v --no-preserve mode $in_nii $scratch_dir/input.nii
gzip $scratch_dir/input.nii
elif [ "$ext" = "gz" ]
then
cp -v --no-preserve mode $in_nii $scratch_dir/$in
else
echo "Unknown file extension, $ext, for $in_nii"
exit 1
fi
cp -v --no-preserve mode $grad_coeff $scratch_dir/$coeff
pushd $scratch_dir
#run gradunwarp
gradient_unwarp.py $in $unwarped siemens -g $coeff -n --fovmin -$fovmin --fovmax $fovmin --numpoints $numpoints --interp_order $interporder --verbose
#get jacobian determinant
reg_jacobian -ref $unwarped -def $warp -jac $detjac
fslmaths $detjac -mul -1 -abs $detjac
#modulate by det jac (intensity correction)
fslmaths $unwarped -mul $detjac $unwarped_intcorr
if [ -n "$grad_dev_nii" ]
then
#gen grad_dev (for dwi)
calc_grad_perc_dev --fullwarp=$warp --out=$grad_dev_prefix
fslmerge -t $grad_dev ${grad_dev_prefix}_x ${grad_dev_prefix}_y ${grad_dev_prefix}_z
fslmaths $grad_dev -div 100 $grad_dev
fi
popd
#copy files to output paths
if [ -n "$unwarped_nii" ]
then
cp -v $scratch_dir/$unwarped $unwarped_nii
fi
if [ -n "$unwarped_intcorr_nii" ]
then
cp -v $scratch_dir/$unwarped_intcorr $unwarped_intcorr_nii
fi
if [ -n "$warp_nii" ]
then
cp -v $scratch_dir/$warp $warp_nii
fi
if [ -n "$detjac_nii" ]
then
cp -v $scratch_dir/$detjac $detjac_nii
fi
if [ -n "$grad_dev_nii" ]
then
cp -v $scratch_dir/$grad_dev $grad_dev_nii
fi
if [ -n "$unwarped_nii" ]
then
cp -v $scratch_dir/$unwarped $unwarped_nii
fi
if [ "$keep_scratch" = 0 ]
then
rm -f $scratch_dir/*
rmdir $scratch_dir
fi