-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_state.py
More file actions
181 lines (144 loc) · 6.11 KB
/
Copy pathtest_state.py
File metadata and controls
181 lines (144 loc) · 6.11 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
import unittest
import numpy as np
import torch
from trajdata.data_structures.state import NP_STATE_TYPES, TORCH_STATE_TYPES
AgentStateArray = NP_STATE_TYPES["x,y,z,xd,yd,xdd,ydd,h"]
AgentObsArray = NP_STATE_TYPES["x,y,z,xd,yd,xdd,ydd,s,c"]
AgentStateTensor = TORCH_STATE_TYPES["x,y,z,xd,yd,xdd,ydd,h"]
AgentObsTensor = TORCH_STATE_TYPES["x,y,z,xd,yd,xdd,ydd,s,c"]
class TestStateTensor(unittest.TestCase):
def test_construction(self):
a = AgentStateTensor(torch.rand(2, 8))
b = torch.rand(8).as_subclass(AgentStateTensor)
c = AgentObsTensor(torch.rand(5, 9))
def test_class_propagation(self):
a = AgentStateTensor(torch.rand(2, 8))
self.assertTrue(isinstance(a.to("cpu"), AgentStateTensor))
a = AgentStateTensor(torch.rand(2, 8))
self.assertTrue(isinstance(a.cpu(), AgentStateTensor))
b = AgentStateTensor(torch.rand(2, 8))
self.assertTrue(isinstance(a + b, AgentStateTensor))
b = torch.rand(2, 8)
self.assertTrue(isinstance(a + b, AgentStateTensor))
a += 1
self.assertTrue(isinstance(a, AgentStateTensor))
def test_property_access(self):
a = AgentStateTensor(torch.rand(2, 8))
position = a[..., :3]
velocity = a[..., 3:5]
acc = a[..., 5:7]
h = a[..., 7:]
self.assertTrue(torch.allclose(a.position3d, position))
self.assertTrue(torch.allclose(a.velocity, velocity))
self.assertTrue(torch.allclose(a.acceleration, acc))
self.assertTrue(torch.allclose(a.heading, h))
def test_heading_conversion(self):
a = AgentStateTensor(torch.rand(2, 8))
h = a[..., 7:]
hv = a.heading_vector
self.assertTrue(torch.allclose(torch.atan2(hv[..., 1], hv[..., 0])[:, None], h))
def test_long_lat_velocity(self):
a = AgentStateTensor(torch.rand(8))
velocity = a[3:5]
h = a[7]
lonlat_v = a.as_format("v_lon,v_lat")
lonlat_v_correct = (
torch.tensor([[np.cos(h), np.sin(h)], [-np.sin(h), np.cos(h)]])[None, ...]
@ velocity[..., None]
)[..., 0]
self.assertTrue(torch.allclose(lonlat_v, lonlat_v_correct))
b = a.as_format("x,y,xd,yd,s,c")
s = b[-2]
c = b[-1]
lonlat_v = b.as_format("v_lon,v_lat")
lonlat_v_correct = (
torch.tensor([[c, s], [-s, c]])[None, ...] @ velocity[..., None]
)[..., 0]
self.assertTrue(torch.allclose(lonlat_v, lonlat_v_correct))
def test_long_lat_conversion(self):
a = AgentStateTensor(torch.rand(2, 8))
b = a.as_format("xd,yd,h")
c = b.as_format("v_lon,v_lat,h")
d = c.as_format("xd,yd,h")
self.assertTrue(torch.allclose(b, d))
def test_as_format(self):
a = AgentStateTensor(torch.rand(2, 8))
b = a.as_format("x,y,z,xd,yd,xdd,ydd,s,c")
self.assertTrue(isinstance(b, AgentObsTensor))
self.assertTrue(torch.allclose(a, b.as_format(a._format)))
def test_as_tensor(self):
a = AgentStateTensor(torch.rand(2, 8))
b = a.as_tensor()
self.assertTrue(isinstance(b, torch.Tensor))
self.assertFalse(isinstance(b, AgentStateTensor))
def test_tensor_ops(self):
a = AgentStateTensor(torch.rand(2, 8))
b = a[0] + a[1]
c = torch.mean(b)
self.assertFalse(isinstance(c, AgentStateTensor))
self.assertTrue(isinstance(c, torch.Tensor))
class TestStateArray(unittest.TestCase):
def test_construction(self):
a = np.random.rand(2, 8).view(AgentStateArray)
c = np.random.rand(5, 9).view(AgentObsArray)
def test_property_access(self):
a = np.random.rand(2, 8).view(AgentStateArray)
position = a[..., :3]
velocity = a[..., 3:5]
acc = a[..., 5:7]
h = a[..., 7:]
self.assertTrue(np.allclose(a.position3d, position))
self.assertTrue(np.allclose(a.velocity, velocity))
self.assertTrue(np.allclose(a.acceleration, acc))
self.assertTrue(np.allclose(a.heading, h))
def test_property_setting(self):
a = np.random.rand(2, 8).view(AgentStateArray)
a.heading = 0.0
self.assertTrue(np.allclose(a[..., -1], np.zeros([2, 1])))
def test_heading_conversion(self):
a = np.random.rand(2, 8).view(AgentStateArray)
h = a[..., 7:]
hv = a.heading_vector
self.assertTrue(np.allclose(np.arctan2(hv[..., 1], hv[..., 0])[:, None], h))
def test_long_lat_velocity(self):
a = np.random.rand(8).view(AgentStateArray)
velocity = a[3:5]
h = a[7]
lonlat_v = a.as_format("v_lon,v_lat")
lonlat_v_correct = (
np.array([[np.cos(h), np.sin(h)], [-np.sin(h), np.cos(h)]])[None, ...]
@ velocity[..., None]
)[..., 0]
self.assertTrue(np.allclose(lonlat_v, lonlat_v_correct))
b = a.as_format("x,y,xd,yd,s,c")
s = b[-2]
c = b[-1]
lonlat_v = b.as_format("v_lon,v_lat")
lonlat_v_correct = (
np.array([[c, s], [-s, c]])[None, ...] @ velocity[..., None]
)[..., 0]
self.assertTrue(np.allclose(lonlat_v, lonlat_v_correct))
def test_long_lat_conversion(self):
a = np.random.rand(2, 8).view(AgentStateArray)
b = a.as_format("xd,yd,h")
c = b.as_format("v_lon,v_lat,h")
d = c.as_format("xd,yd,h")
self.assertTrue(np.allclose(b, d))
def test_as_format(self):
a = np.random.rand(2, 8).view(AgentStateArray)
b = a.as_format("x,y,z,xd,yd,xdd,ydd,s,c")
self.assertTrue(isinstance(b, AgentObsArray))
self.assertTrue(np.allclose(a, b.as_format(a._format)))
def test_as_ndarray(self):
a: AgentStateArray = np.random.rand(2, 8).view(AgentStateArray)
b = a.as_ndarray()
self.assertTrue(isinstance(b, np.ndarray))
self.assertFalse(isinstance(b, AgentStateArray))
def test_tensor_ops(self):
a = np.random.rand(2, 8).view(AgentStateArray)
b = a[0] + a[1]
c = np.mean(b)
self.assertFalse(isinstance(c, AgentStateArray))
self.assertTrue(isinstance(c, float))
if __name__ == "__main__":
unittest.main()