-
Notifications
You must be signed in to change notification settings - Fork 0
/
keep_largest_masks.sh
99 lines (79 loc) · 3.38 KB
/
keep_largest_masks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# 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
# GET PARAMS
# ======================================================================================================================
# SET DEFAULT VALUES FOR PARAMETERS.
# ----------------------------------------------------------------------------------------------------------------------
PATH_CONFIG="/home/GRAMES.POLYMTL.CA/p118739/data/config_data/add_sacrum.json"
DERIVATIVE_FOLDER="labels"
LABEL_SUFFIX="_label-sacrum_seg"
AUTHOR="Nathan Molinier"
PROCESS="nnUNet3D"
PATH_REPO="/home/GRAMES.POLYMTL.CA/p118739/code/totalsegmentator-mri"
PATH_NNUNET_MODEL="/home/GRAMES.POLYMTL.CA/p118739/data/nnUNet_results/Dataset300_SacrumDataset/nnUNetTrainer__nnUNetPlans__3d_fullres/"
PATH_NNUNET_ENV="${HOME}/code/nnUNet/nnUNet_env"
FOLD="0"
# Print variables to allow easier debug
echo "See variables:"
echo "PATH_CONFIG: ${PATH_CONFIG}"
echo "DERIVATIVE_FOLDER: ${DERIVATIVE_FOLDER}"
echo "LABEL_SUFFIX: ${LABEL_SUFFIX}"
echo
echo "PATH_REPO: ${PATH_REPO}"
echo "PATH_NNUNET_MODEL: ${PATH_NNUNET_MODEL}"
echo "PATH_NNUNET_ENV: ${PATH_NNUNET_ENV}"
echo "FOLD: ${FOLD}"
echo
# FUNCTIONS
# ======================================================================================================================
# Update a json sidecar file
update_json(){
local path_json="$1"
local process="$2"
local author="$3"
# Call python function
# TODO: the hard-coded path to the conda environment is not ideal.
"${PATH_NNUNET_ENV}"/bin/python3 "${PATH_REPO}"/scripts/create_jsonsidecar.py -path-json "${path_json}" -process "${process}" -author "${author}"
}
#
keep_largest_component(){
local path_json="$1"
local process="$2"
local author="$3"
# Call python function
# TODO: the hard-coded path to the conda environment is not ideal.
"${PATH_NNUNET_ENV}"/bin/python3 keep_largest_component.py -path-json "${path_json}" -process "${process}" -author "${author}"
}
# ======================================================================================================================
# SCRIPT STARTS HERE
# ======================================================================================================================
# Fetch datasets path
DATASETS_PATH=$(jq -r '.DATASETS_PATH' ${PATH_CONFIG})
# Go to folder where data will be copied and processed
cd "$DATASETS_PATH"
# Fetch TESTING files
FILES=$(jq -r '.TESTING[]' ${PATH_CONFIG})
# Loop across the files
for FILE_PATH in $FILES; do
BIDS_FOLDER=$(echo "$FILE_PATH" | cut -d / -f 1)
IN_FILE_NAME=$(echo "$FILE_PATH" | awk -F / '{print $NF}' )
OUT_FILE_NAME=${IN_FILE_NAME/".nii.gz"/"${LABEL_SUFFIX}.nii.gz"}
IMG_PATH=${FILE_PATH/"${BIDS_FOLDER}/"/}
SUB_PATH=${IMG_PATH/"/${IN_FILE_NAME}"/}
BIDS_DERIVATIVES="${BIDS_FOLDER}/derivatives/${DERIVATIVE_FOLDER}"
OUT_FOLDER="${BIDS_DERIVATIVES}/${SUB_PATH}"
OUT_PATH="${OUT_FOLDER}/${OUT_FILE_NAME}"
# Generate json sidecar
JSON_PATH=${OUT_PATH/".nii.gz"/".json"}
echo "Generate jsonsidecar ${JSON_PATH}"
generate_json "$JSON_PATH" "$PROCESS" "$AUTHOR"
# Create QC report
QC_PATH="${DATASETS_PATH}/qc"
echo "Add ${FILE_PATH} to QC ${QC_PATH}"
sct_qc -i "$FILE_PATH" -s "$OUT_PATH" -p sct_label_vertebrae -qc "$QC_PATH"
done