tiamat-python 0.1.12__tar.gz → 0.1.13__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.
Files changed (48) hide show
  1. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/PKG-INFO +1 -1
  2. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/metadata/metadata.py +42 -2
  3. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/pipeline.py +10 -4
  4. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/processing.py +7 -3
  5. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/stack.py +8 -1
  6. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/zarr.py +38 -29
  7. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/serialization.py +10 -0
  8. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/color.py +0 -1
  9. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat_python.egg-info/PKG-INFO +1 -1
  10. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/LICENSE +0 -0
  11. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/README.md +0 -0
  12. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/README.rst +0 -0
  13. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/pyproject.toml +0 -0
  14. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/setup.cfg +0 -0
  15. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/setup.py +0 -0
  16. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tests/test_metadata.py +0 -0
  17. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tests/test_package.py +0 -0
  18. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/__init__.py +0 -0
  19. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/array.py +0 -0
  20. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/cache.py +0 -0
  21. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/cli.py +0 -0
  22. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/constants.py +0 -0
  23. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/errors.py +0 -0
  24. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/io.py +0 -0
  25. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/metadata/__init__.py +0 -0
  26. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/metadata/dimensions.py +0 -0
  27. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/__init__.py +0 -0
  28. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/factory.py +0 -0
  29. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/generic.py +0 -0
  30. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/memory.py +0 -0
  31. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/nifti.py +0 -0
  32. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/pipeline.py +0 -0
  33. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/readers/protocol.py +0 -0
  34. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/__init__.py +0 -0
  35. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/access.py +0 -0
  36. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/affine.py +0 -0
  37. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/axes.py +0 -0
  38. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/coordinates.py +0 -0
  39. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/dfield.py +0 -0
  40. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/metadata.py +0 -0
  41. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/normalization.py +0 -0
  42. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/protocol.py +0 -0
  43. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat/transformers/view.py +0 -0
  44. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat_python.egg-info/SOURCES.txt +0 -0
  45. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat_python.egg-info/dependency_links.txt +0 -0
  46. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat_python.egg-info/entry_points.txt +0 -0
  47. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/tiamat_python.egg-info/requires.txt +0 -0
  48. {tiamat_python-0.1.12 → tiamat_python-0.1.13}/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.12
3
+ Version: 0.1.13
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:
@@ -3,8 +3,9 @@ Metadata on images.
3
3
  """
4
4
 
5
5
  from collections.abc import Iterable
6
- from dataclasses import dataclass, field
6
+ from dataclasses import dataclass, field, fields
7
7
  from itertools import product, repeat
8
+ from typing import Any, Mapping
8
9
 
9
10
  import numpy as np
10
11
 
@@ -138,7 +139,7 @@ class ImageMetadata:
138
139
  file_path: Iterable[str] | str | None = None
139
140
  spacing: float | tuple[float, ...] | None = None
140
141
  additional_metadata: dict = field(default_factory=dict)
141
- scales: float | int | Iterable[float | int] | None = None
142
+ scales: float | int | Iterable[float | int] | Iterable[tuple[float | int, ...]] | None = None
142
143
 
143
144
  @property
144
145
  def spatial_dimensions(self):
@@ -204,3 +205,42 @@ class ImageMetadata:
204
205
 
205
206
  def __str__(self):
206
207
  return self.__repr__()
208
+
209
+
210
+ def _convert_metadata_value(key: str, value: Any) -> Any:
211
+ """Convert metadata values to match ImageMetadata field conventions."""
212
+ if key in {"shape", "value_range", "dimensions", "spacing"}:
213
+ if isinstance(value, list):
214
+ return tuple(value)
215
+
216
+ if key == "dtype":
217
+ if isinstance(value, str):
218
+ return np.dtype(value)
219
+
220
+ if key == "scales":
221
+ if isinstance(value, list):
222
+ if value and all(isinstance(v, (list, tuple)) for v in value):
223
+ return [tuple(v) for v in value]
224
+
225
+ return value
226
+
227
+
228
+ def update_metadata_from_dict(
229
+ image_metadata: ImageMetadata,
230
+ update_dict: Mapping[str, Any],
231
+ ) -> ImageMetadata:
232
+ """
233
+ Update an ImageMetadata instance from a dict.
234
+
235
+ Matching keys update dataclass fields; unknown keys are stored in
236
+ ``additional_metadata``.
237
+ """
238
+ valid_fields = {f.name for f in fields(image_metadata)}
239
+
240
+ for key, value in update_dict.items():
241
+ if key in valid_fields:
242
+ setattr(image_metadata, key, _convert_metadata_value(key, value))
243
+ else:
244
+ image_metadata.additional_metadata[key] = value
245
+
246
+ return image_metadata
@@ -5,10 +5,10 @@ Helper functions for running a tiamat processing pipeline.
5
5
  from __future__ import annotations
6
6
 
7
7
  from collections.abc import Callable, Iterable
8
- from typing import Any
8
+ from typing import Any, Mapping
9
9
 
10
10
  from .io import ImageAccessor, ImageResult
11
- from .metadata import ImageMetadata
11
+ from .metadata import ImageMetadata, update_metadata_from_dict
12
12
  from .readers.factory import get_reader
13
13
  from .readers.protocol import ImageReader
14
14
  from .transformers.protocol import Transformer
@@ -29,6 +29,7 @@ class Pipeline:
29
29
  image_transformers: Iterable[Transformer] | None = None,
30
30
  reader_factory: Callable[[str], ImageReader] | None = None,
31
31
  auto_register_default_readers: bool = True,
32
+ metadata_overrides: Mapping[str, Any] | None = None,
32
33
  ):
33
34
  """
