-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_lit_containerized.sh
executable file
·207 lines (177 loc) · 5.52 KB
/
run_lit_containerized.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
set -e
function usage()
{
cat << EOF
Usage: run_lit_containerized.sh --input_image <input_t1w_volume> --mask_image <lesion_mask_volume> --output_directory <output_directory> [OPTIONS]
run_lit_containerized.sh takes a T1 full head image and creates:
(i) an inpainted T1w image using a lesion mask
(ii) (optional) whole brain segmentation and cortical surface reconstruction using FastSurferVINN
FLAGS:
-h, --help
Print this message and exit
--version
Print the version number and exit
--gpus <gpus>
GPUs to use. Default: all
-i, --input_image <input_image>
Path to the input T1w volume
-m, --mask_image <mask_image>
Path to the lesion mask volume (same dimensions as input_image, >0 for lesion, 0 for background)
-o, --output_directory <output_directory>
Path to the output directory
Examples:
./run_lit_containerized.sh -i t1w.nii.gz -m lesion.nii.gz -o ./output
./run_lit_containerized.sh -i t1w.nii.gz -m lesion.nii.gz -o ./output --fastsurfer --gpus 0
REFERENCES:
If you use LIT for research publications, please cite:
Pollak C, Kuegler D, Bauer T, Rueber T, Reuter M, FastSurfer-LIT: Lesion Inpainting Tool for Whole
Brain MRI Segmentation with Tumors, Cavities and Abnormalities, Accepted for Imaging Neuroscience.
EOF
}
# Validate required parameters
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
POSITIONAL_ARGS=()
VERSION="$(grep "^version\\s*=\\s*\"" "$(dirname "${BASH_SOURCE[0]}")/pyproject.toml")"
VERSION="${VERSION/version = /}"
VERSION="${VERSION//\"/}"
# Initialize RUN_FASTSURFER to false by default
RUN_FASTSURFER=false
# Initialize USE_SINGULARITY to false by default
USE_SINGULARITY=false
while [[ $# -gt 0 ]]; do
case $1 in
--gpus)
GPUS="$2"
shift # past argument
shift # past value
;;
-i|--input_image)
INPUT_IMAGE="$2"
shift # past argument
shift # past value
;;
-m|--mask_image)
MASK_IMAGE="$2"
shift # past argument
shift # past value
;;
-o|--output_directory)
OUT_DIR="$2"
shift # past argument
shift # past value
;;
--fastsurfer)
RUN_FASTSURFER=true
shift # past value
;;
-h|--help)
usage
exit
;;
--version)
project_dir="$(dirname "${BASH_SOURCE[0]}")"
hash_file="$(dirname "${BASH_SOURCE[0]}")/git.hash"
if [[ -n "$(which git)" ]] && (git -C "$project_dir" rev-parse 2>/dev/null ) ; then
HASH="+$(git -C "$project_dir" rev-parse --short HEAD)"
elif [[ -e "$hash_file" ]] ; then
HASH="+$(cat "$hash_file")"
else
HASH=""
fi
echo "$VERSION$HASH"
exit
;;
--fs_license)
fs_license="$2"
shift # past argument
shift # past value
;;
--singularity)
USE_SINGULARITY=true
shift # past value
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
# Validate required parameters and files
if [ -z "$INPUT_IMAGE" ] || [ -z "$MASK_IMAGE" ] || [ -z "$OUT_DIR" ]; then
echo "Error: input_image, mask_image, and output_directory are required parameters"
usage
exit 1
fi
if [ ! -f "$INPUT_IMAGE" ]; then
echo "Error: Input image not found: $INPUT_IMAGE"
exit 1
fi
if [ ! -f "$MASK_IMAGE" ]; then
echo "Error: Mask image not found: $MASK_IMAGE"
exit 1
fi
mkdir -p "$OUT_DIR"
# Make all inputs absolute paths
INPUT_IMAGE=$(realpath "$INPUT_IMAGE")
MASK_IMAGE=$(realpath "$MASK_IMAGE")
OUT_DIR=$(realpath "$OUT_DIR")
if [ -z "$GPUS" ]; then
GPUS="all"
fi
fs_license=""
# try to find license file, using default locations
if [ "$RUN_FASTSURFER" = true ]; then
if [ -z "$fs_license" ]; then
for license_path in \
"/fs_license/license.txt" \
"$FREESURFER_HOME/license.txt" \
"$FREESURFER_HOME/.license"; do
if [ -f "$license_path" ]; then
fs_license="$license_path"
break
fi
done
if [ -z "$fs_license" ]; then
echo "Error: FreeSurfer license file not found"
exit 1
fi
fi
POSITIONAL_ARGS+=("--fastsurfer")
else
fs_license=/dev/null
fi
# Run command based on the containerization tool
if [ "$USE_SINGULARITY" = true ]; then
if [ ! -f "containerization/deepmi_lit.simg" ]; then
echo "=============== Downloading Singularity image... ==============="
wget https://zenodo.org/records/14497226/files/deepmi_lit.simg -O containerization/deepmi_lit_download.simg
mv containerization/deepmi_lit_download.simg containerization/deepmi_lit.simg
fi
if [ ! -f "containerization/deepmi_lit.simg" ]; then
echo "Error: Singularity image not found: containerization/deepmi_lit.simg"
exit 1
fi
singularity exec --nv \
-B "${INPUT_IMAGE}":"${INPUT_IMAGE}":ro \
-B "${MASK_IMAGE}":"${MASK_IMAGE}":ro \
-B "${OUT_DIR}":"${OUT_DIR}" \
-B "$(pwd)":/workspace \
-B "${fs_license:-/dev/null}":/fs_license/license.txt:ro \
./containerization/deepmi_lit.simg \
/inpainting/run_lit.sh -i "${INPUT_IMAGE}" -m "${MASK_IMAGE}" -o "${OUT_DIR}" "${POSITIONAL_ARGS[@]}"
else
docker run --gpus "device=$GPUS" -it --ipc=host \
--ulimit memlock=-1 --ulimit stack=67108864 --rm \
-v "${INPUT_IMAGE}":"${INPUT_IMAGE}":ro \
-v "${MASK_IMAGE}":"${MASK_IMAGE}":ro \
-v "${OUT_DIR}":"${OUT_DIR}" \
-u "$(id -u):$(id -g)" \
-v "$(pwd)":/workspace \
-v "${fs_license:-/dev/null}":/fs_license/license.txt:ro \
deepmi/lit:$VERSION -i "${INPUT_IMAGE}" -m "${MASK_IMAGE}" -o "${OUT_DIR}" "${POSITIONAL_ARGS[@]}"
fi