-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDVARS.sh
executable file
·169 lines (137 loc) · 4.37 KB
/
DVARS.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
#!/bin/bash
#
# Script: DVARS.sh
# Purpose: Create standardized version of DVARS
# Author: T. Nichols
# Version: http://github.com/nicholst/DVARS/tree/$Format:%h$
# $Format:%ci$
#
###############################################################################
#
# Environment set up
#
###############################################################################
shopt -s nullglob # No-match globbing expands to null
TmpDir=/tmp
Tmp=$TmpDir/`basename $0`-${$}-
trap CleanUp INT
###############################################################################
#
# Functions
#
###############################################################################
Usage() {
cat <<EOF
Usage: `basename $0` [options] fMRI_4Dtimeseries DVARSout
`basename $0` [options] fMRI_1 fMRI_2 ... fMRI_T DVARSout
Options
-all Produce 2 additional versions of DVARS.
Creates standardized version of DVARS, normalizing according to the expected
value of DVARS under an AR1 model. DVARSout, consists of plain text file with
this normalized version of DVARS, scaled so that it is approximately 1 if
there are no artifacts.
With the -all option, 2 additional columns are added to DVARSout. The 2nd column
is the raw DVARS with no scaling (precisely the root mean square (RMS) of the temporal
difference). The 3rd is the precision-normalized DVARS: Before taking the RMS,
temporal difference images are standardized voxel-wise giving a more precisely
normalized DVARS measure. A side effect, however, is that high-variance parts of
the image are down-weighted relative to low-variance areas.
_________________________________________________________________________
\$Id$
EOF
exit
}
CleanUp () {
/bin/rm -f ${Tmp}*
exit 0
}
###############################################################################
#
# Parse arguments
#
###############################################################################
while (( $# > 1 )) ; do
case "$1" in
"-help")
Usage
;;
"-all")
shift
AllVers="$1"
;;
-*)
echo "ERROR: Unknown option '$1'"
exit 1
break
;;
*)
break
;;
esac
done
Tmp=$TmpDir/DVARS-${$}-
if (( $# < 2 )) ; then
Usage
elif (( $# == 2 )) ; then
FUNC="$1"
OUT="$2"
else
((i=0))
Imgs=()
while (( $# >= 2 )) ; do
Imgs[i]="$1"
((i++))
shift
done
FUNC=$Tmp-1
fslmerge -t $FUNC "${Imgs[@]}"
OUT="$1"
fi
###############################################################################
#
# Script Body
#
###############################################################################
echo -n "."
# Find mean over time
fslmaths "$FUNC" -Tmean $Tmp-Mean
# Find the brain
bet $Tmp-Mean $Tmp-MeanBrain
# Compute robust estimate of standard deviation
fslmaths "$FUNC" -Tperc 25 $Tmp-lq
fslmaths "$FUNC" -Tperc 75 $Tmp-uq
fslmaths $Tmp-uq -sub $Tmp-lq -div 1.349 $Tmp-SD -odt float
# Compute (non-robust) estimate of lag-1 autocorrelation
fslmaths "$FUNC" -sub $Tmp-Mean -Tar1 $Tmp-AR1 -odt float
# Compute (predicted) standard deviation of temporal difference time series
fslmaths $Tmp-AR1 -mul -1 -add 1 -mul 2 -sqrt -mul $Tmp-SD $Tmp-DiffSDhat
# Save mean value
DiffSDmean=$(fslstats $Tmp-DiffSDhat -k $Tmp-MeanBrain -M)
echo -n "."
# Compute temporal difference squared time series
nVol=$(fslnvols "$FUNC")
fslroi "$FUNC" $Tmp-FUNC0 0 $((nVol-1))
fslroi "$FUNC" $Tmp-FUNC1 1 $nVol
fslmaths $Tmp-FUNC0 -sub $Tmp-FUNC1 -sqr $Tmp-DiffSq -odt float
echo -n "."
# Compute DVARS, no standization
fslstats -t $Tmp-DiffSq -k $Tmp-MeanBrain -M > $Tmp-DiffVar.dat
if [ "$AllVers" = "" ] ; then
# Standardized
awk '{printf("%g\n",sqrt($1)/'"$DiffSDmean"')}' $Tmp-DiffVar.dat > "$OUT"
else
# Compute DVARS, based on voxel-wise standardized image
fslmaths $Tmp-FUNC0 -sub $Tmp-FUNC1 -div $Tmp-DiffSDhat -sqr $Tmp-DiffSqVxStdz
fslstats -t $Tmp-DiffSqVxStdz -k $Tmp-MeanBrain -M | awk '{print sqrt($1)}' > $Tmp-DiffVxStdzSD.dat
# Sew it all together
awk '{printf("%g\t%g\n",sqrt($1)/'"$DiffSDmean"',sqrt($1))}' $Tmp-DiffVar.dat > $Tmp-DVARS
paste $Tmp-DVARS $Tmp-DiffVxStdzSD.dat > "$OUT"
fi
echo -n "."
echo "."
###############################################################################
#
# Exit & Clean up
#
###############################################################################
CleanUp