Skip to content

Commit

Permalink
Merge pull request #50 from sct-pipeline/sb/process_pathology
Browse files Browse the repository at this point in the history
Add processing of datasets with pathology
  • Loading branch information
sandrinebedard authored Mar 9, 2023
2 parents 86d2356 + e899407 commit c516d9d
Show file tree
Hide file tree
Showing 4 changed files with 399 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ python gen_charts.py --contrasts T1w T2w T2star rec-average_dwi \
--predictions_folder ../duke/projects/ivadomed/contrast-agnostic-seg/csa_measures_pred/group8-9_combined-2022-12-21/ \
--baseline_folder ../duke/projects/ivadomed/contrast-agnostic-seg/archive_derivatives_softsegs-seg/contrast-agnostic-centerofmass-preprocess-clean-all-2022-10-22\results_MTS_renamed
```
## Run qc report on prediction masks

~~~
sct_run_batch -path-data <PATH_DATA> -path-out <PATH-OUT> -script-args <PATH_PRED_MASK> -jobs 20 -script run_qc_prediction.sh
~~~
102 changes: 102 additions & 0 deletions run_qc_prediction.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash
#
# Run QC report on prediction masks
# Usage:
# ./run_qc_prediction.sh <SUBJECT>
#
#
# Authors: Sandrine Bédard
#
# 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"

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

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

# 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 list of participants in processed data folder
if [[ ! -f "participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv .
fi
# Copy list of participants in resutls folder
if [[ ! -f $PATH_RESULTS/"participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv $PATH_RESULTS/"participants.tsv"
fi

# Copy source images
# Copy session if specified
if [[ $SUBJECT == *"ses"* ]];then
rsync -Ravzh $PATH_DATA/./$SUBJECT .
else
rsync -avzh $PATH_DATA/$SUBJECT .
fi


# FUNCTIONS
# ==============================================================================

find_contrast(){
local file="$1"
local dwi="dwi"
if echo "$file" | grep -q "$dwi"; then
echo "./${dwi}/"
else
echo "./anat/"
fi
}


# SCRIPT STARTS HERE
# ==============================================================================
# Go to anat folder where all structural data are located
cd ${SUBJECT}
# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/'
file_sub="${SUBJECT//[\/]/_}"

for file_pred in ${PATH_PRED_SEG}/*; do
if [[ $file_pred == *$file_sub* ]];then
echo " File found, running QC report $file_pred"
# Find if anat or dwi
file_seg_basename=${file_pred##*/}
echo $file_seg_basename
type=$(find_contrast $file_path)
# rsync prediction mask
rsync -avzh $file_pred ${type}/$file_seg_basename
file_image=${file_seg_basename//"_class-0_pred"}
echo $file_image
# Create QC for pred mask
sct_qc -i ${type}/${file_image} -s ${type}/${file_seg_basename} -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}


fi
done
#TODO find anat or dwi

# Display useful info for the log
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 "~~~"
141 changes: 141 additions & 0 deletions scripts/process_data_basel_mp2rage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash
#
# Process data from sci-colorado
# Crop all images.
# Authors: Sandrine Bédard

# 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"

# Uncomment for full verbose
set -x

# Immediately exit if error
#set -e -o pipefail --> will not enter in the loop if so...

# 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

# 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 list of participants in processed data folder
if [[ ! -f "participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv .
fi
if [[ ! -f "participants.json" ]]; then
rsync -avzh $PATH_DATA/participants.json .
fi
if [[ ! -f "dataset_description.json" ]]; then
rsync -avzh $PATH_DATA/dataset_description.json .
fi
if [[ ! -f "README.md" ]]; then
rsync -avzh $PATH_DATA/README.md .
fi
# Copy list of participants in results folder
if [[ ! -f $PATH_RESULTS/"participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv $PATH_RESULTS/"participants.tsv"
fi
# Copy source images
rsync -avzh $PATH_DATA/$SUBJECT .


# FUNCTIONS
# ==============================================================================
segment_if_does_not_exist(){
local file="$1"
local contrast="$2"
local contrast_for_seg="$3"
# Find contrast
if [[ $contrast == "./dwi/" ]]; then
folder_contrast="dwi"
else
folder_contrast="anat"
fi

# Update global variable with segmentation file name
FILESEG="${file}_label-SC_seg"
FILESEGMANUAL="${PATH_DATA}/derivatives/labels/${SUBJECT}/${folder_contrast}/${FILESEG}.nii.gz"
echo
echo "Looking for manual segmentation: $FILESEGMANUAL"
if [[ -e $FILESEGMANUAL ]]; then
echo "Found! Using manual segmentation."
rsync -avzh $FILESEGMANUAL "${FILESEG}.nii.gz"
sct_qc -i ${file}.nii.gz -s ${FILESEG}.nii.gz -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}
else
echo "Manual segmentation not found."
# Segment spinal cord
sct_deepseg_sc -i ${file}.nii.gz -c $contrast_for_seg -qc ${PATH_QC} -qc-subject ${SUBJECT} -o ${FILESEG}.nii.gz

fi
}

# Go to anat folder where all structural data are located
cd ${SUBJECT}/anat/


# MP2RAGE
# ------------------------------------------------------------------------------
file_mp2rage="${SUBJECT}_UNIT1"
segment_if_does_not_exist ${file_mp2rage} 'anat' 't1'

file_path=$file_mp2rage
fileseg="${file_mp2rage}_label-SC_seg"
# Crop image
sct_crop_image -i ${file_path}.nii.gz -dilate 7 -m ${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${fileseg}.nii.gz -o ${file_path}_crop.nii.gz
# Crop segmentation
sct_crop_image -i ${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${fileseg}.nii.gz -dilate 7 -m ${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${fileseg}.nii.gz -o ${fileseg}_crop.nii.gz

# Go back to root output folder
cd $PATH_OUTPUT
# Create and populate clean data processed folder for training
PATH_DATA_PROCESSED_CLEAN="${PATH_DATA_PROCESSED}_clean"
# Copy over required BIDs files
rsync -avzh $PATH_DATA_PROCESSED/dataset_description.json $PATH_DATA_PROCESSED_CLEAN/
rsync -avzh $PATH_DATA_PROCESSED/participants.* $PATH_DATA_PROCESSED_CLEAN/
rsync -avzh $PATH_DATA_PROCESSED/README.md $PATH_DATA_PROCESSED_CLEAN/
rsync -avzh $PATH_DATA_PROCESSED/dataset_description.json $PATH_DATA_PROCESSED_CLEAN/
mkdir -p $PATH_DATA_PROCESSED_CLEAN $PATH_DATA_PROCESSED_CLEAN/${SUBJECT}/anat $PATH_DATA_PROCESSED_CLEAN/derivatives/labels/${SUBJECT}/anat
# Put cropped image and json in cleaned dataset
rsync -avzh $PATH_DATA_PROCESSED/${SUBJECT}/anat/${file_path}_crop.nii.gz $PATH_DATA_PROCESSED_CLEAN/${SUBJECT}/anat/${file_path}.nii.gz
#rsync -avzh $PATH_DATA_PROCESSED/${SUBJECT}/anat/${file_path}.json $PATH_DATA_PROCESSED_CLEAN/${SUBJECT}/anat/${file_path}.json
# Move segmentation
rsync -avzh $PATH_DATA_PROCESSED/${SUBJECT}/anat/${fileseg}_crop.nii.gz $PATH_DATA_PROCESSED_CLEAN/derivatives/labels/${SUBJECT}/anat/${fileseg}.nii.gz
# Move json files of derivatives
rsync -avzh "${PATH_DATA}/derivatives/labels/${SUBJECT}/anat/${fileseg}.json" $PATH_DATA_PROCESSED_CLEAN/derivatives/labels/${SUBJECT}/anat/${fileseg}.json

# Verify presence of output files and write log file if error
# ------------------------------------------------------------------------------
FILES_TO_CHECK=(
"${SUBJECT}_UNIT1.nii.gz"
)
pwd
for file in ${FILES_TO_CHECK[@]}; do
if [[ ! -e $file ]]; then
echo "${SUBJECT}/anat/${file} does not exist" >> $PATH_LOG/_error_check_output_files.log
fi
done

# Display useful info for the log
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 "~~~"
Loading

0 comments on commit c516d9d

Please sign in to comment.