Skip to content

Instantly share code, notes, and snippets.

@valosekj
Created July 18, 2023 19:36
Show Gist options
  • Save valosekj/4a066f1aa52bc3df18e87b6e05d5df6f to your computer and use it in GitHub Desktop.
Save valosekj/4a066f1aa52bc3df18e87b6e05d5df6f to your computer and use it in GitHub Desktop.
Template script for `sct_run_batch`

Template script for sct_run_batch

This gist contains a template script that can be run across multiple subjects using the sct_run_batch wrapper script.

The template bash script below can be run from you command line using the following command:

sct_run_batch -script example_script.sh -path-data <DATA> -path-output <DATA>_202X-XX-XX -jobs 16
#!/bin/bash
#
# This is a template script that can be run for multiple subjects using the sct_run_batch wrapper script.
#
# Usage:
#       sct_run_batch -script example_script.sh -path-data <DATA> -path-output <DATA>_202X-XX-XX -jobs 16
#
# 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"
#
# Authors: 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}"

# Retrieve input params and other params
SUBJECT=$1

# get starting time:
start=`date +%s`

# ------------------------------------------------------------------------------
# SCRIPT STARTS HERE
# ------------------------------------------------------------------------------
# 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 images
# Note: we use '/./' in order to include the sub-folder 'ses-0X'
rsync -Ravzh $PATH_DATA/./$SUBJECT .

# Go to subject folder for source images
cd ${SUBJECT}/anat

# Define variables
# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/'
file="${SUBJECT//[\/]/_}"

# -------------------------------------------------------------------------
# T2w
# -------------------------------------------------------------------------
# Add suffix corresponding to contrast
file_t2w=${file}_T2w
# Check if T2w image exists
if [[ -f ${file_t2w}.nii.gz ]];then
    # Get image header
    sct_image -i ${file_t2w}.nii.gz -header
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 "~~~"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment