Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index-to-unit mappings from to_bipartite_graph. Fixes #322 #323

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ def test_to_bipartite_graph(edgelist1, edgelist3, edgelist4):
[5, 2],
]

G1, node_dict, edge_dict = xgi.to_bipartite_graph(H1)
G1, node_dict, edge_dict = xgi.to_bipartite_graph(H1, index=True)
bi_el1 = [[node_dict[u], edge_dict[v]] for u, v in G1.edges]

assert sorted(bi_el1) == sorted(true_bi_el1)

G2, node_dict, edge_dict = xgi.to_bipartite_graph(H2)
G2, node_dict, edge_dict = xgi.to_bipartite_graph(H2, index=True)
bi_el2 = [[node_dict[u], edge_dict[v]] for u, v in G2.edges]

assert sorted(bi_el2) == sorted(true_bi_el2)

G3, node_dict, edge_dict = xgi.to_bipartite_graph(H3)
G3, node_dict, edge_dict = xgi.to_bipartite_graph(H3, index=True)
bi_el3 = [[node_dict[u], edge_dict[v]] for u, v in G3.edges]

assert sorted(bi_el3) == sorted(true_bi_el3)
Expand Down
28 changes: 18 additions & 10 deletions xgi/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,19 +613,22 @@ def from_bipartite_graph(G, create_using=None, dual=False):
return H.dual() if dual else H


def to_bipartite_graph(H):
"""
Create a NetworkX bipartite network from a hypergraph.
def to_bipartite_graph(H, index=False):
"""Create a NetworkX bipartite network from a hypergraph.

Parameters
----------
H: xgi.Hypergraph
The XGI hypergraph object of interest
index: bool (default False)
If False (default), return only the graph. If True, additionally return the
index-to-node and index-to-edge mappings.

Returns
-------
nx.Graph
The resulting equivalent bipartite graph
nx.Graph[, dict, dict]
The resulting equivalent bipartite graph, and optionally the index-to-unit
mappings.

References
----------
Expand All @@ -639,6 +642,8 @@ def to_bipartite_graph(H):
>>> hyperedge_list = [[1, 2], [2, 3, 4]]
>>> H = xgi.Hypergraph(hyperedge_list)
>>> G = xgi.to_bipartite_graph(H)
>>> G, itn, ite = xgi.to_bipartite_graph(H, index=True)

"""
G = nx.Graph()

Expand All @@ -650,11 +655,14 @@ def to_bipartite_graph(H):
for edge in H.nodes.memberships(node):
G.add_edge(node_dict[node], edge_dict[edge])

return (
G,
dict(zip(range(H.num_nodes), H.nodes)),
dict(zip(range(H.num_nodes, H.num_nodes + H.num_edges), H.edges)),
)
if index:
return (
G,
dict(zip(range(H.num_nodes), H.nodes)),
dict(zip(range(H.num_nodes, H.num_nodes + H.num_edges), H.edges)),
)
else:
return G


def dict_to_hypergraph(data, nodetype=None, edgetype=None, max_order=None):
Expand Down