diff --git a/baselines/run_inference_spine-generic.sh b/baselines/run_inference_spine-generic.sh new file mode 100644 index 0000000..c7fa87d --- /dev/null +++ b/baselines/run_inference_spine-generic.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# +# Segment SC and lesions using our nnUNet SCIsegV2 model (part of SCT v6.4) on spine-generic multi-subject T2w iso images +# and generate QC report +# +# Usage: +# sct_run_batch -config config.json +# +# Example of config.json: +# { +# "path_data" : "data-multi-subject", +# "path_output" : "data-multi-subject_2024-08-25", +# "script" : "/model_seg_sci/baselines/run_inference_spine-generic.sh", +# "jobs" : 8 +# } +# +# +# 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 + +echo "SUBJECT: ${SUBJECT}" + +# ------------------------------------------------------------------------------ +# 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/' +# copy only T2w to save space +rsync -Ravzh ${PATH_DATA}/./${SUBJECT}/anat/${SUBJECT//[\/]/_}_*T2w.* . + +# Go to subject folder for source images +cd ${SUBJECT}/anat + +# ------------------------------------------------------------------------------ +# T2w +# ------------------------------------------------------------------------------ + +# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/' +file_t2="${SUBJECT//[\/]/_}"_T2w + +# 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 +else + # Segment SC and lesion using SCIsegV2 (part of SCT v6.4) + # Note: a single axial QC report contains both SC and lesion segmentations + # Note: we use CUDA_VISIBLE_DEVICES=0 SCT_USE_GPU=1 to run the inference on GPU 0; details: + # https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/4421#issuecomment-2263344151 + CUDA_VISIBLE_DEVICES=0 SCT_USE_GPU=1 sct_deepseg -i ${file_t2}.nii.gz -task seg_sc_lesion_t2w_sci -qc ${PATH_QC} -qc-subject ${SUBJECT} +fi + +# ------------------------------------------------------------------------------ +# 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 "~~~"