-
Notifications
You must be signed in to change notification settings - Fork 5
/
level.py
268 lines (231 loc) · 8.43 KB
/
level.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
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
# Copyright 2021, 2022, 2023 SECTRA AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
from typing import Iterable, Optional
from PIL.Image import Image
from wsidicom.errors import WsiDicomNoResolutionError, WsiDicomOutOfBoundsError
from wsidicom.geometry import Point, Region, Size, SizeMm
from wsidicom.group.group import Group
from wsidicom.instance import WsiInstance
from wsidicom.stringprinting import dict_pretty_str
from wsidicom import settings
class Level(Group):
"""Represents a level in the pyramid and contains one or more instances
having the same pyramid level index, pixel spacing, and size but possibly
different focal planes and/or optical paths.
"""
def __init__(self, instances: Iterable[WsiInstance], base_pixel_spacing: SizeMm):
"""Create a level from list of WsiInstances. Assign the pyramid level
index from pixel spacing of base level.
Parameters
----------
instances: Sequence[WsiInstance]
Instances to build the level.
base_pixel_spacing: SizeMm
Pixel spacing of base level.
"""
super().__init__(instances)
self._base_pixel_spacing = base_pixel_spacing
self._level = self._assign_level(self._base_pixel_spacing)
def __repr__(self) -> str:
return (
f"{type(self).__name__}({self.instances}, " f"{self._base_pixel_spacing})"
)
def __str__(self) -> str:
return self.pretty_str()
def pretty_str(self, indent: int = 0, depth: Optional[int] = None) -> str:
string = f"Level: {self.level}, size: {self.size} px, mpp: {self.mpp} um/px"
if depth is not None:
depth -= 1
if depth < 0:
return string
string += " Instances: " + dict_pretty_str(self.instances, indent, depth)
return string
@property
def pyramid(self) -> str:
"""Return string representation of the level"""
return (
f"Level [{self.level}]"
f" tiles: {self.default_instance.tiled_size},"
f" size: {self.size}, mpp: {self.mpp} um/px"
)
@property
def tile_size(self) -> Size:
return self.default_instance.tile_size
@property
def level(self) -> int:
"""Return pyramid level"""
return self._level
@property
def mpp(self) -> SizeMm:
if self.pixel_spacing is None:
raise WsiDicomNoResolutionError()
return self.pixel_spacing * 1000.0
@property
def pixel_spacing(self) -> SizeMm:
if self._pixel_spacing is None:
raise WsiDicomNoResolutionError()
return self._pixel_spacing
def matches(self, other_level: "Level") -> bool:
"""Check if level matches other level. If strict common Uids should
match. Wsi type and tile size should always match.
Parameters
----------
other_level: Group
Other level to match against.
Returns
-------
bool
True if other level matches.
"""
return (
self.uids.matches(other_level.uids)
and other_level.image_type == self.image_type
and other_level.tile_size == self.tile_size
)
def get_highest_level(self) -> int:
"""Return lowest level that produces a single pixel sized image.
Returns
-------
int
Relative level where the pixel size becomes 1x1.
"""
return math.ceil(math.log2(max(self.size.width, self.size.height)))
def get_lowest_single_tile_level(self) -> int:
"""Return lowest level that produces a single tile sized image.
Returns
-------
int
Relative level where the pixel size becomes a single tile size.
"""
return math.ceil(
math.log2(
max(
self.size.width / self.tile_size.width,
self.size.height / self.tile_size.height,
)
)
)
def get_scaled_tile(
self,
tile: Point,
level: int,
z: Optional[float] = None,
path: Optional[str] = None,
) -> Image:
"""Return tile in another level by scaling a region.
If the tile is an edge tile, the resulting tile is croped
to remove part outside of the image (as defined by level size).
Parameters
----------
tile: Point
Non scaled tile coordinate
level: int
Level to scale from
z: Optional[float] = None
Z coordinate
path: Optional[str] = None
Optical path
Returns
-------
Image
A tile image
"""
scale = self.calculate_scale(level)
instance = self.get_instance(z, path)
scaled_region = Region.from_tile(tile, instance.tile_size) * scale
cropped_region = scaled_region.crop(instance.image_data.image_size)
if not self.valid_pixels(cropped_region):
raise WsiDicomOutOfBoundsError(
f"Region {cropped_region}", f"level size {self.size}"
)
image = self.get_region(cropped_region, z, path)
tile_size = cropped_region.size.ceil_div(scale)
image = image.resize(
tile_size.to_tuple(), resample=settings.pillow_resampling_filter
)
return image
def get_scaled_encoded_tile(
self,
tile: Point,
scale: int,
z: Optional[float] = None,
path: Optional[str] = None,
) -> bytes:
"""Return encoded tile in another level by scaling a region.
Parameters
----------
tile: Point
Non scaled tile coordinate
level: int
Level to scale from
z: Optional[float] = None
Z coordinate
path: Optional[str] = None
Optical path
Returns
-------
bytes
A transfer syntax encoded tile
"""
image = self.get_scaled_tile(tile, scale, z, path)
instance = self.get_instance(z, path)
return instance.image_data.encoder.encode(image)
def calculate_scale(self, level_to: int) -> int:
"""Return scaling factor to given level.
Parameters
----------
level_to -- index of level to scale to
Returns
-------
int
Scaling factor between this level and given level
"""
return int(2 ** (level_to - self.level))
def _assign_level(self, base_pixel_spacing: SizeMm) -> int:
"""Return (2^level scale factor) based on pixel spacing.
Will round to closest integer. Raises NotImplementedError if level is
to far from integer.
Parameters
----------
base_pixel_spacing: SizeMm
The pixel spacing of the base level
Returns
-------
int
The pyramid order of the level
"""
float_level = math.log2(self.pixel_spacing.width / base_pixel_spacing.width)
level = int(round(float_level))
TOLERANCE = 1e-2
if not math.isclose(float_level, level, rel_tol=TOLERANCE):
raise NotImplementedError(
f"Levels needs to be integer. Got {float_level} that is more than set"
f"tolerance {TOLERANCE} from the closest integer {level}. "
f"Base spacing is {base_pixel_spacing}, this level has spacing "
f"{self.pixel_spacing}.",
)
return level
class BaseLevel(Level):
def __init__(self, instances: Iterable[WsiInstance]):
"""Create a base level from list of WsiInstances.
Parameters
----------
instances: Sequence[WsiInstance]
Instances to build the level.
"""
pixel_spacing = next(iter(instances)).pixel_spacing
if pixel_spacing is None:
raise WsiDicomNoResolutionError()
super().__init__(instances, pixel_spacing)