34
35
  Initializes the Pipeline.
@@ -41,6 +42,8 @@ class Pipeline:
41
42
  Cannot be used together with `transformers`.
42
43
  reader_factory (Callable[[str], ImageReader] | None): Function returning a reader for a given file name.
43
44
  auto_register_default_readers (bool): Whether to auto-register default readers on pipeline call.
45
+ metadata_overrides (Mapping[str, Any] | None): Optional metadata values that override or extend
46
+ metadata returned by the reader. Unknown keys are stored in ``additional_metadata``.
44
47
  """
45
48
  if transformers:
46
49
  assert (
@@ -60,6 +63,8 @@ class Pipeline:
60
63
  self.reader_factory = reader_factory or get_reader
61
64
  self.auto_register_default_readers = auto_register_default_readers
62
65
 
66
+ self.metadata_overrides = metadata_overrides if metadata_overrides is not None else {}
67
+
63
68
  def __call__(
64
69
  self,
65
70
  file_name: str,
@@ -87,7 +92,7 @@ class Pipeline:
87
92
  reader = self.reader_factory(file_name, **reader_kwargs)
88
93
 
89
94
  # Forward rollout of metadata through transformers
90
- metadata = [reader.read_metadata()]
95
+ metadata = [update_metadata_from_dict(reader.read_metadata(), self.metadata_overrides)]
91
96
 
92
97
  for transformer in self.transformers:
93
98
  # Check for transformers that do not implement transform_metadata
@@ -135,10 +140,11 @@ class Pipeline:
135
140
  from dataclasses import replace
136
141
 
137
142
  reader = self.reader_factory(file_name, **reader_kwargs)
138
- metadata = reader.read_metadata()
143
+ metadata = update_metadata_from_dict(reader.read_metadata(), self.metadata_overrides)
139
144
  # backwards pass through the transformers to transform the accessor
140
145
  for transformer in self.transformers:
141
146
  if hasattr(transformer, "transform_metadata"):
142
147
  # check for transformers that do not implement transform_metadata
143
148
  metadata = transformer.transform_metadata(metadata=replace(metadata))
149
+
144
150
  return metadata
@@ -49,9 +49,13 @@ def expand_to_length(value: Any, length: int) -> list[Any]:
49
49
  Returns:
50
50
  List of length `length`.
51
51
  """
52
- if not isinstance(value, (list, tuple, np.ndarray)):
53
- return [value] * length
54
- elif len(value) == 1:
52
+
53
+ if np.ndim(value) == 0:
54
+ if isinstance(value, np.ndarray):
55
+ return [value.item()] * length
56
+ else:
57
+ return [value] * length
58
+ if len(value) == 1:
55
59
  return [value[0]] * length
