Skip to content

Commit

Permalink
fix HyperGraph.plot with non-consec nodes (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmgray committed May 27, 2024
1 parent 18dbed3 commit e19e7db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cotengra/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ def _edge_colorer(ix):
if node_color is True:
from .schematic import auto_colors

node_colors = auto_colors(H.get_num_nodes())
node_colors = dict(
zip(sorted(H.nodes), auto_colors(H.get_num_nodes()))
)

def _node_colorer(nd):
return node_colors[nd]
Expand Down
25 changes: 24 additions & 1 deletion tests/test_hypergraph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import cotengra as ctg


def test_shortest_distances():
inputs, output, _, size_dict = ctg.utils.lattice_equation([3, 3])
hg = ctg.HyperGraph(inputs, output, size_dict)
Expand Down Expand Up @@ -34,6 +35,28 @@ def test_compute_loops():
hg = ctg.HyperGraph(inputs, output, size_dict)
loops = list(hg.compute_loops())
assert set(loops) == {
(0, 1, 3, 4), (1, 2, 4, 5), (3, 4, 6, 7), (4, 5, 7, 8)
(0, 1, 3, 4),
(1, 2, 4, 5),
(3, 4, 6, 7),
(4, 5, 7, 8),
}
assert len(list(hg.compute_loops(max_loop_length=6))) == 8


def test_plot_nonconsec():
import matplotlib as mpl

mpl.use("Template")

inputs = [
("a", "b", "x"),
("b", "c", "d"),
("c", "e", "y"),
("e", "a", "d"),
]
output = ("x", "y")
size_dict = {"x": 2, "y": 3, "a": 4, "b": 5, "c": 6, "d": 7, "e": 8}

hg = ctg.HyperGraph(inputs, output, size_dict)
hg.contract(0, 1)
hg.plot()

0 comments on commit e19e7db

Please sign in to comment.