Skip to content

Commit 20d9adb

Browse files
authored
Transforming custom axes to canonical up to 5D (#150)
1 parent 7ee7f1b commit 20d9adb

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

tests/unit/test_axes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ class TestAxes:
9191
def test_init(self):
9292
assert Axes("XYZ").dims == "XYZ"
9393

94-
with pytest.raises(ValueError) as excinfo:
95-
Axes("XYZW")
96-
assert str(excinfo.value) == "'W' is not a valid Axis"
97-
9894
with pytest.raises(ValueError) as excinfo:
9995
Axes("XYZX")
10096
assert "Duplicate axes" in str(excinfo.value)

tiledb/bioimg/converters/axes.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import ABC, abstractmethod
44
from dataclasses import dataclass
5-
from typing import Any, Iterable, Iterator, MutableSequence, Sequence, Tuple
5+
from typing import Any, Iterable, Iterator, MutableSequence, Sequence, Set, Tuple
66

77
import numpy as np
88
from pyeditdistance.distance import levenshtein
@@ -209,11 +209,27 @@ def __init__(self, dims: Iterable[str]):
209209
for required_axis in "X", "Y":
210210
if required_axis not in axes:
211211
raise ValueError(f"Missing required axis {required_axis!r}")
212+
212213
axes.difference_update(self.CANONICAL_DIMS)
213214
if axes:
214-
raise ValueError(f"{axes.pop()!r} is not a valid Axis")
215+
if len(axes) == 1:
216+
# Assign extra/custom dimension as Time TBC
217+
dims = self._canonical_transformation(dims, axes)
218+
else:
219+
raise ValueError(f"{axes.pop()!r} is not a valid Axis")
215220
object.__setattr__(self, "dims", dims)
216221

222+
@staticmethod
223+
def _canonical_transformation(dims: str, axes: Set[str]) -> str:
224+
custom_axis = f"{axes.pop()}".replace("'", "")
225+
if "T" not in dims:
226+
dims.replace(custom_axis, "T")
227+
elif "Z" not in dims:
228+
dims.replace(custom_axis, "Z")
229+
else:
230+
raise ValueError(f"{custom_axis!r} cannot be mapped to a canonical value")
231+
return dims
232+
217233
def canonical(self, shape: Tuple[int, ...]) -> Axes:
218234
"""
219235
Return a new Axes instance with the dimensions of this axes whose size in `shape`

0 commit comments

Comments
 (0)