-
Notifications
You must be signed in to change notification settings - Fork 1
/
stress.py
159 lines (100 loc) · 4 KB
/
stress.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import networkx as nx
import math
def euclidean_distance(source, target):
x_source1 = float(source['pos'].split(",")[0])
x_target1 = float(target['pos'].split(",")[0])
y_source1 = float(source['pos'].split(",")[1])
y_target1 = float(target['pos'].split(",")[1])
geomDistance = math.sqrt((x_source1 - x_target1)**2 + (y_source1 - y_target1)**2)
return geomDistance
def scale_graph(G, alpha):
H = G.copy()
for currVStr in nx.nodes(H):
currV = H.nodes[currVStr]
x = float(currV['pos'].split(",")[0])
y = float(currV['pos'].split(",")[1])
x = x * alpha
y = y * alpha
currV['pos'] = str(x)+","+str(y)
return H
def computeScalingFactor(S, all_sp):
num = 0
den = 0
nodes = list(nx.nodes(S))
for i in range(0, len(nodes)):
sourceStr = nodes[i]
source = S.nodes[sourceStr]
for j in range(i+1, len(nodes)):
targetStr = nodes[j]
if(sourceStr == targetStr):
continue
target = S.nodes[targetStr]
graph_theoretic_distance = 0
graph_theoretic_distance = len(all_sp[sourceStr][targetStr])-1
geomDistance = euclidean_distance(source, target)
if (graph_theoretic_distance <= 0):
continue
weight = 1/(graph_theoretic_distance**2)
num = num + (graph_theoretic_distance * geomDistance * weight)
den = den + (weight * (geomDistance**2))
scale = num/den
return scale
def stress(S, G=None, weighted=True, all_sp=None):
'''Computes the strees of the layout <tt>S</tt> if the parameter <tt>G</tt>
is passed it computes the stress of the layout <tt>S</tt>
with respect the graph distances on <tt>G</tt>'''
S = nx.Graph(S)
if G is not None:
G = nx.Graph(G)
# converting weights in float
all_weights_n = nx.get_node_attributes(S, "weight")
for nk in all_weights_n.keys():
all_weights_n[nk] = float(all_weights_n[nk])
nx.set_node_attributes(S, all_weights_n, "weight")
all_weights_e = nx.get_edge_attributes(S, "weight")
for ek in all_weights_e.keys():
all_weights_e[ek] = float(all_weights_e[ek])
nx.set_edge_attributes(S, all_weights_e, "weight")
S_original = S.copy()
alpha = 1
if all_sp is None:
if(G is None):
if(weighted):
all_sp = nx.shortest_path(S, weight="weight")
else:
all_sp = nx.shortest_path(S)
else:
# converting weights in float
all_weights_n = nx.get_node_attributes(G, "weight")
for nk in all_weights_n.keys():
all_weights_n[nk] = float(all_weights_n[nk])
nx.set_node_attributes(G, all_weights_n, "weight")
all_weights_e = nx.get_edge_attributes(G, "weight")
for ek in all_weights_e.keys():
all_weights_e[ek] = float(all_weights_e[ek])
nx.set_edge_attributes(G, all_weights_e, "weight")
all_sp = None
if(weighted):
all_sp = nx.shortest_path(G, weight="weight")
else:
all_sp = nx.shortest_path(G)
alpha = computeScalingFactor(S_original, all_sp)
S = scale_graph(S_original, alpha)
vertices = list(nx.nodes(S))
stress = 0
for i in range(0, len(vertices)):
sourceStr = vertices[i]
source = S.nodes[sourceStr]
for j in range(i+1, len(vertices)):
targetStr = vertices[j]
target = S.nodes[targetStr]
graph_theoretic_distance = len(all_sp[sourceStr][targetStr])-1
eu_dist = euclidean_distance(source, target)
if (graph_theoretic_distance <= 0):
continue
delta_squared = (eu_dist - graph_theoretic_distance)**2
weight = 1/(graph_theoretic_distance**2)
stress = stress + (weight * delta_squared)
scale_graph(S, 1/alpha)
stress = round(stress, 3)
return stress