From c9275a45efca42f2a53936d4cc1008a1743d3f58 Mon Sep 17 00:00:00 2001 From: valosekj Date: Thu, 14 Sep 2023 15:32:27 -0400 Subject: [PATCH 1/6] Add script to crop the input image, spinal cord GT, and lesion GT around the spinal cord segmentation (based on the dilated spinal cord GT) --- dataset-conversion/crop_around_sc.sh | 157 +++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 dataset-conversion/crop_around_sc.sh diff --git a/dataset-conversion/crop_around_sc.sh b/dataset-conversion/crop_around_sc.sh new file mode 100644 index 00000000..18415e8d --- /dev/null +++ b/dataset-conversion/crop_around_sc.sh @@ -0,0 +1,157 @@ +#!/bin/bash +# +# The script crops the input image, spinal cord GT, and lesion GT around the spinal cord segmentation (based on the +# dilated spinal cord GT) +# +# Usage: +# sct_run_batch -config config.json +# +# Example of config.json: +# { +# "path_data" : "", +# "path_output" : "_2023-09-14", +# "script" : "/model_seg_sci/dataset-conversion/crop_around_sc.sh", +# "jobs" : 8 +# } +# +# 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, Naga Karthik +# + +# 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 + +echo "SUBJECT: ${SUBJECT}" + + +# CONVENIENCE FUNCTIONS +# ====================================================================================================================== +# Copy GT spinal cord segmentation +copy_gt_sc(){ + 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 +} + +# Copy GT lesion segmentation +copy_gt_lesion(){ + local file="$1" + # Construct file name to GT segmentation located under derivatives/labels + FILELESIONMANUAL="${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${file}_lesion-manual.nii.gz" + echo "" + echo "Looking for manual segmentation: $FILELESIONMANUAL" + if [[ -e $FILELESIONMANUAL ]]; then + echo "Found! Copying ..." + rsync -avzh $FILELESIONMANUAL ${file}_lesion-manual.nii.gz + else + echo "File ${FILELESIONMANUAL}.nii.gz does not exist" >> ${PATH_LOG}/missing_files.log + echo "ERROR: Manual GT segmentation ${FILELESIONMANUAL}.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 +# ------------------------------------------------------------------------------ +# T2w +# ------------------------------------------------------------------------------ + +# Copy GT spinal cord and lesion segmentations +copy_gt_sc "${file_t2}" +copy_gt_lesion "${file_t2}" + +# Dilate spinal cord mask +sct_maths -i ${file_t2}_seg.nii.gz -dilate 5 -shape ball -o ${file_sag}_seg-manual_dilate.nii.gz + +# Use the dilated mask to crop the original image +sct_crop_image -i ${file_t2}.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_crop.nii.gz + +# Use the dilated mask to crop the spinal cord GT +sct_crop_image -i ${file_t2}_seg-manual.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_seg-manual_crop.nii.gz + +# Use the dilated mask to crop the lesion GT +sct_crop_image -i ${file_t2}_lesion-manual.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_lesion-manual_crop.nii.gz + +# Generate QC to assess the cropping +sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -p sct_deepseg_sc -qc-subject ${SUBJECT} -qc-dataset ${DATASET} + +# ------------------------------------------------------------------------------ +# 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 "~~~" From c88c3fb1b424c71965d465750f5ae43ed2a5aef7 Mon Sep 17 00:00:00 2001 From: valosekj Date: Thu, 14 Sep 2023 16:56:25 -0400 Subject: [PATCH 2/6] Fix typo in filename --- dataset-conversion/crop_around_sc.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dataset-conversion/crop_around_sc.sh b/dataset-conversion/crop_around_sc.sh index 18415e8d..31b57a59 100644 --- a/dataset-conversion/crop_around_sc.sh +++ b/dataset-conversion/crop_around_sc.sh @@ -131,16 +131,17 @@ copy_gt_lesion "${file_t2}" sct_maths -i ${file_t2}_seg.nii.gz -dilate 5 -shape ball -o ${file_sag}_seg-manual_dilate.nii.gz # Use the dilated mask to crop the original image -sct_crop_image -i ${file_t2}.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_crop.nii.gz +sct_crop_image -i ${file_t2}.nii.gz -m ${file_t2}_seg-manual_dilate.nii.gz -o ${file_t2}_crop.nii.gz # Use the dilated mask to crop the spinal cord GT -sct_crop_image -i ${file_t2}_seg-manual.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_seg-manual_crop.nii.gz +sct_crop_image -i ${file_t2}_seg-manual.nii.gz -m ${file_t2}_seg-manual_dilate.nii.gz -o ${file_t2}_seg-manual_crop.nii.gz # Use the dilated mask to crop the lesion GT -sct_crop_image -i ${file_t2}_lesion-manual.nii.gz -m ${file_sag}_seg-manual_dilate.nii.gz -o ${file_t2}_lesion-manual_crop.nii.gz +sct_crop_image -i ${file_t2}_lesion-manual.nii.gz -m ${file_t2}_seg-manual_dilate.nii.gz -o ${file_t2}_lesion-manual_crop.nii.gz # Generate QC to assess the cropping -sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -p sct_deepseg_sc -qc-subject ${SUBJECT} -qc-dataset ${DATASET} +sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -d ${file_t2}_seg-manual_crop.nii.gz -p sct_deepseg_lesion -plane sagittal -qc ${PATH_QC} -qc-subject ${SUBJECT} +sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -d ${file_t2}_lesion-manual_crop.nii.gz -p sct_deepseg_lesion -plane sagittal -qc ${PATH_QC} -qc-subject ${SUBJECT} # ------------------------------------------------------------------------------ # End From 667e73ed76152a1f3a68fcfb327b124dd200e2f6 Mon Sep 17 00:00:00 2001 From: valosekj Date: Thu, 14 Sep 2023 16:56:45 -0400 Subject: [PATCH 3/6] Change dilate from 5 to 10 --- dataset-conversion/crop_around_sc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 dataset-conversion/crop_around_sc.sh diff --git a/dataset-conversion/crop_around_sc.sh b/dataset-conversion/crop_around_sc.sh old mode 100644 new mode 100755 index 31b57a59..e6479ede --- a/dataset-conversion/crop_around_sc.sh +++ b/dataset-conversion/crop_around_sc.sh @@ -128,7 +128,7 @@ copy_gt_sc "${file_t2}" copy_gt_lesion "${file_t2}" # Dilate spinal cord mask -sct_maths -i ${file_t2}_seg.nii.gz -dilate 5 -shape ball -o ${file_sag}_seg-manual_dilate.nii.gz +sct_maths -i ${file_t2}_seg-manual.nii.gz -dilate 10 -shape ball -o ${file_t2}_seg-manual_dilate.nii.gz # Use the dilated mask to crop the original image sct_crop_image -i ${file_t2}.nii.gz -m ${file_t2}_seg-manual_dilate.nii.gz -o ${file_t2}_crop.nii.gz From 7ca8aad3823607532c2b2ab956d92674f61a956b Mon Sep 17 00:00:00 2001 From: valosekj Date: Thu, 14 Sep 2023 16:59:15 -0400 Subject: [PATCH 4/6] Comment out QC for SC -- only lesion QC is relevant. --- dataset-conversion/crop_around_sc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataset-conversion/crop_around_sc.sh b/dataset-conversion/crop_around_sc.sh index e6479ede..987d8f74 100755 --- a/dataset-conversion/crop_around_sc.sh +++ b/dataset-conversion/crop_around_sc.sh @@ -141,7 +141,7 @@ sct_crop_image -i ${file_t2}_lesion-manual.nii.gz -m ${file_t2}_seg-manual_dilat # Generate QC to assess the cropping sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -d ${file_t2}_seg-manual_crop.nii.gz -p sct_deepseg_lesion -plane sagittal -qc ${PATH_QC} -qc-subject ${SUBJECT} -sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -d ${file_t2}_lesion-manual_crop.nii.gz -p sct_deepseg_lesion -plane sagittal -qc ${PATH_QC} -qc-subject ${SUBJECT} +#sct_qc -i ${file_t2}_crop.nii.gz -s ${file_t2}_seg-manual_crop.nii.gz -d ${file_t2}_lesion-manual_crop.nii.gz -p sct_deepseg_lesion -plane sagittal -qc ${PATH_QC} -qc-subject ${SUBJECT} # ------------------------------------------------------------------------------ # End From 6c4be8e78a5ecae6a8eeea48e84d60e98c6d57a6 Mon Sep 17 00:00:00 2001 From: valosekj Date: Thu, 14 Sep 2023 18:32:00 -0400 Subject: [PATCH 5/6] Add info about cropping to the README.md --- dataset-conversion/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dataset-conversion/README.md b/dataset-conversion/README.md index d35c34f8..74785ed9 100644 --- a/dataset-conversion/README.md +++ b/dataset-conversion/README.md @@ -71,3 +71,8 @@ This command takes as inputs the list of RPI-reoriented datasets, the output pat > **Note** > This assumes the nnUNet has been successfully installed and the necessary nnUNet-related environment variables have been set. Please refer to the [nnUNet documentation](https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/installation_instructions.md) for more details. + +#### Cropping around the spinal cord + +The cropping of the input images, spinal cord GT, and lesion GT around the spinal cord segmentation can be done using +the `crop_around_sc.sh` script. The cropping is done based on the dilated spinal cord GT. \ No newline at end of file From c487f9b7f26c21f833c174a9687d9b01e99e10af Mon Sep 17 00:00:00 2001 From: valosekj Date: Mon, 18 Sep 2023 16:41:35 -0400 Subject: [PATCH 6/6] Update description --- dataset-conversion/crop_around_sc.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dataset-conversion/crop_around_sc.sh b/dataset-conversion/crop_around_sc.sh index 987d8f74..1b538f48 100755 --- a/dataset-conversion/crop_around_sc.sh +++ b/dataset-conversion/crop_around_sc.sh @@ -3,6 +3,10 @@ # The script crops the input image, spinal cord GT, and lesion GT around the spinal cord segmentation (based on the # dilated spinal cord GT) # +# +# Note that the script outputs the cropped images (images, SC GT, lesion GT) in the data_processed folder, which is +# not BIDS-compatible. +# # Usage: # sct_run_batch -config config.json # @@ -14,6 +18,8 @@ # "jobs" : 8 # } # +# Note that has to have both SC and lesion GT in the derivatives/labels folder. +# # 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"