|
| 1 | +from typing import Literal |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from iohub.ngff import TransformationMeta |
| 5 | + |
| 6 | + |
| 7 | +def _get_all_transforms(node, image: str | Literal["*"]) -> list[TransformationMeta]: |
| 8 | + """Get all transforms metadata |
| 9 | + for one image array or the whole FOV. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + image : str | Literal["*"] |
| 14 | + Name of one image array (e.g. "0") to query, |
| 15 | + or "*" for the whole FOV |
| 16 | +
|
| 17 | + Returns |
| 18 | + ------- |
| 19 | + list[TransformationMeta] |
| 20 | + All transforms applicable to this image or FOV. |
| 21 | + """ |
| 22 | + transforms: list[TransformationMeta] = ( |
| 23 | + [t for t in node.metadata.multiscales[0].coordinate_transformations] |
| 24 | + if node.metadata.multiscales[0].coordinate_transformations is not None |
| 25 | + else [] |
| 26 | + ) |
| 27 | + if image != "*" and image in node: |
| 28 | + for i, dataset_meta in enumerate(node.metadata.multiscales[0].datasets): |
| 29 | + if dataset_meta.path == image: |
| 30 | + transforms.extend( |
| 31 | + node.metadata.multiscales[0].datasets[i].coordinate_transformations |
| 32 | + ) |
| 33 | + elif image != "*": |
| 34 | + raise ValueError(f"Key {image} not recognized.") |
| 35 | + return transforms |
| 36 | + |
| 37 | + |
| 38 | +def get_effective_scale( |
| 39 | + node, |
| 40 | + image: str | Literal["*"], |
| 41 | +) -> list[float]: |
| 42 | + """Get the effective coordinate scale metadata |
| 43 | + for one image array or the whole FOV. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + image : str | Literal["*"] |
| 48 | + Name of one image array (e.g. "0") to query, |
| 49 | + or "*" for the whole FOV |
| 50 | +
|
| 51 | + Returns |
| 52 | + ------- |
| 53 | + list[float] |
| 54 | + A list of floats representing the total scale |
| 55 | + for the image or FOV for each axis. |
| 56 | + """ |
| 57 | + transforms = node._get_all_transforms(image) |
| 58 | + |
| 59 | + full_scale = np.ones(len(node.axes), dtype=float) |
| 60 | + for transform in transforms: |
| 61 | + if transform.type == "scale": |
| 62 | + full_scale *= np.array(transform.scale) |
| 63 | + |
| 64 | + return [float(x) for x in full_scale] |
| 65 | + |
| 66 | + |
| 67 | +def get_effective_translation( |
| 68 | + node, |
| 69 | + image: str | Literal["*"], |
| 70 | +) -> TransformationMeta: |
| 71 | + """Get the effective coordinate translation metadata |
| 72 | + for one image array or the whole FOV. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + image : str | Literal["*"] |
| 77 | + Name of one image array (e.g. "0") to query, |
| 78 | + or "*" for the whole FOV |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + list[float] |
| 83 | + A list of floats representing the total translation |
| 84 | + for the image or FOV for each axis. |
| 85 | + """ |
| 86 | + transforms = node._get_all_transforms(image) |
| 87 | + full_translation = np.zeros(len(node.axes), dtype=float) |
| 88 | + for transform in transforms: |
| 89 | + if transform.type == "translation": |
| 90 | + full_translation += np.array(transform.translation) |
| 91 | + |
| 92 | + return [float(x) for x in full_translation] |
0 commit comments