-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate.py
More file actions
554 lines (444 loc) · 20.3 KB
/
evaluate.py
File metadata and controls
554 lines (444 loc) · 20.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
"""
The following is a simple example evaluation method.
It is meant to run within a container.
To run it locally, you can call the following bash script:
./test_run.sh
This will start the evaluation, reads from ./test/input and outputs to ./test/output
To export the container and prep it for upload to Grand-Challenge.org you can call:
docker save example-evaluation-test-phase | gzip -c > example-evaluation-test-phase.tar.gz
Any container that shows the same behavior will do, this is purely an example of how one COULD do it.
Happy programming!
"""
from torch.multiprocessing import Pool, Process, set_start_method
set_start_method('spawn', force=True)
import json
from glob import glob
import SimpleITK
import numpy as np
import random
from statistics import mean
from collections import defaultdict
from pathlib import Path
from pprint import pformat, pprint
from image_metrics import ImageMetrics
from dose_metrics import DoseMetrics
from segmentation_metrics import SegmentationMetrics
import gc
import torch
from functools import partial
import os
numthreads="4"
# os.environ['OMP_NUM_THREADS'] = numthreads
# os.environ['MKL_NUM_THREADS'] = numthreads
# os.environ['MKL_DOMAIN_NUM_THREADS'] = numthreads
# os.environ['OPENBLAS_NUM_THREADS'] = numthreads
# os.environ['VECLIB_MAXIMUM_THREADS'] = numthreads
# os.environ['NUMEXPR_NUM_THREADS'] = numthreads
INPUT_DIRECTORY = Path("/input")
OUTPUT_DIRECTORY = Path("/output")
GROUND_TRUTH_DIRECTORY = Path("/opt/ml/input/data/ground_truth")
def tree(dir_path: Path, prefix: str=''):
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
"""A recursive generator, given a directory Path object
will yield a visual tree structure line by line
with each line prefixed by the same characters
"""
contents = list(dir_path.iterdir())
# contents each get pointers that are ├── with a final └── :
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, path in zip(pointers, contents):
yield prefix + pointer + path.name
if path.is_dir(): # extend the prefix and recurse:
extension = branch if pointer == tee else space
# i.e. space because last, └── , above so no more |
yield from tree(path, prefix=prefix+extension)
def init_pool(image_evaluator, segmentation_evaluator, dose_evaluator, debug, has_predictions):
"""Initializer to set global variables in worker processes."""
# this is a bit hacky but it works. Basically we load every evaluator
# once and put them in a global variable. Then each spawned process can access these
# variables. This means we only load our pytorch model once
global _image_evaluator, _segmentation_evaluator, _dose_evaluator, _debug, _has_predictions
_image_evaluator = image_evaluator
_dose_evaluator = dose_evaluator
_segmentation_evaluator = segmentation_evaluator
_debug = debug
_has_predictions = has_predictions
def main():
_debug = 'DEBUG' in os.environ and str(os.environ['DEBUG']).lower() in ['1', 'true', 't', 'y', 'yes']
nprocs = int(os.environ['NPROCS']) if 'NPROCS' in os.environ and int(os.environ['NPROCS']) > 0 else 1
if _debug:
print("INPUT DIR")
for line in tree(INPUT_DIRECTORY):
print(line)
print("")
print("OUTPUT DIR")
for line in tree(OUTPUT_DIRECTORY):
print(line)
print("")
print("GT DIR")
for line in tree(GROUND_TRUTH_DIRECTORY):
print(line)
if os.path.isfile(INPUT_DIRECTORY / "inputs.json"):
print("")
print("Found inputs.json. Contents: ")
with open(INPUT_DIRECTORY / "inputs.json", 'r') as f:
j = json.loads(f.read())
print(json.dumps(j, indent=2))
print("")
print(f"Running {nprocs} process{'' if nprocs==1 else 'es'}")
metrics = {}
if os.path.isfile(INPUT_DIRECTORY / "predictions.json"):
predictions = read_predictions()
has_predictions = True
else:
print(f"We're in a prediction-only phase. Reading files directly from {INPUT_DIRECTORY}")
predictions = glob(str(INPUT_DIRECTORY / "*.mha"))
has_predictions = False
# # We now process each algorithm job for this submission
# # Note that the jobs are not in any order!
# # We work that out from predictions.json
# # Start a number of process workers, using multiprocessing
# # The optimal number of workers ultimately depends on how many
# # resources each process() would call upon
# # global _image_evaluator, _segmentation_evaluator
_image_evaluator = ImageMetrics(debug=_debug)
# with torch.multiprocessing.Pool(processes=4, initializer=init_pool, initargs=(_image_evaluator, None, None, _debug, has_predictions)) as pool:
# try:
# metrics['image_eval_results'] = pool.map(evaluate_image_quality, predictions)
# except KeyboardInterrupt:
# print('Caught Ctrl+C, shutting pool down...')
# pool.terminate()
# pool.join()
# del _image_evaluator
# gc.collect()
_segmentation_evaluator = SegmentationMetrics(debug=_debug)
# with torch.multiprocessing.Pool(processes=2, initializer=init_pool, initargs=(None, _segmentation_evaluator, None, _debug, has_predictions)) as pool:
# try:
# metrics['seg_eval_results'] = pool.map(evaluate_geometric_fidelity, predictions)
# except KeyboardInterrupt:
# print('Caught Ctrl+C, shutting pool down...')
# pool.terminate()
# pool.join()
# del _segmentation_evaluator
# gc.collect()
_dose_evaluator = DoseMetrics(debug=_debug)
# with torch.multiprocessing.Pool(processes=1, initializer=init_pool, initargs=(None, None, _dose_evaluator, _debug, has_predictions)) as pool:
# try:
# metrics['dose_eval_results'] = pool.map(evaluate_dose_accuracy, predictions)
# except KeyboardInterrupt:
# print('Caught Ctrl+C, shutting pool down...')
# pool.terminate()
# pool.join()
# # print(metrics)
# if _debug:
# print(metrics)
# # Step 1: Create a mapping from common_key to merged dict
# merged = defaultdict(dict)
# for key in metrics:
# for item in metrics[key]:
# item_id = item.pop('case', None)
# if item_id is not None:
# merged[item_id].update(item)
# metrics = {}
# metrics['results'] = list(merged.values())
# if _debug:
# print(metrics)
# _dose_evaluator = DoseMetrics(debug=_debug)
# metrics['results'] = []
with torch.multiprocessing.Pool(processes=nprocs, initializer=init_pool, initargs=(_image_evaluator, _segmentation_evaluator, _dose_evaluator, _debug, has_predictions)) as pool:
try:
metrics["results"] = pool.map(process, predictions)
except KeyboardInterrupt:
print('Caught Ctrl+C, shutting pool down...')
pool.terminate()
pool.join()
# if _debug:
# print(metrics)
# Now generate an overall score(s) for this submission.
# For every case in the dataset, we have a metric. For each metric,
# The aggregates listed below are computed over the entire dataset
aggregate_functions = [
{
'name': 'mean',
'function': np.mean
},
{
'name': 'max',
'function': np.max
},
{
'name': 'min',
'function': np.min
},
{
'name': 'std',
'function': np.std
},
{
'name': '25pc',
'function': partial(np.quantile, q=0.25)
},
{
'name': '50pc',
'function': partial(np.quantile, q=0.50)
},
{
'name': '75pc',
'function': partial(np.quantile, q=0.75)
},
{
'name': 'count',
'function': len
},
]
metrics["aggregates"] = {}
if len(metrics['results']) > 0:
for metric in metrics["results"][0].keys():
metrics["aggregates"][metric] = {}
all_results = [result[metric] for result in metrics["results"]]
for aggregate_function in aggregate_functions:
metrics["aggregates"][metric][aggregate_function['name']] = aggregate_function['function'](all_results)
if _debug:
print(metrics)
# Make sure to save the metrics
write_metrics(metrics=metrics)
return 0
def evaluate_dose_accuracy(job):
# Extract the patient ID
if _has_predictions:
patient_id = find_patient_id(values=job["inputs"], slug='body')
else:
# filename is like "/input/sct_1HNXxxx.mha"
patient_id = job.split('/')[-1].split('.')[0][-7:]
_, spacing, origin, direction = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "ct" / f"{patient_id}.mha", return_orientation=True)
if _has_predictions:
synthetic_ct_location = get_file_location(
job_pk=job["pk"],
values=job["outputs"],
slug="synthetic-ct-image",
)
else:
synthetic_ct_location = job
if _has_predictions:
# Then, read the sCT and impose the spatial dimension of the ground truth
synthetic_ct, full_sct_path = load_image_file(
location=synthetic_ct_location, spacing=spacing, origin=origin, direction=direction
)
else:
synthetic_ct = load_image_file_directly(location=synthetic_ct_location, set_orientation=(spacing, origin, direction))
full_sct_path = synthetic_ct_location
# if _has_predictions:
# synthetic_ct_location = get_file_location(
# job_pk=job["pk"],
# values=job["outputs"],
# slug="synthetic-ct-image",
# )
# else:
# synthetic_ct_location = job
if os.path.isdir(GROUND_TRUTH_DIRECTORY / "dose" / patient_id):
dose_metrics = _dose_evaluator.score_patient(full_sct_path, GROUND_TRUTH_DIRECTORY / "mask" / f"{patient_id}.mha", GROUND_TRUTH_DIRECTORY / "dose" / patient_id, patient_id)
else:
dose_metrics = {}
gc.collect()
return dose_metrics
def evaluate_image_quality(job):
# Firstly, find the location of the results
if _has_predictions:
synthetic_ct_location = get_file_location(
job_pk=job["pk"],
values=job["outputs"],
slug="synthetic-ct-image",
)
else:
synthetic_ct_location = job
# Extract the patient ID
if _has_predictions:
patient_id = find_patient_id(values=job["inputs"], slug='body')
else:
# filename is like "/input/sct_1HNXxxx.mha"
patient_id = job.split('/')[-1].split('.')[0][-7:]
# and load the ground-truth along the affine image matrix (Or direction/origin/spacing in SimpleITK terms)
gt_img, spacing, origin, direction = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "ct" / f"{patient_id}.mha", return_orientation=True)
if _has_predictions:
# Then, read the sCT and impose the spatial dimension of the ground truth
synthetic_ct, full_sct_path = load_image_file(
location=synthetic_ct_location, spacing=spacing, origin=origin, direction=direction
)
else:
synthetic_ct = load_image_file_directly(location=synthetic_ct_location, set_orientation=(spacing, origin, direction))
full_sct_path = synthetic_ct_location
mask = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "mask" / f"{patient_id}.mha", set_orientation=(spacing, origin, direction))
# score the subject based on image metrics
image_metrics = _image_evaluator.score_patient(gt_img, synthetic_ct, mask, patient_id)
return image_metrics
def evaluate_geometric_fidelity(job):
# Extract the patient ID
if _has_predictions:
patient_id = find_patient_id(values=job["inputs"], slug='body')
else:
# filename is like "/input/sct_1HNXxxx.mha"
patient_id = job.split('/')[-1].split('.')[0][-7:]
if _has_predictions:
synthetic_ct_location = get_file_location(
job_pk=job["pk"],
values=job["outputs"],
slug="synthetic-ct-image",
)
else:
synthetic_ct_location = job
_, spacing, origin, direction = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "ct" / f"{patient_id}.mha", return_orientation=True)
if _has_predictions:
# Then, read the sCT and impose the spatial dimension of the ground truth
synthetic_ct, full_sct_path = load_image_file(
location=synthetic_ct_location, spacing=spacing, origin=origin, direction=direction
)
else:
synthetic_ct = load_image_file_directly(location=synthetic_ct_location, set_orientation=(spacing, origin, direction))
full_sct_path = synthetic_ct_location
# Do the same for the ground-truth TotalSegmentator segmentation and the mask
gt_segmentation = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "segmentation" / f"{patient_id}.mha", set_orientation=(spacing, origin, direction))
mask = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "mask" / f"{patient_id}.mha", set_orientation=(spacing, origin, direction))
seg_metrics = _segmentation_evaluator.score_patient(full_sct_path, mask, gt_segmentation, patient_id, orientation=(spacing, origin, direction))
del mask
del gt_segmentation
gc.collect()
return seg_metrics
def process(job):
# Processes a single algorithm job, looking at the outputs
gc.collect()
report = "Processing:\n"
report += pformat(job)
report += "\n"
# Firstly, find the location of the results
if _has_predictions:
synthetic_ct_location = get_file_location(
job_pk=job["pk"],
values=job["outputs"],
slug="synthetic-ct-image",
)
else:
synthetic_ct_location = job
# Extract the patient ID
if _has_predictions:
patient_id = find_patient_id(values=job["inputs"], slug='body')
else:
# filename is like "/input/sct_1HNXxxx.mha"
patient_id = job.split('/')[-1].split('.')[0][-7:]
# and load the ground-truth along the affine image matrix (Or direction/origin/spacing in SimpleITK terms)
gt_img, spacing, origin, direction = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "ct" / f"{patient_id}.mha", return_orientation=True)
if _has_predictions:
# Then, read the sCT and impose the spatial dimension of the ground truth
synthetic_ct, full_sct_path = load_image_file(
location=synthetic_ct_location, spacing=spacing, origin=origin, direction=direction
)
else:
synthetic_ct = load_image_file_directly(location=synthetic_ct_location, set_orientation=(spacing, origin, direction))
full_sct_path = synthetic_ct_location
# Do the same for the ground-truth TotalSegmentator segmentation and the mask
gt_segmentation = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "segmentation" / f"{patient_id}.mha", set_orientation=(spacing, origin, direction))
mask = load_image_file_directly(location=GROUND_TRUTH_DIRECTORY / "mask" / f"{patient_id}.mha", set_orientation=(spacing, origin, direction))
# score the subject based on image metrics
image_metrics = _image_evaluator.score_patient(gt_img, synthetic_ct, mask)
gc.collect()
#... and segmentation metrics
seg_metrics = _segmentation_evaluator.score_patient(full_sct_path, mask, gt_segmentation, patient_id, orientation=(spacing, origin, direction))
# if we are in test phase, there is a doseplan for every patient in this folder
gc.collect()
if os.path.isdir(GROUND_TRUTH_DIRECTORY / "dose" / patient_id):
dose_metrics = _dose_evaluator.score_patient(full_sct_path, GROUND_TRUTH_DIRECTORY / "mask" / f"{patient_id}.mha", GROUND_TRUTH_DIRECTORY / "dose" / patient_id, patient_id)
else:
dose_metrics = {}
gc.collect()
# dose_metrics = {}
if _debug:
print(patient_id, {**image_metrics, **seg_metrics, **dose_metrics})
# Finally, return the results
gc.collect()
return {
**image_metrics,
**seg_metrics,
**dose_metrics
}
def find_patient_id(*, values, slug):
# find the patient id (e.g. TXXXYYY, where T is task (1 or 2), XXX is anatomy and center
# (e.g., THC for thorax from center C) and YYY is the patient number (e.g., 001))
for value in values:
if value["interface"]["slug"] == slug:
full_name = value['image']['name'] # this name is like "mask_1ABCxxx.mha"
return full_name.split('.')[0].split('_')[-1]
raise RuntimeError(f"Cannot get patient name because interface {slug} not found!")
def print_inputs():
# Just for convenience, in the logs you can then see what files you have to work with
input_files = [str(x) for x in Path(INPUT_DIRECTORY).rglob("*.mha") if x.is_file()]
print("Input Files:")
pprint(input_files)
print("")
def read_predictions():
# The prediction file tells us the location of the users' predictions
with open(INPUT_DIRECTORY / "predictions.json") as f:
return json.loads(f.read())
def get_image_name(*, values, slug):
# This tells us the user-provided name of the input or output image
for value in values:
if value["interface"]["slug"] == slug:
return value["image"]["name"]
raise RuntimeError(f"Image with interface {slug} not found!")
def get_interface_relative_path(*, values, slug):
# Gets the location of the interface relative to the input or output
for value in values:
if value["interface"]["slug"] == slug:
return value["interface"]["relative_path"]
raise RuntimeError(f"Value with interface {slug} not found!")
def get_input_file_location(*, values, slug):
relative_path = get_interface_relative_path(values=values, slug=slug)
for value in values:
if value["interface"]["slug"] == slug:
full_name = value['image']['name'] # this name is like "mask_1ABCxxx.mha"
return INPUT_DIRECTORY / relative_path / full_name
raise RuntimeError(f"Cannot find input file for {slug}!")
def get_file_location(*, job_pk, values, slug):
# Where a job's output file will be located in the evaluation container
relative_path = get_interface_relative_path(values=values, slug=slug)
return INPUT_DIRECTORY / job_pk / "output" / relative_path
def load_image_file_directly(*, location, return_orientation=False, set_orientation=None):
# immediatly load the file and find its orientation
result = SimpleITK.ReadImage(location)
# Note, transpose needed because Numpy is ZYX according to SimpleITKs XYZ
img_arr = np.transpose(SimpleITK.GetArrayFromImage(result), [2, 1, 0])
if return_orientation:
spacing = result.GetSpacing()
origin = result.GetOrigin()
direction = result.GetDirection()
return img_arr, spacing, origin, direction
else:
# If desired, force the orientation on an image before converting to NumPy array
if set_orientation is not None:
spacing, origin, direction = set_orientation
result.SetSpacing(spacing)
result.SetOrigin(origin)
result.SetDirection(direction)
# Note, transpose needed because Numpy is ZYX according to SimpleITKs XYZ
return np.transpose(SimpleITK.GetArrayFromImage(result), [2, 1, 0])
def load_image_file(*, location, spacing=None, origin=None, direction=None):
# Use SimpleITK to read a file in a directory
input_files = glob(str(location / "*.nii.gz")) + glob(str(location / "*.tiff")) + glob(str(location / "*.mha"))
result = SimpleITK.ReadImage(input_files[0])
if spacing is not None:
result.SetSpacing(spacing)
if origin is not None:
result.SetOrigin(origin)
if direction is not None:
result.SetDirection(direction)
# Convert it to a Numpy array
return np.transpose(SimpleITK.GetArrayFromImage(result), [2, 1, 0]), input_files[0]
def write_metrics(*, metrics):
# Write a json document used for ranking results on the leaderboard
with open(OUTPUT_DIRECTORY / "metrics.json", "w") as f:
f.write(json.dumps(metrics, indent=4))
if __name__ == "__main__":
raise SystemExit(main())