-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.py
49 lines (38 loc) · 1.47 KB
/
replay.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
import csv
import pandas as pd
class Replay:
history = []
prev_position = 0.0
def __init__(self):
self.history.append(["X", "Y", "Z", "throttle", "brake", "steer", "speed", "position", "lap_time"])
def save(self, file) -> None:
with open(file, 'w', newline='') as f:
writer = csv.writer(f)
for r in self.history:
writer.writerow(r)
print("Saved replay to", file)
def update(self, vehicle) -> None:
if vehicle.position != self.prev_position:
self.history.append([vehicle.location[0], vehicle.location[1], vehicle.location[2],
round(vehicle.throttle, 3), round(vehicle.brake, 3),
round(vehicle.steer, 3), vehicle.speed, vehicle.position, vehicle.lap_time])
self.prev_position = vehicle.position
def load(self, file, driver="default"):
df = pd.read_csv(file)
df = df.reset_index()
lap_counter = -1
lap = []
label = []
prev_position = 0.0
for index, row in df.iterrows():
if row['position'] < prev_position:
lap_counter += 1
prev_position = row['position']
lap.append(lap_counter)
label.append(driver)
if row["position"] <= 0.001:
df.loc[index, "lap_time"] = 0
df['lap'] = lap
df["driver"] = label
print("Loaded replay", file)
return df