-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
148 lines (107 loc) · 4.79 KB
/
helper.py
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
import json
import tempfile
from pathlib import Path
from pprint import pprint
import SimpleITK as sitk
from PIL import Image
import tifffile
import numpy as np
DEFAULT_GLAUCOMATOUS_FEATURES = {
"appearance neuroretinal rim superiorly": None,
"appearance neuroretinal rim inferiorly": None,
"retinal nerve fiber layer defect superiorly": None,
"retinal nerve fiber layer defect inferiorly": None,
"baring of the circumlinear vessel superiorly": None,
"baring of the circumlinear vessel inferiorly": None,
"nasalization of the vessel trunk": None,
"disc hemorrhages": None,
"laminar dots": None,
"large cup": None,
}
def inference_tasks():
input_files = [x for x in Path("/input").rglob("*") if x.is_file()]
print("Input Files:")
pprint(input_files)
is_referable_glaucoma_stacked = []
is_referable_glaucoma_likelihood_stacked = []
glaucomatous_features_stacked = []
def save_prediction(
is_referable_glaucoma,
likelihood_referable_glaucoma,
glaucomatous_features=None,
):
is_referable_glaucoma_stacked.append(is_referable_glaucoma)
is_referable_glaucoma_likelihood_stacked.append(likelihood_referable_glaucoma)
if glaucomatous_features is not None:
glaucomatous_features_stacked.append({**DEFAULT_GLAUCOMATOUS_FEATURES, **glaucomatous_features})
else:
glaucomatous_features_stacked.append(DEFAULT_GLAUCOMATOUS_FEATURES)
for file_path in input_files:
if file_path.suffix == ".mha" or file_path.suffix == ".png": # A single image
yield from single_file_inference(image_file=file_path, callback=save_prediction)
elif file_path.suffix == ".tiff": # A stack of images
yield from stack_inference(path=file_path, callback=save_prediction)
write_referable_glaucoma_decision(is_referable_glaucoma_stacked)
write_referable_glaucoma_decision_likelihood(
is_referable_glaucoma_likelihood_stacked
)
write_glaucomatous_features(glaucomatous_features_stacked)
def single_file_inference(image_file, callback):
with tempfile.TemporaryDirectory() as temp_dir:
image = sitk.ReadImage(image_file)
# Define the output file path
output_path = Path(temp_dir) / "image.jpg"
# Save the 2D slice as a JPG file
sitk.WriteImage(image, str(output_path))
# Call back that saves the result
def save_prediction(
is_referable_glaucoma,
likelihood_referable_glaucoma,
glaucomatous_features=None,
):
glaucomatous_features = (
glaucomatous_features or DEFAULT_GLAUCOMATOUS_FEATURES
)
write_referable_glaucoma_decision([is_referable_glaucoma])
write_referable_glaucoma_decision_likelihood(
[likelihood_referable_glaucoma]
)
write_glaucomatous_features(
[{**DEFAULT_GLAUCOMATOUS_FEATURES, **glaucomatous_features}]
)
yield output_path, callback
def stack_inference(path, callback):
de_stacked_images = []
# Unpack the stack
#with Image.open(path) as tiff_image:
#for page_num in range(tiff_image.n_frames):
# Select the current page
#tiff_image.seek(page_num)
# Define the output file path
#output_path ='./data/'+ f"image_{page_num + 1}.jpg"
#tiff_image.save(output_path, "JPEG")
#de_stacked_images.append(output_path)
#print(f"De-Stacked {output_path}")
with tifffile.TiffFile(path) as stack:
#print(len(stack.pages))
for page_num in range(len(stack.pages)):
page = stack.pages[page_num]
input_image_array = page.asarray()
im = Image.fromarray(input_image_array)
output_path = './data/'+ f"image_{page_num + 1}.jpg"
im.save(output_path, "JPEG")
de_stacked_images.append(output_path)
print(f"De-Stacked {output_path}")
# Loop over the images, and generate the actual tasks
for index, image in enumerate(de_stacked_images):
# Call back that saves the result
yield image, callback
def write_referable_glaucoma_decision(result):
with open(f"/output/multiple-referable-glaucoma-binary.json", "w") as f:
f.write(json.dumps(result))
def write_referable_glaucoma_decision_likelihood(result):
with open(f"/output/multiple-referable-glaucoma-likelihoods.json", "w") as f:
f.write(json.dumps(result))
def write_glaucomatous_features(result):
with open(f"/output/stacked-referable-glaucomatous-features.json", "w") as f:
f.write(json.dumps(result))