-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
1513 lines (1307 loc) · 65.6 KB
/
Copy pathdata_utils.py
File metadata and controls
1513 lines (1307 loc) · 65.6 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import torch
import numpy as np
from geometry_msgs.msg import Point, PoseStamped
from nav_msgs.msg import Path
from rclpy.duration import Duration
from rclpy.node import Node
from visualization_msgs.msg import Marker, MarkerArray
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from controller_config import *
import pickle
import os
import cv2
import json
from scipy.stats import shapiro
from numpy.random import randn
import glob
from collections import defaultdict
import pandas as pd
import pingouin as pg
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
from scipy.interpolate import griddata
from rectangle import OBB, obb_intersect, closest_distance_two_particles_with_stops
TLX = {
'hst': [[0, 0, 0, 21, 2, 0], [5, 3, 5, 18, 5, 12], [1, 1, 2, 18, 2, 2], [13, 3, 3, 20, 10, 4]],
'cvp': [[0, 0, 2, 21, 2, 0], [3, 3, 3, 18, 4, 8], [1, 1, 1, 17, 6, 3], [12, 3, 6, 20, 8, 6]],
'static': [[0, 0, 0, 21, 0, 0], [2, 3, 3, 18, 5, 9], [2, 2, 5, 15, 6, 4], [14, 4, 4, 20, 6, 4]],
'blind': [[0, 0, 2, 21, 2, 4], [3, 3, 5, 18, 6, 6], [1, 1, 3, 19, 6, 2], [16, 3, 2, 20, 6, 8]]
}
ROSAS = {
'hst': [[4.83, 5.16, 2.67], [5.0, 4.5, 3.83], [1.5, 6.16, 1.83], [1.0, 2.83, 3.5]],
'cvp': [[5.0, 6.83, 2.0], [4.33, 5.33, 4.16], [1.67, 6.67, 2.16], [1.0, 2.0, 3.0]],
'static': [[5.0, 6.33, 2.0], [4.83, 6.16, 4.0], [1.5, 6.0, 2.83], [1.0, 3.66, 3.33]],
'blind': [[4.0, 5.0, 4.67], [4.67, 4.5, 4.33], [3.66, 5.83, 2.33], [1.0, 2.0, 4.83]]
}
class DataProcessor:
def __init__(self, filepath, use_predictions=True, count_turning=False, done_step=0):
self.filepath = filepath
self.reaction_time = 0
self.avg_reaction = []
self.robot_states = None
self.agent_states = None
self.robot_goals = None
self.max_rollouts = None
self.min_rollouts = None
self.turning = None
self.goal_step = 0
self.use_predictions = use_predictions
self.done_step = done_step
self.time = None
self.ADE_AVERAGE = 0.0
self.ADE_SKIP_AVERAGE = 0.0
self.ADE_TOTAL_AVERAGE = 0.0
def write_data(self, data_to_save):
if JSON:
with open(self.filepath, "w+") as json_data:
json.dump(data_to_save, json_data)
json_data.write('\n')
else:
with open(self.filepath, "ab") as pickle_data:
pickle.dump(data_to_save, pickle_data)
def read_pkl(self, cohan=False):
counter = 0
robot_states = []
agent_states = []
robot_goals = []
max_rollouts = []
min_rollouts = []
robot_goals = []
logits = []
predictions = []
robot_predictions = []
turning = []
time_list = [0.0]
last_size = None
counter = 1
raw_cohan_predictions = []
pickle_data = open(self.filepath, "rb")
while True:
try:
dict_to_read = pickle.load(pickle_data)
counter = counter + 1
if self.filepath == '/home/socnav/experiment_data/cohan_test.pkl' and counter < 2960:
continue
agent_states_arr = np.array(dict_to_read['agent_states'])
if agent_states_arr.shape[0] != ACTIVE_AGENTS:
continue
prediction_arr = np.array(dict_to_read['predictions'])
if counter == 0:
last_size = prediction_arr.shape
else:
if prediction_arr.shape != last_size:
last_size = prediction_arr.shape
if self.use_predictions and prediction_arr.shape[3] == ACTIVE_AGENTS and np.array(dict_to_read['logits']).shape[0] == 1:
predictions.append(dict_to_read['predictions'])
if cohan:
if len(dict_to_read['robot_prediction']) > 0:
robot_predictions.append(dict_to_read['robot_prediction'])
else:
robot_predictions.append([[0.0, 0.0]])
else:
robot_predictions.append(dict_to_read['robot_prediction'])
elif not self.use_predictions and prediction_arr.shape[3] == 1:
predictions.append(dict_to_read['predictions'])
else:
continue
if 'time' in dict_to_read:
time_list.append(dict_to_read['time'])
else:
time_list.append(time_list[-1] + 0.02)
robot_states.append(dict_to_read['robot_state'])
logits.append(dict_to_read['logits'])
agent_states.append(dict_to_read['agent_states'])
robot_goals.append(dict_to_read['robot_goal'])
#max_rollouts.append(dict_to_read['max_rollout'])
#min_rollouts.append(dict_to_read['min_rollout'])
if 'turning' in dict_to_read:
turning.append(dict_to_read['turning'])
else:
turning.append(False)
if cohan:
if dict_to_read['raw_cohan_predictions'] is not None:
rp = dict_to_read['raw_cohan_predictions']
rpl = []
for p in range(len(rp)):
pl = []
for s in range(len(rp[p])):
pl.append([rp[p][s].position.x, rp[p][s].position.y])
rpl.append(pl)
raw_cohan_predictions.append(rpl)
else:
raw_cohan_predictions.append([[[0.0, 0.0]], [[0.0, 0.0]]])
except EOFError:
break
self.robot_states = np.array(robot_states)
self.agent_states = np.array(agent_states)
self.robot_goals = np.array(robot_goals)
self.max_rollouts = np.array(max_rollouts)
self.min_rollouts = np.array(min_rollouts)
self.predictions = np.array(predictions)
self.logits = np.array(logits)
self.turning = np.array(turning)
if cohan:
self.robot_prediction = robot_predictions
else:
self.robot_prediction = np.array(robot_predictions)
self.time = np.array(time_list)
self.raw_cohan_predictions = raw_cohan_predictions
def read_json(self, cohan):
json_data = open(self.filepath, "rb")
dict_to_read = json.load(json_data)
robot_states = []
agent_states = []
robot_goals = []
logits = []
predictions = []
robot_predictions = []
turning = []
time_list = []
last_size = None
counter = 1
raw_cohan_predictions = []
for i in range(len(dict_to_read)):
robot_states.append(dict_to_read[i]['robot_state'])
agent_states.append(dict_to_read[i]['agent_states'])
robot_goals.append(dict_to_read[i]['robot_goal'])
logits.append(dict_to_read[i]['logits'])
predictions.append(dict_to_read[i]['predictions'])
robot_predictions.append(dict_to_read[i]['robot_prediction'])
turning.append(dict_to_read[i]['turning'])
time_list.append(dict_to_read[i]['time'])
if cohan:
raw_cohan_predictions.append(dict_to_read[i]['raw_cohan_predictions'])
self.robot_states = np.array(robot_states)
self.agent_states = np.array(agent_states)
self.robot_goals = np.array(robot_goals)
self.predictions = np.array(predictions)
self.logits = np.array(logits)
self.turning = np.array(turning)
if cohan:
self.robot_prediction = robot_predictions
else:
self.robot_prediction = np.array(robot_predictions)
self.time = np.array(time_list)
self.raw_cohan_predictions = raw_cohan_predictions
def moving_average_numpy(self, data, window_size):
if window_size <= 0:
raise ValueError("Window size must be a positive integer.")
if window_size > len(data):
raise ValueError("Window size cannot be greater than the data length.")
weights = np.ones(window_size) / window_size
return np.convolve(data, weights, mode='same')
def save_to_json(self, name, cohan=False):
data = []
for i in range(self.robot_states.shape[0]):
step_data = {}
step_data['robot_state'] = self.robot_states[i].tolist()
step_data['agent_states'] = self.agent_states[i].tolist()
step_data['predictions'] = self.predictions[i].tolist()
step_data['robot_prediction'] = self.robot_prediction[i].tolist()
step_data['robot_goal'] = self.robot_goals[i].tolist()
step_data['turning'] = self.turning[i].tolist()
step_data['time'] = self.time[i].tolist()
step_data['logits'] = self.logits[i].tolist()
if 'cohan' in name:
step_data['raw_cohan_predictions'] = self.raw_cohan_predictions[i]
data.append(step_data)
for i in range(len(data)):
for key in data[i]:
if isinstance(data[i][key], np.ndarray):
print(i, key)
fp = open(name, 'w+')
json.dump(data, fp)
def calculate_metrics(self, agent_labels=0, cohan=False, fr=False):
ADE = 0.0
FDE = 0.0
ADE_skips = 0.0
min_dist = 1e5
avg_min_dist = 0.0
distance = 0.0
rotation = 0.0
acceleration = 0.0
velocity = 0.0
resp0 = 0.0
resp1 = 0.0
goal_step = 0
time = 0.0
agent_times = []
success = False
min_dist_to_travel = 0.0
first_goal = True
robot_goals_visited = 0
agent_distance = np.zeros(ACTIVE_AGENTS)
agent_rotation = np.zeros(ACTIVE_AGENTS)
agent_acceleration = np.zeros(ACTIVE_AGENTS)
agent_velocity = np.zeros(ACTIVE_AGENTS)
agent_min_dist_to_travel = np.zeros(ACTIVE_AGENTS)
agent_agent_intersections = np.zeros(ACTIVE_AGENTS)
agent_robot_intersections = np.zeros(ACTIVE_AGENTS)
responsibility_robot = np.zeros(ACTIVE_AGENTS)
responsibility_other = np.zeros(ACTIVE_AGENTS)
rsteps_robot = np.zeros(ACTIVE_AGENTS)
rsteps_other = np.zeros(ACTIVE_AGENTS)
rsteps_r0 = 0
rsteps_r1 = 0
ADE_a = np.zeros(ACTIVE_AGENTS)
goal_step_agent = np.zeros(ACTIVE_AGENTS)
done_step = self.robot_states.shape[0]
agent_goals = []
agent_goal_indices = []
agent_goals_visited = []
dist_thresh = [0.8, 1.0, 1.2, 1.5]
heading_thresh = [0.25, 0.175, 0.1, 0.05, 0.01]
norm_thresh = [0.5, 0.65, 0.80, 1.0, 1.2]
for a in range(self.agent_states.shape[1]):
bl = False
for d in dist_thresh:
for h in heading_thresh:
for n in norm_thresh:
goals, mdt, new_goals, goal_indices = self.find_agent_goals_overfit(a, done_step, agent_labels, fr=fr, dist=d, head=h, nm=n)
if new_goals >= 40:
bl = True
break
if bl:
break
if bl:
break
agent_goals.append(goals)
agent_goal_indices.append(goal_indices)
agent_min_dist_to_travel[a] = mdt
agent_goals_visited.append(new_goals)
self.agent_goals = np.array(agent_goals)
last_min_distance = 0.0
robot_accels = []
robot_vels = []
human_accels = [[], []]
human_vels = [[], []]
steps_plot = []
steps_ade = []
ades = []
human_pos = [[], []]
human_pis = [[], []]
human_ades = [[], []]
skipped_steps = 0
non_skipped = 0
goals_visited = {}
agents_done = {}
for a in range(ACTIVE_AGENTS):
goals_visited[a] = 0
agents_done[a] = False
num_resp_timesteps = 0
for step in range(2, done_step):
if not np.all(self.robot_goals[step] == self.robot_goals[step-1]):
robot_goals_visited += 1
dt = self.time[step] - self.time[step-1]
dt_prev = self.time[step-1] - self.time[step-2]
time = time + dt
for a in range(ACTIVE_AGENTS):
if not np.all(agent_goals[a][step] == agent_goals[a][step-1]):
goals_visited[a] = goals_visited[a] + 1
if goals_visited[a] >= agent_goals_visited[a]:
if agents_done[a] == False:
agents_done[a] = True
agent_times.append(time)
continue
if self.turning[step]:
continue
# human_edge_filter = self.workspace_edge_filter(step, dist=0.5, fr=fr)
# human_goal_filter = self.goal_crossing_filter(step, [agent_goal_indices[0][step], agent_goal_indices[1][step]], self.robot_goals[step], fr=fr)
# human_half_filter = self.first_half_filter(step, [agent_goal_indices[0][step], agent_goal_indices[1][step]], fr=fr)
human_step_filter = [True, True]
if FILTER_BOTH_AGENTS and ((not human_step_filter[0]) or (not human_step_filter[1])):
continue
goal_step += 1
if RESPONSIBILITY_ONLY_TIMESTEPS:
skipped_steps += 1
resp = [False, False]
for a in range(ACTIVE_AGENTS):
distance_a, extra_rotation_a, acceleration_a, velocity_a, intersect_robot, intersect_other, resp_robot, resp_other = self.collect_agent_metrics(a, step, dt, dt_prev, fr=fr)
if resp_other != 0:
resp[a] = True
if (not resp[0]) and (not resp[1]):
continue
else:
num_resp_timesteps += 1
for a in range(ACTIVE_AGENTS):
if not human_step_filter[a]:
continue
goal_step_agent[a] = goal_step_agent[a] + 1
distance_a, extra_rotation_a, acceleration_a, velocity_a, intersect_robot, intersect_other, resp_robot, resp_other = self.collect_agent_metrics(a, step, dt, dt_prev, fr=fr)
if step - self.goal_step > 5:
self.calculate_reaction_time(a, step)
agent_distance[a] = agent_distance[a] + distance_a
agent_rotation[a] = agent_rotation[a] + extra_rotation_a
agent_acceleration[a] = agent_acceleration[a] + acceleration_a
agent_velocity[a] = agent_velocity[a] + velocity_a
if intersect_other:
agent_agent_intersections[a] += 1
if intersect_robot:
agent_robot_intersections[a] += 1
responsibility_robot[a] += resp_robot
responsibility_other[a] += resp_other
if not resp_robot == 0.0:
rsteps_robot[a] += 1
if not resp_other == 0.0:
rsteps_other[a] += 1
human_accels[a].append(acceleration_a)
human_vels[a].append(velocity_a)
human_pis[a].append(extra_rotation_a)
if self.use_predictions:
ADE_s, FDE_s, ADE_as, pid, vd, skip = self.collect_prediction_metrics(step, dt, cohan)
if skip:
skipped_steps += 1
ADE_skips += ADE_s
else:
non_skipped += 1
ADE += ADE_s
FDE += FDE_s
ades.append(ADE_s)
steps_ade.append(step)
for a in range(ACTIVE_AGENTS):
human_pos[a].append(self.agent_states[step,a,:2])
human_ades[a].append(ADE_as[a])
if human_step_filter[a]:
ADE_a[a] = ADE_a[a] + ADE_as[a]
min_dist_s, distance_s, extra_rotation_s, acceleration_s, velocity_s, rr0, rr1 = self.collect_navigation_metrics(step, dt, dt_prev)
if min_dist_s < min_dist:
min_dist = min_dist_s
avg_min_dist += min_dist_s
distance += distance_s
rotation += extra_rotation_s
acceleration += acceleration_s
velocity += velocity_s
robot_accels.append(acceleration_s)
robot_vels.append(velocity_s)
steps_plot.append(step)
if not rr0 == 0.0:
rsteps_r0 += 1
if not rr1 == 0.0:
rsteps_r1 += 1
resp0 += rr0
resp1 += rr1
if not np.allclose(self.robot_goals[step], self.robot_goals[step-1]):
if first_goal:
min_dist_to_travel += (np.linalg.norm(self.robot_goals[step-1] - self.robot_states[0,:2]) - 1.0)
min_dist_to_travel += (np.linalg.norm(self.robot_goals[step] - self.robot_goals[step-1]) - 1.0)
last_min_distance = np.linalg.norm(self.robot_goals[step] - self.robot_goals[step-1]) - 1.0
ADE = ADE / (non_skipped)
ADE_skips = ADE_skips / skipped_steps
FDE = FDE / (non_skipped)
ADE_a = ADE_a / (goal_step_agent - skipped_steps)
min_dist_to_travel = min_dist_to_travel - last_min_distance
distance = distance + 1e-8
path_efficiency = distance - min_dist_to_travel
path_irregularity = rotation / distance
average_acceleration = acceleration / goal_step
average_velocity = velocity / goal_step
average_min_dist = avg_min_dist / goal_step
agent_distance = agent_distance + 1e-8
api = agent_rotation / agent_distance
ape = agent_distance
aaa = agent_acceleration / goal_step_agent
aav = agent_velocity / goal_step_agent
if rsteps_robot[0] > 0:
responsibility_robot[0] = responsibility_robot[0] / rsteps_robot[0]
else:
responsibility_robot[0] = 0.0
if rsteps_robot[1] > 0:
responsibility_robot[1] = responsibility_robot[1] / rsteps_robot[1]
else:
responsibility_robot[1] = 0.0
if rsteps_other[0] > 0:
responsibility_other[0] = responsibility_other[0] / rsteps_other[0]
else:
responsibility_other[0] = 0.0
if rsteps_other[1] > 0:
responsibility_other[1] = responsibility_other[1] / rsteps_other[1]
else:
responsibility_other[1] = 0.0
if rsteps_r0 > 0:
resp0 = resp0 / rsteps_r0
else:
resp0 = 0.0
if rsteps_r1 > 0:
resp1 = resp1 / rsteps_r1
else:
resp1 = 0.0
resp = (resp0 + resp1) / 2
agent_gps = []
for a in range(ACTIVE_AGENTS):
agent_gps.append(40 / agent_times[a])
robot_gps = robot_goals_visited / time
rob_stats = np.array([ADE, FDE, min_dist, average_min_dist, path_efficiency, min_dist_to_travel, path_irregularity, average_acceleration, average_velocity, time, robot_goals_visited, robot_gps, resp])
human_stats = np.array([ape, aaa, aav, api, agent_gps, agent_gps, ADE_a, agent_robot_intersections, agent_agent_intersections, responsibility_robot, responsibility_other])
per_human_stats = [ades, human_ades, robot_accels, robot_vels, human_accels, human_vels, human_pis, human_pos, steps_plot, steps_ade]
return rob_stats, human_stats, per_human_stats
def workspace_edge_filter(self, step, dist=0.5, fr=False):
if fr:
workspace_edges = ROBOT_BOUNDARY_FR
else:
workspace_edges = ROBOT_BOUNDARY
include = [True, True]
for a in range(ACTIVE_AGENTS):
pos = self.agent_states[step,a,:2]
if (pos[0] - dist < workspace_edges[0]) or (pos[0] + dist > workspace_edges[2]) or (pos[1] - dist < workspace_edges[1]) or (pos[1] + dist > workspace_edges[3]):
include[a] = False
return include
def goal_crossing_filter(self, step, agent_goal_inds, robot_goal, fr=False):
if fr:
goals = AGENT_GOALS_FR
else:
goals = AGENT_GOALS
include = [False, False]
robot_pos = self.robot_states[step,:2]
robot_goal_ind = 0
for g in range(GOALS.shape[0]):
if torch.all(GOALS[g] == robot_goal):
robot_goal_ind = g
for a in range(ACTIVE_AGENTS):
alt = 0
if a == 0:
alt = 1
if agent_goal_inds[a] == 6 or agent_goal_inds[alt] == 6:
include[a] = False
elif robot_goal_ind == agent_goal_inds[a]:
include[a] = True
else:
pos = self.agent_states[step,a,:2]
intersect_rob = segments_intersect(robot_pos, pos, robot_goal, goals[agent_goal_inds[a]])
intersect_agent = segments_intersect(self.agent_states[step,alt, :2], pos, goals[agent_goal_inds[alt]], goals[agent_goal_inds[a]])
if intersect_rob or intersect_agent:
include[a] = True
return include
def first_half_filter(self, step, agent_goal_inds, fr=False):
if fr:
goals = AGENT_GOALS_FR
else:
goals = AGENT_GOALS
include = [False, False]
for a in range(ACTIVE_AGENTS):
if agent_goal_inds[a] == 6:
include[a] = False
else:
pos = self.agent_states[step,a,:2]
dist = np.linalg.norm(pos - goals[agent_goal_inds[a]])
if dist > 1.75:
include[a] = True
return include
def collect_prediction_metrics(self, step, dt, cohan=False):
if self.robot_states.shape[0] - step < PREDICTION_LENGTH * (DT / HZ):
return 0.0, 0.0, 0.0, 0.0, 0.0, True
best_mode_idx = np.argmax(self.logits[step])
#steps batch modes prediction_length max_agent_num xy
ADE = 0.0
FDE = 0.0
ADE_a = np.zeros(ACTIVE_AGENTS)
for s in range(PREDICTION_LENGTH):
pred = self.predictions[step,0,best_mode_idx,s,:,:]
for a in range(ACTIVE_AGENTS):
dist = np.linalg.norm(self.agent_states[step+(s * int(DT / HZ)),a,:2] - pred[a])
ADE += dist
ADE_a[a] = ADE_a[a] + dist
if s == PREDICTION_LENGTH - 1:
FDE += dist
ADE = ADE / ACTIVE_AGENTS / PREDICTION_LENGTH
ADE_a = ADE_a / PREDICTION_LENGTH
FDE = FDE / ACTIVE_AGENTS
self.ADE_TOTAL_AVERAGE += ADE
timesteps = int(DT / HZ)
future_end_timestep = s + int(timesteps * 12)
pred = self.predictions[s,0,best_mode_idx,:,:,:]
gt = self.agent_states[s:future_end_timestep:timesteps,:,:2]
vp = np.zeros(ACTIVE_AGENTS)
vgt = np.zeros(ACTIVE_AGENTS)
dp = np.zeros(ACTIVE_AGENTS)
dgt = np.zeros(ACTIVE_AGENTS)
rp = np.zeros(ACTIVE_AGENTS)
rgt = np.zeros(ACTIVE_AGENTS)
for a in range(ACTIVE_AGENTS):
for t in range(1, pred.shape[0]):
v_pred = (pred[t,a,:2] - pred[t-1,a,:2]) / 0.4
v_gt = (gt[t,a,:2] - gt[t-1,a,:2]) / 0.4
vp[a] += np.linalg.norm(v_pred)
vgt[a] += np.linalg.norm(v_gt)
if np.linalg.norm((pred[-1,a,:2] - pred[t,a,:2])) == 0.0:
vpg = np.zeros(ACTIVE_AGENTS)
else:
vpg = (pred[-1,a,:2] - pred[t,a,:2]) / np.linalg.norm((pred[-1,a,:2] - pred[t,a,:2]))
if np.linalg.norm((gt[-1,a,:2] - gt[t,a,:2])) == 0.0:
gtpg = np.zeros(ACTIVE_AGENTS)
else:
gtpg = (gt[-1,a,:2] - gt[t,a,:2]) / np.linalg.norm((gt[-1,a,:2] - gt[t,a,:2]))
v_pred_r = np.arctan2(v_pred[1], v_pred[0])
v_gt_r = np.arctan2(v_gt[1], v_gt[0])
vpg_r = np.arctan2(vpg[1], vpg[0])
gtpg_r = np.arctan2(gtpg[1], gtpg[0])
rp += np.abs(vpg_r - v_pred_r)
rgt += np.abs(gtpg_r - v_gt_r)
dp += np.linalg.norm(pred[t,a,:2] - pred[t-1,a,:2])
dgt += np.linalg.norm(gt[t,a,:2] - gt[t-1,a,:2])
if dp[0] == 0.0 or dp[1] == 0.0:
ppi = np.zeros(ACTIVE_AGENTS)
else:
ppi = rp / dp
vp = vp / pred.shape[0]
if dgt[0] == 0.0 or dgt[1] == 0.0:
gtpi = np.zeros(ACTIVE_AGENTS)
gtpi = rgt / dgt
vgt = vgt / pred.shape[0]
return ADE, FDE, ADE_a, gtpi - ppi, vgt - vp, False
def calculate_social_zone(self, pos, vel, r=0.3, t=0.77, d=0.5):
l = r / 2 + d + t * np.linalg.norm(vel)
theta = np.arctan2(vel[1], vel[0])
if np.linalg.norm(vel) < 1e6:
center = pos
else:
center = pos + (vel / np.linalg.norm(vel)) * (l / 2)
social_zone = OBB(cx=center[0], cy=center[1], hx=r / 2, hy=l / 2, theta=theta)
return social_zone
def calculate_cp(self, agent_pos, agent_vel, other_pos, other_vel, agent_r=0.3, other_r=0.3, heading=None, other_heading=None):
if USE_MOCAP_HEADING:
agent_vel_h = np.array([np.sin(heading), np.cos(heading)]) * np.linalg.norm(agent_vel)
other_vel_h = np.array([np.sin(other_heading), np.cos(other_heading)]) * np.linalg.norm(other_vel)
d_min, t_min, pa, po = closest_distance_two_particles_with_stops(agent_pos, agent_vel_h, other_pos, other_vel_h, -1.75, 1.75, -1.75, 1.75)
else:
d_min, t_min, pa, po = closest_distance_two_particles_with_stops(agent_pos, agent_vel, other_pos, other_vel, -1.75, 1.75, -1.75, 1.75)
return max(0, 1 - (d_min / (agent_r + other_r)))
def collect_agent_metrics(self, agent, step, dt, dt_prev, fr=False):
if step < 2:
return None, None, None
np.seterr(divide='raise', invalid='raise')
approx_v = (self.agent_states[step,agent,:2] - self.agent_states[step-1,agent,:2]) / dt
approx_vprev = (self.agent_states[step-1,agent,:2] - self.agent_states[step-2,agent,:2]) / dt_prev
try:
optimal_v = (self.agent_goals[agent,step] - self.agent_states[step-1,agent,:2]) / np.linalg.norm(self.agent_goals[agent,step] - self.agent_states[step-1,agent,:2]) * VMAX
except:
return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
if USE_MOCAP_HEADING:
tan1 = self.agent_states[step,agent,2]
else:
tan1 = np.arctan2(approx_v[1], approx_v[0])
tan2 = np.arctan2(optimal_v[1], optimal_v[0])
if np.linalg.norm(self.agent_goals[agent,step] - self.agent_states[step-1,agent,:2]) > 0.2:
extra_rotation = np.abs(tan1 - tan2)
else:
extra_rotation = 0.0
acceleration = np.linalg.norm(approx_v - approx_vprev)
distance = np.linalg.norm(self.agent_states[step,agent,:2] - self.agent_states[step-1,agent,:2])
#Social zone violation
other = 0
if agent == 0:
other = 1
other_v = (self.agent_states[step,other,:2] - self.agent_states[step-1,other,:2]) / dt
robot_v = (self.robot_states[step,:2] - self.robot_states[step-1,:2]) / dt
agent_sz = self.calculate_social_zone(self.agent_states[step,agent,:2], approx_v)
other_sz = self.calculate_social_zone(self.agent_states[step,other,:2], other_v)
robot_sz = self.calculate_social_zone(self.robot_states[step,:2], robot_v)
#pDCE calculation
if fr:
rr = R_PR2
else:
rr = R_STRETCH
cp_ar = self.calculate_cp(self.agent_states[step,agent,:2], approx_v, self.robot_states[step,:2], robot_v, agent_r=R_HUM, other_r=rr, heading=self.agent_states[step,agent,2], other_heading=self.robot_states[step,2])
cp_ar_prev = self.calculate_cp(self.agent_states[step,agent,:2], approx_vprev, self.robot_states[step,:2], robot_v, agent_r=R_HUM, other_r=rr, heading=self.agent_states[step,agent,2], other_heading=self.robot_states[step,2])
cp_ao = self.calculate_cp(self.agent_states[step,agent,:2], approx_v, self.agent_states[step,other,:2], other_v, agent_r=R_HUM, other_r=R_HUM, heading=self.agent_states[step,agent,2], other_heading=self.agent_states[step,other,2])
cp_ao_prev = self.calculate_cp(self.agent_states[step,agent,:2], approx_vprev, self.agent_states[step,other,:2], other_v, agent_r=R_HUM, other_r=R_HUM, heading=self.agent_states[step,agent,2], other_heading=self.agent_states[step,other,2])
return distance, extra_rotation, acceleration, np.linalg.norm(approx_v), obb_intersect(agent_sz, robot_sz), obb_intersect(agent_sz, other_sz), cp_ar - cp_ar_prev, cp_ao - cp_ao_prev
def collect_navigation_metrics(self, step, dt, dt_prev, fr=False):
if step < 2:
return None, None, None, None
dists = np.linalg.norm(self.robot_states[step,:2] - self.agent_states[step,:,:2], axis=1)
min_dist = np.min(dists)
approx_v = (self.robot_states[step,:2] - self.robot_states[step-1,:2]) / dt
approx_vprev = (self.robot_states[step-1,:2] - self.robot_states[step-2,:2]) / dt_prev
optimal_v = (self.robot_goals[step] - self.robot_states[step-1,:2]) / np.linalg.norm(self.robot_goals[step] - self.robot_states[step-1,:2]) * VMAX
tan1 = np.arctan2(approx_v[1], approx_v[0])
tan2 = np.arctan2(optimal_v[1], optimal_v[0])
extra_rotation = np.abs(tan1 - tan2)
acceleration = np.linalg.norm(approx_v - approx_vprev)
distance = np.linalg.norm(self.robot_states[step,:2] - self.robot_states[step-1,:2])
if fr:
rr = R_PR2
else:
rr = R_STRETCH
approx_v0 = (self.agent_states[step,0,:2] - self.agent_states[step-1,0,:2]) / dt
approx_v1 = (self.agent_states[step,1,:2] - self.agent_states[step-1,1,:2]) / dt
robot_v = (self.robot_states[step,:2] - self.robot_states[step-1,:2]) / dt
cp_ar0 = self.calculate_cp(self.robot_states[step,:2], robot_v, self.agent_states[step,0,:2], approx_v0, agent_r=rr, other_r=R_HUM, heading=self.robot_states[step,2], other_heading=self.agent_states[step,0,2])
cp_ar0_prev = self.calculate_cp(self.robot_states[step,:2], approx_vprev, self.agent_states[step,0,:2], approx_v0, agent_r=rr, other_r=R_HUM, heading=self.robot_states[step,2], other_heading=self.agent_states[step,0,2])
cp_ar1 = self.calculate_cp(self.robot_states[step,:2], robot_v, self.agent_states[step,1,:2], approx_v1, agent_r=rr, other_r=R_HUM, heading=self.robot_states[step,2], other_heading=self.agent_states[step,1,2])
cp_ar1_prev = self.calculate_cp(self.robot_states[step,:2], approx_vprev, self.agent_states[step,1,:2], approx_v1, agent_r=rr, other_r=R_HUM, heading=self.robot_states[step,2], other_heading=self.agent_states[step,1,2])
return min_dist, distance, extra_rotation, acceleration, np.linalg.norm(approx_v), cp_ar0 - cp_ar0_prev, cp_ar1 - cp_ar1_prev
def find_agent_goals(self, agent, done_step, threshold=18, dist=0.5):
min_dist_travel = 0.0
goal_steps = []
goals = []
prev_goal_step = 0
for step in range(threshold, done_step-8):
if step == threshold:
goal_steps.append(step)
dists = np.linalg.norm(AGENT_GOALS - self.agent_states[step,agent,:2])
mind = np.argmin(dists)
for i in range(threshold+1):
goals.append(AGENT_GOALS[mind])
continue
for g in AGENT_GOALS:
if np.linalg.norm(self.agent_states[step,agent,:2] - g) < dist and np.linalg.norm(g - goals[goal_steps[-1]]) > 2.0:
if abs(self.agent_states[step+8,agent,2] - self.agent_states[step-8,agent,2]) > 1.0:
min_dist_travel += np.linalg.norm(self.agent_states[step,agent,:2] - self.agent_states[goal_steps[-1],agent,:2])
for i in range(step-prev_goal_step):
goals.append(g)
prev_goal_step = step
goal_steps.append(step)
break
if step == done_step-9:
np.linalg.norm(self.agent_states[step,agent,:2] - self.agent_states[goal_steps[-1],agent,:2])
for i in range(step-prev_goal_step+9):
goals.append(self.agent_states[step,agent,:2])
return np.array(goals), min_dist_travel
def find_agent_goals_overfit(self, agent, done_step, agent_labels, dist=0.8, head=0.25, nm=0.5, fr=False):
if fr:
goal_positions = AGENT_GOALS_FR
else:
goal_positions = AGENT_GOALS
new_goals = 0
min_dist_travel = 0.0
goal_steps = []
goals = []
goal_indices = []
prev_goal_step = 0
if agent == AGENTS[agent_labels][0]:
prev_goal = 5
else:
prev_goal = 0
for step in range(done_step):
if step == 0:
goal_steps.append(step)
if agent == AGENTS[agent_labels][1]:
goals.append(goal_positions[0])
goal_indices.append(0)
else:
goals.append(goal_positions[5])
goal_indices.append(5)
if agent == AGENTS[agent_labels][1]:
if prev_goal == 0 and new_goals == 20:
goal_set = [2]
elif prev_goal == 0:
goal_set = [4]
elif prev_goal == 2:
goal_set = [5]
elif prev_goal == 4:
goal_set = [0]
elif prev_goal == 5:
goal_set = [2]
else:
if prev_goal == 5 and new_goals == 20:
goal_set = [3]
elif prev_goal == 0:
goal_set = [3]
elif prev_goal == 1:
goal_set = [5]
elif prev_goal == 3:
goal_set = [0]
elif prev_goal == 5:
goal_set = [1]
for g in goal_set:
if np.linalg.norm(self.agent_states[step,agent,:2] - goal_positions[g]) < dist and np.linalg.norm(self.agent_states[step, agent,:2] - self.agent_states[step-1, agent, :2]) < nm:
if step + 8 >= done_step:
continue
if abs(self.agent_states[step+8,agent,2] - self.agent_states[step-8,agent,2]) > head and (step - prev_goal_step > 30):
min_dist_travel += np.linalg.norm(self.agent_states[step,agent,:2] - self.agent_states[goal_steps[-1],agent,:2])
new_goals += 1
prev_goal = g
for i in range(step-prev_goal_step):
goals.append(goal_positions[g])
goal_indices.append(g)
prev_goal_step = step
goal_steps.append(step)
break
if step == done_step-9:
np.linalg.norm(self.agent_states[step,agent,:2] - self.agent_states[goal_steps[-1],agent,:2])
for i in range(step-prev_goal_step+9):
goals.append(self.agent_states[step,agent,:2])
goal_indices.append(6)
return np.array(goals), min_dist_travel, new_goals, np.array(goal_indices)
def find_agent_goals_true(self, agent, done_step):
# Extract agent positions and yaw
min_dist_travel = 0
goals = []
for step in range(0, done_step):
agent_position = self.agent_states[step,agent,:2]
agent_yaw = self.agent_states[step,agent,2]
direction_to_goals = AGENT_GOALS - agent_position
distance_to_goals = np.linalg.norm(direction_to_goals,axis=1)
direction_to_goals_normalized = direction_to_goals / (distance_to_goals[:, None] + 1e-6)
agent_direction = np.array([np.cos(agent_yaw), np.sin(agent_yaw)])
alignment_scores = direction_to_goals_normalized @ agent_direction
scores = -distance_to_goals + 10*alignment_scores # Higher is better
best_goal_index = np.argmax(scores).item()
goals.append(AGENT_GOALS[best_goal_index])
if min_dist_travel < np.linalg.norm(AGENT_GOALS[best_goal_index] - agent_position):
min_dist_travel = np.linalg.norm(AGENT_GOALS[best_goal_index] - agent_position)
return np.array(goals),min_dist_travel
def filter_agent_goals(self, agent, agent_goals):
steps_with_goal = 0
last_goal = agent_goals[agent,0]
for i in range(1, agent_goals.shape[1]):
if np.allclose(agent_goals[agent, i], last_goal):
steps_with_goal += 1
else:
last_goal = agent_goals[agent,i]
def create_video(self, output_file='trial.mp4', use_predictions=False, cv_predictions=False, cohan=False, blind=False, static=False, adem=None, adestd=None, data_path=None, data=None):
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlabel("X Coordinate")
ax.set_ylabel("Y Coordinate")
ax.set_title("Min Rollout Trajectories (5 Time Horizons)")
ax.grid(True)
high_ade_x = []
high_ade_y = []
# Function to update the plot for each frame
def update(step):
if adem is not None:
ade, _, _, _ = self.collect_prediction_metrics(step, 0.02)
if ade < adem + adestd:
return
else:
high_ade_x.append(step)
high_ade_y.append(ade)
agent_colors = ['red', 'red']
ax.clear()
best_mode_idx = np.argmax(self.logits[step])
dt = int(DT / HZ)
s = 100
norm = Normalize(vmin=0, vmax=1)
if not blind:
norm = Normalize(vmin=0, vmax=1)
colors = np.linspace(1, 0, self.predictions.shape[3])
cl = np.linspace(1, 0, self.predictions.shape[3]-1)
ax.scatter(self.robot_states[step,0], self.robot_states[step,1], s=s, color=plt.cm.Blues(norm(1)))
ax.scatter(self.robot_states[step-(dt*HISTORY_LENGTH):step:dt,0], self.robot_states[step-(dt*HISTORY_LENGTH):step:dt,1], s=s, facecolors='none', edgecolors=plt.cm.Blues(norm(1)))
ax.scatter(self.robot_states[step+dt:step+(dt*PREDICTION_LENGTH):dt,0], self.robot_states[step+dt:step+(dt*PREDICTION_LENGTH):dt,1], s=s, c=cl, cmap=plt.cm.Purples)
for i in range(ACTIVE_AGENTS):
ax.scatter(self.agent_states[step,i,0], self.agent_states[step,i,1], s=s, color=plt.cm.Reds(norm(1)))
ax.scatter(self.agent_states[step-(dt*HISTORY_LENGTH):step:dt,i,0], self.agent_states[step-(dt*HISTORY_LENGTH):step:dt,i,1], s=s, facecolors='none', edgecolors=plt.cm.Reds(norm(1)))
ax.scatter(self.agent_states[step+dt:step+(dt*PREDICTION_LENGTH):dt,i,0], self.agent_states[step+dt:step+(dt*PREDICTION_LENGTH):dt,i,1], s=s, c=cl, cmap=plt.cm.Purples)
steps = min(step, HISTORY_LENGTH * 4)
if use_predictions and not (static or blind):
colors = np.linspace(1, 0, self.predictions.shape[3])
for a in range(self.predictions.shape[4]):
cb = plt.cm.Blues(norm(a)) # 'Blues' goes from white (low) to blue (high)
cr = plt.cm.Reds(norm(a))
if cv_predictions:
ax.scatter(self.predictions[step, 0, best_mode_idx,:,a,0], self.predictions[step, 0, best_mode_idx,:,a,1], s=s, c=colors, cmap=plt.cm.Reds)
ax.scatter(self.robot_prediction[step, 0, best_mode_idx,:,0,0], self.robot_prediction[step,0,best_mode_idx,:,0,1], s=s, c=colors, cmap=plt.cm.Blues)
else:
if not HIGHEST_PROB_ONLY:
top_logs = np.argpartition(self.logits[step], -5)[-5:]
for m in top_logs:
if m == best_mode_idx:
continue
ax.scatter(self.predictions[step, 0, m, :, a, 0], self.predictions[step, 0, m, :, a, 1], s=s, color=(1.0, 0.65, 0.0, 0.4))
if cohan:
rpa = np.array(self.robot_prediction[step])
ax.scatter(rpa[0,0,:,0,0], rpa[0,0,:,0,1], c=colors, s=s, cmap=plt.cm.Blues)
else:
ax.scatter(self.robot_prediction[step, 0, best_mode_idx,:,0,0], self.robot_prediction[step,0,best_mode_idx,:,0,1], s=s, c=colors, cmap=plt.cm.Blues)
ax.scatter(self.predictions[step, 0, best_mode_idx,:,a,0], self.predictions[step, 0, best_mode_idx,:,a,1], s=s, c=colors, cmap=plt.cm.Reds)
ax.set_xlim(-2.25, 2.25)
ax.set_ylim(-2.25, 2.25)
ax.set_xlabel('x(m)', fontsize=16)
ax.set_ylabel('y(m)', fontsize=16)
ax.set_title('frame ' + str(step) + " " + str(self.agent_states[step,0,:2]))
if adem is not None:
plt.clf()
plt.close()
plt.figure(figsize=(24,12))
plt.scatter(data[-1], data[0], s=2, c='blue')
plt.savefig(data_path + 'ade' + '.png')
plt.clf()
plt.close()
def calculate_reaction_time(self, agent, step, deviation_threshold=0.3):
human_position = self.agent_states[step,agent,:2]
goal = self.agent_goals[agent,step]
start = self.agent_states[0,agent,:2]
if np.linalg.norm(human_position - goal) < 0.2:
start = goal
self.reaction_time = 0
self.goal_step = step
else:
direction_vector = goal - start
direction_vector /= np.linalg.norm(direction_vector)
projection = start + np.dot(human_position - start, direction_vector) * direction_vector
# Calculate the deviation as the perpendicular distance
deviation = np.linalg.norm(human_position - projection)
if deviation > deviation_threshold and self.reaction_time == 0:
self.reaction_time = step - self.goal_step
self.avg_reaction.append(self.reaction_time)
def visualize_trajectories(self):
plt.scatter(self.robot_states[:,0], self.robot_states[:,1], cmap='plasma', s=8)
for i in range(ACTIVE_AGENTS):
plt.scatter(self.agent_states[:,:,0], self.agent_states[:,:,1], cmap='viridis', s=5)
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.show()