From 66efa39ea242c67c6724b8c3b0c7c0447f23d8cf Mon Sep 17 00:00:00 2001 From: Martin Coll Date: Tue, 27 Aug 2024 13:03:28 -0300 Subject: [PATCH] NetworkX from HIF --- scripts/hif.py | 7 +++++-- scripts/nx.py | 11 +++++++++++ __init__.py => tests/__init__.py | 0 tests/conftest.py | 11 +++++++++++ tests/test_files/single_edge.json | 1 + tests/test_files/single_incidence.json | 1 + tests/test_files/single_node.json | 1 + tests/test_nx.py | 26 ++++++++++++++++++++++++++ 8 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 scripts/nx.py rename __init__.py => tests/__init__.py (100%) create mode 100644 tests/test_files/single_edge.json create mode 100644 tests/test_files/single_incidence.json create mode 100644 tests/test_files/single_node.json create mode 100644 tests/test_nx.py diff --git a/scripts/hif.py b/scripts/hif.py index 3aeded4..45f4962 100644 --- a/scripts/hif.py +++ b/scripts/hif.py @@ -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 @@ -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 diff --git a/scripts/nx.py b/scripts/nx.py new file mode 100644 index 0000000..6d1d1be --- /dev/null +++ b/scripts/nx.py @@ -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 \ No newline at end of file diff --git a/__init__.py b/tests/__init__.py similarity index 100% rename from __init__.py rename to tests/__init__.py diff --git a/tests/conftest.py b/tests/conftest.py index 44e6ab5..7df4f09 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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(): diff --git a/tests/test_files/single_edge.json b/tests/test_files/single_edge.json new file mode 100644 index 0000000..770692e --- /dev/null +++ b/tests/test_files/single_edge.json @@ -0,0 +1 @@ +{"incidences": [], "edges": [ { "edge": 3 } ]} \ No newline at end of file diff --git a/tests/test_files/single_incidence.json b/tests/test_files/single_incidence.json new file mode 100644 index 0000000..30f703a --- /dev/null +++ b/tests/test_files/single_incidence.json @@ -0,0 +1 @@ +{"incidences": [ { "edge": "abcd", "node": 42 } ]} \ No newline at end of file diff --git a/tests/test_files/single_node.json b/tests/test_files/single_node.json new file mode 100644 index 0000000..e9f08e4 --- /dev/null +++ b/tests/test_files/single_node.json @@ -0,0 +1 @@ +{"incidences": [], "nodes": [ { "node": 42 } ]} \ No newline at end of file diff --git a/tests/test_nx.py b/tests/test_nx.py new file mode 100644 index 0000000..6f38125 --- /dev/null +++ b/tests/test_nx.py @@ -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)