-
Notifications
You must be signed in to change notification settings - Fork 0
/
detection_supervised_voronoi.py
149 lines (107 loc) · 3.93 KB
/
detection_supervised_voronoi.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
import statistics
import csv
import math
from matplotlib import pyplot as plt
def findEntropy(xlist,ylist):
plist = [0]*9
tsteps = len(xlist)
for i in range(1,len(xlist)-1):
xold = xlist[i-1]
yold = ylist[i-1]
x = xlist[i]
y = ylist[i]
dx = x-xold
dy = y-yold
if dx == 1:
if dy == 1:
plist[0] += 1
elif dy == -1: plist[1] += 1
else: plist[2] += 1
elif dx == -1:
if dy == 1:
plist[3] += 1
elif dy == -1: plist[4] += 1
else: plist[5] += 1
else:
if dy == 1: plist[6]+=1
elif dy == -1: plist[7] += 1
else: plist[8] += 1
H = 0
for p in plist:
P = p/tsteps
if P == 0: continue
H += P*math.log2(1/P)
return H
def findEntropyBuffer(buffersize,traj,prop = 1):
l = len(traj)//2
s = 0; counter = 0
for i in range(int(l*prop)-buffersize): #iterate over buffers
counter += 1
x = traj[i:2*(i+buffersize):2]
y = traj[i+1:2*(i+1+buffersize):2]
e = findEntropy(x,y)
s += e
return s/counter
filename = 'trajectories.csv'
weighted_thresholds = []
with open('weighted_thresholds.csv') as csvfile: # change threshold file depending on what thresholds we are using
csvread = csv.reader(csvfile)
for line in csvread:
weighted_thresholds.append([float(x) for x in line])
print(f'Weighted Means Thresholds: {weighted_thresholds}')
voronoi_thresholds = []
with open('voronoi_thresholds.csv') as csvfile: # change threshold file depending on what thresholds we are using
csvread = csv.reader(csvfile)
for line in csvread:
voronoi_thresholds.append([float(x) for x in line])
print(f'K means Voronoi Thresholds: {voronoi_thresholds}')
buffersize = 128
prop = .1
timesteps = 3000
randtraj = []
chasetraj = []
foltraj = []
# get following trajectories real quick
with open("trajectories_following.csv") as csvfile:
csvread = csv.reader(csvfile)
for line in csvread:
if len(line) > 1:
foltraj.extend([int(x) for x in line[:2]])
with open(filename) as csvfile:
csvread = csv.reader(csvfile)
for line in csvread: #iterate over trajectories
if len(line)>1:
randtraj.extend([int(x) for x in line[:2]])
chasetraj.extend([int(x) for x in line[2:4]])
# randtraj, chasetraj, foltraj all list of coordinate pairs
n = len(foltraj)//timesteps//2
print(f'{n} trajectories')
error_d = {'r':0,'c':0,'f':0}
with open(f'detection_supervised_voronoi_p{prop}.csv','w',newline='') as csvfile:
csvwrite = csv.writer(csvfile)
for i in range(n): # looping through all trajectories
e = {'r':findEntropyBuffer(buffersize,randtraj[i*timesteps:(i+1)*timesteps],prop),
'c':findEntropyBuffer(buffersize,chasetraj[i*timesteps:(i+1)*timesteps],prop),
'f':findEntropyBuffer(buffersize,foltraj[i*timesteps:(i+1)*timesteps],prop)}
# detection/comparison for voronoi
thresholds = voronoi_thresholds[0]
t1,t2,t3 = thresholds
for key in e: #loop through r,c,f
z = e[key]
if z > t1 and z <= t2:
category = 'chasing'
elif z > t2 and z <= t3:
category = 'following'
elif z > t3:
category = 'random walk'
if key[0] != category[0]: # if classification is wrong
error = f'Boat is {key} instead of {category}'
csvwrite.writerow([error])
error_d[key] += 1
else: # if classify correctly
pass
r_error = error_d['r']/n*100
c_error = error_d['c']/n*100
f_error = error_d['f']/n*100
csvwrite.writerows(error_d.items())
csvwrite.writerows([['\nAccuracy (%): '],[f"randomwalk: {100-r_error}"],[f"chasing: {100-c_error}"],[f"following: {100-f_error}"]])