sbollmann commited on
Commit
21552f3
·
verified ·
1 Parent(s): 18767b9

Upload test_data/batch_processing.sh

Browse files
Files changed (1) hide show
  1. test_data/batch_processing.sh +276 -0
test_data/batch_processing.sh ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ #
3
+ # Example of commands to process multi-parametric data of the spinal cord.
4
+ #
5
+ # Please note that this batch script has a lot of redundancy and should not
6
+ # be used as a pipeline for regular processing. For example, there is no need
7
+ # to process both t1 and t2 to extract CSA values.
8
+ #
9
+ # For information about acquisition parameters, see: https://osf.io/wkdym/
10
+ # N.B. The parameters are set for these type of data. With your data, parameters
11
+ # might be slightly different.
12
+ #
13
+ # Usage:
14
+ #
15
+ # [option] ./batch_processing.sh
16
+ #
17
+ # Prevent (re-)downloading sct_example_data:
18
+ # SCT_BP_DOWNLOAD=0 ./batch_processing.sh
19
+ #
20
+ # Specify quality control (QC) folder (Default is ~/qc_batch_processing):
21
+ # SCT_BP_QC_FOLDER=/user/toto/my_qc_folder ./batch_processing.sh
22
+
23
+ # Abort on error
24
+ set -ve
25
+
26
+ # For full verbose, uncomment the next line
27
+ # set -x
28
+
29
+ # Fetch OS type
30
+ if uname -a | grep -i darwin > /dev/null 2>&1; then
31
+ # OSX
32
+ open_command="open"
33
+ elif uname -a | grep -i linux > /dev/null 2>&1; then
34
+ # Linux
35
+ open_command="xdg-open"
36
+ fi
37
+
38
+ # Check if users wants to use his own data
39
+ if [[ -z "$SCT_BP_DOWNLOAD" ]]; then
40
+ SCT_BP_DOWNLOAD=1
41
+ fi
42
+
43
+ # QC folder
44
+ if [[ -z "$SCT_BP_QC_FOLDER" ]]; then
45
+ SCT_BP_QC_FOLDER=$(pwd)/"qc_example_data"
46
+ fi
47
+
48
+ # Remove QC folder
49
+ if [ -z "$SCT_BP_NO_REMOVE_QC" ] && [ -d "$SCT_BP_QC_FOLDER" ]; then
50
+ echo "Removing $SCT_BP_QC_FOLDER folder."
51
+ rm -rf "$SCT_BP_QC_FOLDER"
52
+ fi
53
+
54
+ # get starting time:
55
+ start=$(date +%s)
56
+
57
+ # download example data
58
+ if [[ "$SCT_BP_DOWNLOAD" == "1" ]]; then
59
+ sct_download_data -d sct_example_data
60
+ fi
61
+
62
+ # Enter our data directory
63
+ cd "$SCT_DIR/data/sct_example_data"
64
+
65
+ # install the sct_deepseg model this script will use later
66
+ sct_deepseg spinalcord -install
67
+
68
+ # t2
69
+ # ===========================================================================================
70
+ cd t2
71
+ # Segment spinal cord
72
+ sct_deepseg spinalcord -i t2.nii.gz -qc "$SCT_BP_QC_FOLDER"
73
+ # Tips: If you are not satisfied with the results you can try with another algorithm:
74
+ # sct_propseg -i t2.nii.gz -c t2 -qc "$SCT_BP_QC_FOLDER"
75
+ # Fit binarized centerline from SC seg (default settings)
76
+ sct_get_centerline -i t2_seg.nii.gz -method fitseg -qc "$SCT_BP_QC_FOLDER"
77
+ # Fit soft centerline from SC seg
78
+ sct_get_centerline -i t2_seg.nii.gz -method fitseg -centerline-soft 1 -o t2_seg_centerline_soft.nii.gz -qc "$SCT_BP_QC_FOLDER"
79
+ # Vertebral labeling
80
+ # Tips: for manual initialization of labeling by clicking at disc C2-C3, use flag -initc2
81
+ sct_label_vertebrae -i t2.nii.gz -s t2_seg.nii.gz -c t2 -qc "$SCT_BP_QC_FOLDER"
82
+ # Create labels at in the cord at C2 and C5 mid-vertebral levels
83
+ sct_label_utils -i t2_seg_labeled.nii.gz -vert-body 2,5 -o labels_vert.nii.gz
84
+ # Tips: you can also create labels manually using:
85
+ # sct_label_utils -i t2.nii.gz -create-viewer 2,5 -o labels_vert.nii.gz
86
+ # Register to template
87
+ sct_register_to_template -i t2.nii.gz -s t2_seg.nii.gz -l labels_vert.nii.gz -c t2 -qc "$SCT_BP_QC_FOLDER"
88
+ # Tips: If you are not satisfied with the results, you can tweak registration parameters.
89
+ # For example here, we would like to take into account the rotation of the cord, as well as
90
+ # adding a 3rd registration step that uses the image intensity (not only cord segmentations).
91
+ # so we could do something like this:
92
+ # sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_t2s.nii.gz -iseg "$SCT_DIR/data/PAM50/template/PAM50_cord.nii.gz -d t2s.nii.gz -dseg t2s_seg.nii.gz -param step=1,type=seg,algo=slicereg,smooth=3:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3 -initwarp ../t2/warp_template2anat.nii.gz
93
+ # Warp template without the white matter atlas (we don't need it at this point)
94
+ sct_warp_template -d t2.nii.gz -w warp_template2anat.nii.gz -a 0
95
+ # Compute cross-sectional area (and other morphometry measures) for each slice
96
+ sct_process_segmentation -i t2_seg.nii.gz
97
+ # Compute cross-sectional area and average between C2 and C3 levels
98
+ sct_process_segmentation -i t2_seg.nii.gz -vert 2:3 -o csa_c2c3.csv
99
+ # Compute cross-sectionnal area based on distance from pontomedullary junction (PMJ)
100
+ # Detect PMJ
101
+ sct_detect_pmj -i t2.nii.gz -c t2 -qc "$SCT_BP_QC_FOLDER"
102
+ # Compute cross-section area at 60 mm from PMJ averaged on a 30 mm extent
103
+ sct_process_segmentation -i t2_seg.nii.gz -pmj t2_pmj.nii.gz -pmj-distance 60 -pmj-extent 30 -qc "$SCT_BP_QC_FOLDER" -qc-image t2.nii.gz -o csa_pmj.csv
104
+ # Compute morphometrics in PAM50 anatomical dimensions
105
+ sct_process_segmentation -i t2_seg.nii.gz -vertfile t2_seg_labeled.nii.gz -perslice 1 -normalize-PAM50 1 -o csa_pam50.csv
106
+ # Go back to root folder
107
+ cd ..
108
+
109
+
110
+ # t2s (stands for t2-star)
111
+ # ===========================================================================================
112
+ cd t2s
113
+ # Spinal cord segmentation
114
+ sct_deepseg spinalcord -i t2s.nii.gz -qc "$SCT_BP_QC_FOLDER"
115
+ # Segment gray matter
116
+ sct_deepseg_gm -i t2s.nii.gz -qc "$SCT_BP_QC_FOLDER"
117
+ # Register template->t2s (using warping field generated from template<->t2 registration)
118
+ sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_t2s.nii.gz" -iseg "$SCT_DIR/data/PAM50/template/PAM50_cord.nii.gz" -d t2s.nii.gz -dseg t2s_seg.nii.gz -param step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3:step=3,type=im,algo=syn,slicewise=1,iter=1,metric=CC -initwarp ../t2/warp_template2anat.nii.gz -initwarpinv ../t2/warp_anat2template.nii.gz -owarp warp_template2t2s.nii.gz -owarpinv warp_t2s2template.nii.gz
119
+ # Warp template
120
+ sct_warp_template -d t2s.nii.gz -w warp_template2t2s.nii.gz
121
+ # Subtract GM segmentation from cord segmentation to obtain WM segmentation
122
+ sct_maths -i t2s_seg.nii.gz -sub t2s_gmseg.nii.gz -o t2s_wmseg.nii.gz
123
+ # Compute cross-sectional area of the gray and white matter between C2 and C5
124
+ sct_process_segmentation -i t2s_wmseg.nii.gz -vert 2:5 -perlevel 1 -o csa_wm.csv -angle-corr-centerline t2s_seg.nii.gz
125
+ sct_process_segmentation -i t2s_gmseg.nii.gz -vert 2:5 -perlevel 1 -o csa_gm.csv -angle-corr-centerline t2s_seg.nii.gz
126
+ # OPTIONAL: Update template registration using information from gray matter segmentation
127
+ # # <<<
128
+ # # Register WM/GM template to WM/GM seg
129
+ # sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_wm.nii.gz" -d t2s_wmseg.nii.gz -dseg t2s_seg.nii.gz -param step=1,type=im,algo=syn,slicewise=1,iter=5 -initwarp warp_template2t2s.nii.gz -initwarpinv warp_t2s2template.nii.gz -qc "$SCT_BP_QC_FOLDER" -owarp warp_template2t2s.nii.gz -owarpinv warp_t2s2template.nii.gz
130
+ # # Warp template (this time corrected for internal structure)
131
+ # sct_warp_template -d t2s.nii.gz -w warp_template2t2s.nii.gz
132
+ # # >>>
133
+ cd ..
134
+
135
+
136
+ # t1
137
+ # ===========================================================================================
138
+ cd t1
139
+ # Contrast-agnostic registration
140
+ # 1. t1w preprocessing (cropping around spinal cord)
141
+ sct_deepseg spinalcord -i t1.nii.gz
142
+ sct_create_mask -i t1.nii.gz -p centerline,t1_seg.nii.gz -size 35mm -f cylinder -o mask_t1.nii.gz
143
+ sct_crop_image -i t1.nii.gz -m mask_t1.nii.gz
144
+ # 2. t2w preprocessing (cropping around spinal cord)
145
+ cp ../t2/t2.nii.gz ./t2.nii.gz
146
+ sct_deepseg spinalcord -i t2.nii.gz
147
+ sct_create_mask -i t2.nii.gz -p centerline,t2_seg.nii.gz -size 35mm -f cylinder -o mask_t2.nii.gz
148
+ sct_crop_image -i t2.nii.gz -m mask_t2.nii.gz
149
+ # 3. Perform registration
150
+ sct_register_multimodal -i t1_crop.nii.gz -d t2_crop.nii.gz -param step=1,type=im,algo=dl
151
+
152
+ # Other useful utilities
153
+ # 1. Smooth spinal cord along superior-inferior axis
154
+ sct_smooth_spinalcord -i t1.nii.gz -s t1_seg.nii.gz
155
+ # 2. Flatten cord in the right-left direction (to make nice figure)
156
+ sct_flatten_sagittal -i t1.nii.gz -s t1_seg.nii.gz
157
+ # Go back to root folder
158
+ cd ..
159
+
160
+
161
+ # mt
162
+ # ===========================================================================================
163
+ cd mt
164
+ # Get centerline from mt1 data
165
+ sct_get_centerline -i mt1.nii.gz -c t2
166
+ # sct_get_centerline -i mt1.nii.gz -c t2 -qc "$SCT_BP_QC_FOLDER"
167
+ # Create mask
168
+ sct_create_mask -i mt1.nii.gz -p centerline,mt1_centerline.nii.gz -size 45mm
169
+ # Crop data for faster processing
170
+ sct_crop_image -i mt1.nii.gz -m mask_mt1.nii.gz -o mt1_crop.nii.gz
171
+ # Segment spinal cord
172
+ sct_deepseg spinalcord -i mt1_crop.nii.gz -qc "$SCT_BP_QC_FOLDER"
173
+ # Register mt0->mt1
174
+ # Tips: here we only use rigid transformation because both images have very similar sequence parameters. We don't want to use SyN/BSplineSyN to avoid introducing spurious deformations.
175
+ # Tips: here we input -dseg because it is needed by the QC report
176
+ sct_register_multimodal -i mt0.nii.gz -d mt1_crop.nii.gz -dseg mt1_crop_seg.nii.gz -param step=1,type=im,algo=slicereg,metric=CC -x spline -qc "$SCT_BP_QC_FOLDER"
177
+ # Register template->mt1
178
+ # Tips: here we only use the segmentations due to poor SC/CSF contrast at the bottom slice.
179
+ # Tips: First step: slicereg based on images, with large smoothing to capture potential motion between anat and mt, then at second step: bpslinesyn in order to adapt the shape of the cord to the mt modality (in case there are distortions between anat and mt).
180
+ sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_t2.nii.gz" -iseg "$SCT_DIR/data/PAM50/template/PAM50_cord.nii.gz" -d mt1_crop.nii.gz -dseg mt1_crop_seg.nii.gz -param step=1,type=seg,algo=slicereg,smooth=3:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3 -initwarp ../t2/warp_template2anat.nii.gz -initwarpinv ../t2/warp_anat2template.nii.gz -owarp warp_template2mt.nii.gz -owarpinv warp_mt2template.nii.gz
181
+ # Warp template
182
+ sct_warp_template -d mt1_crop.nii.gz -w warp_template2mt.nii.gz -qc "$SCT_BP_QC_FOLDER"
183
+ # Compute mtr
184
+ sct_compute_mtr -mt0 mt0_reg.nii.gz -mt1 mt1_crop.nii.gz
185
+ # Register t1w->mt1
186
+ # Tips: We do not need to crop the t1w image before registration because step=0 of the registration is to put the source image in the space of the destination image (equivalent to cropping the t1w)
187
+ sct_register_multimodal -i t1w.nii.gz -d mt1_crop.nii.gz -dseg mt1_crop_seg.nii.gz -param step=1,type=im,algo=slicereg,metric=CC -x spline -qc "$SCT_BP_QC_FOLDER"
188
+ # Compute MTsat
189
+ # Tips: Check your TR and Flip Angle from the Dicom data
190
+ sct_compute_mtsat -mt mt1_crop.nii.gz -pd mt0_reg.nii.gz -t1 t1w_reg.nii.gz -trmt 0.030 -trpd 0.030 -trt1 0.015 -famt 9 -fapd 9 -fat1 15
191
+ # Extract MTR, T1 and MTsat within the white matter between C2 and C5.
192
+ # Tips: Here we use "-discard-neg-val 1" to discard inconsistent negative values in MTR calculation which are caused by noise.
193
+ sct_extract_metric -i mtr.nii.gz -method map -o mtr_in_wm.csv -l 51 -vert 2:5
194
+ sct_extract_metric -i mtsat.nii.gz -method map -o mtsat_in_wm.csv -l 51 -vert 2:5
195
+ sct_extract_metric -i t1map.nii.gz -method map -o t1_in_wm.csv -l 51 -vert 2:5
196
+ # Bring MTR to template space (e.g. for group mapping)
197
+ sct_apply_transfo -i mtr.nii.gz -d "$SCT_DIR/data/PAM50/template/PAM50_t2.nii.gz" -w warp_mt2template.nii.gz
198
+ # Go back to root folder
199
+ cd ..
200
+
201
+
202
+ # dmri
203
+ # ===========================================================================================
204
+ cd dmri
205
+ # bring t2 segmentation in dmri space to create mask (no optimization)
206
+ sct_dmri_separate_b0_and_dwi -i dmri.nii.gz -bvec bvecs.txt
207
+ sct_register_multimodal -i ../t2/t2_seg.nii.gz -d dmri_dwi_mean.nii.gz -identity 1 -x nn
208
+ # create mask to help moco and for faster processing
209
+ sct_create_mask -i dmri_dwi_mean.nii.gz -p centerline,t2_seg_reg.nii.gz -size 35mm
210
+ # motion correction
211
+ # Tips: if data have very low SNR you can increase the number of successive images that are averaged into group with "-g". Also see: sct_dmri_moco -h
212
+ sct_dmri_moco -i dmri.nii.gz -bvec bvecs.txt -m mask_dmri_dwi_mean.nii.gz
213
+ # segment spinal cord
214
+ sct_deepseg spinalcord -i dmri_moco_dwi_mean.nii.gz -qc "$SCT_BP_QC_FOLDER"
215
+ # Generate QC for sct_dmri_moco ('dmri_moco_dwi_mean_seg.nii.gz' is needed to align each slice in the QC mosaic)
216
+ sct_qc -i dmri.nii.gz -d dmri_moco.nii.gz -s dmri_moco_dwi_mean_seg.nii.gz -p sct_dmri_moco -qc "$SCT_BP_QC_FOLDER"
217
+ # Register template to dwi
218
+ # Tips: Again, here, we prefer to stick to segmentation-based registration. If there are susceptibility distortions in your EPI, then you might consider adding a third step with bsplinesyn or syn transformation for local adjustment.
219
+ sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_t1.nii.gz" -iseg "$SCT_DIR/data/PAM50/template/PAM50_cord.nii.gz" -d dmri_moco_dwi_mean.nii.gz -dseg dmri_moco_dwi_mean_seg.nii.gz -param step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,metric=MeanSquares,smooth=1,iter=3 -initwarp ../t2/warp_template2anat.nii.gz -initwarpinv ../t2/warp_anat2template.nii.gz -qc "$SCT_BP_QC_FOLDER" -owarp warp_template2dmri.nii.gz -owarpinv warp_dmri2template.nii.gz
220
+ # Warp template and white matter atlas
221
+ sct_warp_template -d dmri_moco_dwi_mean.nii.gz -w warp_template2dmri.nii.gz -qc "$SCT_BP_QC_FOLDER"
222
+ # Compute DTI metrics
223
+ # Tips: The flag -method "restore" allows you to estimate the tensor with robust fit (see: sct_dmri_compute_dti -h)
224
+ sct_dmri_compute_dti -i dmri_moco.nii.gz -bval bvals.txt -bvec bvecs.txt
225
+ # Compute FA within right and left lateral corticospinal tracts from slices 2 to 14 using weighted average method
226
+ sct_extract_metric -i dti_FA.nii.gz -z 2:14 -method wa -l 4,5 -o fa_in_cst.csv
227
+ # Bring metric to template space (e.g. for group mapping)
228
+ sct_apply_transfo -i dti_FA.nii.gz -d "$SCT_DIR/data/PAM50/template/PAM50_t2.nii.gz" -w warp_dmri2template.nii.gz
229
+ # Go back to root folder
230
+ cd ..
231
+
232
+
233
+ # fmri
234
+ # ===========================================================================================
235
+ cd fmri
236
+ # Average all fMRI time series (to be able to do the next step)
237
+ sct_maths -i fmri.nii.gz -mean t -o fmri_mean.nii.gz
238
+ # Get cord centerline
239
+ sct_get_centerline -i fmri_mean.nii.gz -c t2s
240
+ # Create mask around the cord to help motion correction and for faster processing
241
+ sct_create_mask -i fmri_mean.nii.gz -p centerline,fmri_mean_centerline.nii.gz -size 35mm
242
+ # Motion correction
243
+ # Tips: Here data have sufficient SNR and there is visible motion between two consecutive scans, so motion correction is more efficient with -g 1 (i.e. not average consecutive scans)
244
+ sct_fmri_moco -i fmri.nii.gz -g 1 -m mask_fmri_mean.nii.gz
245
+ # Segment spinal cord manually
246
+ # Since these data have very poor cord/CSF contrast, it is difficult to segment the cord properly using sct_deepseg
247
+ # and hence in this case we do it manually. The file is called: fmri_crop_moco_mean_seg_manual.nii.gz
248
+ # There is no command for this step, because the file is included in the 'sct_example_data' dataset.
249
+ # Generate QC for sct_fmri_moco ('fmri_crop_moco_mean_seg_manual.nii.gz' is needed to align each slice in the QC mosaic)
250
+ sct_qc -i fmri.nii.gz -d fmri_moco.nii.gz -s fmri_crop_moco_mean_seg_manual.nii.gz -p sct_fmri_moco -qc "$SCT_BP_QC_FOLDER"
251
+ # Register template->fmri
252
+ sct_register_multimodal -i "$SCT_DIR/data/PAM50/template/PAM50_t2.nii.gz" -iseg "$SCT_DIR/data/PAM50/template/PAM50_cord.nii.gz" -d fmri_moco_mean.nii.gz -dseg fmri_crop_moco_mean_seg_manual.nii.gz -param step=1,type=seg,algo=slicereg,metric=MeanSquares,smooth=2:step=2,type=im,algo=bsplinesyn,metric=MeanSquares,iter=5,gradStep=0.5 -initwarp ../t2/warp_template2anat.nii.gz -initwarpinv ../t2/warp_anat2template.nii.gz -qc "$SCT_BP_QC_FOLDER" -owarp warp_template2fmri.nii.gz -owarpinv warp_fmri2template.nii.gz
253
+ # Warp template, including spinal levels (here we don't need the WM atlas)
254
+ sct_warp_template -d fmri_moco_mean.nii.gz -w warp_template2fmri.nii.gz -a 0
255
+ # Note, once you have computed fMRI statistics in the subject's space, you can use
256
+ # warp_fmri2template.nii.gz to bring the statistical maps on the template space, for group analysis.
257
+
258
+
259
+ # Display results (to easily compare integrity across SCT versions)
260
+ # ===========================================================================================
261
+ set +v
262
+ end=$(date +%s)
263
+ runtime=$((end-start))
264
+ echo "~~~" # these are used to format as code when copy/pasting in github's markdown
265
+ echo "Version: $(sct_version)"
266
+ echo "Ran on: $(uname -nsr)"
267
+ echo "Duration: $((runtime / 3600))hrs $(((runtime / 60) % 60))min $((runtime % 60))sec"
268
+ echo "---"
269
+ # The file `test_batch_processing.py` will output tested values when run as a script
270
+ "$SCT_DIR"/python/envs/venv_sct/bin/python "$SCT_DIR"/testing/batch_processing/test_batch_processing.py ||
271
+ "$SCT_DIR"/python/envs/venv_sct/python.exe "$SCT_DIR"/testing/batch_processing/test_batch_processing.py
272
+ echo "~~~"
273
+
274
+ # Display syntax to open QC report on web browser
275
+ echo "To open Quality Control (QC) report on a web-browser, run the following:"
276
+ echo "$open_command $SCT_BP_QC_FOLDER/index.html"