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

Support DiHypergraph in from_bipartite_graph #633

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ def bipartite_graph4():
return G


@pytest.fixture
def bipartite_dihypergraph1():
G = nx.DiGraph()
G.add_nodes_from([1, 2, 3, 4], bipartite=0)
G.add_nodes_from(["a", "b", "c"], bipartite=1)
G.add_edges_from([
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
("a", 1),
("b", 1, {"direction": "tail"}),
("b", 2),
("c", 2, {"direction": "tail"}),
("c", 3),
("a", 4, {"direction": "tail"})])
return G


@pytest.fixture
def attr0():
return {"color": "brown", "name": "camel"}
Expand Down
15 changes: 15 additions & 0 deletions tests/convert/test_bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,18 @@ def test_from_bipartite_graph(
# not bipartite
with pytest.raises(XGIError):
H = xgi.from_bipartite_graph(bipartite_graph4, dual=True)


def test_from_bipartite_graph_to_dihypergraph(
bipartite_dihypergraph1
):
H = xgi.from_bipartite_graph(bipartite_dihypergraph1)

assert set(H.nodes) == {1, 2, 3, 4}
assert set(H.edges) == {"a", "b", "c"}
assert H.edges.head("a") == {1}
assert H.edges.tail("a") == {4}
assert H.edges.head("b") == {2}
assert H.edges.tail("b") == {1}
assert H.edges.head("c") == {3}
assert H.edges.tail("c") == {2}
23 changes: 17 additions & 6 deletions xgi/convert/bipartite_graph.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Methods for converting to and from bipartite graphs."""

import networkx as nx
import xgi
from networkx import bipartite

from ..exception import XGIError
from ..generators import empty_hypergraph
from ..generators import empty_hypergraph, empty_dihypergraph

__all__ = ["from_bipartite_graph", "to_bipartite_graph"]

Expand Down Expand Up @@ -73,14 +74,24 @@ def from_bipartite_graph(G, create_using=None, dual=False):
else:
raise XGIError("Invalid type specifier")

if not bipartite.is_bipartite_node_set(G, nodes):
raise XGIError("The network is not bipartite")
if G.is_directed():
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
H = empty_dihypergraph(create_using)
else:
if not bipartite.is_bipartite_node_set(G, nodes):
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
raise XGIError("The network is not bipartite")

H = empty_hypergraph(create_using)

H = empty_hypergraph(create_using)
H.add_nodes_from(nodes)
for edge in edges:
nodes_in_edge = list(G.neighbors(edge))
H.add_edge(nodes_in_edge, idx=edge)
for u, v, d in G.edges(edge, data="direction"):
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(H, xgi.DiHypergraph):
if d == "tail":
H.add_node_to_edge(u, v, direction="in")
else:
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
H.add_node_to_edge(u, v, direction="out")
else:
H.add_node_to_edge(u, v)
return H.dual() if dual else H


Expand Down