56
60
  return value
57
61
 
@@ -187,7 +187,9 @@ class ImageStackReader(ImageReader):
187
187
  """
188
188
  from tiamat.readers.processing import expand_to_length
189
189
 
190
- if slice_spacing is None:
190
+ if hasattr(spacing, "__len__") and len(spacing) == 3:
191
+ return spacing
192
+ elif slice_spacing is None:
191
193
  spacing_2d = expand_to_length(spacing, 2)
192
194
  assert (
193
195
  spacing_2d[0] == spacing_2d[1]
@@ -768,6 +770,10 @@ class VolumeStackReader(ImageReader):
768
770
  # Size of the current subvolume
769
771
  z_size = shape[z_dim]
770
772
 
773
+ # Everything read
774
+ if coverage_offset >= z_to:
775
+ break
776
+
771
777
  # Access only required subvolumes that lay in the accessor range and are not covered yet
772
778
  if volume_z_offset + z_size > coverage_offset and volume_z_offset < z_to:
773
779
 
@@ -777,6 +783,7 @@ class VolumeStackReader(ImageReader):
777
783
 
778
784
  # Access subvolume and read from it
779
785
  tmp_accessor = replace(accessor, z=(from_ix, to_ix))
786
+
780
787
  tmp_image = handle.read_image(accessor=tmp_accessor)
781
788
 
782
789
  # Position result in the output array
@@ -25,10 +25,10 @@ from tiamat.io import ImageAccessor
25
25
  from tiamat.metadata import ImageMetadata
26
26
  from tiamat.readers.protocol import ImageReader
27
27
 
28
-
29
28
  # retrieved from https://ngff.openmicroscopy.org/specifications/0.5/index.html#axes-metadata
30
29
  # on 2026-03-05
31
30
 
31
+
32
32
  class SiUnit(NamedTuple):
33
33
  symbol: str
34
34
  exponent: int
@@ -258,9 +258,7 @@ class OmeZarrReader(ImageReader):
258
258
  @cached_property
259
259
  def _kvstore(self):
260
260
  if not _TENSORSTORE_AVAILABLE:
261
- raise ImportError(
262
- "tensorstore is not installed. Install `tensorstore` to read OME-Zarr data."
263
- )
261
+ raise ImportError("tensorstore is not installed. Install `tensorstore` to read OME-Zarr data.")
264
262
  return ts.KvStore.open(self._kvstore_spec).result()
265
263
 
266
264
  @cached_property
@@ -346,40 +344,51 @@ class OmeZarrReader(ImageReader):
346
344
 
347
345
  @cached_property
348
346
  def pixel_spacing(self) -> tuple[float, ...]:
349
- axes = self.axes_names
347
+ return tuple(self._dataset_physical_scales(0))
348
+
349
+ def _scale_transform_for_dataset(self, level: int) -> list[float]:
350
+ transforms = self._datasets[level].get("coordinateTransformations", [])
351
+ for transform in transforms or []:
352
+ if isinstance(transform, dict) and transform.get("type") == "scale":
353
+ scale = transform.get("scale")
354
+ if isinstance(scale, list) and len(scale) == len(self.axes_names):
355
+ return [float(x) for x in scale]
356
+ raise ValueError(f"OME-Zarr dataset at level {level} is missing a valid scale transformation.")
357
+
358
+ @cached_property
359
+ def _axis_unit_factors(self) -> list[float]:
360
+ axis_entries = self._pyramid.get("axes")
361
+ if not isinstance(axis_entries, list) or len(axis_entries) != len(self.axes_names):
362
+ return [1.0] * len(self.axes_names)
363
+
364
+ factors: list[float] = []
365
+ for axis in axis_entries:
366
+ if not isinstance(axis, dict):
367
+ factors.append(1.0)
368
+ continue
369
+
370
+ if axis.get("type") != "space":
371
+ factors.append(1.0)
372
+ continue
350
373
 
351
- mul_factor: list[float] = []
352
- for axis in self._pyramid.get("axes", []):
353
374
  unit = axis.get("unit")
354
375
  if factor := spatial_units_to_micron.get(unit):
355
- mul_factor.append(factor)
376
+ factors.append(factor)
356
377
  else:
357
378
  raise RuntimeError(f"zarr unit {unit} either not recognized or factor not yet encoded.")
358
379
 
359
- transforms = self._datasets[0].get("coordinateTransformations", [])
360
- scale_vec = None
361
- for transform in transforms or []:
362
- if isinstance(transform, dict) and transform.get("type") == "scale":
363
- scale = transform.get("scale")
364
- if isinstance(scale, list) and len(scale) == len(axes) == len(mul_factor):
365
- scale_vec = [float(x) * factor for x, factor in zip(scale, mul_factor)]
366
- break
367
- if scale_vec is None:
368
- return tuple([1.0] * len(axes))
369
- return tuple(scale_vec)
380
+ return factors
381
+
382
+ @instance_cache
383
+ def _dataset_physical_scales(self, level: int) -> list[float]:
384
+ scale = self._scale_transform_for_dataset(level)
385
+ return [axis_scale * factor for axis_scale, factor in zip(scale, self._axis_unit_factors)]
370
386
 
371
387
  @instance_cache
372
388
  def _level_factors(self, level: int) -> list[float]:
373
- base = self._get_level_array(0).shape
374
- cur = self._get_level_array(level).shape
375
- factors = []
376
- spatial = {"z", "y", "x"}
377
- for i, axis_name in enumerate(self.axes_names):
378
- if axis_name in spatial:
379
- factors.append(float(cur[i]) / float(base[i]))
380
- else:
381
- factors.append(1.0)
382
- return factors
389
+ base_scale = self._dataset_physical_scales(0)
390
+ level_scale = self._dataset_physical_scales(level)
391
+ return [float(base) / float(current) for base, current in zip(base_scale, level_scale)]
383
392
 
384
393
  @cached_property
385
394
  def scales(self) -> list[tuple[float, ...]]:
@@ -14,6 +14,9 @@ from tiamat.readers.protocol import ImageReader
14
14
  class_registry: dict[str, type] = {}
15
15
 
16
16
 
17
+ pipeline_keys = ["transformers", "access_transformers", "image_transformers", "reader", "reader_factory"]
18
+
19
+
17
20
  def register_class(cls: type) -> type:
18
21
  """
