forked from asoroosh/DVARS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSEImageDiag.sh
214 lines (171 loc) · 7.39 KB
/
DSEImageDiag.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
#!/bin/bash -x
#
# Script: SvarDiag.sh
# Purpose: Produce aux images to help diagnose the SVAR spikes
# Author: T. Nichols & S. Afyouni
# Version: $Id$
#
###############################################################################
#
# Environment set up
#
###############################################################################
shopt -s nullglob # No-match globbing expands to null
TmpDir=/tmp
Tmp=$TmpDir/`basename $0`-${$}-
trap CleanUp INT
TMPOUTPUTTYPE="$FSLOUTPUTTYPE"
export FSLOUTPUTTYPE="NIFTI_GZ"
# you might wanna change this to NIFTI, but note on 7T resting-state data, a single file will exceed 5GB!
###############################################################################
#
# Functions
#
###############################################################################
Usage() {
cat <<EOF
Usage: `basename $0` [options] fMRI_4Dtimeseries OUT 4DFlag
==INPUTS:
fMRI_4Dtimeseries : 4D Resting-state image
OUT : Directory and a prefix for the outputs, e.g. path/to/dir/SUB001
4DFlag [optional] : if set to 1, both 3D and 4D variability images are saved, if set to 0 then only 3D images are saved [default 1].
==OUTPUTS:
4D images:
Svar, Dvar & Avar. pSvar & pDvar. DeltapDvar & DeltapSvar
3D images:
Svar, Dvar & Avar. pSvar & pDvar. DeltapDvar & DeltapSvar
TXT:
mean Svar, mean Dvar & mean Avar. mean pSvar & mean pDvar. mean DeltapSvar & mean DeltapDvar
==DEPENDENCIES:
FSL should have been already installed.
==EXAMPLES:
sh DSEImageDiag.sh ${WhereFrom}/rfMRI_REST1_7T_PA.nii.gz ${WhereTo}/${SubID}/${SubID} 0
==REFERENCE:
Afyouni, Soroosh, and Thomas E. Nichols. "Insight and Inference for DVARS." NeuroImage (2018).
SA & TEN, Ox, 2018
_________________________________________________________________________
\$Id$
EOF
exit
}
CleanUp () {
/bin/rm -f ${Tmp}*
exit 0
}
###############################################################################
#
# Parse arguments
#
###############################################################################
while (( $# > 1 )) ; do
case "$1" in
"-help")
Usage
;;
-*)
echo "ERROR: Unknown option '$1'"
exit 1
break
;;
*)
break
;;
esac
done
if (( $# < 2 )) ; then
Usage
fi
FUNC="$1"
OUT="$2"
if [ -z "$3" ];
then
echo "4D flag is unset so 4D images will be saved..."
need4D=1
else
need4D="$3"
echo "4D flag is set to $need4D so 4D images will not be saved."
fi
#Create a new directory, if doesn't already exists, as variable OUT
Dir2Save=`dirname "$OUT"`
PreFix=`basename "$OUT"`
mkdir -p $Dir2Save/DSE
mkdir -p $Dir2Save/pDSE
mkdir -p $Dir2Save/DeltapDSE
echo "Created: $Dir2Save"
###############################################################################
#
# Script Body
#
###############################################################################
Nvol=$(fslnvols "$FUNC")
echo "The input image has $Nvol volumes."
# Find mean over time
fslmaths "$FUNC" -Tmean $Tmp-Mean
echo $Tmp-Mean
# Form a mask, later will be used to get the Dvar and Svar time series
fslmaths $Tmp-Mean -bin $Tmp-Mean-mask
# Demean
#fslmaths "$FUNC" -sub $Tmp-Mean -mas $Tmp-MeanBrain $Tmp-Demean
fslmaths "$FUNC" -sub $Tmp-Mean $Tmp-Demean
#std
fslmaths $Tmp-Demean -Tstd $Dir2Save/$PreFix-DemeanStd
fslroi $Tmp-Demean $Tmp-BND0 0 $((Nvol - 1))
fslroi $Tmp-Demean $Tmp-BND1 1 "$Nvol"
#Sanity check
#fslinfo $Tmp-BND0
#fslinfo $Tmp-BND1
echo "Generating Avar, Svar and Dvar 4D data + non-normalised time series."
fslmaths $Tmp-Demean -sqr $Dir2Save/DSE/$PreFix-Avar
fslmaths $Tmp-BND0 -add $Tmp-BND1 -div 2 -sqr $Dir2Save/DSE/$PreFix-Svar
fslmaths $Tmp-BND0 -sub $Tmp-BND1 -div 2 -sqr $Dir2Save/DSE/$PreFix-Dvar
fslmeants -i $Dir2Save/DSE/$PreFix-Avar -m $Tmp-Mean-mask -o $Dir2Save/DSE/$PreFix-Avar-meants.txt
fslmeants -i $Dir2Save/DSE/$PreFix-Svar -m $Tmp-Mean-mask -o $Dir2Save/DSE/$PreFix-Svar-meants.txt
fslmeants -i $Dir2Save/DSE/$PreFix-Dvar -m $Tmp-Mean-mask -o $Dir2Save/DSE/$PreFix-Dvar-meants.txt
awk '{ total += $1; count++ } END { print total/count }' $Dir2Save/DSE/$PreFix-Avar-meants.txt > $Dir2Save/DSE/$PreFix-Whole-DSE.txt
awk '{ total += $1; count++ } END { print total/count }' $Dir2Save/DSE/$PreFix-Svar-meants.txt >> $Dir2Save/DSE/$PreFix-Whole-DSE.txt
awk '{ total += $1; count++ } END { print total/count }' $Dir2Save/DSE/$PreFix-Dvar-meants.txt >> $Dir2Save/DSE/$PreFix-Whole-DSE.txt
echo "Generating Avar 3D image..."
fslmaths $Dir2Save/DSE/$PreFix-Avar -Tmean $Dir2Save/DSE/$PreFix-mAvar
#Generate the median images for calculating the DeltapDvar and DeltapSvar...
fslmaths $Dir2Save/DSE/$PreFix-Svar -Tmedian $Dir2Save/$PreFix-mdSvar
fslmaths $Dir2Save/DSE/$PreFix-Dvar -Tmedian $Dir2Save/$PreFix-mdDvar
echo "Generating %Svar and %Dvar 4D images..."
#Normalise Dvar and Svar by Avar i.e. %Svar and %Dvar
fslmaths $Dir2Save/DSE/$PreFix-Svar -div $Dir2Save/DSE/$PreFix-mAvar $Dir2Save/pDSE/$PreFix-pSvar
fslmaths $Dir2Save/DSE/$PreFix-Dvar -div $Dir2Save/DSE/$PreFix-mAvar $Dir2Save/pDSE/$PreFix-pDvar
echo "Generating Delta%Svar and Delta%Dvar 4D images..."
fslmaths $Dir2Save/DSE/$PreFix-Svar -sub $Dir2Save/$PreFix-mdSvar -div $Dir2Save/DSE/$PreFix-mAvar $Dir2Save/DeltapDSE/$PreFix-DeltapSvar
fslmaths $Dir2Save/DSE/$PreFix-Dvar -sub $Dir2Save/$PreFix-mdDvar -div $Dir2Save/DSE/$PreFix-mAvar $Dir2Save/DeltapDSE/$PreFix-DeltapDvar
#remove the median images
rm $Dir2Save/$PreFix-mdSvar.nii.gz $Dir2Save/$PreFix-mdDvar.nii.gz
echo "Generating %Svar and %Dvar time series..."
fslmeants -i $Dir2Save/pDSE/$PreFix-pSvar -m $Tmp-Mean-mask -o $Dir2Save/pDSE/$PreFix-pSvar-meants.txt
fslmeants -i $Dir2Save/pDSE/$PreFix-pDvar -m $Tmp-Mean-mask -o $Dir2Save/pDSE/$PreFix-pDvar-meants.txt
awk '{ total += $1; count++ } END { print total/count }' $Dir2Save/pDSE/$PreFix-pSvar-meants.txt > $Dir2Save/pDSE/$PreFix-Whole-p-DSE.txt
awk '{ total += $1; count++ } END { print total/count }' $Dir2Save/pDSE/$PreFix-pDvar-meants.txt >> $Dir2Save/pDSE/$PreFix-Whole-p-DSE.txt
echo "Generating Delta%Svar and Delta%Dvar time series..."
fslmeants -i $Dir2Save/DeltapDSE/$PreFix-DeltapSvar -m $Tmp-Mean-mask -o $Dir2Save/DeltapDSE/$PreFix-DeltapSvar-meants.txt
fslmeants -i $Dir2Save/DeltapDSE/$PreFix-DeltapDvar -m $Tmp-Mean-mask -o $Dir2Save/DeltapDSE/$PreFix-DeltapDvar-meants.txt
echo "Generating %Svar and %Dvar 3D images..."
fslmaths $Dir2Save/pDSE/$PreFix-pSvar -Tmean $Dir2Save/pDSE/$PreFix-mpSvar
fslmaths $Dir2Save/pDSE/$PreFix-pDvar -Tmean $Dir2Save/pDSE/$PreFix-mpDvar
echo "Generating Delta%Svar and Delta%Dvar 3D images..."
fslmaths $Dir2Save/DeltapDSE/$PreFix-DeltapSvar -Tmean $Dir2Save/DeltapDSE/$PreFix-mDeltapSvar
fslmaths $Dir2Save/DeltapDSE/$PreFix-DeltapDvar -Tmean $Dir2Save/DeltapDSE/$PreFix-mDeltapDvar
if [ $need4D == 0 ]
then
echo "Now deleting 4D images to free up some space..."
# remove the raw Svar and Dvar
rm $Dir2Save/DSE/$PreFix-Dvar.nii.gz $Dir2Save/DSE/$PreFix-Svar.nii.gz $Dir2Save/DSE/$PreFix-Avar.nii.gz
# remove the pSvar and pDvar images
rm $Dir2Save/pDSE/$PreFix-pDvar.nii.gz $Dir2Save/pDSE/$PreFix-pSvar.nii.gz
# remove DeltapSvar and DeltapDvar
rm $Dir2Save/DeltapDSE/$PreFix-DeltapDvar.nii.gz $Dir2Save/DeltapDSE/$PreFix-DeltapSvar.nii.gz
fi
echo "Done!"
###############################################################################
#
# Exit & Clean up
#
###############################################################################
CleanUp