-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo.py
550 lines (447 loc) · 19.2 KB
/
demo.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from Tkinter import *
import operator
from collections import OrderedDict
from clustering import Clustering
from drawing import Map
from peewee_models import Cart, Aoi
from track import Track
from trajectory import Trajectory
###########################
# 1) Global variables #
###########################
MAX_CLUSTERS = 8 # MAX_CLUSTERS <= 10
MAX_CLUSTERS_USER_DEFINED = False
colors = {"purple": "#A020F0",
"orange": "#FF8C00",
"red": "#FF0000",
"yellow": "#FFFF00",
"green": "#228B22",
"lime": "#7FFF00",
"cyan": "#00FFFF",
"blue": "#4169E1",
"pink": "#FF69B4",
"gray": "#2F4F4F"}
COLOR_BLACK = "#000000"
# Origin area
origin = Aoi(x_min=0.1, x_max=14., y_min=28.5, y_max=35.18)
# Extended origin area
extended_origin = Aoi(x_min=0.1, x_max=17., y_min=26.5, y_max=35.18)
# Control areas
controls = {
"c1": Aoi(x_min=41.18, x_max=44.23, y_min=19.53, y_max=21.49),
# "c2": Aoi(x_min=31.13, x_max=34.28, y_min=19.53, y_max=21.49),
"c3": Aoi(x_min=31.13, x_max=34.24, y_min=9.55, y_max=12.43),
# "c4": Aoi(x_min=41.26, x_max=44.22, y_min=9.55, y_max=12.43),
# "c5": Aoi(x_min=0.74, x_max=4.4, y_min=18.74, y_max=22.00),
# "c6": Aoi(x_min=8.1, x_max=11.15, y_min=18.63, y_max=22.00),
"c7": Aoi(x_min=8.1, x_max=11.88, y_min=9.08, y_max=12.03),
# "c8": Aoi(x_min=19.08, x_max=22.12, y_min=9.08, y_max=11.35)
}
# List of all trajectories
trajectories = []
# Index of the current trajectory to draw
trajectory_index = 0
# List of clusters
clusters = Clustering()
# Index of the current cluster to draw
cluster_index = 0
# Number of Trajectories per Cluster
ntc = []
# List of tracks
tracks = []
# Index of the current track to draw
track_index = 0
# Flag to prevent tracks from being computed again
tracks_computed = False
# List of macro-clusters
macro_clusters = {}
# Index of the current macro-clusters to draw
macro_index = 0
##########################
# 2) Drawing the map #
##########################
# Inizializza la mappa
tkmaster = Tk(className="Demo")
map = Map(tkmaster, scale=18, width=1200, height=640, bg="#FFFFFF")
map.pack(expand=True, fill="both", side="right")
# Disegna la mappa
map.draw_init(Aoi.select(), origin, controls)
##########################
# 3) Selecting carts #
##########################
# Preleva la lista dei singoli carrelli (len(carts_id) = 16)
carts = Cart.select().where(Cart.tag_id == "0x00205EFE0E93") \
.group_by(Cart.tag_id)
########################################################################################################################
# LEGEND
########################################################################################################################
def show_legend():
map.clear_log()
map.log(txt=">> Legend (keys)\n\n")
map.log(txt="1: Compute trajectories\n")
map.log(txt="2: Draw single trajectory\n")
map.log(txt="3: Draw all trajectories\n")
map.log(txt="4: Clustering (agglomerative)\n")
map.log(txt="5: Clustering (spectral)\n")
map.log(txt="6: Draw single cluster\n")
map.log(txt="7: Draw all clusters\n")
map.log(txt="8: Compute tracks\n")
map.log(txt="9: Draw single track\n")
map.log(txt="0: Draw macro cluster\n")
map.log(txt="L: Show legend\n")
show_legend()
########################################################################################################################
########################################################################################################################
# FUNCTIONS #
########################################################################################################################
def compute_trajectories(event):
map.clear_log()
map.log(txt=">> 1: Compute trajectories\n\n")
global trajectory_index, ntc
trajectory_index = 0
ntc = []
progress_carts = 0
trajectories[:] = []
# For each cart:
for cart in carts:
# ProgressBar
map.log(txt="Progress:\t" + '{0:.3g}'.format(100 * (float(progress_carts) / float(carts.count()))) + "%\n")
map.update()
# Get all the cart's instances ordered by time,
# deleting the one out-of-bounds:
instances = list(
Cart.select()
.order_by(Cart.time_stamp.desc())
.where(Cart.tag_id == cart.tag_id)
.where(Cart.x > 0.).where(Cart.y > 0.)
)
# Divide all the instances in trajectories which are origin2origin or origin2control or
# control2control, and build the array of trajectories.
# NB: if the last run does not reach a control or the origin, it is not taken.
# Minimum length di of an origin2origin trajectory
complete_min_run_length = 25
# Minimum length di of an origin2control trajectory
middle_min_run_length = 15
# Maximum length di of a trajectory
max_run_length = 350
# Index of the beginning run instance
begin = 0
# Index of the current run instance
i = 0
# Flag: run has started
has_run_started = False
# For each instance:
for instance in instances:
# If the started run has not reached the origin or a control
# or if the non-started run is inside the origin or a control:
if (not instance.inside(origin) and not instance.multinside(controls) and has_run_started) \
or (instance.inside(origin) and not has_run_started) \
or (instance.multinside(controls) and not has_run_started):
pass
else:
# If it needs to start the run:
if not instance.inside(origin) and not instance.multinside(controls) and not has_run_started:
# Start the run
has_run_started = True
# Save the begin index (exception check: run starts outside the origin/control)
if i > 0:
begin = i - 1
else:
begin = 0
# If it needs to stop the run:
else:
# Stops the run
has_run_started = False
# Save the run interval
run = instances[begin:i]
# If the run is an endingrun (inside the origin):
if instance.inside(origin):
trajectory = Trajectory(run)
# If the trajcetory is between complete_min_run_length and max_run_length
if (complete_min_run_length < trajectory.prefixSum[len(trajectory.prefixSum) - 1] < \
max_run_length) and \
((str(instances[begin].time_stamp - instances[i].time_stamp)) < str(3)):
# Pulisce la traiettoria
trajectory.clean()
# Filtra la traiettoria attraverso un filtro di Kalman
trajectory.filter()
# Aggiunge la traiettoria alla lista
trajectories.append(trajectory)
# If the run is a middlerun (inside a control):
else:
trajectory = Trajectory(run)
# If the trajcetory is between middle_min_run_length and max_run_length
if (middle_min_run_length < trajectory.prefixSum[len(trajectory.prefixSum) - 1] < \
max_run_length) and \
((str(instances[begin].time_stamp - instances[i].time_stamp)) < str(3)):
# Pulisce la traiettoria
trajectory.clean()
# Filtra la traiettoria attraverso un filtro di Kalman
trajectory.filter()
# Aggiunge la traiettoria alla lista
trajectories.append(trajectory)
i += 1
progress_carts += 1
map.log(txt="Progress: 100%\n\n")
map.log(txt="Number of trajectories:\t" + str(len(trajectories)) + "\n")
map.log(txt="\nComputing tracks..\n")
trajectory_index = len(trajectories) - 1
# Set the track attribute to each trajectory to find the complete macro-trajectories
n_track = -1
flag = False
for trajectory in trajectories:
# Descendent order
stop = trajectory.run[0].inside(extended_origin)
start = trajectory.run[len(trajectory.run) - 1].inside(extended_origin)
if start:
trajectory.track = n_track
n_track += 1
flag = True
else:
if stop:
if flag:
flag = False
else:
n_track += 1
trajectory.track = n_track
map.log(txt="Tracks computed!\n")
def draw_single_trajectory(event):
global trajectory_index
map.clear_log()
map.log(txt=">> 2: Draw single trajectory\n\n")
map.draw_init(Aoi.select(), origin, controls)
if len(trajectories) > 0:
map.draw_trajectory(trajectories[trajectory_index], color="red")
map.log(txt="Cart id: " + str(trajectories[trajectory_index].run[0].tag_id) + "\n")
map.log(txt=
"Start:\t"
+ str(trajectories[trajectory_index].run[len(trajectories[trajectory_index].run) - 1].time_stamp)
+ "\n"
)
map.log(txt="End:\t" + str(trajectories[trajectory_index].run[0].time_stamp) + "\n")
if trajectory_index >= 0:
trajectory_index -= 1
else:
trajectory_index = len(trajectories) - 1
else:
map.log(txt="Error: No trajectories computed.\n")
def draw_all_trajectories(event):
map.clear_log()
map.log(txt=">> 3: Draw all trajectories\n\n")
map.draw_init(Aoi.select(), origin, controls)
if len(trajectories) == 0:
map.log(txt="Error: No trajectories computed.\n")
else:
for trajectory in trajectories:
map.draw_trajectory(trajectory, color="red")
map.log(txt="N. of trajectories:\t" + str(len(trajectories)) + "\n")
def cluster_trajectories_agglomerative(event):
map.clear_log()
map.log(txt=">> 4: Clustering (agglomerative)\n\n")
map.update()
global cluster_index, ntc
cluster_index = 0
if len(trajectories) == 0:
map.log(txt="Error: No trajectories computed.\n")
else:
map.log(txt="Clustering..\n\n")
map.update()
# Clustering
clusters.clusterAgglomerative(trajectories, MAX_CLUSTERS)
map.draw_init(Aoi.select(), origin, controls)
# Computes the number of trajectories per cluster
ntc = [0] * MAX_CLUSTERS
for t in trajectories:
ntc[t.getClusterIdx()] += 1
map.log(txt="Clusters:\n")
for i in range(MAX_CLUSTERS):
if ntc[i] > 0:
perc = float(ntc[i]) / float(len(trajectories)) * 100
map.log(txt="- " + '{0:.2f}'.format(perc) + "% " + colors.keys()[i] + " (" + str(ntc[i]) + ")\n")
def cluster_trajectories_spectral(event):
map.clear_log()
map.log(txt=">> 5: Clustering (spectral)\n\n")
map.update()
global cluster_index, ntc, g
cluster_index = 0
if len(trajectories) == 0:
map.log(txt="Error: No trajectories computed.\n")
else:
map.log(txt="Clustering..\n\n")
map.update()
# Clustering
if MAX_CLUSTERS_USER_DEFINED:
clusters.clusterSpectral(trajectories, MAX_CLUSTERS)
else:
g = clusters.clusterSpectral(trajectories)
map.draw_init(Aoi.select(), origin, controls)
# Computes the number of trajectories per cluster
ntc = [0] * g
for t in trajectories:
ntc[t.getClusterIdx()] += 1
map.log(txt="Clusters:\n")
if MAX_CLUSTERS_USER_DEFINED:
for i in range(MAX_CLUSTERS):
if ntc[i] > 0:
perc = float(ntc[i]) / float(len(trajectories)) * 100
map.log(txt="- " + '{0:.2f}'.format(perc) + "% " + colors.keys()[i] + " (" + str(ntc[i]) + ")\n")
else:
for i in range(g):
if ntc[i] > 0:
perc = float(ntc[i]) / float(len(trajectories)) * 100
map.log(txt="- " + '{0:.2f}'.format(perc) + "% " + colors.keys()[i] + " (" + str(ntc[i]) + ")\n")
def draw_single_cluster(event):
global cluster_index, ntc
map.clear_log()
map.log(txt='>> 6: Draw single cluster\n\n')
if len(trajectories) == 0:
map.log(txt="Error: No trajectories computed.\n")
if len(ntc) == 0:
map.log(txt="Error: No cluster computed.\n")
else:
if len(ntc) == 0:
map.log(txt="Error: No cluster computed.\n")
else:
map.draw_init(Aoi.select(), origin, controls)
for trajectory in trajectories:
if trajectory.getClusterIdx() == cluster_index:
map.draw_trajectory(trajectory, color=colors.values()[cluster_index])
perc = float(ntc[cluster_index]) / float(len(trajectories)) * 100
map.log(txt=
"- " + '{0:.2f}'.format(perc) + "% " + colors.keys()[cluster_index]
+ " (" + str(ntc[cluster_index]) + ")\n"
)
if cluster_index < len(ntc) - 1:
cluster_index += 1
else:
cluster_index = 0
def draw_all_clusters(event):
map.clear_log()
map.log(txt='>> 7: Draw all clusters\n\n')
if len(trajectories) == 0:
map.log(txt="Error: No trajectories computed.\n")
if len(ntc) == 0:
map.log(txt="Error: No cluster computed.\n")
else:
if len(ntc) == 0:
map.log(txt="Error: No cluster computed.\n")
else:
map.draw_init(Aoi.select(), origin, controls)
for trajectory in trajectories:
map.draw_trajectory(trajectory, colors.values()[trajectory.getClusterIdx()])
for i in range(len(ntc)):
if ntc[i] > 0:
perc = float(ntc[i]) / float(len(trajectories)) * 100
map.log(txt=
"- " + '{0:.2f}'.format(perc) + "% "
+ colors.keys()[i] + " (" + str(ntc[i]) + ")\n"
)
def compute_tracks(event):
map.clear_log()
map.log(txt='>> 8: Compute tracks\n\n')
if len(trajectories) == 0 or len(ntc) == 0:
map.log(txt="Error: No trajectories or cluster computed.\n")
else:
global tracks_computed
if not tracks_computed:
global track_index, macro_index
track_index = 0
macro_index = 0
print(macro_clusters)
map.draw_init(Aoi.select(), origin, controls)
for traj in trajectories:
if len(tracks) == 0:
tracks.append(Track())
tracks[0].add_trajectory(traj)
else:
if tracks[len(tracks) - 1].id == traj.track:
tracks[len(tracks) - 1].add_trajectory(traj)
else:
tracks.append(Track())
tracks[len(tracks) - 1].add_trajectory(traj)
tracks_computed = True
map.log(txt="Tracks computed.\n")
# Macro cluster
for track in tracks:
key = str(track.cluster_code)
macro_clusters[key] = macro_clusters.get(key, 0) + 1
map.log(txt="Macro clusters computed.\n\n")
else:
map.log(txt="Tracks already computed. \n\n")
ord_macroclusters = OrderedDict(sorted(macro_clusters.items(), key=operator.itemgetter(1), reverse=True))
map.log(txt="Macro clusters:\t\n")
for macrocluster_code in ord_macroclusters:
color_keys = []
cluster_codes = list(eval(macrocluster_code))
for cluster_code in sorted(cluster_codes, reverse=True):
color_keys.append(colors.keys()[cluster_code])
map.log(txt=str(color_keys) + " " + str(ord_macroclusters[macrocluster_code]) + "\n")
def draw_single_track(event):
global track_index
map.clear_log()
map.log(txt='>> 9: Draw single track\n\n')
if len(trajectories) == 0 or len(ntc) == 0 or len(tracks) == 0:
map.log(txt="Error: No trajectory, cluster or track computed.\n")
else:
# Canvas refresh
map.draw_init(Aoi.select(), origin, controls)
for i in tracks[track_index].trajectories:
map.draw_trajectory(i, colors.values()[i.getClusterIdx()])
map.log(txt="Cart id: " + tracks[track_index].trajectories[0].run[0].tag_id + "\n")
map.log(txt=
"Start:\t"
+ str(tracks[track_index].trajectories[len(tracks[track_index].trajectories) - 1]
.run[len(
tracks[track_index].trajectories[len(tracks[track_index].trajectories) - 1].run) - 1]
.time_stamp) + "\n"
)
map.log(txt=
"End:\t"
+ str(tracks[track_index].trajectories[0].run[0].time_stamp) + "\n"
)
if track_index < len(tracks) - 1:
track_index += 1
else:
track_index = 0
def draw_macro_cluster(event):
global macro_index
map.clear_log()
map.log(txt='>> 0: Draw macro-clusters\n\n')
if len(trajectories) == 0 or len(ntc) == 0 or len(tracks) == 0:
map.log(txt="Error: No trajectory, cluster or track computed.\n")
else:
map.draw_init(Aoi.select(), origin, controls)
ord_macro_clusters = OrderedDict(
sorted(macro_clusters.items(), key=operator.itemgetter(1), reverse=True))
for track in tracks:
if str(track.cluster_code) == ord_macro_clusters.keys()[macro_index]:
for traj in track.trajectories:
map.draw_trajectory(traj, color=colors.values()[traj.getClusterIdx()])
map.log(txt="N. of Tracks in each Macro Cluster:\t" + str(ord_macro_clusters.values()[macro_index]) \
+ "\n")
if macro_index < len(ord_macro_clusters) - 1:
macro_index += 1
else:
macro_index = 0
def legend(event):
show_legend()
pass
# Command line parsing
if (len(sys.argv) == 2):
MAX_CLUSTERS = int(sys.argv[1])
MAX_CLUSTERS_USER_DEFINED = True
########################################################################################################################
tkmaster.bind("1", compute_trajectories)
tkmaster.bind("2", draw_single_trajectory)
tkmaster.bind("3", draw_all_trajectories)
tkmaster.bind("4", cluster_trajectories_agglomerative)
tkmaster.bind("5", cluster_trajectories_spectral)
tkmaster.bind("6", draw_single_cluster)
tkmaster.bind("7", draw_all_clusters)
tkmaster.bind("8", compute_tracks)
tkmaster.bind("9", draw_single_track)
tkmaster.bind("0", draw_macro_cluster)
tkmaster.bind("l", legend)
mainloop()