-
Notifications
You must be signed in to change notification settings - Fork 3
/
dcm_anonymize.bash
executable file
·292 lines (238 loc) · 8.34 KB
/
dcm_anonymize.bash
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
#!/bin/bash
#
# dcm_anonymize.bash
#
# Copyright 2008 Rudolph Pienaar
# Massachusetts General Hospital
#
# GPL v2
#
# "include" the set of common script functions
source common.bash
declare -i Gi_verbose=0
declare -i Gb_useExpertOptions=0
declare -i Gb_useOverrideOut=0
declare -i Gb_forceStage=1
G_LOGDIR="-x"
G_OUTDIR="/space/kaos/5/users/dicom/postproc"
G_OUTPREFIX="-x"
G_DIRSUFFIX=""
G_OUTPREFIX="anon-"
G_DICOMINPUTDIR="-x"
G_UBUNTUJAVA=""
G_MATLAB="/usr/pubsw/packages/matlab/new/bin/matlab"
G_STAGES="1"
G_SYNOPSIS="
NAME
dcm_anonymize.bash
SYNOPSIS
dcm_anonymize.bash -D <dicomInputDir> \\
[-v <verbosity>] \\
[-O <experimentTopDir>] \\
[-R <DIRsuffix> \\
[-o <outputRunDir>] \\
[-p <outputPrefix>]
DESCRIPTION
'dcm_anonymize.bash' accepts an input directory containing DICOM
files and anonymizes the data. Output directory is the standard
postproc stream.
ARGUMENTS
-v <level> (optional)
Verbosity level.
-D <dicomInputDir>
The directory containing DICOM files for a particular study.
-O <experimentTopDir> (optional) (Default: $G_OUTDIR)
The root directory node that contains the outputs of a particular
registration run. Each run is stored in its own directory.
If this is specified on the command line, then output from the
anonymization will be written to this directory.
-o <outputRunDir>
A descriptive name prefixed to each volume generated by a registration
run.
-R <DIRsuffix> (Optional)
Appends <DIRsuffix> to the postproc/<MRID> as well as <logDir>. Since
multiple studies on the same patient can in principle have the same
MRID, interference can result in some of the log files and source
data. By adding this <DIRsuffix>, different analyses on the same MRID
can be cleanly separated.
-p <outputPrefix> (Optional)
If specified, prefix all generated output files with <outputPrefix>.
PRECONDITIONS
o nde
o MatLAB -- the actual anonymization is performed in MatLAB. This
script is merely a thin wrapper that drives the MatLAB process.
POSTCONDITIONS
o Output anonymized volumes are stored in:
<experimentTopDir>/<outputRunDir><DIRsuffix>
and each file is prefixed by 'anon-'. If an <experimentTopDir>
has been explicitly set, then output files are stored directly in
<experimentTopDir>.
HISTORY
24 March 2009
o Initial design and coding.
"
###\\\
# Globals are in capital letters. Immutable globals are prefixed by 'G'.
###///
G_SELF=`basename $0`
G_PID=$$
# Actions
A_fileCheck="checking for a required file dependency"
A_noDicomDir="checking on input DICOM directory"
A_noOutRunDir="checking on output run directory"
A_noDicomDirArg="checking on -d <dicomInputDir> argument"
A_noExpDir="checking on the output root directory"
A_metaLog="checking the meta log file"
A_dependencyStage="checking for a required dependency from an earlier stage"
A_stageRun="running a stage in the processing pipeline"
# Error messages
EM_fileCheck="it seems that a dependency is missing."
EM_noDicomDir="I couldn't access the input DICOM dir. Does it exist?"
EM_noOutRunDir="I couldn't access the output run dir. Does it exist?"
EM_noDicomDirArg="it seems as though you didn't specify a -D <dicomInputDir>."
EM_noExpDir="I couldn't find the <expDir>."
EM_metaLog="it seems as though this stage has already run.\n\tYou can force execution with a '-F'"
EM_dependencyStage="it seems that a stage dependency is missing."
EM_stageRun="I encountered an error processing this stage."
# Error codes
EC_fileCheck=1
EC_dependencyStage=2
EC_stageRun=30
EC_noDicomDir=50
EC_noOutRunDir=54
EC_noDicomDirArg=51
EC_noExpDir=23
EC_metaLog=80
# Defaults
D_whatever=
function matlab_scriptCreate
{
local SCRIPT=$1
cat > $SCRIPT <<-end-of-script
function [c] = dcm_anonymize_drive()
c = dcm_anonymize();
str_in = '$G_DICOMINPUTDIR';
str_out = '$OUTDIR';
c = set(c, 'dicomInputDir', str_in);
c = set(c, 'dicomOutputDir', str_out);
c = set(c, 'verbosity', 10);
c = set(c, 'b_newSeries', 0);
c = set(c, 'anonPrefix', '$G_OUTPREFIX');
c = set(c, 'keep', 'reset');
c = set(c, 'keep', 'PatientAge');
c = set(c, 'keep', 'PatientSex');
c = set(c, 'keep', 'StudyDescription');
c = set(c, 'keep', 'SeriesDescription');
c = set(c, 'keep', 'ProtocolName');
c = run(c);
end
end-of-script
}
###\\\
# Function definitions
###///
###\\\
# Process command options
###///
while getopts D:Ev:O:o:p:t:R: option ; do
case "$option"
in
D) G_DICOMINPUTDIR=$OPTARG ;;
E) Gb_useExpertOptions=1 ;;
v) let Gi_verbose=$OPTARG ;;
O) Gb_useOverrideOut=1
G_OUTDIR=$OPTARG ;;
o) Gb_useOverrideOut=1
G_OUTRUNDIR="$OPTARG" ;;
p) G_OUTPREFIX="$OPTARG" ;;
R) G_DIRSUFFIX=$OPTARG ;;
t) G_STAGES="$OPTARG" ;;
\?) synopsis_show
exit 0;;
esac
done
verbosity_check
topDir=$(pwd)
cprint "hostname" "[ $(hostname) ]"
## Check on script preconditions
REQUIREDFILES="matlab"
for file in $REQUIREDFILES ; do
printf "%40s" "Checking for $file"
file_checkOnPath $file || fatal fileCheck
done
## Check on input directory and files
statusPrint "Checking -D <dicomInputDir>"
if [[ "$G_DICOMINPUTDIR" == "-x" ]] ; then fatal noDicomDirArg ; fi
ret_check $?
statusPrint "Checking on <dicomInputDir>"
dirExist_check $G_DICOMINPUTDIR || fatal noDicomDir
cd $G_DICOMINPUTDIR >/dev/null
G_DICOMINPUTDIR=$(pwd)
cd $topDir
cd ${G_DICOMINPUTDIR}
statusPrint "Scanning <dicomInputDir>"
ret_check $?
SCANTABLE=$(dcm_mkIndx.bash -t '_' 2>/dev/null)
cd $topDir
if [[ $G_OUTPREFIX == "-x" ]] ; then
G_OUTPREFIX="$INPUTSCAN-To-$REFSCAN"
fi
statusPrint "Checking on <outputRunDir>"
if (( !Gb_useOverrideOut )) ; then
MRID=$(echo "$SCANTABLE" | grep ID | awk '{print $3}')
G_OUTRUNDIR=${MRID}${G_DIRSUFFIX}/anonymized
fi
dirExist_check ${G_OUTDIR}/$G_OUTRUNDIR "not found - creating" \
|| mkdir -p ${G_OUTDIR}/$G_OUTRUNDIR \
|| fatal noOutRunDir
cd ${G_OUTDIR}/$G_OUTRUNDIR >/dev/null
OUTDIR=$(pwd)
lprint "Anonymized directory"
rprint "[ $OUTDIR ]"
cd $topDir
lprint "Are we on ubuntu?"
let b_ubuntu=$(grep Ubuntu /etc/issue | wc -l)
if (( b_ubuntu )) ; then
G_UBUNTUJAVA="MATLAB_JAVA=/usr/lib/jvm/java-6-sun/jre"
fi
rprint "[ $b_ubuntu ]"
## Check which stages to process
statusPrint "Checking which stages to process"
barr_stage=([0]=0 [1]=0 [2]=0 [3]=0 [4]=0 [5]=0)
for i in $(seq 1 1) ; do
b_test=$(expr index $G_STAGES "$i")
if (( b_test )) ; then b_flag="1" ; else b_flag="0" ; fi
barr_stage[$i]=$b_flag
done
ret_check $?
G_LOGDIR=$OUTDIR
STAMPLOG=${G_LOGDIR}/${G_SELF}.log
stage_stamp "Init | ($(pwd)) $G_SELF $*" $STAMPLOG
STAGENUM="1"
STAGEPROC=matlab
STAGE=${STAGENUM}-${STAGEPROC}
STAGE1RELDIR=${G_OUTRUNDIR}
STAGE1FULLDIR=${OUTDIR}
statusPrint "Checking stage 1 output dir"
dirExist_check ${STAGE1FULLDIR} "not found - creating" \
|| mkdir -p ${STAGE1FULLDIR} \
|| fatal noOutRunDir
if (( ${barr_stage[1]} )) ; then
cd $STAGE1FULLDIR
statusPrint "$(date) | Processing STAGE 1 - anonymizing DICOM dir | START" "\n"
statusPrint "creating MatLAB script file..." "\n"
MATLABSCRIPT=dcm_anonymize_drive.m
matlab_scriptCreate $MATLABSCRIPT
statusPrint "running MatLAB script file..." "\n"
STAGECMD="eval \"$G_MATLAB -nodesktop -nosplash \
-r \\\"c = $(basename $MATLABSCRIPT .m)(); exit\\\"\""
stage_run "$STAGE" "$STAGECMD" \
"${STAGE1FULLDIR}/${STAGEPROC}.std" \
"${STAGE1FULLDIR}/${STAGEPROC}.err" \
"SILENT" \
|| fatal stageRun
statusPrint "$(date) | Processing STAGE 1 - anonymizing DICOM dir | END" "\n"
fi
cd $topDir
verbosity_check
shut_down 0