Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1d0d1ec
Add script to generate missing SC GT (using the SCIseg model)
valosekj Feb 1, 2024
0255d29
Add convert_bids_to_nnUNetv2.py script.
valosekj Feb 1, 2024
5e46951
rename 'train_subjects' --> 'train_images'
valosekj Feb 2, 2024
1acefe6
rename 'test_subjects' --> 'test_images'
valosekj Feb 2, 2024
163d613
change '--region-based' to default=True
valosekj Feb 2, 2024
2fe15f8
update suffixes to be compatible with dcm-zurich-lesions datasets
valosekj Feb 2, 2024
7c9d9d7
make clear the script work with images not subjects
valosekj Feb 2, 2024
10ea865
make clear the script work with images not subjects
valosekj Feb 2, 2024
2127bba
Add print
valosekj Feb 2, 2024
d10ac8c
remove empty line
valosekj Feb 2, 2024
c8d5bd5
update description
valosekj Feb 2, 2024
4d787ae
Add nnUNetv2 training scripts
valosekj Feb 2, 2024
186839e
Improve print
valosekj Feb 2, 2024
ef20212
Add example usage
valosekj Feb 2, 2024
1df7a90
update permissions to 770
valosekj Feb 2, 2024
b09b313
Fix input args for training/02_compute_anima_metrics.py
valosekj Feb 4, 2024
375960f
Make explicit the script converts data to REGION-BASED format
valosekj Feb 5, 2024
8f350f6
Fix typo
valosekj Feb 5, 2024
c4452b1
Fix example usage
valosekj Feb 5, 2024
e6cc7d5
update '--dataset-name' arg default value
valosekj Feb 5, 2024
5146f92
remove obsolete condition
valosekj Feb 5, 2024
d714bdd
update 'create_region_based_label' docstring
valosekj Feb 5, 2024
bfe1d8f
Add script for MULTI-CHANNEL trianing and update utils.py
valosekj Feb 5, 2024
bbc391b
remove unnecessary code
valosekj Feb 8, 2024
dea4640
refactor the code to make it compatible with both region-based and mu…
valosekj Feb 8, 2024
2b59f2b
Add the 'nnUNetTrainerDiceCELoss_noSmooth' trainer
valosekj Feb 8, 2024
51736dc
Print number of training and validation images for each site
valosekj Feb 9, 2024
02b078f
udpate help
valosekj Feb 12, 2024
71a373d
include seed into dataset name
valosekj Feb 12, 2024
22e28d5
Add 'seed'
valosekj Feb 12, 2024
73b9614
Remove 'seed' -- seems not to be compatible with 'nnunetv2/utilities/…
valosekj Feb 12, 2024
5aa6211
Revert "Remove 'seed' -- seems not to be compatible with 'nnunetv2/ut…
valosekj Feb 12, 2024
baba718
Update 'dataset_num'
valosekj Feb 12, 2024
cc4348a
fix typo
valosekj Feb 17, 2024
4ec0b3a
fix example usage
valosekj Feb 17, 2024
d6ebed8
remove unused import
valosekj Feb 17, 2024
686a679
fix comment
valosekj Feb 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
418 changes: 418 additions & 0 deletions dataset_conversion/convert_bids_to_nnUNetv2_multi-channel.py

Large diffs are not rendered by default.

431 changes: 431 additions & 0 deletions dataset_conversion/convert_bids_to_nnUNetv2_region-based.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions dataset_conversion/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
loguru
scikit-learn
tqdm
nibabel
pyyaml
collections-extended
127 changes: 127 additions & 0 deletions dataset_conversion/segment_sc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash
#
# Run the SCIseg nnUNet model on T2w images to segment spinal cord.
#
# Note: conda environment with nnUNetV2 is required to run this script.
# For details how to install nnUNetV2, see:
# https://github.com/ivadomed/utilities/blob/main/quick_start_guides/nnU-Net_quick_start_guide.md#installation
#
# Usage:
# sct_run_batch -config config.json
#
# Example of config.json:
# {
# "path_data" : "<PATH_TO_DATASET>",
# "path_output" : "<PATH_TO_DATASET>_2024-XX-XX",
# "script" : "~/code/model-seg-dcm/dataset_conversion/segment_sc",
# "jobs" : 8,
# "script_args" : "~/code/model_seg_sci/packaging/run_inference_single_subject.py ~/models/sci-multisite-model"
# }
#
# 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
PATH_NNUNET_SCRIPT=$2
PATH_NNUNET_MODEL=$3

echo "SUBJECT: ${SUBJECT}"
echo "PATH_NNUNET_SCRIPT: ${PATH_NNUNET_SCRIPT}"
echo "PATH_NNUNET_MODEL: ${PATH_NNUNET_MODEL}"

# ------------------------------------------------------------------------------
# CONVENIENCE FUNCTIONS
# ------------------------------------------------------------------------------

# Segment spinal cord using our nnUNet model
segment_sc_nnUNet(){
local file="$1"
local kernel="$2" # 2d or 3d

# output file name
FILESEG="${file}_seg_nnunet_${kernel}"

# Get the start time
start_time=$(date +%s)
# Run SC segmentation
python ${PATH_NNUNET_SCRIPT} -i ${file}.nii.gz -o ${FILESEG}.nii.gz -path-model ${PATH_NNUNET_MODEL}/nnUNetTrainer__nnUNetPlans__${kernel}_fullres -pred-type sc -use-gpu
# Get the end time
end_time=$(date +%s)
# Calculate the time difference
execution_time=$(python3 -c "print($end_time - $start_time)")
echo "${FILESEG},${execution_time}" >> ${PATH_RESULTS}/execution_time.csv

# Generate spinal cord QC report
sct_qc -i ${file}.nii.gz -s ${FILESEG}.nii.gz -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute ANIMA segmentation performance metrics
#compute_anima_metrics ${FILESEG} ${file}_seg-manual
}

# ------------------------------------------------------------------------------
# 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/'
# Note: we copy only axial T2w image to save space
rsync -Ravzh ${PATH_DATA}/./${SUBJECT}/anat/${SUBJECT//[\/]/_}_*acq-ax_T2w.* .

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

# ------------------------------------------------------------------------------
# T2w axial
# ------------------------------------------------------------------------------
# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/'

file_t2="${SUBJECT//[\/]/_}"_acq-ax_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
fi

# Segment SC using the SCIseg nnUNet model
segment_sc_nnUNet "${file_t2}" '3d'

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