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..0fe890d 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -19,6 +19,14 @@ def test_add_transforms(): t.add_transform(Translate([10, 20], spaces=Spaces("b", "c"))) +@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"))) + s = t.get_sequence("a", "a", full=full) + assert s.apply(coords5x3) == pytest.approx(coords5x3) + + def test_path(): t1 = Scale([2, 3], spaces=Spaces("a", "b")) t2 = Translate([10, 20], spaces=Spaces("b", "c"))