19
22
  Decorator to register a class for safe instantiation from configuration.
@@ -142,6 +145,12 @@ def load_pipeline_from_config(
142
145
 
143
146
  register_all_readers()
144
147
 
148
+ metadata_overrides = {k: config[k] for k in set(config) - set(pipeline_keys)}
149
+
150
+ # Resolve confusion between reader_factory and reader args
151
+ if "reader_factory" in config.keys():
152
+ config["reader"] = config["reader_factory"]
153
+
145
154
  return Pipeline(
146
155
  transformers=[make_object_from_config(item) for item in config.get("transformers", [])],
147
156
  access_transformers=[make_object_from_config(item) for item in config.get("access_transformers", [])],
@@ -150,4 +159,5 @@ def load_pipeline_from_config(
150
159
  config.get("reader", None), reader_post_creation_hook=reader_post_creation_hook
151
160
  ),
152
161
  auto_register_default_readers=False,
162
+ metadata_overrides=metadata_overrides,
153
163
  )
@@ -109,7 +109,6 @@ class LUTTransformer(Transformer):
109
109
  metadata.dtype = np.asarray(self.color_map).dtype
110
110
  ldim = len(self.color_map[0])
111
111
  if isinstance(self.color_map, np.ndarray):
112
- print(self.color_map)
113
112
  metadata.value_range = (np.min(self.color_map).item(), np.max(self.color_map).max().item())
114
113
  else:
115
114
  metadata.value_range = (min(self.color_map), max(self.color_map))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tiamat-python
3
- Version: 0.1.12
3
+ Version: 0.1.13
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:
File without changes
File without changes
File without changes
File without changes