-
Notifications
You must be signed in to change notification settings - Fork 2
/
comparison_with_other_methods_sc.sh
executable file
·280 lines (244 loc) · 10.6 KB
/
comparison_with_other_methods_sc.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
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
#!/bin/bash
#
# Compare our nnUNet model with other methods (sct_propseg, sct_deepseg_sc 2d, sct_deepseg_sc 3d,
# MONAI contrast-agnostic (part of SCT v6.2)) on sci-zurich and sci-colorado datasets
#
# Note: subjects from both datasets have to be located in the same BIDS-like folder, example:
# ├── derivatives
# │ └── labels
# │ ├── sub-5416 # sci-colorado subject
# │ │ └── anat
# │ │ ├── sub-5416_T2w_lesion-manual.json
# │ │ ├── sub-5416_T2w_lesion-manual.nii.gz
# │ │ ├── sub-5416_T2w_seg-manual.json
# │ │ └── sub-5416_T2w_seg-manual.nii.gz
# │ └── sub-zh01 # sci-zurich subject
# │ └── ses-01
# │ └── anat
# │ ├── sub-zh01_ses-01_acq-sag_T2w_lesion-manual.json
# │ ├── sub-zh01_ses-01_acq-sag_T2w_lesion-manual.nii.gz
# │ ├── sub-zh01_ses-01_acq-sag_T2w_seg-manual.json
# │ └── sub-zh01_ses-01_acq-sag_T2w_seg-manual.nii.gz
# ├── sub-5416 # sci-colorado subject
# │ └── anat
# │ ├── sub-5416_T2w.json
# │ └── sub-5416_T2w.nii.gz
# └── sub-zh01 # sci-zurich subject
# └── ses-01
# └── anat
# ├── sub-zh01_ses-01_acq-sag_T2w.json
# └── sub-zh01_ses-01_acq-sag_T2w.nii.gz
#
# Note: conda environment with nnUNetV2 is required to run this script.
# For details how to install nnUNetV2, see:
# https://github.com/ivadomed/utilities/blob/main/quick_start_guides/nnU-Net_quick_start_guide.md#installation
#
# Usage:
# sct_run_batch -config config.json
#
# Example of config.json:
# {
# "path_data" : "<PATH_TO_DATASET>",
# "path_output" : "<PATH_TO_DATASET>_2023-08-18",
# "script" : "<PATH_TO_REPO>/model_seg_sci/baselines/comparison_with_other_methods.sh",
# "jobs" : 8,
# "script_args" : "<PATH_TO_REPO>/model_seg_sci/packaging/run_inference_single_subject.py <PATH_TO_MODEL>/sci-multisite-model"
# }
#
# The following global variables are retrieved from the caller sct_run_batch
# but could be overwritten by uncommenting the lines below:
# PATH_DATA_PROCESSED="~/data_processed"
# PATH_RESULTS="~/results"
# PATH_LOG="~/log"
# PATH_QC="~/qc"
#
# Author: Jan Valosek
#
# Uncomment for full verbose
set -x
# Immediately exit if error
set -e -o pipefail
# Exit if user presses CTRL+C (Linux) or CMD+C (OSX)
trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT
# Print retrieved variables from the sct_run_batch script to the log (to allow easier debug)
echo "Retrieved variables from from the caller sct_run_batch:"
echo "PATH_DATA: ${PATH_DATA}"
echo "PATH_DATA_PROCESSED: ${PATH_DATA_PROCESSED}"
echo "PATH_RESULTS: ${PATH_RESULTS}"
echo "PATH_LOG: ${PATH_LOG}"
echo "PATH_QC: ${PATH_QC}"
SUBJECT=$1
PATH_NNUNET_SCRIPT=$2 # path to the nnUNet SCIseg run_inference_single_subject.py
PATH_NNUNET_MODEL=$3 # path to the nnUNet SCIseg model
echo "SUBJECT: ${SUBJECT}"
echo "PATH_NNUNET_SCRIPT: ${PATH_NNUNET_SCRIPT}"
echo "PATH_NNUNET_MODEL: ${PATH_NNUNET_MODEL}"
# ------------------------------------------------------------------------------
# CONVENIENCE FUNCTIONS
# ------------------------------------------------------------------------------
# Get ANIMA binaries path
anima_binaries_path=$(grep "^anima = " ~/.anima/config.txt | sed "s/.* = //" | sed 's/\/$//')
# Segment spinal cord using methods available in SCT (sct_deepseg_sc or sct_propseg)
segment_sc() {
local file="$1"
local contrast="$2"
local method="$3" # deepseg or propseg
local kernel="$4" # 2d or 3d; only relevant for deepseg
# Segment spinal cord
if [[ $method == 'deepseg' ]];then
FILESEG="${file}_seg_${method}_${kernel}"
# Get the start time
start_time=$(date +%s)
# Run SC segmentation
sct_deepseg_sc -i ${file}.nii.gz -o ${FILESEG}.nii.gz -c ${contrast} -kernel ${kernel} -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Get the end time
end_time=$(date +%s)
# Calculate the time difference
execution_time=$(python3 -c "print($end_time - $start_time)")
echo "${FILESEG},${execution_time}" >> ${PATH_RESULTS}/execution_time.csv
# Compute ANIMA segmentation performance metrics
compute_anima_metrics ${FILESEG} ${file}_seg-manual.nii.gz
elif [[ $method == 'propseg' ]]; then
FILESEG="${file}_seg_${method}"
# Get the start time
start_time=$(date +%s)
# Run SC segmentation
sct_propseg -i ${file}.nii.gz -o ${FILESEG}.nii.gz -c ${contrast} -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Get the end time
end_time=$(date +%s)
# Calculate the time difference
execution_time=$(python3 -c "print($end_time - $start_time)")
echo "${FILESEG},${execution_time}" >> ${PATH_RESULTS}/execution_time.csv
# Remove centerline (we don't need it)
rm ${file}_centerline.nii.gz
# Compute ANIMA segmentation performance metrics
compute_anima_metrics ${FILESEG} ${file}_seg-manual.nii.gz
fi
}
# Segment spinal cord using our SCIseg nnUNet model
segment_sc_nnUNet(){
local file="$1"
local kernel="$2" # 2d or 3d
FILESEG="${file}_seg_nnunet_${kernel}"
# Get the start time
start_time=$(date +%s)
# Run SC segmentation
python ${PATH_NNUNET_SCRIPT} -i ${file}.nii.gz -o ${FILESEG}.nii.gz -path-model ${PATH_NNUNET_MODEL}/nnUNetTrainerDiceCELoss_noSmooth__nnUNetPlans__${kernel} -pred-type sc
# Get the end time
end_time=$(date +%s)
# Calculate the time difference
execution_time=$(python3 -c "print($end_time - $start_time)")
echo "${FILESEG},${execution_time}" >> ${PATH_RESULTS}/execution_time.csv
# Generate QC report
sct_qc -i ${file}.nii.gz -s ${FILESEG}.nii.gz -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute ANIMA segmentation performance metrics
compute_anima_metrics ${FILESEG} ${file}_seg-manual.nii.gz
}
# Segment spinal cord using the MONAI contrast-agnostic model (part of SCT v6.2)
segment_sc_MONAI(){
local file="$1"
FILESEG="${file}_seg_monai"
# Get the start time
start_time=$(date +%s)
# Run SC segmentation using SCT v6.2
sct_deepseg -i ${file}.nii.gz -o ${FILESEG}.nii.gz -task seg_sc_contrast_agnostic
# Get the end time
end_time=$(date +%s)
# Calculate the time difference
execution_time=$(python3 -c "print($end_time - $start_time)")
echo "${FILESEG},${execution_time}" >> ${PATH_RESULTS}/execution_time.csv
# Generate QC report
sct_qc -i ${file}.nii.gz -s ${FILESEG}.nii.gz -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute ANIMA segmentation performance metrics
compute_anima_metrics ${FILESEG} ${file}_seg-manual.nii.gz
}
# Compute ANIMA segmentation performance metrics
compute_anima_metrics(){
# We have to copy qform matrix from seg-manual to the automatically generated segmentation to avoid ITK error:
# "Description: ITK ERROR: SegmentationMeasuresImageFilter(): Inputs do not occupy the same physical space!"
# Related to the following issue : https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/4135
sct_image -i ${file}_seg-manual.nii.gz -copy-header ${FILESEG}.nii.gz -o ${FILESEG}_updated_header.nii.gz
# Compute ANIMA segmentation performance metrics
# -i : input segmentation
# -r : GT segmentation
# -o : output file
# -d : surface distances evaluation
# -s : compute metrics to evaluate a segmentation
# -X : stores results into a xml file.
${anima_binaries_path}/animaSegPerfAnalyzer -i ${FILESEG}_updated_header.nii.gz -r ${file}_seg-manual.nii.gz -o ${PATH_RESULTS}/${FILESEG} -d -s -X
rm ${FILESEG}_updated_header.nii.gz
}
# Copy GT spinal cord segmentation (located under derivatives/labels)
copy_gt(){
local file="$1"
# Construct file name to GT segmentation located under derivatives/labels
FILESEGMANUAL="${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${file}_seg-manual.nii.gz"
echo ""
echo "Looking for manual segmentation: $FILESEGMANUAL"
if [[ -e $FILESEGMANUAL ]]; then
echo "Found! Copying ..."
rsync -avzh $FILESEGMANUAL ${file}_seg-manual.nii.gz
else
echo "File ${FILESEGMANUAL}.nii.gz does not exist" >> ${PATH_LOG}/missing_files.log
echo "ERROR: Manual GT segmentation ${FILESEGMANUAL}.nii.gz does not exist. Exiting."
exit 1
fi
}
# ------------------------------------------------------------------------------
# SCRIPT STARTS HERE
# ------------------------------------------------------------------------------
# get starting time:
start=`date +%s`
# Display useful info for the log, such as SCT version, RAM and CPU cores available
sct_check_dependencies -short
# Go to folder where data will be copied and processed
cd $PATH_DATA_PROCESSED
# Copy source T2w images
# Note: we use '/./' in order to include the sub-folder 'ses-0X'
# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/'
if [[ $SUBJECT =~ "sub-zh" ]]; then
# for sci-zurich, copy only sagittal T2w to save space
rsync -Ravzh ${PATH_DATA}/./${SUBJECT}/anat/${SUBJECT//[\/]/_}_*sag_T2w.* .
else
rsync -Ravzh ${PATH_DATA}/./${SUBJECT}/anat/${SUBJECT//[\/]/_}_*T2w.* .
fi
# Go to subject folder for source images
cd ${SUBJECT}/anat
# ------------------------------------------------------------------------------
# T2w
# ------------------------------------------------------------------------------
# sci-zurich
if [[ $SUBJECT =~ "sub-zh" ]]; then
# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/'
file_t2="${SUBJECT//[\/]/_}"_acq-sag_T2w
# sci-colorado
else
file_t2="${SUBJECT}"_T2w
fi
# Copy GT spinal cord segmentation
copy_gt "${file_t2}"
# Check if file_t2 exists
if [[ ! -e ${file_t2}.nii.gz ]]; then
echo "File ${file_t2}.nii.gz does not exist" >> ${PATH_LOG}/missing_files.log
echo "ERROR: File ${file_t2}.nii.gz does not exist. Exiting."
exit 1
fi
# Segment SC using different methods and compute ANIMA segmentation performance metrics
segment_sc "${file_t2}" 't2' 'deepseg' '2d'
segment_sc "${file_t2}" 't2' 'deepseg' '3d'
segment_sc "${file_t2}" 't2' 'propseg'
segment_sc_nnUNet "${file_t2}" '2d'
segment_sc_nnUNet "${file_t2}" '3d_fullres'
segment_sc_MONAI "${file_t2}"
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
# Display results (to easily compare integrity across SCT versions)
end=`date +%s`
runtime=$((end-start))
echo
echo "~~~"
echo "SCT version: `sct_version`"
echo "Ran on: `uname -nsr`"
echo "Duration: $(($runtime / 3600))hrs $((($runtime / 60) % 60))min $(($runtime % 60))sec"
echo "~~~"