forked from gtagency/graph-reduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckDistributedEdges.py
59 lines (51 loc) · 1.85 KB
/
checkDistributedEdges.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 json
import math
import statistics
#the smaller the result, the more evenly distributed the angles of the edges on the nodes are.
#expects graph edges to be a list of tuples, and vertices to be a JSON obj containing a dict of coords
def checkDistributedEdges(tupleList, vertices):
vertices = json.loads(vertices)
totalVariance = 0.0
for key, coords in vertices.items():
edges = []
angles = []
angleDist = []
#find connecting nodes
for a, b in tupleList:
if (a == key):
edges.append(vertices[b])
elif (b == key):
edges.append(vertices[a])
#find angles from current node to connecting nodes
if (len(edges) > 1):
for node in edges:
x = node[0] - coords[0]
y = node[1] - coords[1]
angle = math.atan2(y, x)
angles.append(angle)
#find angles between edges
angles = sorted(angles)
i = len(angles) - 1
totalAng = 0
while i > 0:
angleDist.append(angles[i] - angles[i - 1])
i = i - 1
angleDist.append((2 * math.pi) - (angles[len(angles) - 1] - angles[0]))
#calc variance
#num = len(angleDist)
#mean = (2 * math.pi) / num
#variance = 0
#for angle in angleDist:
# variance += ((angle - mean) ** 2) / num
totalVariance += statistics.variance(angleDist)
return totalVariance
#test code
#testVertices = {
# "A": [15, 15],
# "B": [20, 10],
# "C": [10, 10],
# "D": [15, 25]
#}
#testVertices = json.dumps(testVertices)
#testTuples = [("A", "C"), ("B", "C"), ("D", "A"), ("B", "D")]
#print(checkDistributedEdges(testTuples, testVertices))