Skip to content

Commit

Permalink
organize processing by script
Browse files Browse the repository at this point in the history
  • Loading branch information
andreanne-lemay committed Apr 16, 2020
1 parent c561889 commit 9b31e13
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
83 changes: 83 additions & 0 deletions crop_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# Pipeline for spinal cord tumor data.
#
# Generate sc segmentation, preprocess data before training.
#
# Note: All images have .nii extension.
#
# Usage:
# sct_run_batch <FILEPARAM> process_data.sh
#
# Author: Andreanne Lemay

# Uncomment for full verbose
# set -v

# Immediately exit if error
set -e

# Exit if user presses CTRL+C (Linux) or CMD+C (OSX)
trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT

# Retrieve input params
SUBJECT=$1
FILEPARAM=$2

# SCRIPT STARTS HERE
# ==============================================================================
# shellcheck disable=SC1090
source $FILEPARAM
# Go to results folder, where most of the outputs will be located
cd $PATH_RESULTS
# Copy source images and segmentations
mkdir -p data/derivatives/labels
cd data

cp -r $PATH_DATA/$SUBJECT .
cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels

# Go to data folder
cd $PATH_RESULTS/data/$SUBJECT/anat/
## Setup file names
file_t2w=${SUBJECT}_T2w
file_t1w=${SUBJECT}_T1w

path_t1w=`pwd`/${file_t1w}.nii.gz

# Oversample to avoid losing slice info when cropping
sct_resample -i ${file_t2w}.nii.gz -vox 512x512x100 -x spline -o ${file_t2w}.nii.gz;

sct_get_centerline -i ${file_t2w}.nii.gz -c t2;
sct_create_mask -i ${file_t2w}.nii.gz -p centerline,${file_t2w}_centerline.nii.gz;
sct_crop_image -i ${file_t2w}.nii.gz -m mask_${file_t2w}.nii.gz -o ${file_t2w}.nii.gz;
cropped_img=`pwd`/${file_t2w}.nii.gz;


if [ -f ${path_t1w} ]; then
sct_resample -i ${file_t1w}.nii.gz -vox 512x512x100 -x spline -o ${file_t1w}.nii.gz;
sct_crop_image -i ${file_t1w}.nii.gz -ref ${cropped_img} -o ${file_t1w}.nii.gz;
sct_resample -i ${file_t1w}.nii.gz -vox 96x512x16 -x spline -o ${file_t1w}.nii.gz;
fi

cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/

file_t2w_seg=${SUBJECT}_T2w_seg-tumor
file_t1w_seg=${SUBJECT}_T1w_seg-tumor

if [ -f ${path_t1w} ]; then
sct_resample -i ${file_t1w_seg}.nii.gz -vox 512x512x100 -x linear -o ${file_t1w_seg}.nii.gz;
sct_crop_image -i ${file_t1w_seg}.nii.gz -ref ${cropped_img} -o ${file_t1w_seg}.nii.gz;
sct_resample -i ${file_t1w_seg}.nii.gz -vox 96x512x16 -x linear -o ${file_t1w_seg}.nii.gz;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9
sct_image -i ${file_t1w_seg}.nii.gz -setorient AIL;
fi

sct_resample -i ${file_t2w_seg}.nii.gz -vox 512x512x100 -x linear -o ${file_t2w_seg}.nii.gz;
sct_crop_image -i ${file_t2w_seg}.nii.gz -ref ${cropped_img} -o ${file_t2w_seg}.nii.gz;
sct_resample -i ${file_t2w_seg}.nii.gz -vox 96x512x16 -x linear -o ${file_t2w_seg}.nii.gz;
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_seg}.nii.gz -setorient AIL;

cd $PATH_RESULTS/data/$SUBJECT/anat/
sct_resample -i ${file_t2w}.nii.gz -vox 96x512x16 -x spline -o ${file_t2w}.nii.gz;
81 changes: 81 additions & 0 deletions merge_labels.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash
#
# Pipeline for spinal cord tumor data.
#
# Generate sc segmentation, preprocess data before training.
#
# Note: All images have .nii extension.
#
# Usage:
# sct_run_batch <FILEPARAM> process_data.sh
#
# Author: Andreanne Lemay

# Uncomment for full verbose
# set -v

# Immediately exit if error
set -e

# Exit if user presses CTRL+C (Linux) or CMD+C (OSX)
trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT

# Retrieve input params
SUBJECT=$1
FILEPARAM=$2

# SCRIPT STARTS HERE
# ==============================================================================
# shellcheck disable=SC1090
source $FILEPARAM
# Go to results folder, where most of the outputs will be located
cd $PATH_RESULTS
# Copy source images and segmentations
mkdir -p data/derivatives/labels
cd data

cp -r $PATH_DATA/$SUBJECT .
cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels

# Go to data labels folder
cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/

file_t2w_seg=${SUBJECT}_T2w_seg-tumor
file_t1w_seg=${SUBJECT}_T1w_seg-tumor
file_t1w_edema=${SUBJECT}_T1w_edema
file_t1w_cavity=${SUBJECT}_T1w_cavity
file_t2w_edema=${SUBJECT}_T2w_edema
file_t2w_cavity=${SUBJECT}_T2w_cavity

sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_seg}.nii.gz -setorient AIL;

sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_seg}.nii.gz -setorient AIL;

path_edema=`pwd`/${file_t1w_edema}.nii.gz
if [ -f ${path_edema} ]; then
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_seg}.nii.gz -setorient AIL;

sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_seg}.nii.gz -setorient AIL;

sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -add ${file_t2w_edema}.nii.gz;
sct_maths -i ${file_t1w_edema}.nii.gz -o ${file_t1w_edema}.nii.gz -add ${file_t1w_edema}.nii.gz;
fi

path_cavity=`pwd`/${file_t1w_cavity}.nii.gz
if [ -f ${path_cavity} ]; then
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_seg}.nii.gz -setorient AIL;

sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_seg}.nii.gz -setorient AIL;

sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -add ${file_t2w_cavity}.nii.gz;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -add ${file_t1w_cavity}.nii.gz;
fi

sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;
12 changes: 7 additions & 5 deletions parameters/parameters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
# Set every other path relative to this path for convenience
# Do not add "/" at the end. Path should be absolute (i.e. do not use "~")
# Example:
export PATH_PARENT="/home/GRAMES.POLYMTL.CA/anlemj/results/dataset"

export PATH_PARENT="/home/andreanne/Documents/dataset"


# Path to the folder containing the BIDS dataset.
# Do not add "/" at the end. Path should be absolute (i.e. do not use "~")
export PATH_DATA="$PATH_PARENT/sctumor_BIDS"
export PATH_DATA="$PATH_PARENT/new_labels320/data"

# If each subject folder starts with a prefix, indicate it here. Otherwise, set to ""
SUBJECT_PREFIX="sub-"

# Paths to where to save the new dataset.
# Do not add "/" at the end. Path should be absolute (i.e. do not use "~")
export PATH_RESULTS="$PATH_PARENT/results"
# Endroit où seront copiees les données
export PATH_RESULTS="$PATH_PARENT/reg"
export PATH_QC="$PATH_RESULTS/qc"
export PATH_LOG="$PATH_RESULTS/log"

Expand All @@ -36,8 +38,8 @@ export PATH_LOG="$PATH_RESULTS/log"
# Number of jobs for parallel processing
# To know the number of available cores, run: getconf _NPROCESSORS_ONLN
# We recommend not using more than half the number of available cores.
export JOBS=4
export JOBS=1

# Number of jobs for ANTs routine. Set to 1 if ANTs functions crash when CPU saturates.
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=4
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1

77 changes: 67 additions & 10 deletions process_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ FILEPARAM=$2

# SCRIPT STARTS HERE
# ==============================================================================
# shellcheck disable=SC1090
source $FILEPARAM
# Go to results folder, where most of the outputs will be located
cd $PATH_RESULTS
Expand All @@ -36,19 +37,75 @@ cd data
cp -r $PATH_DATA/$SUBJECT .
cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels

cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/

file_t2w_seg=${SUBJECT}_T2w_seg-tumor
file_t1w_seg=${SUBJECT}_T1w_seg-tumor
#
sct_resample -i ${file_t2w_seg}.nii.gz -vox 512x512x11 -x spline -o ${file_t2w_seg}.nii.gz
sct_resample -i ${file_t1w_seg}.nii.gz -vox 512x512x11 -x spline -o ${file_t1w_seg}.nii.gz

# Go to data folder
cd $PATH_RESULTS/data/$SUBJECT/anat/
## Setup file names
file_t2w=${SUBJECT}_T2w
file_t1w=${SUBJECT}_T1w
sct_resample -i ${file_t2w}.nii.gz -vox 512x512x11 -x spline -o ${file_t2w}.nii.gz
sct_resample -i ${file_t1w}.nii.gz -vox 512x512x11 -x spline -o ${file_t1w}.nii.gz

path_t1w=`pwd`/${file_t1w}.nii.gz

sct_image -i ${file_t2w}.nii.gz -setorient AIL
sct_resample -i ${file_t2w}.nii.gz -vox 320x320x16 -x spline -o ${file_t2w}.nii.gz;
sct_maths -i ${file_t2w}.nii.gz -o ${file_t2w}.nii.gz -bin 0.9;

if [ -f ${path_t1w} ]; then
sct_image -i ${file_t1w}.nii.gz -setorient AIL
sct_resample -i ${file_t1w}.nii.gz -vox 320x320x16 -x spline -o ${file_t1w}.nii.gz;
sct_maths -i ${file_t1w}.nii.gz -o ${file_t1w}.nii.gz -bin 0.9;
fi

cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/

file_t2w_seg=${SUBJECT}_T2w_seg-tumor
file_t1w_seg=${SUBJECT}_T1w_seg-tumor
file_t1w_edema=${SUBJECT}_T1w_edema
file_t1w_cavity=${SUBJECT}_T1w_cavity
file_t2w_edema=${SUBJECT}_T2w_edema
file_t2w_cavity=${SUBJECT}_T2w_cavity


