tiamat-python 0.1.13__tar.gz → 0.1.16__tar.gz
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_python-0.1.13 → tiamat_python-0.1.16}/PKG-INFO +1 -1
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/color.py +50 -5
- tiamat_python-0.1.16/tiamat/transformers/intensity_mapping.py +334 -0
- tiamat_python-0.1.16/tiamat/transformers/mask.py +108 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/normalization.py +2 -4
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/PKG-INFO +1 -1
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/SOURCES.txt +2 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/LICENSE +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/README.md +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/README.rst +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/pyproject.toml +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/setup.cfg +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/setup.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tests/test_metadata.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tests/test_package.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/__init__.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/array.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/cache.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/cli.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/constants.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/errors.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/io.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/metadata/__init__.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/metadata/dimensions.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/metadata/metadata.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/pipeline.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/__init__.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/factory.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/generic.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/memory.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/nifti.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/pipeline.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/processing.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/protocol.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/stack.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/readers/zarr.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/serialization.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/__init__.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/access.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/affine.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/axes.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/coordinates.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/dfield.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/metadata.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/protocol.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat/transformers/view.py +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/dependency_links.txt +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/entry_points.txt +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/requires.txt +0 -0
- {tiamat_python-0.1.13 → tiamat_python-0.1.16}/tiamat_python.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tiamat-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.16
|
|
4
4
|
Summary: Tiled Image Access, Manipulation, and Analysis Toolkit
|
|
5
5
|
Author-email: "Big Data Analytics Group, Forschungszentrum Juelich, Institute of Neuroscience and Medicine (INM-1)" <inm1-bda@fz-juelich.de>
|
|
6
6
|
License:
|
|
@@ -14,15 +14,31 @@ class LUTTransformer(Transformer):
|
|
|
14
14
|
Apply a look-up table (LUT) or color map to an image.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
-
def __init__(
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
color_map: str | np.ndarray | list | tuple,
|
|
20
|
+
vmin: float | None = None,
|
|
21
|
+
vmax: float | None = None,
|
|
22
|
+
nan_color: float | tuple[float, ...] | None = None,
|
|
23
|
+
) -> None:
|
|
18
24
|
"""
|
|
19
25
|
Initialize a LUTTransformer.
|
|
20
26
|
|
|
21
27
|
Args:
|
|
22
28
|
color_map: Colormap to apply. Can be a string (matplotlib name), numpy array,
|
|
23
29
|
list, or tuple.
|
|
30
|
+
vmin: Optional lower bound used for normalization when `color_map` is a
|
|
31
|
+
matplotlib colormap name. If `None`, metadata.value_range[0] is used.
|
|
32
|
+
vmax: Optional upper bound used for normalization when `color_map` is a
|
|
33
|
+
matplotlib colormap name. If `None`, metadata.value_range[1] is used.
|
|
34
|
+
nan_color: Optional color value used for NaN pixels after color mapping.
|
|
35
|
+
If `None`, the colormap's default NaN behavior is preserved.
|
|
24
36
|
"""
|
|
37
|
+
|
|
25
38
|
self.color_map = color_map
|
|
39
|
+
self.vmin = vmin
|
|
40
|
+
self.vmax = vmax
|
|
41
|
+
self.nan_color = nan_color
|
|
26
42
|
|
|
27
43
|
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
28
44
|
"""
|
|
@@ -49,11 +65,40 @@ class LUTTransformer(Transformer):
|
|
|
49
65
|
Returns:
|
|
50
66
|
Color-mapped image as ndarray.
|
|
51
67
|
"""
|
|
52
|
-
|
|
68
|
+
if self.vmin is None or self.vmax is None:
|
|
69
|
+
assert metadata.value_range is not None, "LUTTransformer requires metadata.value_range."
|
|
70
|
+
metadata_vmin, metadata_vmax = metadata.value_range
|
|
71
|
+
else:
|
|
72
|
+
metadata_vmin, metadata_vmax = (None, None)
|
|
53
73
|
|
|
54
|
-
|
|
74
|
+
vmin = self.vmin if self.vmin is not None else metadata_vmin
|
|
75
|
+
vmax = self.vmax if self.vmax is not None else metadata_vmax
|
|
55
76
|
|
|
56
|
-
|
|
77
|
+
nan_mask = None
|
|
78
|
+
if self.nan_color is not None:
|
|
79
|
+
nan_mask = np.isnan(image)
|
|
80
|
+
image = np.where(nan_mask, vmin, image)
|
|
81
|
+
|
|
82
|
+
mapped_image = self._apply_color_map(
|
|
83
|
+
image=image,
|
|
84
|
+
value_range=(vmin, vmax),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
if self.nan_color is not None and np.any(nan_mask):
|
|
88
|
+
output_channels = mapped_image.shape[-1] if mapped_image.ndim > image.ndim else None
|
|
89
|
+
if np.isscalar(self.nan_color):
|
|
90
|
+
nan_color = (self.nan_color,) * 3
|
|
91
|
+
else:
|
|
92
|
+
nan_color = tuple(self.nan_color)
|
|
93
|
+
|
|
94
|
+
if len(nan_color) == 3 and output_channels == 4:
|
|
95
|
+
nan_color = (*nan_color, 1)
|
|
96
|
+
elif len(nan_color) != output_channels:
|
|
97
|
+
raise ValueError("nan_color length must match the output channel count")
|
|
98
|
+
|
|
99
|
+
mapped_image[nan_mask] = nan_color
|
|
100
|
+
|
|
101
|
+
return mapped_image
|
|
57
102
|
|
|
58
103
|
def _apply_color_map(self, image: np.ndarray, value_range: tuple[float, float]) -> np.ndarray:
|
|
59
104
|
"""
|
|
@@ -81,7 +126,7 @@ class LUTTransformer(Transformer):
|
|
|
81
126
|
elif isinstance(self.color_map, (np.ndarray, (tuple, list))):
|
|
82
127
|
# Color map provided as indexable array.
|
|
83
128
|
color_map = np.asarray(self.color_map)
|
|
84
|
-
return color_map[image]
|
|
129
|
+
return color_map[image.astype(int)]
|
|
85
130
|
else:
|
|
86
131
|
raise RuntimeError(f"Unknown type for color map: {type(self.color_map)}")
|
|
87
132
|
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module containing transformers for intensity mapping of images.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from ..io import ImageAccessor
|
|
10
|
+
from ..metadata import ImageMetadata
|
|
11
|
+
from .protocol import Transformer
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MappingTransformer(Transformer):
|
|
15
|
+
"""
|
|
16
|
+
Pointwise intensity mapping defined by a mathematical function.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
mapping_function: Callable[[np.ndarray | float], np.ndarray | float],
|
|
22
|
+
target_dtype: type[np.floating] = np.float32,
|
|
23
|
+
) -> None:
|
|
24
|
+
"""
|
|
25
|
+
Initialize the transformer.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
mapping_function: Callable used to map both image values and metadata
|
|
29
|
+
range bounds.
|
|
30
|
+
target_dtype: Floating point dtype for the mapped image.
|
|
31
|
+
Defaults to np.float32.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
self.mapping_function = mapping_function
|
|
35
|
+
self.target_dtype = target_dtype
|
|
36
|
+
|
|
37
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
38
|
+
"""
|
|
39
|
+
No changes to the accessor are needed for intensity mapping.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
accessor: ImageAccessor of the target image.
|
|
43
|
+
metadata: Metadata of the target image.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
The unmodified ImageAccessor.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
return accessor
|
|
50
|
+
|
|
51
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
52
|
+
"""
|
|
53
|
+
Update metadata to reflect intensity mapping.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
metadata: Original ImageMetadata.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
Updated ImageMetadata with dtype set to target_dtype and value_range mapped
|
|
60
|
+
through the transform function.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
from dataclasses import replace
|
|
64
|
+
|
|
65
|
+
metadata = replace(metadata)
|
|
66
|
+
|
|
67
|
+
metadata.dtype = self.target_dtype
|
|
68
|
+
assert metadata.value_range is not None, f"{type(self).__name__} requires metadata.value_range."
|
|
69
|
+
metadata.value_range = self._map_value_range(metadata.value_range)
|
|
70
|
+
|
|
71
|
+
return metadata
|
|
72
|
+
|
|
73
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
74
|
+
"""
|
|
75
|
+
Apply pointwise intensity mapping to the image.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
image: Image to transform.
|
|
79
|
+
metadata: Metadata of the image.
|
|
80
|
+
accessor: Accessor to the image.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
np.ndarray: Mapped image.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
transformed_image = self._map_array(image)
|
|
87
|
+
return transformed_image.astype(self.target_dtype)
|
|
88
|
+
|
|
89
|
+
def _map_value_range(self, value_range: tuple[float, float]) -> tuple[float, float]:
|
|
90
|
+
"""Map the metadata value range through the transform function."""
|
|
91
|
+
|
|
92
|
+
vmin, vmax = value_range
|
|
93
|
+
return (self._map_scalar(vmin), self._map_scalar(vmax))
|
|
94
|
+
|
|
95
|
+
def _map_array(self, image: np.ndarray) -> np.ndarray:
|
|
96
|
+
"""Map the image values through the transform function."""
|
|
97
|
+
|
|
98
|
+
return self.mapping_function(image)
|
|
99
|
+
|
|
100
|
+
def _map_scalar(self, value: float) -> float:
|
|
101
|
+
"""Map a scalar through the transform function."""
|
|
102
|
+
|
|
103
|
+
return self.mapping_function(value)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class ReplaceValueTransformer(MappingTransformer):
|
|
107
|
+
"""
|
|
108
|
+
Replace one numeric value with another.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
value_to_replace: float,
|
|
114
|
+
replacement_value: float,
|
|
115
|
+
target_dtype: type[np.floating] = np.float32,
|
|
116
|
+
) -> None:
|
|
117
|
+
"""
|
|
118
|
+
Initialize the transformer.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
value_to_replace: Numeric value to replace. NaN is supported.
|
|
122
|
+
replacement_value: Numeric value to use as the replacement.
|
|
123
|
+
target_dtype: Floating point dtype for the transformed image.
|
|
124
|
+
Defaults to np.float32.
|
|
125
|
+
"""
|
|
126
|
+
|
|
127
|
+
super().__init__(
|
|
128
|
+
mapping_function=self._replace_value(
|
|
129
|
+
value_to_replace,
|
|
130
|
+
replacement_value,
|
|
131
|
+
),
|
|
132
|
+
target_dtype=target_dtype,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
@staticmethod
|
|
136
|
+
def _replace_value(value_to_replace: float, replacement_value: float) -> Callable:
|
|
137
|
+
"""Create a mapping function that also supports matching NaN."""
|
|
138
|
+
|
|
139
|
+
if np.isnan(value_to_replace):
|
|
140
|
+
|
|
141
|
+
def mapping_function(input_value):
|
|
142
|
+
return np.where(np.isnan(input_value), replacement_value, input_value)
|
|
143
|
+
|
|
144
|
+
else:
|
|
145
|
+
|
|
146
|
+
def mapping_function(input_value):
|
|
147
|
+
return np.where(input_value == value_to_replace, replacement_value, input_value)
|
|
148
|
+
|
|
149
|
+
return mapping_function
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# maybe only in tiamat-openseadragon?
|
|
153
|
+
# Commen used functions
|
|
154
|
+
class LogIntensityMappingTransformer(MappingTransformer):
|
|
155
|
+
"""
|
|
156
|
+
Map image intensities using a logarithmic response curve.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
def __init__(
|
|
160
|
+
self,
|
|
161
|
+
target_dtype: type[np.floating] = np.float32,
|
|
162
|
+
) -> None:
|
|
163
|
+
"""Initialize the transformer with a logarithmic mapping."""
|
|
164
|
+
|
|
165
|
+
super().__init__(
|
|
166
|
+
mapping_function=np.log,
|
|
167
|
+
target_dtype=target_dtype,
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class Log1pIntensityMappingTransformer(MappingTransformer):
|
|
172
|
+
"""
|
|
173
|
+
Map image intensities using a logarithmic response curve with log1p.
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
def __init__(
|
|
177
|
+
self,
|
|
178
|
+
target_dtype: type[np.floating] = np.float32,
|
|
179
|
+
) -> None:
|
|
180
|
+
"""Initialize the transformer with a log1p mapping."""
|
|
181
|
+
|
|
182
|
+
super().__init__(
|
|
183
|
+
mapping_function=np.log1p,
|
|
184
|
+
target_dtype=target_dtype,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class GammaIntensityMappingTransformer(MappingTransformer):
|
|
189
|
+
"""
|
|
190
|
+
Map image intensities using gamma correction.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def __init__(
|
|
194
|
+
self,
|
|
195
|
+
gamma: float,
|
|
196
|
+
target_dtype: type[np.floating] = np.float32,
|
|
197
|
+
) -> None:
|
|
198
|
+
"""
|
|
199
|
+
Initialize the transformer.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
gamma: Gamma value for correction.
|
|
203
|
+
target_dtype: Floating point dtype for the mapped image.
|
|
204
|
+
Defaults to np.float32.
|
|
205
|
+
"""
|
|
206
|
+
|
|
207
|
+
self.gamma = gamma
|
|
208
|
+
super().__init__(
|
|
209
|
+
mapping_function=lambda value: np.power(value, self.gamma),
|
|
210
|
+
target_dtype=target_dtype,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class ClipIntensityMappingTransformer(MappingTransformer):
|
|
215
|
+
"""
|
|
216
|
+
Map image intensities by clipping to a specified range.
|
|
217
|
+
"""
|
|
218
|
+
|
|
219
|
+
def __init__(
|
|
220
|
+
self,
|
|
221
|
+
vmin: float,
|
|
222
|
+
vmax: float,
|
|
223
|
+
target_dtype: type[np.floating] = np.float32,
|
|
224
|
+
) -> None:
|
|
225
|
+
"""
|
|
226
|
+
Initialize the transformer.
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
vmin: Minimum intensity value for clipping.
|
|
230
|
+
vmax: Maximum intensity value for clipping.
|
|
231
|
+
target_dtype: Floating point dtype for the mapped image.
|
|
232
|
+
Defaults to np.float32.
|
|
233
|
+
"""
|
|
234
|
+
|
|
235
|
+
self.vmin = vmin
|
|
236
|
+
self.vmax = vmax
|
|
237
|
+
super().__init__(
|
|
238
|
+
mapping_function=lambda value: np.clip(value, self.vmin, self.vmax),
|
|
239
|
+
target_dtype=target_dtype,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class BrightnessContrastIntensityMappingTransformer(Transformer):
|
|
244
|
+
"""
|
|
245
|
+
Map image intensities using brightness and contrast adjustment.
|
|
246
|
+
"""
|
|
247
|
+
|
|
248
|
+
def __init__(
|
|
249
|
+
self,
|
|
250
|
+
brightness: float = 0.0,
|
|
251
|
+
contrast: float = 1.0,
|
|
252
|
+
target_dtype: type[np.floating] = np.float32,
|
|
253
|
+
) -> None:
|
|
254
|
+
"""
|
|
255
|
+
Initialize the transformer.
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
brightness: Additive offset expressed as a fraction of the
|
|
259
|
+
metadata value range.
|
|
260
|
+
contrast: Multiplicative contrast factor around the metadata
|
|
261
|
+
range midpoint.
|
|
262
|
+
target_dtype: Floating point dtype for the mapped image.
|
|
263
|
+
Defaults to np.float32.
|
|
264
|
+
"""
|
|
265
|
+
|
|
266
|
+
self.brightness = brightness
|
|
267
|
+
self.contrast = contrast
|
|
268
|
+
self.target_dtype = target_dtype
|
|
269
|
+
super().__init__(
|
|
270
|
+
mapping_function=lambda value: value,
|
|
271
|
+
target_dtype=target_dtype,
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
275
|
+
"""Update metadata to reflect brightness and contrast mapping."""
|
|
276
|
+
|
|
277
|
+
from dataclasses import replace
|
|
278
|
+
|
|
279
|
+
metadata = replace(metadata)
|
|
280
|
+
metadata.dtype = self.target_dtype
|
|
281
|
+
|
|
282
|
+
assert (
|
|
283
|
+
metadata.value_range is not None
|
|
284
|
+
), "BrightnessContrastIntensityMappingTransformer requires metadata.value_range."
|
|
285
|
+
|
|
286
|
+
vmin, vmax = metadata.value_range
|
|
287
|
+
midpoint = (vmin + vmax) / 2.0
|
|
288
|
+
value_range = vmax - vmin
|
|
289
|
+
|
|
290
|
+
mapped_vmin = np.clip(
|
|
291
|
+
(vmin - midpoint) * self.contrast + midpoint + self.brightness * value_range,
|
|
292
|
+
vmin,
|
|
293
|
+
vmax,
|
|
294
|
+
)
|
|
295
|
+
mapped_vmax = np.clip(
|
|
296
|
+
(vmax - midpoint) * self.contrast + midpoint + self.brightness * value_range,
|
|
297
|
+
vmin,
|
|
298
|
+
vmax,
|
|
299
|
+
)
|
|
300
|
+
metadata.value_range = (float(mapped_vmin), float(mapped_vmax))
|
|
301
|
+
|
|
302
|
+
return metadata
|
|
303
|
+
|
|
304
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
305
|
+
"""
|
|
306
|
+
Apply brightness and contrast intensity mapping to the image.
|
|
307
|
+
|
|
308
|
+
Args:
|
|
309
|
+
image: Image to transform.
|
|
310
|
+
metadata: Metadata of the image.
|
|
311
|
+
accessor: Accessor to the image.
|
|
312
|
+
|
|
313
|
+
Returns:
|
|
314
|
+
np.ndarray: Brightness/contrast-mapped image clipped to metadata
|
|
315
|
+
value_range.
|
|
316
|
+
"""
|
|
317
|
+
|
|
318
|
+
assert (
|
|
319
|
+
metadata.value_range is not None
|
|
320
|
+
), "BrightnessContrastIntensityMappingTransformer requires metadata.value_range."
|
|
321
|
+
|
|
322
|
+
vmin, vmax = metadata.value_range
|
|
323
|
+
midpoint = (vmin + vmax) / 2.0
|
|
324
|
+
value_range = vmax - vmin
|
|
325
|
+
|
|
326
|
+
# Apply contrast around native midpoint and brightness as a fraction of range,
|
|
327
|
+
# then clip to metadata range.
|
|
328
|
+
self.mapping_function = lambda value: np.clip(
|
|
329
|
+
(value.astype(self.target_dtype) - midpoint) * self.contrast + midpoint + self.brightness * value_range,
|
|
330
|
+
vmin,
|
|
331
|
+
vmax,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
return self.mapping_function(image).astype(self.target_dtype)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Mask transformers.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from tiamat.cache import instance_cached_property
|
|
13
|
+
from tiamat.io import ImageAccessor
|
|
14
|
+
from tiamat.metadata import ImageMetadata
|
|
15
|
+
from tiamat.readers.protocol import ImageReader
|
|
16
|
+
from tiamat.transformers.protocol import Transformer
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ApplyMaskTransformer(Transformer):
|
|
20
|
+
"""Transformer which applies a mask to an image, setting masked pixels to a specified value."""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
mask_file: str,
|
|
25
|
+
mask_value: int | float = 0,
|
|
26
|
+
reader_factory: Callable[[str], ImageReader] | None = None,
|
|
27
|
+
):
|
|
28
|
+
"""
|
|
29
|
+
Creates an instance of ApplyMaskTransformer.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
mask_file: Path to the mask file.
|
|
33
|
+
mask_value: Value to set masked pixels to. Defaults to 0.
|
|
34
|
+
reader_factory: Callable returning ImageReader for a given file path.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
from tiamat.readers.factory import get_reader
|
|
38
|
+
|
|
39
|
+
self.mask_file = mask_file
|
|
40
|
+
self.mask_value = mask_value
|
|
41
|
+
self.reader_factory = reader_factory or get_reader
|
|
42
|
+
|
|
43
|
+
@instance_cached_property
|
|
44
|
+
def mask_file_handle(self) -> ImageReader:
|
|
45
|
+
"""Return a cached ImageReader for the mask file."""
|
|
46
|
+
return self.reader_factory(self.mask_file)
|
|
47
|
+
|
|
48
|
+
@staticmethod
|
|
49
|
+
def apply_mask(image: np.ndarray, mask: np.ndarray, mask_value: int | float) -> np.ndarray:
|
|
50
|
+
"""Apply the mask to the image, setting masked pixels to mask_value."""
|
|
51
|
+
masked_image = image.copy()
|
|
52
|
+
masked_image[mask == 0] = mask_value
|
|
53
|
+
return masked_image
|
|
54
|
+
|
|
55
|
+
def transform_access(self, accessor: ImageAccessor, metadata: ImageMetadata) -> ImageAccessor:
|
|
56
|
+
return accessor
|
|
57
|
+
|
|
58
|
+
def transform_metadata(self, metadata: ImageMetadata) -> ImageMetadata:
|
|
59
|
+
return metadata
|
|
60
|
+
|
|
61
|
+
def transform_image(self, image: np.ndarray, metadata: ImageMetadata, accessor: ImageAccessor) -> np.ndarray:
|
|
62
|
+
|
|
63
|
+
metadata_mask = self.mask_file_handle.read_metadata()
|
|
64
|
+
if metadata.spatial_shape != metadata_mask.spatial_shape:
|
|
65
|
+
raise ValueError(
|
|
66
|
+
"Spatial shape mismatch: "
|
|
67
|
+
f"mask={metadata_mask.spatial_shape}, "
|
|
68
|
+
f"image={metadata.spatial_shape}. "
|
|
69
|
+
"It is currently required to match exactly."
|
|
70
|
+
)
|
|
71
|
+
if metadata.spacing != metadata_mask.spacing:
|
|
72
|
+
raise ValueError(
|
|
73
|
+
"Spacing mismatch: "
|
|
74
|
+
f"mask={metadata_mask.spacing}, "
|
|
75
|
+
f"image={metadata.spacing}. "
|
|
76
|
+
"It is currently required to match exactly."
|
|
77
|
+
)
|
|
78
|
+
if metadata.scales != metadata_mask.scales:
|
|
79
|
+
raise ValueError(
|
|
80
|
+
"Scales mismatch: "
|
|
81
|
+
f"mask={metadata_mask.scales}, "
|
|
82
|
+
f"image={metadata.scales}. "
|
|
83
|
+
"It is currently required to match exactly."
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
return self.apply_mask(image, self.mask_file_handle.read_image(accessor), self.mask_value)
|
|
87
|
+
|
|
88
|
+
@classmethod
|
|
89
|
+
def from_json(cls, args: dict[str, Any]):
|
|
90
|
+
"""
|
|
91
|
+
Instantiate an ApplyMaskTransformer from a JSON-like dictionary.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
args: Dictionary containing configuration keys:
|
|
95
|
+
- mask_file: Path to the mask file (required).
|
|
96
|
+
- mask_value: Value to set masked pixels to (optional, default=0).
|
|
97
|
+
- reader_factory: Optional configuration for a custom ImageReader factory.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
An instance of ApplyMaskTransformer configured according to args.
|
|
101
|
+
"""
|
|
102
|
+
from tiamat.serialization import get_reader_from_config
|
|
103
|
+
|
|
104
|
+
return cls(
|
|
105
|
+
mask_file=args["mask_file"],
|
|
106
|
+
mask_value=args.get("mask_value", 0),
|
|
107
|
+
reader_factory=get_reader_from_config(args.get("reader_factory")),
|
|
108
|
+
)
|
|
@@ -50,7 +50,6 @@ class MinMaxNormalizationTransformer(Transformer):
|
|
|
50
50
|
from dataclasses import replace
|
|
51
51
|
|
|
52
52
|
metadata = replace(metadata)
|
|
53
|
-
|
|
54
53
|
metadata.dtype = self.target_dtype
|
|
55
54
|
metadata.value_range = (0.0, 1.0)
|
|
56
55
|
|
|
@@ -71,7 +70,6 @@ class MinMaxNormalizationTransformer(Transformer):
|
|
|
71
70
|
assert metadata.value_range is not None, "MinMaxNormalizationTransformer requires metadata.value_range."
|
|
72
71
|
|
|
73
72
|
vmin, vmax = metadata.value_range
|
|
73
|
+
image = (image - vmin) / (vmax - vmin)
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return image
|
|
75
|
+
return image.astype(self.target_dtype)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tiamat-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.16
|
|
4
4
|
Summary: Tiled Image Access, Manipulation, and Analysis Toolkit
|
|
5
5
|
Author-email: "Big Data Analytics Group, Forschungszentrum Juelich, Institute of Neuroscience and Medicine (INM-1)" <inm1-bda@fz-juelich.de>
|
|
6
6
|
License:
|
|
@@ -34,6 +34,8 @@ tiamat/transformers/axes.py
|
|
|
34
34
|
tiamat/transformers/color.py
|
|
35
35
|
tiamat/transformers/coordinates.py
|
|
36
36
|
tiamat/transformers/dfield.py
|
|
37
|
+
tiamat/transformers/intensity_mapping.py
|
|
38
|
+
tiamat/transformers/mask.py
|
|
37
39
|
tiamat/transformers/metadata.py
|
|
38
40
|
tiamat/transformers/normalization.py
|
|
39
41
|
tiamat/transformers/protocol.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|