-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyseClustering.py
78 lines (58 loc) · 2.23 KB
/
analyseClustering.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
from numpy.core.defchararray import partition
import ijson
from itertools import groupby
from operator import itemgetter
from sklearn.neighbors import KDTree
import numpy as np
import matplotlib.pyplot as plt
# change this
f = open("./fixedEnergyStats.soupstats", "r")
objects = ijson.items(f, 'item')
positionReports = (o for o in objects if o['$type'] == 'SoupV2.Simulation.Statistics.StatReporting.SimulationPositionInfo, SoupV2')
# "$type": "SoupV2.Simulation.Statistics.StatReporting.SimulationPositionInfo, SoupV2",
# "Position": "-2386.593, -523.1633",
# "RotationRadians": 0.8083147,
# "EntityId": 126,
# "EntityTag": "DefSoupling",
# "TimeStamp": 30.033335
neighbours = 5
averageClusterForTimeStamp = []
currentTimeStamp = 0
firstIter = True
def calculateClusering(positions):
positions = np.array(positions)
kdt = KDTree(positions, leaf_size=30, metric='euclidean')
f = kdt.query_radius(positions, r=100, return_distance=True)
total = 0
for distances in f[1]:
#print(distances)
total += np.average(distances)
total /= len(f[1])
#print(total)
return total
# for each, find k nearest neigbours and calculate average distance squared
# The array of positions in this current timestamp
currentPositions = []
timeStamps = []
for report in positionReports:
# split clusterings based on timestamp
if report["TimeStamp"] != currentTimeStamp:
if firstIter == False:
averageClusterForTimeStamp.append(calculateClusering(currentPositions))
# reset everything
currentPositions = []
currentTimeStamp = report["TimeStamp"]
print(f'new timestamp: {currentTimeStamp}')
print(f'{report["EntityTag"]}')
timeStamps.append(currentTimeStamp)
firstIter = False
(xstr, ystr) = report['Position'].split(', ')
currentPositions.append(np.array([float(xstr), float(ystr)]))
# append straggler
averageClusterForTimeStamp.append(calculateClusering(currentPositions))
print(averageClusterForTimeStamp)
plt.title("Average distance of other critters within 100 world units")
plt.xlabel("Time (seconds)")
plt.ylabel("Distance (world units)")
plt.plot(timeStamps, averageClusterForTimeStamp)
plt.show()