Skip to content

Commit abbfabc

Browse files
committed
fix plot_json.py
1 parent a6cbe77 commit abbfabc

1 file changed

Lines changed: 56 additions & 31 deletions

File tree

tuning logs/plot_json.py

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,68 @@
11
import json
22
import matplotlib.pyplot as plt
3-
import matplotlib.dates as mdates
43
from datetime import datetime
4+
from bisect import bisect_left
55

6-
# Read the NDJSON data
7-
data = []
8-
with open('test1/behavior.json', 'r') as f:
6+
# Read the file and separate pose and speed entries
7+
pose_data = []
8+
speed_data = []
9+
10+
with open("test1/behavior.json", "r") as f:
911
for line in f:
10-
if line.strip():
11-
data.append(json.loads(line))
12+
if not line.strip():
13+
continue
14+
entry = json.loads(line)
15+
if "vehicle" in entry:
16+
pose = entry["vehicle"]["data"]["pose"]
17+
timestamp = entry["time"]
18+
pose_data.append({
19+
"time": timestamp,
20+
"x": pose["x"],
21+
"y": pose["y"]
22+
})
23+
elif "vehicle_interface_reading" in entry:
24+
timestamp = entry["time"]
25+
speed = entry["vehicle_interface_reading"]["data"].get("speed", 0.0)
26+
speed_data.append({
27+
"time": timestamp,
28+
"speed": speed
29+
})
30+
31+
# Sort both lists by time
32+
pose_data.sort(key=lambda x: x["time"])
33+
speed_data.sort(key=lambda x: x["time"])
34+
speed_times = [s["time"] for s in speed_data]
1235

13-
# Extract fields
14-
x_vals = []
15-
y_vals = []
16-
speeds = []
17-
times = []
36+
# Match each pose with the nearest speed
37+
matched_x = []
38+
matched_y = []
39+
matched_speeds = []
1840

19-
for entry in data:
20-
# Skip if position is missing
21-
if 'position' not in entry or 'vehicle_interface_reading' not in entry:
22-
continue
41+
for pose in pose_data:
42+
t = pose["time"]
43+
idx = bisect_left(speed_times, t)
2344

24-
pos = entry['position']
25-
x_vals.append(pos.get('x', 0.0))
26-
y_vals.append(pos.get('y', 0.0))
45+
# Get closest speed entry within 0.01 seconds
46+
candidates = []
47+
if idx > 0:
48+
candidates.append(speed_data[idx - 1])
49+
if idx < len(speed_data):
50+
candidates.append(speed_data[idx])
2751

28-
speeds.append(entry['vehicle_interface_reading']['data'].get('speed', 0.0))
29-
times.append(datetime.fromtimestamp(entry['time']))
30-
31-
# Plotting
32-
fig, ax = plt.subplots(figsize=(10, 6))
33-
sc = ax.scatter(x_vals, y_vals, c=speeds, cmap='plasma', s=30, edgecolor='k')
34-
cbar = plt.colorbar(sc, ax=ax)
35-
cbar.set_label('Speed (m/s)')
36-
37-
ax.set_title('Vehicle Position Colored by Speed')
38-
ax.set_xlabel('X Position (m)')
39-
ax.set_ylabel('Y Position (m)')
40-
ax.grid(True)
52+
best_match = min(candidates, key=lambda s: abs(s["time"] - t), default=None)
53+
54+
if best_match and abs(best_match["time"] - t) < 0.01:
55+
matched_x.append(pose["x"])
56+
matched_y.append(pose["y"])
57+
matched_speeds.append(best_match["speed"])
4158

59+
# Plot
60+
plt.figure(figsize=(10, 6))
61+
sc = plt.scatter(matched_x, matched_y, c=matched_speeds, cmap='viridis', edgecolor='k', s=30)
62+
plt.colorbar(sc, label='Speed (m/s)')
63+
plt.xlabel("X Position (m)")
64+
plt.ylabel("Y Position (m)")
65+
plt.title("Vehicle Position Colored by Speed")
66+
plt.grid(True)
4267
plt.tight_layout()
4368
plt.show()

0 commit comments

Comments
 (0)