From ed4cfc2c727d6bcbca2ffb406d75026cb26845a9 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Tue, 7 Jul 2026 10:49:51 +0100 Subject: [PATCH 1/2] Allow transform graph to get self-edge sequence Resolves #77 --- src/transformnd/graph.py | 18 ++++++++++++------ tests/test_graph.py | 6 ++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/transformnd/graph.py b/src/transformnd/graph.py index a18d051..6c6bff9 100644 --- a/src/transformnd/graph.py +++ b/src/transformnd/graph.py @@ -10,6 +10,8 @@ import networkx as nx +from transformnd.transforms.simple import Identity + from .transforms.bijection import Bijection from .base import Transform, TransformSequence from .util import SpaceRef, ArrayT, same_or_none @@ -216,13 +218,17 @@ def get_sequence( """ path = nx.shortest_path(self.graph, source_space, target_space, weight) # type:ignore transforms = [] - wfn = normalise_edge_weight_fn(weight) + if len(path) == 1: + # source == target + transforms.append(Identity[ArrayT](self.space_ndims[source_space])) + else: + wfn = normalise_edge_weight_fn(weight) - for src, tgt in pairwise(path): - edges = self.graph[src][tgt] - transforms.append( - min(edges.values(), key=lambda d: wfn(src, tgt, d))[TRANSFORM_KEY] - ) + for src, tgt in pairwise(path): + edges = self.graph[src][tgt] + transforms.append( + min(edges.values(), key=lambda d: wfn(src, tgt, d))[TRANSFORM_KEY] + ) seq = TransformSequence( transforms, diff --git a/tests/test_graph.py b/tests/test_graph.py index eeb9f71..485d65d 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -19,6 +19,12 @@ def test_add_transforms(): t.add_transform(Translate([10, 20], spaces=Spaces("b", "c"))) +def test_noop_path(): + t: TransformGraph = TransformGraph() + t.add_transform(Scale([2, 2], spaces=Spaces("a", "b"))) + t.get_sequence("a", "a") + + def test_path(): t1 = Scale([2, 3], spaces=Spaces("a", "b")) t2 = Translate([10, 20], spaces=Spaces("b", "c")) From 73944441255131a4169c65ff784c5c8cf0f285a9 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Tue, 7 Jul 2026 10:55:14 +0100 Subject: [PATCH 2/2] Test full/ not full --- tests/test_graph.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_graph.py b/tests/test_graph.py index 485d65d..0fe890d 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -19,10 +19,12 @@ def test_add_transforms(): t.add_transform(Translate([10, 20], spaces=Spaces("b", "c"))) -def test_noop_path(): +@pytest.mark.parametrize(("full",), [(True,), (False,)]) +def test_noop_path(coords5x3, full): t: TransformGraph = TransformGraph() t.add_transform(Scale([2, 2], spaces=Spaces("a", "b"))) - t.get_sequence("a", "a") + s = t.get_sequence("a", "a", full=full) + assert s.apply(coords5x3) == pytest.approx(coords5x3) def test_path():