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 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
31 changes: 31 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@ def bipartite_graph4():
return G


@pytest.fixture
def bipartite_digraph1():
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(
[
("a", 1, {"direction": "head"}),
("b", 1, {"direction": "tail"}),
("b", 2, {"direction": "head"}),
("c", 2, {"direction": "tail"}),
("c", 3, {"direction": "head"}),
("a", 4, {"direction": "tail"}),
]
)
return G


@pytest.fixture
def bipartite_digraph2():
G = nx.DiGraph()
G.add_nodes_from([1], bipartite=0)
G.add_nodes_from(["a"], bipartite=1)
G.add_edges_from(
[
("a", 1, {"direction": "invalid"}),
]
)
return G


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


def test_from_bipartite_digraph_to_dihypergraph(
bipartite_digraph1, bipartite_digraph2, bipartite_graph1
):
H = xgi.from_bipartite_graph(bipartite_digraph1)

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}

# missing direction
with pytest.raises(XGIError):
H = xgi.from_bipartite_graph(bipartite_graph1, xgi.DiHypergraph)

# unknown direction
with pytest.raises(XGIError):
H = xgi.from_bipartite_graph(bipartite_digraph2)
40 changes: 34 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
from networkx import bipartite

import xgi

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

__all__ = ["from_bipartite_graph", "to_bipartite_graph"]

Expand Down Expand Up @@ -73,17 +74,44 @@ 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):
if not _is_bipartite(G, nodes, edges):
raise XGIError("The network is not bipartite")

H = empty_hypergraph(create_using)
if G.is_directed():
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
H = empty_dihypergraph(create_using)
else:
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=True):
if isinstance(H, xgi.DiHypergraph):
try:
edge_direction = d["direction"]
except KeyError as e:
raise XGIError("direction property not set in bipartite graph") from e

if edge_direction == "tail":
H.add_node_to_edge(u, v, direction="in")
elif edge_direction == "head":
H.add_node_to_edge(u, v, direction="out")
else:
raise XGIError("Invalid direction specifier")
else:
H.add_node_to_edge(u, v)
return H.dual() if dual else H


def _is_bipartite(G, nodes1, nodes2):
"""Assumption is that nodes1.union(nodes2) == G.nodes"""
for i, j in G.edges:
cond1 = i in nodes1
cond2 = j in nodes2
if not cond1 == cond2: # if not both true or both false
return False
return True


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

Expand Down