Skip to content

Commit 91202fc

Browse files
committed
Pull request #231: Release/2.4 visualization
Merge in HYP/hypernetx from release/2.4_visualization to master * commit '7e915833cb189db49f86ef4654ce19f2331f0216': (78 commits) bump: version 2.3.13 → 2.4.0 fix pre-commit add widget Implemented and added an example of size-aware layout for Euler diagrams. Replaced hnx.drawing.draw with hnx.draw updated tutorials to match on new HNX methods updated Basic Tutorials to new version of HNX precommit updated linting removing h2vec images and json files from develop until approved by sponsor removed docs for hedge2vec from develop until approved by sponsor post linting changes removed hedge2vec until approved by sponsor changed image sizes added images to .rst updated hedge2vec rst with images added doc file, moved json file for tutorial added hedge2vec files Added description of fill_edge_alpha to comment block. Reran to update figures. ...
2 parents bc07b3e + 7e91583 commit 91202fc

28 files changed

+84455
-122046
lines changed

.cz.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.commitizen]
22
name = "cz_conventional_commits"
3-
version = "2.3.13"
3+
version = "2.4.0"
44
version_provider = "poetry"
55
version_files = [
66
"pyproject.toml",

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020

2121

22-
__version__ = "2.3.13"
22+
__version__ = "2.4.0"
2323

2424

2525
# If extensions (or modules to document with autodoc) are in another directory,

hypernetx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
from hypernetx.utils import *
1212
from hypernetx.utils.toys import *
1313

14-
__version__ = "2.3.13"
14+
__version__ = "2.4.0"

hypernetx/drawing/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from hypernetx.drawing.rubber_band import draw
2-
from hypernetx.drawing.two_column import draw as draw_two_column
1+
from .rubber_band import draw
2+
from .draw_incidence import draw_incidence_upset
3+
from .draw_bipartite import draw_bipartite_using_euler
34

4-
__all__ = ["draw", "draw_two_column"]
5+
__all__ = ["draw", "draw_incidence_upset", "draw_bipartite_using_euler"]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import numpy as np
2+
import networkx as nx
3+
4+
import hypernetx as hnx
5+
6+
7+
def rebalance(B, pos, side):
8+
new_pos = {}
9+
10+
y = -1
11+
for u in side:
12+
x = pos[u][0]
13+
y = max(y + 1, min([pos[v][1] for v in B[u]]))
14+
new_pos[u] = (x, y)
15+
16+
return {**pos, **new_pos}
17+
18+
19+
def sort_left_by_right(B, left, right):
20+
pos = {v: i for i, v in enumerate(right)}
21+
print(left, right)
22+
23+
return sorted(left, key=lambda u: np.mean([pos[v] for v in B[u]]))
24+
25+
26+
def bipartite_layout(B, left_side, right_side, width=1):
27+
28+
pos = {}
29+
30+
for i, v in enumerate(left_side):
31+
pos[v] = (0, i)
32+
33+
for i, v in enumerate(right_side):
34+
pos[v] = (width, i)
35+
36+
if len(left_side) < len(right_side):
37+
pos = rebalance(B, pos, left_side)
38+
pos = rebalance(B, pos, right_side)
39+
elif len(left_side) > len(right_side):
40+
pos = rebalance(B, pos, right_side)
41+
pos = rebalance(B, pos, left_side)
42+
43+
return pos
44+
45+
46+
def draw_bipartite_using_euler(
47+
H, pos=None, node_order=None, edge_order=None, edge_labels_kwargs={}, **kwargs
48+
):
49+
"""
50+
Draw a hypergraph as a two column bipartite graph using hypernetx.draw.
51+
52+
This function calculates the x- and y-coordinates of nodes and edges by placing
53+
edges in the left column and nodes in the right column. The ordering of edges
54+
and nodes is determined by default using the networkx.spectral_order method
55+
on the bipartite representation of the hypergraph. Node and edge order can
56+
also be specified by the user. If one or the other is specified, then the
57+
unspecified column (nodes or edges) are sorted using the barycenter method.
58+
Additional minor adjustment of node and edge height is performed to improve
59+
readability.
60+
61+
Additional encoding kwargs are passed through to the hypernetx.draw method.
62+
63+
Parameters
64+
----------
65+
H: hnx.Hypergraph
66+
the entity to be drawn
67+
pos: dict
68+
the positioning of the hypergraph
69+
node_order: list
70+
specify to override the order of the nodes in the drawing
71+
edge_order: list
72+
specify to override the order of the edges in the drawing
73+
"""
74+
75+
B = H.bipartite().to_undirected()
76+
if pos is None:
77+
if node_order is None and edge_order is not None:
78+
node_order = sort_left_by_right(B, H.nodes, edge_order)
79+
elif node_order is not None and edge_order is None:
80+
edge_order = sort_left_by_right(B, H.edges, node_order)
81+
elif node_order is None and edge_order is None:
82+
order = nx.spectral_ordering(B, seed=1234567890)
83+
node_order = list(filter(H.nodes.__contains__, order))
84+
edge_order = list(filter(H.edges.__contains__, order))
85+
86+
pos = bipartite_layout(
87+
B, edge_order, node_order, width=0.5 * max(len(H.nodes), len(H.edges))
88+
)
89+
90+
return hnx.drawing.draw(
91+
H,
92+
pos=pos,
93+
with_additional_edges=B,
94+
contain_hyper_edges=True,
95+
edge_labels_on_edge=False,
96+
edge_labels_kwargs={
97+
"xytext": (-10, 0),
98+
"textcoords": "offset points",
99+
**edge_labels_kwargs,
100+
},
101+
**kwargs
102+
)

0 commit comments

Comments
 (0)