Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/transformnd/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading