From c15ed7cf85f0a3519676ab63974757df8d0acaba Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Mon, 5 Oct 2026 12:59:52 +0100 Subject: [PATCH 1/2] Test adding and removing axis --- tests/transforms/test_project_axis.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/transforms/test_project_axis.py b/tests/transforms/test_project_axis.py index 545c684..5c9eb33 100644 --- a/tests/transforms/test_project_axis.py +++ b/tests/transforms/test_project_axis.py @@ -25,6 +25,22 @@ def test_invert(): assert ti.created == {0} +def test_change_ndim(): + orig = as_floats([[1, 2]]) + add0 = ProjectAxis(created={0}, source_ndim=2) + assert add0.ndims.source == 2 + assert add0.ndims.target == 3 + added = add0.apply(orig) + assert added == pytest.approx(as_floats([[0, 1, 2]])) + + rem0 = add0.invert() + assert rem0 is not None + assert rem0.ndims.source == 3 + assert rem0.ndims.target == 2 + removed = rem0.apply(added) + assert removed == pytest.approx(orig) + + def random_ops(in_ndim: int, out_ndim: int, seed=1991) -> ProjectAxis: rng = Random(seed) n_dropped = rng.randint(max(0, in_ndim - out_ndim), in_ndim - 1) From 0ede8ff916f32fef54ff62d793d0740d4a81aff6 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Mon, 5 Oct 2026 13:23:03 +0100 Subject: [PATCH 2/2] Fix bug in Spaced.invert --- src/transformnd/spaced.py | 2 +- tests/transforms/test_spaced.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/transforms/test_spaced.py diff --git a/src/transformnd/spaced.py b/src/transformnd/spaced.py index ee93f4f..2bb466c 100644 --- a/src/transformnd/spaced.py +++ b/src/transformnd/spaced.py @@ -38,7 +38,7 @@ def invert(self) -> Self | None: t = self.transform.invert() if t is None: return None - return type(self)(t, self.spaces.source, self.spaces.target) + return type(self)(t, self.spaces.target, self.spaces.source) def __invert__(self): out = self.invert() diff --git a/tests/transforms/test_spaced.py b/tests/transforms/test_spaced.py new file mode 100644 index 0000000..07a30d8 --- /dev/null +++ b/tests/transforms/test_spaced.py @@ -0,0 +1,15 @@ +import pytest +from transformnd.spaced import Spaced +from transformnd.transforms import Translate +from transformnd.util import as_floats + + +def test_invert_spaced(coords5x3): + t1 = Translate(as_floats([1, 2, 3])) + s1 = Spaced(t1, "a", "b") + s2 = s1.invert() + assert s2 is not None + assert s2.spaces.source == s1.spaces.target + assert s2.spaces.target == s1.spaces.source + + assert s2.transform.apply(s1.transform.apply(coords5x3)) == pytest.approx(coords5x3)