-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_tree.py
60 lines (48 loc) · 1.64 KB
/
parse_tree.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys,re,json,os.path
infile = open(sys.argv[1])
pattern = re.compile("^#")
g_communities = {}
g_nodes = {}
for line in infile:
if pattern.match(line):
continue
s = line.split(' ')
c = tuple([int(x) for x in s[0].split(':')[:-1]])
flow = float(s[1])
n = int(s[3])
if not c in g_communities:
g_communities[c] = []
g_communities[c].append(n)
if not n in g_nodes:
g_nodes[n] = []
g_nodes[n].append(c)
#print(communities, nodes)
min_depth = min([len(coms) for coms in g_communities.keys()])
def merge_communities(communities, d):
ret = {}
for coms,nodes in communities.items():
key = coms[:d]
if not key in ret:
ret[key] = []
ret[key] += nodes
for k in ret.keys():
ret[k] = set(ret[k]) # make it unique
return ret
def merge_nodes(nodes, d):
ret = {}
for n,coms in nodes.items():
ret[n] = set([c[:d] for c in coms])
return ret
def calculate_avg_num_coms_per_node(nodes):
num_coms = [ len(coms) for n,coms in nodes.items() ]
return sum(num_coms) / len(num_coms)
def calculate_avg_com_size(communities):
com_sizes = [len(ns) for c,ns in communities.items() ]
return sum(com_sizes) / len(com_sizes)
results = {}
for d in range(min_depth):
coms = merge_communities(g_communities, d+1)
nodes = merge_nodes(g_nodes, d+1)
results[d] = {"AvgNumComsPerNode": calculate_avg_num_coms_per_node(nodes), "AvgComSize": calculate_avg_com_size(coms) }
results["lowest"] = {"AvgNumComsPerNode": calculate_avg_num_coms_per_node(g_nodes), "AvgComSize": calculate_avg_com_size(g_communities) }
print(json.dumps(results))