Skip to content

Commit

Permalink
NetworkX from HIF
Browse files Browse the repository at this point in the history
  • Loading branch information
colltoaction committed Aug 27, 2024
1 parent c4d8b6f commit 66efa39
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/hif.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ def validate_hif(path):
info["valid-field-names"] = 0
fields = {"network-type", "metadata", "nodes", "edges", "incidences"}
if not set(data).issubset(fields):
fields = ", ".join(fields)
data = ", ".join(set(data))
warn(
f"Acceptable field names are: {", ".join(fields)}\nand the field names are {", ".join(set(data))}"
f"Acceptable field names are: {fields}\nand the field names are {data}"
)
info["valid-field-names"] = 1

Expand All @@ -33,8 +35,9 @@ def validate_hif(path):
network_types = {"asc", "undirected", "directed"}
if "network-type" in data:
if data["network-type"] not in network_types:
network_types = ", ".join(network_types)
warn(
f"Unsupported network type. Valid types are: {", ".join(network_types)}"
f"Unsupported network type. Valid types are: {network_types}"
)
info["valid-network-type"] = 1

Expand Down
11 changes: 11 additions & 0 deletions scripts/nx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import networkx as nx

def from_hif(data) -> nx.Graph:
g = nx.Graph()
for n in data.get("nodes", []):
g.add_node(n["node"], bipartite=0, weight=n.get("weight", 0))
for e in data.get("edges", []):
g.add_node(e["edge"], bipartite=1, weight=e.get("weight", 0))
for i in data["incidences"]:
g.add_edge(i["node"], i["edge"], weight=i.get("weight", 0))
return g
File renamed without changes.
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def validator():
def empty():
return json.load(open(f"{json_dir}/empty.json", "r"))

@pytest.fixture
def single_node():
return json.load(open(f"{json_dir}/single_node.json", "r"))

@pytest.fixture
def single_edge():
return json.load(open(f"{json_dir}/single_edge.json", "r"))

@pytest.fixture
def single_incidence():
return json.load(open(f"{json_dir}/single_incidence.json", "r"))

@pytest.fixture
def bad_top_level_field():
Expand Down
1 change: 1 addition & 0 deletions tests/test_files/single_edge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incidences": [], "edges": [ { "edge": 3 } ]}
1 change: 1 addition & 0 deletions tests/test_files/single_incidence.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incidences": [ { "edge": "abcd", "node": 42 } ]}
1 change: 1 addition & 0 deletions tests/test_files/single_node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incidences": [], "nodes": [ { "node": 42 } ]}
26 changes: 26 additions & 0 deletions tests/test_nx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import networkx as nx
from scripts.nx import from_hif


def test_empty_hypergraph(empty_hypergraph):
result = from_hif(empty_hypergraph)
expected = nx.Graph()
assert nx.utils.graphs_equal(result, expected)

def test_single_node(single_node):
result = from_hif(single_node)
expected = nx.Graph()
expected.add_node(42, bipartite=0, weight=0)
assert nx.utils.graphs_equal(result, expected)

def test_single_edge(single_edge):
result = from_hif(single_edge)
expected = nx.Graph()
expected.add_node(3, bipartite=1, weight=0)
assert nx.utils.graphs_equal(result, expected)

def test_single_incidence(single_incidence):
result = from_hif(single_incidence)
expected = nx.Graph()
expected.add_edge("abcd", 42, weight=0)
assert nx.utils.graphs_equal(result, expected)

0 comments on commit 66efa39

Please sign in to comment.