stainnormprogram 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stainnormprogram/__about__.py +1 -0
- stainnormprogram/__init__.py +2 -0
- stainnormprogram/assessment/__init__.py +1 -0
- stainnormprogram/assessment/assessment.py +1186 -0
- stainnormprogram/assessment/background.py +211 -0
- stainnormprogram/assessment/color.py +770 -0
- stainnormprogram/assessment/contrast.py +252 -0
- stainnormprogram/assessment/gamut.py +407 -0
- stainnormprogram/assessment/palette.py +648 -0
- stainnormprogram/assessment/plot.py +759 -0
- stainnormprogram/assessment/structure.py +158 -0
- stainnormprogram/correction/__init__.py +1 -0
- stainnormprogram/correction/macenko.py +469 -0
- stainnormprogram/io/__init__.py +0 -0
- stainnormprogram/io/write_slide.py +119 -0
- stainnormprogram/library/TCGA-Manifest.txt +31 -0
- stainnormprogram/library/TCIA-Instructions.md +15 -0
- stainnormprogram/library/library.md +14 -0
- stainnormprogram/norm/__init__.py +1 -0
- stainnormprogram/norm/adjustments.py +88 -0
- stainnormprogram/norm/background.py +273 -0
- stainnormprogram/norm/normalization.py +126 -0
- stainnormprogram/norm/pipeline/__init__.py +0 -0
- stainnormprogram/norm/pipeline/macenko.py +302 -0
- stainnormprogram/norm/pipeline/pipeline.py +61 -0
- stainnormprogram/norm/style_estimation/__init__.py +0 -0
- stainnormprogram/norm/style_estimation/macenko.py +649 -0
- stainnormprogram/norm/subset.py +127 -0
- stainnormprogram/norm/translation/__init__.py +0 -0
- stainnormprogram/norm/translation/macenko.py +92 -0
- stainnormprogram/norm/utils.py +96 -0
- stainnormprogram/slide.py +1085 -0
- stainnormprogram-1.0.0.dist-info/METADATA +75 -0
- stainnormprogram-1.0.0.dist-info/RECORD +36 -0
- stainnormprogram-1.0.0.dist-info/WHEEL +4 -0
- stainnormprogram-1.0.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
from stainnormprogram.slide import SlideImage, SlideImageCollection
|
|
2
|
+
from stainnormprogram.norm.background import HSVBackgroundEstimation
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
class BackgroundEstimation:
|
|
10
|
+
|
|
11
|
+
def __init__(self, slide: SlideImage | SlideImageCollection, method: str = "hsv_threshold", identifier: str | None = None):
|
|
12
|
+
"""Estimate the background color for the assessment.
|
|
13
|
+
|
|
14
|
+
Options for ``method``:
|
|
15
|
+
* "hsv_threshold": Threshold using saturation and value channels in HSV color space.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
slide: Image to estimate the background color of.
|
|
19
|
+
method: Background estimation method.
|
|
20
|
+
identifier: Image ID. If None, taken from slide.
|
|
21
|
+
"""
|
|
22
|
+
self.slide = slide
|
|
23
|
+
self.method = method
|
|
24
|
+
self.identifier = identifier if identifier is not None else self.slide.identifier
|
|
25
|
+
|
|
26
|
+
self.I0 = None # np.ndarray uint8
|
|
27
|
+
|
|
28
|
+
def save(self, output_dir: str):
|
|
29
|
+
"""Save metric to disk.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
output_dir: Folder path to output the metric to.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
json_output_path = os.path.join(output_dir, "background_estimation.json")
|
|
36
|
+
with open(json_output_path, "w") as file:
|
|
37
|
+
json.dump({
|
|
38
|
+
"identifier": self.identifier,
|
|
39
|
+
"method": self.method,
|
|
40
|
+
"I0": self.I0.tolist(),
|
|
41
|
+
}, file)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def load(cls, input_dir: str) -> BackgroundEstimation:
|
|
45
|
+
"""Load metric from disk.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
input_dir: Folder path to load the metric from.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
A BackgroundEstimation.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
json_input_path = os.path.join(input_dir, "background_estimation.json")
|
|
55
|
+
with open(json_input_path, "r") as file:
|
|
56
|
+
json_data = json.load(file)
|
|
57
|
+
|
|
58
|
+
be = cls(None, method=json_data["method"], identifier=json_data["identifier"])
|
|
59
|
+
be.I0 = np.array(json_data["I0"])
|
|
60
|
+
|
|
61
|
+
return be
|
|
62
|
+
|
|
63
|
+
def compute(self):
|
|
64
|
+
"""Compute the background color estimation.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
if self.method == "hsv_threshold":
|
|
68
|
+
slide_be = HSVBackgroundEstimation(self.slide)
|
|
69
|
+
self.I0 = slide_be.compute("tiled")["median"].astype(np.uint8)
|
|
70
|
+
else:
|
|
71
|
+
raise Exception(f"{self.method} is not a valid method.")
|
|
72
|
+
|
|
73
|
+
class BackgroundComparison:
|
|
74
|
+
|
|
75
|
+
def __init__(self, reference: ReferenceBackground, query: BackgroundEstimation):
|
|
76
|
+
"""Compare a query BackgroundEstimation to a ReferenceBackground.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
reference: The reference background.
|
|
80
|
+
query: The query background.
|
|
81
|
+
"""
|
|
82
|
+
self.reference = reference
|
|
83
|
+
self.query = query
|
|
84
|
+
|
|
85
|
+
self.dist = None
|
|
86
|
+
|
|
87
|
+
def save(self, output_dir: str):
|
|
88
|
+
"""Save metric to disk.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
output_dir: Folder path to output the metric to.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
json_output_path = os.path.join(output_dir, "background_comparison.json")
|
|
95
|
+
with open(json_output_path, "w") as file:
|
|
96
|
+
json.dump({
|
|
97
|
+
"reference": self.reference.identifier,
|
|
98
|
+
"query": self.query.identifier,
|
|
99
|
+
"dist": self.dist,
|
|
100
|
+
}, file)
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def load(cls, input_dir: str) -> BackgroundComparison:
|
|
104
|
+
"""Load metric from disk.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
input_dir: Folder path to load the metric from.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
A BackgroundComparison.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
json_input_path = os.path.join(input_dir, "background_comparison.json")
|
|
114
|
+
with open(json_input_path, "r") as file:
|
|
115
|
+
json_data = json.load(file)
|
|
116
|
+
|
|
117
|
+
bc = cls(json_data["reference"], json_data["query"])
|
|
118
|
+
bc.dist = json_data["dist"]
|
|
119
|
+
|
|
120
|
+
return bc
|
|
121
|
+
|
|
122
|
+
def get_val(self) -> float:
|
|
123
|
+
"""Get the background comparison distance.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
The background comparison distance.
|
|
127
|
+
"""
|
|
128
|
+
return self.dist
|
|
129
|
+
|
|
130
|
+
def compute(self):
|
|
131
|
+
"""Compute the background comparison.
|
|
132
|
+
"""
|
|
133
|
+
ref_I0 = self.reference.I0.astype(np.int32)
|
|
134
|
+
q_I0 = self.query.I0.astype(np.int32)
|
|
135
|
+
|
|
136
|
+
self.dist = np.linalg.norm(ref_I0 - q_I0)
|
|
137
|
+
|
|
138
|
+
class ReferenceBackground:
|
|
139
|
+
|
|
140
|
+
def __init__(self, ref_backgrounds: list[BackgroundEstimation] | BackgroundEstimation, identifier: str | None = None):
|
|
141
|
+
"""ReferenceBackground is the representative background of one or more reference images.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
ref_backgrounds: One or more BackgroundEstimation objects representing one or more reference images.
|
|
145
|
+
identifier: An ID for this ReferenceBackground. If None, there should be only one BackgroundEstimation in ref_backgrounds, as the ID is taken from that.
|
|
146
|
+
"""
|
|
147
|
+
self.ref_backgrounds = ref_backgrounds
|
|
148
|
+
if identifier is not None:
|
|
149
|
+
self.identifier = identifier
|
|
150
|
+
elif isinstance(ref_backgrounds, BackgroundEstimation):
|
|
151
|
+
self.identifier = ref_backgrounds.identifier
|
|
152
|
+
elif isinstance(ref_backgrounds, list) and len(ref_backgrounds) == 1:
|
|
153
|
+
self.identifier = ref_backgrounds[0].identifier
|
|
154
|
+
else:
|
|
155
|
+
raise Exception("Set an identifier.")
|
|
156
|
+
|
|
157
|
+
if isinstance(self.ref_backgrounds, BackgroundEstimation):
|
|
158
|
+
self.ref_backgrounds = [self.ref_backgrounds]
|
|
159
|
+
|
|
160
|
+
self.I0 = None
|
|
161
|
+
|
|
162
|
+
def save(self, output_dir: str):
|
|
163
|
+
"""Save metric to disk.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
output_dir: Folder path to output the metric to.
|
|
167
|
+
"""
|
|
168
|
+
|
|
169
|
+
ref_background_identifiers = []
|
|
170
|
+
for b in self.ref_backgrounds:
|
|
171
|
+
ref_background_identifiers.append(b.identifier)
|
|
172
|
+
|
|
173
|
+
json_output_path = os.path.join(output_dir, "reference_background.json")
|
|
174
|
+
with open(json_output_path, "w") as file:
|
|
175
|
+
json.dump({
|
|
176
|
+
"identifier": self.identifier,
|
|
177
|
+
"ref_background_identifiers": ref_background_identifiers,
|
|
178
|
+
"I0": self.I0.tolist(),
|
|
179
|
+
}, file)
|
|
180
|
+
|
|
181
|
+
@classmethod
|
|
182
|
+
def load(cls, input_dir: str) -> ReferenceBackground:
|
|
183
|
+
"""Load metric from disk.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
input_dir: Folder path to load the metric from.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
A ReferenceBackground.
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
json_input_path = os.path.join(input_dir, "reference_background.json")
|
|
193
|
+
with open(json_input_path, "r") as file:
|
|
194
|
+
json_data = json.load(file)
|
|
195
|
+
|
|
196
|
+
rb = cls(None, identifier=json_data["identifier"])
|
|
197
|
+
rb.I0 = np.array(json_data["I0"])
|
|
198
|
+
|
|
199
|
+
return rb
|
|
200
|
+
|
|
201
|
+
def compute(self):
|
|
202
|
+
"""Compute the reference background.
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
I0s = []
|
|
206
|
+
|
|
207
|
+
for b in self.ref_backgrounds:
|
|
208
|
+
I0s.append(b.I0)
|
|
209
|
+
|
|
210
|
+
self.I0 = np.median(I0s, axis=0)
|
|
211
|
+
self.I0 = self.I0.astype(np.uint8)
|