-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorbit_stats.py
45 lines (34 loc) · 1.24 KB
/
orbit_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Enables running orca (calculating orbit statistics) on graphs.
Code duplicated from Stanford repo because we want to use the exact same metrics they use for evaluating our models."""
import os
import subprocess as sp
import numpy as np
COUNT_START_STR = 'orbit counts: \n'
def edge_list_reindexed(G):
idx = 0
id2idx = dict()
for u in G.nodes():
id2idx[str(u)] = idx
idx += 1
edges = []
for (u, v) in G.edges():
edges.append((id2idx[str(u)], id2idx[str(v)]))
return edges
def orca(graph):
tmp_fname = 'tmp.txt'
f = open(tmp_fname, 'w')
f.write(str(graph.number_of_nodes()) + ' ' + str(graph.number_of_edges()) + '\n')
for (u, v) in edge_list_reindexed(graph):
f.write(str(u) + ' ' + str(v) + '\n')
f.close()
output = sp.check_output(['./orca/orca', 'node', '4', tmp_fname, 'std'])
output = output.decode('utf8').strip()
idx = output.find(COUNT_START_STR) + len(COUNT_START_STR)
output = output[idx:]
node_orbit_counts = np.array([list(map(int, node_cnts.strip().split(' ')))
for node_cnts in output.strip('\n').split('\n')])
try:
os.remove(tmp_fname)
except OSError:
pass
return node_orbit_counts