sct_resample -i ${file_t2w_seg}.nii.gz -vox 320x320x16 -x spline -o ${file_t2w_seg}.nii.gz;
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_seg}.nii.gz -setorient AIL;

sct_resample -i ${file_t1w_seg}.nii.gz -vox 320x320x16 -x spline -o ${file_t1w_seg}.nii.gz;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_seg}.nii.gz -setorient AIL;

path_edema=`pwd`/${file_t1w_edema}.nii.gz
if [ -f ${path_edema} ]; then
sct_resample -i ${file_t2w_edema}.nii.gz -vox 320x320x16 -x linear -o ${file_t2w_edema}.nii.gz;
sct_maths -i ${file_t2w_edema}.nii.gz -o ${file_t2w_edema}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_edema}.nii.gz -setorient AIL;

sct_resample -i ${file_t1w_edema}.nii.gz -vox 320x320x16 -x linear -o ${file_t1w_edema}.nii.gz;
sct_maths -i ${file_t1w_edema}.nii.gz -o ${file_t1w_edema}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_edema}.nii.gz -setorient AIL;

# Merge labels
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -add ${file_t2w_edema}.nii.gz;
sct_maths -i ${file_t1w_edema}.nii.gz -o ${file_t1w_edema}.nii.gz -add ${file_t1w_edema}.nii.gz;
fi

path_cavity=`pwd`/${file_t1w_cavity}.nii.gz
if [ -f ${path_cavity} ]; then
sct_resample -i ${file_t2w_cavity}.nii.gz -vox 320x320x16 -x linear -o ${file_t2w_cavity}.nii.gz;
sct_maths -i ${file_t2w_cavity}.nii.gz -o ${file_t2w_cavity}.nii.gz -bin 0.9;
sct_image -i ${file_t2w_cavity}.nii.gz -setorient AIL;

sct_resample -i ${file_t1w_cavity}.nii.gz -vox 320x320x16 -x linear -o ${file_t1w_cavity}.nii.gz;
sct_maths -i ${file_t1w_cavity}.nii.gz -o ${file_t1w_cavity}.nii.gz -bin 0.9;
sct_image -i ${file_t1w_cavity}.nii.gz -setorient AIL;

# Merge labels
sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -add ${file_t2w_cavity}.nii.gz;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -add ${file_t1w_cavity}.nii.gz;
fi

sct_maths -i ${file_t2w_seg}.nii.gz -o ${file_t2w_seg}.nii.gz -bin 0.9;
sct_maths -i ${file_t1w_seg}.nii.gz -o ${file_t1w_seg}.nii.gz -bin 0.9;




63 changes: 63 additions & 0 deletions register_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash
#
# Pipeline for spinal cord tumor data.
#
# Generate sc segmentation, preprocess data before training.
#
# Note: All images have .nii extension.
#
# Usage:
# sct_run_batch <FILEPARAM> process_data.sh
#
# Author: Andreanne Lemay

# Uncomment for full verbose
# set -v

# Immediately exit if error
set -e

# Exit if user presses CTRL+C (Linux) or CMD+C (OSX)
trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT

# Retrieve input params
SUBJECT=$1
FILEPARAM=$2

# SCRIPT STARTS HERE
# ==============================================================================
# shellcheck disable=SC1090
source $FILEPARAM
# Go to results folder, where most of the outputs will be located
cd $PATH_RESULTS
# Copy source images and segmentations
mkdir -p data/derivatives/labels
cd data

cp -r $PATH_DATA/$SUBJECT .
cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels

# Go to data folder
cd $PATH_RESULTS/data/$SUBJECT/anat/
## Setup file names
file_t2w=${SUBJECT}_T2w
file_t1w=${SUBJECT}_T1w
path_t1w=`pwd`/${file_t1w}.nii.gz

if [ -f ${path_t1w} ]; then
sct_register_multimodal -i ${file_t1w}.nii.gz -d ${file_t2w}.nii.gz -x spline
mv ${file_t2w}_reg.nii.gz ${file_t2w}.nii.gz
mv ${file_t1w}_reg.nii.gz ${file_t1w}.nii.gz
warp_T2w2T1w=`pwd`/warp_${file_t2w}2${file_t1w}.nii.gz
warp_T1w2T2w=`pwd`/warp_${file_t1w}2${file_t2w}.nii.gz
fi

cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/

file_t2w_seg=${SUBJECT}_T2w_seg-tumor
file_t1w_seg=${SUBJECT}_T1w_seg-tumor

if [ -f ${path_t1w} ]; then
sct_apply_transfo -i ${file_t1w_seg}.nii.gz -d ${file_t2w_seg}.nii.gz -w ${warp_T1w2T2w} -o ${file_t1w_seg}.nii.gz -x linear
sct_apply_transfo -i ${file_t2w_seg}.nii.gz -d ${file_t1w_seg}.nii.gz -w ${warp_T2w2T1w} -o ${file_t2w_seg}.nii.gz -x linear
fi
Loading

0 comments on commit 9b31e13

Please sign in to comment.