Skip to content

Commit

Permalink
update degree dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
RobRossmiller-TG committed May 21, 2024
1 parent 99fd794 commit e95e9f7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
[
{
"@@top_scores_heap": [
"top_scores": [
{
"Vertex_ID": "A",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "B",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "C",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "D",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "E",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "F",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "G",
"score": 1.2857142857142856
"score": 1.1428571428571428
},
{
"Vertex_ID": "H",
"score": 1.2857142857142856
"score": 1.1428571428571428
}
]
}
Expand Down

This file was deleted.

29 changes: 15 additions & 14 deletions tests/data/create_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,53 @@

import networkx as nx
import numpy as np
from matplotlib import pyplot as plt

baseline_path_root = "baseline/graph_algorithms_baselines"


def run_degree_baseline(g: nx.Graph):
res = nx.centrality.degree_centrality(g)
nx.centrality.degree_centrality
# res = nx.centrality.degree_centrality(g)
s = 1.0 / (len(g) - 1.0)
res = {n: (d-1) * s for n, d in g.degree()} # d-1 because nx will double count the self-edge

out = []
for k, v in res.items():
out.append({"Vertex_ID": k, "score": v})

out = [{"@@top_scores_heap": out}]
out = [{"top_scores": out}]
return out


def create_graph(path, edges, weights):
def create_graph(edges, weights):
g = nx.Graph()
# include edge weights if they exist
if weights is not None:
if weights:
g.add_weighted_edges_from(edges)
else:
g.add_edges_from(edges)
return g


def create_degree_baseline():
# input, output
# input, output, weighed
paths = [
(
"unweighted_edges/complete_edges.csv",
f"{baseline_path_root}/centrality/degree_centrality/CompleteUnweighted.json",
f"{baseline_path_root}/centrality/degree_centrality/Complete.json",
False,
),
(
"weighted_edges/complete_edges.csv",
f"{baseline_path_root}/centrality/degree_centrality/CompleteWeighted.json",
True,
),
# (
# "weighted_edges/complete_edges.csv",
# f"{baseline_path_root}/centrality/weighted_degree_centrality/CompleteWeighted.json",
# True,
# ),
]

for p, o_path, w in paths:
with open(p) as f:
edges = np.array(list(csv.reader(f)))

g = create_graph(p, edges, w)
g = create_graph(edges, w)

res = run_degree_baseline(g)
with open(o_path, "w") as f:
Expand Down
6 changes: 4 additions & 2 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
clear
python test/setup.py &&
pytest
# python test/setup.py &&
pytest test/test_centrality.py::TestCentrality::test_degree_centrality4
# pytest test/test_centrality.py
# pytest
# pytest --junitxml "output.xml" #-n 4
60 changes: 45 additions & 15 deletions tests/test/test_centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
class TestCentrality:
feat = util.get_featurizer()
# undirected graphs
graph_types1 = ["Empty", "Line", "Ring", "Hub_Spoke", "Tree"]
graph_types1 = [
"Empty",
"Line",
"Ring",
"Hub_Spoke",
"Tree",
]
# directed graphs
graph_types2 = [
"Line_Directed",
Expand All @@ -21,16 +27,16 @@ class TestCentrality:
"Ring_Weighted",
"Hub_Spoke_Weighted",
"Tree_Weighted",
"CompleteWeighted",
]
# weighted directed graphs
graph_types4 = [
"Line_Directed_Weighted",
"Ring_Directed_Weighted",
"Hub_Spoke_Directed_Weighted",
"Tree_Directed_Weighted",
"CompleteUnweighted",
]
# Complete Graphs
graph_types5 = ["Complete"]

@pytest.mark.parametrize("test_name", graph_types1)
def test_degree_centrality1(self, test_name):
Expand All @@ -57,7 +63,7 @@ def test_degree_centrality1(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -87,7 +93,7 @@ def test_degree_centrality2(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -117,11 +123,35 @@ def test_degree_centrality3(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()

@pytest.mark.parametrize("test_name", graph_types5)
def test_degree_centrality4(self, test_name):
params = {
"v_type_set": ["V8"],
"e_type_set": [test_name],
"reverse_e_type_set": ["reverse_" + test_name],
"in_degree": False,
"out_degree": True,
"print_results": True,
}
with open(
f"data/baseline/graph_algorithms_baselines/centrality/degree_centrality/{test_name}.json"
) as f:
baseline = json.load(f)

result = self.feat.runAlgorithm("tg_degree_cent", params=params)
result = sorted(result[0]["top_scores"], key=lambda x: x["Vertex_ID"])
baseline = sorted(baseline[0]["top_scores"], key=lambda x: x["Vertex_ID"])

for b in baseline:
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] != pytest.approx(b["score"]):
pytest.fail(f'{r["score"]} != {b["score"]}')

@pytest.mark.parametrize("test_name", graph_types3)
def test_weighted_degree_centrality1(self, test_name):
params = {
Expand All @@ -147,7 +177,7 @@ def test_weighted_degree_centrality1(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -177,7 +207,7 @@ def test_weighted_degree_centrality2(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -207,7 +237,7 @@ def test_weighted_degree_centrality3(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -237,7 +267,7 @@ def test_closeness_centrality(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -267,7 +297,7 @@ def test_closeness_centrality2(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -297,7 +327,7 @@ def test_harmonic_centrality(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -327,7 +357,7 @@ def test_harmonic_centrality2(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -358,7 +388,7 @@ def test_article_rank(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()
Expand Down Expand Up @@ -390,7 +420,7 @@ def test_pagerank(self, test_name):
for b in baseline:
found = False
for r in result:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
found = True
if not found:
pytest.fail()

0 comments on commit e95e9f7

Please sign in to comment.