[ab8281]: / brats_toolkit / cli.py

Download this file

314 lines (293 with data), 10.1 kB

  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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
# Author: Christoph Berger
# Script for evaluation and bulk segmentation of Brain Tumor Scans
# using the MICCAI BRATS algorithmic repository
#
# Please refer to README.md and LICENSE.md for further documentation
# This software is not certified for clinical use.
import argparse
import pprint
import subprocess
import sys
from . import fusionator, preprocessor, segmentor
def list_dockers():
seg = segmentor.Segmentor()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(seg.config)
def list_docker_ids():
seg = segmentor.Segmentor()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(seg.config.keys())
def list_docker_gpu():
seg = segmentor.Segmentor()
print("all these images support GPU computations:")
for id in seg.config.keys():
if seg.config[id]["runtime"] == "nvidia":
print(id)
def list_docker_cpu():
seg = segmentor.Segmentor()
print("all these images support CPU computations:")
for id in seg.config.keys():
if seg.config[id]["runtime"] == "runc":
print(id)
def fusion():
parser = argparse.ArgumentParser(
description="Runs the Docker orchestra to fuse segmentations. All inputs have to have equal shape and label values"
)
parser.add_argument(
"-i",
"--input",
required=True,
help="Input directory containing all .nii.gz files to be fused",
)
parser.add_argument(
"-m",
"--method",
required=True,
help="Method for fusion: mav for majority voting, simple for SIMPLE",
)
parser.add_argument(
"-o", "--output", help="Filename for the output in format filename.nii.gz"
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Verbose mode outputs log info to the command line.",
)
try:
args = parser.parse_args()
except SystemExit as e:
if e.code == 2:
parser.print_help()
sys.exit(e.code)
try:
# runs the segmentation with all the settings wished for by the user
fus = fusionator.Fusionator(verbose=args.verbose)
fus._dirFuse(args.input, method=args.method, outputPath=args.output)
except subprocess.CalledProcessError as e:
# Ignoring errors happening in the Docker Process, otherwise we'd e.g. get error messages on exiting the Docker via CTRL+D.
pass
except Exception as e:
print("ERROR DETAIL: ", e)
def segmentation():
parser = argparse.ArgumentParser(
description="Runs the Docker orchestra to segment and fuse segmentations based on the"
"BraTS algorithmic repository"
"Please keep in mind that some models require Nvidia-Docker to run as"
" they need a supported GPU."
)
parser.add_argument(
"-l",
"--list",
help="List all models available for segmentation.",
action="store_true",
)
parser.add_argument(
"-ll",
"--longlist",
help="List all models available for segmentation with details.",
action="store_true",
)
parser.add_argument(
"-lc", "--cpulist", help="List all models supporting cpus.", action="store_true"
)
parser.add_argument(
"-lg", "--gpulist", help="List all models supporting gpus.", action="store_true"
)
parser.add_argument("-t1", required=True, help="Path to the t1 modality.")
parser.add_argument("-t1c", required=True, help="Path to the t1c modality.")
parser.add_argument("-t2", required=True, help="Path to the t2 modality.")
parser.add_argument("-fla", required=True, help="Path to the fla modality.")
parser.add_argument(
"-d",
"--docker",
required=True,
help="Container ID or method used for fusion. (mav, simple, all). Run brats-orchestra --list to display all options.",
)
parser.add_argument(
"-o", "--output", required=True, help="Path to the desired output file."
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Verbose mode outputs log info to the command line.",
)
parser.add_argument(
"-c", "--config", help="Add a path to a custom config file for dockers here."
)
parser.add_argument(
"-g",
"--gpu",
action="store_true",
help="Pass this flag if your Docker version already supports the --gpus flag.",
)
parser.add_argument("-gi", "--gpuid", help="Specify the GPU bus ID to be used.")
try:
if "-l" in sys.argv[1:] or "--list" in sys.argv[1:]:
list_docker_ids()
sys.exit(0)
elif "-ll" in sys.argv[1:] or "--longlist" in sys.argv[1:]:
list_dockers()
sys.exit(0)
elif "-lg" in sys.argv[1:] or "--gpulist" in sys.argv[1:]:
list_docker_gpu()
sys.exit(0)
elif "-lc" in sys.argv[1:] or "--cpulist" in sys.argv[1:]:
list_docker_cpu()
sys.exit(0)
else:
args = parser.parse_args()
except SystemExit as e:
if e.code == 2:
parser.print_help()
sys.exit(e.code)
try:
# runs the segmentation with all the settings wished for by the user
seg = segmentor.Segmentor(
config=args.config,
verbose=args.verbose,
newdocker=args.gpu,
gpu=str(args.gpuid),
)
seg.segment(
t1=args.t1,
t1c=args.t1c,
t2=args.t2,
fla=args.fla,
cid=args.docker,
outputPath=args.output,
)
except subprocess.CalledProcessError as e:
# Ignoring errors happening in the Docker Process, otherwise we'd e.g. get error messages on exiting the Docker via CTRL+D.
pass
except Exception as e:
print("ERROR DETAIL: ", e)
def batchpreprocess():
parser = argparse.ArgumentParser(
description="Runs the preprocessing for MRI scans on a folder of images."
)
parser.add_argument(
"-i",
"--input",
required=True,
help="The input directory with 4 modalities in unprocessed Nifti format.",
)
parser.add_argument(
"-o", "--output", required=True, help="Path to the desired output directory."
)
parser.add_argument(
"-s",
"--skipupdate",
action="store_true",
help="If passed, the backend will not be updated.",
)
parser.add_argument(
"-c",
"--confirm",
action="store_true",
help="If passed, the container will ask for confirmation",
)
parser.add_argument(
"-g",
"--gpu",
action="store_true",
help="Pass this flag if you want to use GPU computations.",
)
parser.add_argument("-gi", "--gpuid", help="Specify the GPU bus ID to be used.")
try:
args = parser.parse_args()
except SystemExit as e:
if e.code == 2:
parser.print_help()
sys.exit(e.code)
try:
# runs the preprocessing with all the settings wished for by the user
pre = preprocessor.Preprocessor()
if args.gpu:
mode = "gpu"
else:
mode = "cpu"
if args.gpuid:
gpuid = str(args.gpuid)
else:
gpuid = "0"
pre.batch_preprocess(
exam_import_folder=args.input,
exam_export_folder=args.output,
mode=mode,
confirm=args.confirm,
skipUpdate=args.skipupdate,
gpuid=gpuid,
)
except subprocess.CalledProcessError as e:
# Ignoring errors happening in the Docker Process, otherwise we'd e.g. get error messages on exiting the Docker via CTRL+D.
pass
except Exception as e:
print("ERROR DETAIL: ", e)
def singlepreprocess():
parser = argparse.ArgumentParser(
description="Runs the preprocessing for MRI scans on a single set of images."
)
parser.add_argument("-t1", required=True, help="Path to the t1 modality.")
parser.add_argument("-t1c", required=True, help="Path to the t1c modality.")
parser.add_argument("-t2", required=True, help="Path to the t2 modality.")
parser.add_argument("-fla", required=True, help="Path to the fla modality.")
parser.add_argument(
"-o", "--output", required=True, help="Path to the desired output directory."
)
parser.add_argument(
"-s",
"--skipupdate",
action="store_true",
help="If passed, the backend will not be updated.",
)
parser.add_argument(
"-c",
"--confirm",
action="store_true",
help="If passed, the container will ask for confirmation",
)
parser.add_argument(
"-g",
"--gpu",
action="store_true",
help="Pass this flag if you want to use GPU computations.",
)
parser.add_argument("-gi", "--gpuid", help="Specify the GPU bus ID to be used.")
try:
args = parser.parse_args()
except SystemExit as e:
if e.code == 2:
parser.print_help()
sys.exit(e.code)
try:
# runs the preprocessing with all the settings wished for by the user
pre = preprocessor.Preprocessor()
if args.gpu:
mode = "gpu"
else:
mode = "cpu"
if args.gpuid:
gpuid = str(args.gpuid)
else:
gpuid = "0"
pre.single_preprocess(
t1File=args.t1,
t1cFile=args.t1c,
t2File=args.t2,
flaFile=args.fla,
outputFolder=args.output,
mode=mode,
confirm=args.confirm,
skipUpdate=args.skipupdate,
gpuid=gpuid,
)
except subprocess.CalledProcessError as e:
# Ignoring errors happening in the Docker Process, otherwise we'd e.g. get error messages on exiting the Docker via CTRL+D.
pass
except Exception as e:
print("ERROR DETAIL: ", e)
if __name__ == "__main__":
segmentation()