tiamat-python 0.1.8__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.
- tiamat/__init__.py +5 -0
- tiamat/array.py +221 -0
- tiamat/cache.py +138 -0
- tiamat/cli.py +15 -0
- tiamat/constants.py +34 -0
- tiamat/errors.py +27 -0
- tiamat/io.py +85 -0
- tiamat/metadata/__init__.py +2 -0
- tiamat/metadata/dimensions.py +18 -0
- tiamat/metadata/metadata.py +118 -0
- tiamat/pipeline.py +144 -0
- tiamat/readers/__init__.py +31 -0
- tiamat/readers/factory.py +106 -0
- tiamat/readers/generic.py +150 -0
- tiamat/readers/memory.py +223 -0
- tiamat/readers/nifti.py +94 -0
- tiamat/readers/pipeline.py +98 -0
- tiamat/readers/processing.py +509 -0
- tiamat/readers/protocol.py +59 -0
- tiamat/readers/stack.py +851 -0
- tiamat/readers/zarr.py +353 -0
- tiamat/serialization.py +153 -0
- tiamat/transformers/__init__.py +0 -0
- tiamat/transformers/access.py +216 -0
- tiamat/transformers/affine.py +341 -0
- tiamat/transformers/axes.py +418 -0
- tiamat/transformers/color.py +304 -0
- tiamat/transformers/coordinates.py +54 -0
- tiamat/transformers/dfield.py +432 -0
- tiamat/transformers/metadata.py +88 -0
- tiamat/transformers/normalization.py +77 -0
- tiamat/transformers/protocol.py +84 -0
- tiamat/transformers/view.py +197 -0
- tiamat_python-0.1.8.dist-info/METADATA +382 -0
- tiamat_python-0.1.8.dist-info/RECORD +39 -0
- tiamat_python-0.1.8.dist-info/WHEEL +5 -0
- tiamat_python-0.1.8.dist-info/entry_points.txt +2 -0
- tiamat_python-0.1.8.dist-info/licenses/LICENSE +202 -0
- tiamat_python-0.1.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Color transformers.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from ..io import ImageAccessor
|
|
8
|
+
from ..metadata import ImageMetadata
|
|
9
|
+
from .protocol import Transformer
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LUTTransformer(Transformer):
|
|
13
|
+
"""
|
|
14
|
+
Apply a look-up table (LUT) or color map to an image.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, color_map: str | np.ndarray | list | tuple) -> None:
|
|
18
|
+
"""
|
|
19
|
+
Initialize a LUTTransformer.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
color_map: Colormap to apply. Can be a string (matplotlib name), numpy array,
|
|
23
|
+
list, or tuple.
|
|
24
|
+
"""
|
|
25
|
+
self.color_map = color_map
|
|
26
|
+
|
|
27
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
28
|
+
"""
|
|
29
|
+
Leave accessor unchanged.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
accessor: Accessor of the image.
|
|
33
|
+
metadata: Metadata of the image.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
The unchanged accessor.
|
|
37
|
+
"""
|
|
38
|
+
return accessor
|
|
39
|
+
|
|
40
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
41
|
+
"""
|
|
42
|
+
Apply the LUT or colormap to the image.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
image: Input image.
|
|
46
|
+
metadata: Metadata of the image, must provide `value_range`.
|
|
47
|
+
accessor: Accessor of the image.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
Color-mapped image as ndarray.
|
|
51
|
+
"""
|
|
52
|
+
assert metadata.value_range is not None, "LUTTransformer requires metadata.value_range."
|
|
53
|
+
|
|
54
|
+
image = self._apply_color_map(image=image, value_range=metadata.value_range)
|
|
55
|
+
|
|
56
|
+
return image
|
|
57
|
+
|
|
58
|
+
def _apply_color_map(self, image: np.ndarray, value_range: tuple[float, float]) -> np.ndarray:
|
|
59
|
+
"""
|
|
60
|
+
Internal helper to apply the color map.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
image: Input image array.
|
|
64
|
+
value_range: Tuple of (min, max) values for normalization.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Color-mapped image as ndarray.
|
|
68
|
+
"""
|
|
69
|
+
import numpy as np
|
|
70
|
+
|
|
71
|
+
if isinstance(self.color_map, str):
|
|
72
|
+
# Matplotlib colormap
|
|
73
|
+
import matplotlib
|
|
74
|
+
from matplotlib.colors import Normalize
|
|
75
|
+
|
|
76
|
+
color_map = matplotlib.colormaps.get_cmap(self.color_map)
|
|
77
|
+
vmin, vmax = value_range
|
|
78
|
+
normalizer = Normalize(vmin=vmin, vmax=vmax)
|
|
79
|
+
|
|
80
|
+
return color_map(normalizer(image))
|
|
81
|
+
elif isinstance(self.color_map, (np.ndarray, (tuple, list))):
|
|
82
|
+
# Color map provided as indexable array.
|
|
83
|
+
color_map = np.asarray(self.color_map)
|
|
84
|
+
return color_map[image]
|
|
85
|
+
else:
|
|
86
|
+
raise RuntimeError(f"Unknown type for color map: {type(self.color_map)}")
|
|
87
|
+
|
|
88
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
89
|
+
"""
|
|
90
|
+
Update metadata shape for 3-channel color output.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
metadata: Original ImageMetadata.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
Updated ImageMetadata.
|
|
97
|
+
"""
|
|
98
|
+
from dataclasses import replace
|
|
99
|
+
|
|
100
|
+
metadata = replace(metadata)
|
|
101
|
+
|
|
102
|
+
if isinstance(self.color_map, str):
|
|
103
|
+
import matplotlib
|
|
104
|
+
|
|
105
|
+
metadata.dtype = np.asarray(matplotlib.colormaps.get_cmap(self.color_map)(0)).dtype
|
|
106
|
+
ldim = len(matplotlib.colormaps.get_cmap(self.color_map)(0))
|
|
107
|
+
metadata.value_range = (0.0, 1.0)
|
|
108
|
+
elif isinstance(self.color_map, (np.ndarray, (tuple, list))):
|
|
109
|
+
metadata.dtype = np.asarray(self.color_map).dtype
|
|
110
|
+
ldim = len(self.color_map[0])
|
|
111
|
+
if isinstance(self.color_map, np.ndarray):
|
|
112
|
+
print(self.color_map)
|
|
113
|
+
metadata.value_range = (np.min(self.color_map).item(), np.max(self.color_map).max().item())
|
|
114
|
+
else:
|
|
115
|
+
metadata.value_range = (min(self.color_map), max(self.color_map))
|
|
116
|
+
|
|
117
|
+
else:
|
|
118
|
+
raise RuntimeError(f"Unknown type for color map: {type(self.color_map)}")
|
|
119
|
+
metadata.shape = (*metadata.shape, ldim)
|
|
120
|
+
|
|
121
|
+
return metadata
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class GrayscaleTransformer(Transformer):
|
|
125
|
+
"""
|
|
126
|
+
Convert RGB/RGBA images to grayscale.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
130
|
+
"""
|
|
131
|
+
Leave accessor unchanged.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
accessor: Accessor of the image.
|
|
135
|
+
metadata: Metadata of the image.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
The unchanged accessor.
|
|
139
|
+
"""
|
|
140
|
+
return accessor
|
|
141
|
+
|
|
142
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
143
|
+
"""
|
|
144
|
+
Convert RGB/RGBA image to grayscale using OpenCV.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
image: Image to transform.
|
|
148
|
+
metadata: Metadata of the image.
|
|
149
|
+
accessor: Accessor of the image.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
Grayscale image as ndarray.
|
|
153
|
+
"""
|
|
154
|
+
import cv2
|
|
155
|
+
|
|
156
|
+
from tiamat.metadata import dimensions
|
|
157
|
+
|
|
158
|
+
# Only do this if the image contains RGB/RGBA channels
|
|
159
|
+
if dimensions.RGB in metadata.dimensions or dimensions.RGBA in metadata.dimensions:
|
|
160
|
+
# TODO: Check if COLOR_BGR2GRAY is correct or COLOR_RGB2GRAY should be used
|
|
161
|
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
162
|
+
|
|
163
|
+
return image
|
|
164
|
+
|
|
165
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
166
|
+
"""
|
|
167
|
+
Remove RGB/RGBA dimensions from metadata.
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
metadata: Original ImageMetadata.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
Updated ImageMetadata without color channels.
|
|
174
|
+
"""
|
|
175
|
+
from dataclasses import replace
|
|
176
|
+
|
|
177
|
+
from tiamat.metadata import dimensions
|
|
178
|
+
|
|
179
|
+
# remove all color dimensions
|
|
180
|
+
metadata = replace(metadata)
|
|
181
|
+
if dimensions.RGB in metadata.dimensions or dimensions.RGBA in metadata.dimensions:
|
|
182
|
+
metadata.shape = metadata.shape[:-1]
|
|
183
|
+
metadata.dimensions = [
|
|
184
|
+
dimension for dimension in metadata.dimensions if dimension not in (dimensions.RGB, dimensions.RGBA)
|
|
185
|
+
]
|
|
186
|
+
|
|
187
|
+
return metadata
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class GrayscaleToRGBTransformer(Transformer):
|
|
191
|
+
"""
|
|
192
|
+
Convert grayscale images to RGB.
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
196
|
+
"""
|
|
197
|
+
Leave accessor unchanged.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
metadata: Metadata of the Image.
|
|
201
|
+
accessor: Accessor of the image.
|
|
202
|
+
|
|
203
|
+
Returns:
|
|
204
|
+
The unchanged accessor.
|
|
205
|
+
"""
|
|
206
|
+
return accessor
|
|
207
|
+
|
|
208
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
209
|
+
"""
|
|
210
|
+
Convert grayscale image to RGB using OpenCV.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
image: Image to transform.
|
|
214
|
+
metadata:
|
|
215
|
+
accessor:
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
RGB ImageResult.
|
|
219
|
+
"""
|
|
220
|
+
import cv2
|
|
221
|
+
|
|
222
|
+
from tiamat.metadata import dimensions
|
|
223
|
+
|
|
224
|
+
# Only do something if the image is not already RGB.
|
|
225
|
+
if not (dimensions.RGB in metadata.dimensions or dimensions.RGBA in metadata.dimensions):
|
|
226
|
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
|
227
|
+
|
|
228
|
+
return image
|
|
229
|
+
|
|
230
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
231
|
+
"""
|
|
232
|
+
Update metadata shape and dimensions for RGB.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
metadata: Original ImageMetadata.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
Updated ImageMetadata.
|
|
239
|
+
"""
|
|
240
|
+
from dataclasses import replace
|
|
241
|
+
|
|
242
|
+
from tiamat.metadata import dimensions
|
|
243
|
+
|
|
244
|
+
metadata = replace(metadata)
|
|
245
|
+
if not (dimensions.RGB in metadata.dimensions or dimensions.RGBA in metadata.dimensions):
|
|
246
|
+
metadata.shape = (*metadata.shape, 3)
|
|
247
|
+
metadata.dimensions = list(metadata.dimensions) + [
|
|
248
|
+
dimensions.RGB,
|
|
249
|
+
]
|
|
250
|
+
|
|
251
|
+
return metadata
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
class FloatToByteTransformer(Transformer):
|
|
255
|
+
"""
|
|
256
|
+
Convert floating-point images to 8-bit unsigned byte images.
|
|
257
|
+
"""
|
|
258
|
+
|
|
259
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
260
|
+
"""
|
|
261
|
+
Leave accessor unchanged.
|
|
262
|
+
|
|
263
|
+
Args:
|
|
264
|
+
accessor: Accessor of the image.
|
|
265
|
+
metadata: Metadata of the image.
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
The unchanged accessor.
|
|
269
|
+
"""
|
|
270
|
+
return accessor
|
|
271
|
+
|
|
272
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
273
|
+
"""
|
|
274
|
+
Convert float images to 8-bit uint images in range [0, 255].
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
image: Image to transform.
|
|
278
|
+
metadata: Metadata of the image.
|
|
279
|
+
accessor: Accessor of the image.
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
Converted image as uint8.
|
|
283
|
+
"""
|
|
284
|
+
if np.issubdtype(image.dtype, np.floating):
|
|
285
|
+
image = (image * 255).astype(np.uint8)
|
|
286
|
+
|
|
287
|
+
return image
|
|
288
|
+
|
|
289
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
290
|
+
"""
|
|
291
|
+
Set metadata value_range to [0,255] after conversion.
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
metadata: Original ImageMetadata.
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
Updated metadata with value_range 0–255.
|
|
298
|
+
"""
|
|
299
|
+
from dataclasses import replace
|
|
300
|
+
|
|
301
|
+
metadata = replace(metadata)
|
|
302
|
+
|
|
303
|
+
metadata.value_range = (0, 255)
|
|
304
|
+
return metadata
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def resolve_coordinate_slice(
|
|
5
|
+
coordinate_slice: tuple[int | float | None, int | float | None] | int | float | None,
|
|
6
|
+
image_dimension: int | float,
|
|
7
|
+
) -> tuple[int | float, int | float] | int | float:
|
|
8
|
+
"""
|
|
9
|
+
Resolve `None` values in a coordinate slice.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
coordinate_slice: Either a single coordinate (int/float) or a tuple of (start, stop),
|
|
13
|
+
where values may be None.
|
|
14
|
+
image_dimension: Length of the image along the corresponding axis.
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
Either a single coordinate or a tuple of (start, stop), with all None replaced
|
|
18
|
+
by valid numeric values.
|
|
19
|
+
"""
|
|
20
|
+
if coordinate_slice is None:
|
|
21
|
+
return 0, image_dimension
|
|
22
|
+
elif isinstance(coordinate_slice, (np.integer, int)) or isinstance(coordinate_slice, (np.floating, float)):
|
|
23
|
+
return coordinate_slice
|
|
24
|
+
else:
|
|
25
|
+
slice_0, slice_1 = coordinate_slice
|
|
26
|
+
if slice_0 is None:
|
|
27
|
+
slice_0 = 0
|
|
28
|
+
if slice_1 is None:
|
|
29
|
+
slice_1 = image_dimension
|
|
30
|
+
return slice_0, slice_1
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_coordinate_bounds(
|
|
34
|
+
coordinate_slice: tuple[int | float | None, int | float | None] | int,
|
|
35
|
+
image_dimension: int | float,
|
|
36
|
+
) -> tuple[int | float, int | float]:
|
|
37
|
+
"""
|
|
38
|
+
Return numeric start and end values from a coordinate slice.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
coordinate_slice: Slice or single coordinate to resolve.
|
|
42
|
+
image_dimension: Length of the image along the corresponding axis.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Tuple of (start, end) coordinates.
|
|
46
|
+
"""
|
|
47
|
+
coord_fromto = resolve_coordinate_slice(coordinate_slice, image_dimension)
|
|
48
|
+
|
|
49
|
+
if isinstance(coord_fromto, tuple):
|
|
50
|
+
coord_from, coord_to = coord_fromto
|
|
51
|
+
else:
|
|
52
|
+
coord_from = coord_to = coord_fromto
|
|
53
|
+
|
|
54
|
+
return coord_from, coord_to
|