Skip to content

Commit e95e9f7

Browse files
update degree dataset
1 parent 99fd794 commit e95e9f7

File tree

5 files changed

+73
-78
lines changed

5 files changed

+73
-78
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
[
22
{
3-
"@@top_scores_heap": [
3+
"top_scores": [
44
{
55
"Vertex_ID": "A",
6-
"score": 1.2857142857142856
6+
"score": 1.1428571428571428
77
},
88
{
99
"Vertex_ID": "B",
10-
"score": 1.2857142857142856
10+
"score": 1.1428571428571428
1111
},
1212
{
1313
"Vertex_ID": "C",
14-
"score": 1.2857142857142856
14+
"score": 1.1428571428571428
1515
},
1616
{
1717
"Vertex_ID": "D",
18-
"score": 1.2857142857142856
18+
"score": 1.1428571428571428
1919
},
2020
{
2121
"Vertex_ID": "E",
22-
"score": 1.2857142857142856
22+
"score": 1.1428571428571428
2323
},
2424
{
2525
"Vertex_ID": "F",
26-
"score": 1.2857142857142856
26+
"score": 1.1428571428571428
2727
},
2828
{
2929
"Vertex_ID": "G",
30-
"score": 1.2857142857142856
30+
"score": 1.1428571428571428
3131
},
3232
{
3333
"Vertex_ID": "H",
34-
"score": 1.2857142857142856
34+
"score": 1.1428571428571428
3535
}
3636
]
3737
}

tests/data/baseline/graph_algorithms_baselines/centrality/degree_centrality/CompleteWeighted.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/data/create_baseline.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,53 @@
33

44
import networkx as nx
55
import numpy as np
6+
from matplotlib import pyplot as plt
67

78
baseline_path_root = "baseline/graph_algorithms_baselines"
89

910

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

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

18-
out = [{"@@top_scores_heap": out}]
20+
out = [{"top_scores": out}]
1921
return out
2022

2123

22-
def create_graph(path, edges, weights):
24+
def create_graph(edges, weights):
2325
g = nx.Graph()
24-
# include edge weights if they exist
25-
if weights is not None:
26+
if weights:
2627
g.add_weighted_edges_from(edges)
2728
else:
2829
g.add_edges_from(edges)
2930
return g
3031

3132

3233
def create_degree_baseline():
33-
# input, output
34+
# input, output, weighed
3435
paths = [
3536
(
3637
"unweighted_edges/complete_edges.csv",
37-
f"{baseline_path_root}/centrality/degree_centrality/CompleteUnweighted.json",
38+
f"{baseline_path_root}/centrality/degree_centrality/Complete.json",
3839
False,
3940
),
40-
(
41-
"weighted_edges/complete_edges.csv",
42-
f"{baseline_path_root}/centrality/degree_centrality/CompleteWeighted.json",
43-
True,
44-
),
41+
# (
42+
# "weighted_edges/complete_edges.csv",
43+
# f"{baseline_path_root}/centrality/weighted_degree_centrality/CompleteWeighted.json",
44+
# True,
45+
# ),
4546
]
4647

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

51-
g = create_graph(p, edges, w)
52+
g = create_graph(edges, w)
5253

5354
res = run_degree_baseline(g)
5455
with open(o_path, "w") as f:

tests/run.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
clear
2-
python test/setup.py &&
3-
pytest
2+
# python test/setup.py &&
3+
pytest test/test_centrality.py::TestCentrality::test_degree_centrality4
4+
# pytest test/test_centrality.py
5+
# pytest
46
# pytest --junitxml "output.xml" #-n 4

tests/test/test_centrality.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
class TestCentrality:
88
feat = util.get_featurizer()
99
# undirected graphs
10-
graph_types1 = ["Empty", "Line", "Ring", "Hub_Spoke", "Tree"]
10+
graph_types1 = [
11+
"Empty",
12+
"Line",
13+
"Ring",
14+
"Hub_Spoke",
15+
"Tree",
16+
]
1117
# directed graphs
1218
graph_types2 = [
1319
"Line_Directed",
@@ -21,16 +27,16 @@ class TestCentrality:
2127
"Ring_Weighted",
2228
"Hub_Spoke_Weighted",
2329
"Tree_Weighted",
24-
"CompleteWeighted",
2530
]
2631
# weighted directed graphs
2732
graph_types4 = [
2833
"Line_Directed_Weighted",
2934
"Ring_Directed_Weighted",
3035
"Hub_Spoke_Directed_Weighted",
3136
"Tree_Directed_Weighted",
32-
"CompleteUnweighted",
3337
]
38+
# Complete Graphs
39+
graph_types5 = ["Complete"]
3440

3541
@pytest.mark.parametrize("test_name", graph_types1)
3642
def test_degree_centrality1(self, test_name):
@@ -57,7 +63,7 @@ def test_degree_centrality1(self, test_name):
5763
for b in baseline:
5864
found = False
5965
for r in result:
60-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
66+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
6167
found = True
6268
if not found:
6369
pytest.fail()
@@ -87,7 +93,7 @@ def test_degree_centrality2(self, test_name):
8793
for b in baseline:
8894
found = False
8995
for r in result:
90-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
96+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
9197
found = True
9298
if not found:
9399
pytest.fail()
@@ -117,11 +123,35 @@ def test_degree_centrality3(self, test_name):
117123
for b in baseline:
118124
found = False
119125
for r in result:
120-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
126+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
121127
found = True
122128
if not found:
123129
pytest.fail()
124130

131+
@pytest.mark.parametrize("test_name", graph_types5)
132+
def test_degree_centrality4(self, test_name):
133+
params = {
134+
"v_type_set": ["V8"],
135+
"e_type_set": [test_name],
136+
"reverse_e_type_set": ["reverse_" + test_name],
137+
"in_degree": False,
138+
"out_degree": True,
139+
"print_results": True,
140+
}
141+
with open(
142+
f"data/baseline/graph_algorithms_baselines/centrality/degree_centrality/{test_name}.json"
143+
) as f:
144+
baseline = json.load(f)
145+
146+
result = self.feat.runAlgorithm("tg_degree_cent", params=params)
147+
result = sorted(result[0]["top_scores"], key=lambda x: x["Vertex_ID"])
148+
baseline = sorted(baseline[0]["top_scores"], key=lambda x: x["Vertex_ID"])
149+
150+
for b in baseline:
151+
for r in result:
152+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] != pytest.approx(b["score"]):
153+
pytest.fail(f'{r["score"]} != {b["score"]}')
154+
125155
@pytest.mark.parametrize("test_name", graph_types3)
126156
def test_weighted_degree_centrality1(self, test_name):
127157
params = {
@@ -147,7 +177,7 @@ def test_weighted_degree_centrality1(self, test_name):
147177
for b in baseline:
148178
found = False
149179
for r in result:
150-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
180+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
151181
found = True
152182
if not found:
153183
pytest.fail()
@@ -177,7 +207,7 @@ def test_weighted_degree_centrality2(self, test_name):
177207
for b in baseline:
178208
found = False
179209
for r in result:
180-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
210+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
181211
found = True
182212
if not found:
183213
pytest.fail()
@@ -207,7 +237,7 @@ def test_weighted_degree_centrality3(self, test_name):
207237
for b in baseline:
208238
found = False
209239
for r in result:
210-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
240+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
211241
found = True
212242
if not found:
213243
pytest.fail()
@@ -237,7 +267,7 @@ def test_closeness_centrality(self, test_name):
237267
for b in baseline:
238268
found = False
239269
for r in result:
240-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
270+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
241271
found = True
242272
if not found:
243273
pytest.fail()
@@ -267,7 +297,7 @@ def test_closeness_centrality2(self, test_name):
267297
for b in baseline:
268298
found = False
269299
for r in result:
270-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
300+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
271301
found = True
272302
if not found:
273303
pytest.fail()
@@ -297,7 +327,7 @@ def test_harmonic_centrality(self, test_name):
297327
for b in baseline:
298328
found = False
299329
for r in result:
300-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
330+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
301331
found = True
302332
if not found:
303333
pytest.fail()
@@ -327,7 +357,7 @@ def test_harmonic_centrality2(self, test_name):
327357
for b in baseline:
328358
found = False
329359
for r in result:
330-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
360+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
331361
found = True
332362
if not found:
333363
pytest.fail()
@@ -358,7 +388,7 @@ def test_article_rank(self, test_name):
358388
for b in baseline:
359389
found = False
360390
for r in result:
361-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
391+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
362392
found = True
363393
if not found:
364394
pytest.fail()
@@ -390,7 +420,7 @@ def test_pagerank(self, test_name):
390420
for b in baseline:
391421
found = False
392422
for r in result:
393-
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == r["score"]:
423+
if r["Vertex_ID"] == b["Vertex_ID"] and r["score"] == b["score"]:
394424
found = True
395425
if not found:
396426
pytest.fail()

0 commit comments

Comments
 (0)