-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniformity_edge_length.py
75 lines (46 loc) · 1.69 KB
/
uniformity_edge_length.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
##
# EU corresponds to the normalized standard deviation of the edge length.
##
import networkx as nx
import math
def avg_edge_length(G):
'''
Computes the average edge length of the given graph layout <tt>G</tt>
'''
sum_edge_length = 0.0
pos_dict = nx.get_node_attributes(G, 'pos')
for edge in nx.edges(G):
(s,t) = edge
x_source = float(pos_dict[s].split(",")[0])
x_target = float(pos_dict[t].split(",")[0])
y_source = float(pos_dict[s].split(",")[1])
y_target = float(pos_dict[t].split(",")[1])
curr_length = math.sqrt((x_source - x_target)**2 + (y_source - y_target)**2)
sum_edge_length += curr_length
edges_count = len(nx.edges(G))
avg_edge_len = sum_edge_length/edges_count
return avg_edge_len
def uniformity_edge_length(G):
'''
The Edge length uniformity corresponds to the normalized standard deviation of the edge length.
'''
edges = nx.edges(G)
edge_count = len(edges)
avgEdgeLength = avg_edge_length(G)
print(avgEdgeLength)
tot_sum = 0.0
pos_dict = nx.get_node_attributes(G, 'pos')
for edge in edges:
(s,t) = edge
x_source = float(pos_dict[s].split(",")[0])
x_target = float(pos_dict[t].split(",")[0])
y_source = float(pos_dict[s].split(",")[1])
y_target = float(pos_dict[t].split(",")[1])
curr_length = math.sqrt((x_source - x_target)**2 + (y_source - y_target)**2)
num = (curr_length-avgEdgeLength)**2
den = edge_count*(avgEdgeLength**2)
currValue = num/den
tot_sum += currValue
uniformity_e_len = math.sqrt(tot_sum)
result = round(uniformity_e_len, 3)
return result