Mid progress
This commit is contained in:
57
plot.py
57
plot.py
@@ -1,45 +1,58 @@
|
||||
import os
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.dates as mdates
|
||||
import numpy as np
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
DATA_DIR = "data"
|
||||
|
||||
# Find the latest data file
|
||||
files = [f for f in os.listdir(DATA_DIR) if f.startswith("voltages_") and f.endswith(".csv")]
|
||||
# Find the newest summary CSV
|
||||
files = [f for f in os.listdir(DATA_DIR) if f.startswith("summary_") and f.endswith(".csv")]
|
||||
if not files:
|
||||
raise FileNotFoundError("No voltage data files found in 'data/'")
|
||||
raise FileNotFoundError("No summary files found in 'data/'")
|
||||
|
||||
latest_file = max(files, key=lambda f: os.path.getmtime(os.path.join(DATA_DIR, f)))
|
||||
filepath = os.path.join(DATA_DIR, latest_file)
|
||||
print(f"Plotting data from {filepath}")
|
||||
|
||||
# Load CSV data
|
||||
timestamps = []
|
||||
voltages = []
|
||||
# Load CSV
|
||||
start_ms = []
|
||||
end_ms = []
|
||||
vals = [] # rows of [max, mean+std, mean, mean-std, min]
|
||||
|
||||
with open(filepath, "r") as f:
|
||||
next(f) # skip header
|
||||
for line in f:
|
||||
ts_str, v_str = line.strip().split(",")
|
||||
ts = datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S.%f")
|
||||
timestamps.append(ts)
|
||||
voltages.append(float(v_str))
|
||||
parts = line.strip().split(",")
|
||||
if len(parts) != 7:
|
||||
continue
|
||||
s_ms, e_ms, *rest = parts
|
||||
start_ms.append(float(s_ms))
|
||||
end_ms.append(float(e_ms))
|
||||
vals.append([float(x) for x in rest])
|
||||
|
||||
vals = np.array(vals)
|
||||
|
||||
# Time axis: relative seconds since first interval start
|
||||
start_ms = np.array(start_ms)
|
||||
|
||||
# Plot
|
||||
plt.figure(figsize=(12, 5))
|
||||
plt.scatter(timestamps, voltages, marker='o', linestyle='-', s=0.2)
|
||||
plt.figure(figsize=(12, 6))
|
||||
|
||||
plt.xlabel("Time (CET)")
|
||||
plt.ylabel("Voltage (V)")
|
||||
plt.title(f"Voltage Measurements ({latest_file})")
|
||||
max_tot = vals[:, 1].mean()
|
||||
min_tot = vals[:, 3].mean()
|
||||
|
||||
print(max_tot, min_tot)
|
||||
plt.scatter(start_ms, vals[:, 0], label="max", s=3)
|
||||
plt.scatter(start_ms, vals[:, 1], label="mean+std", s=3)
|
||||
plt.scatter(start_ms, vals[:, 2], label="mean", s=3)
|
||||
plt.scatter(start_ms, vals[:, 3], label="mean-std", s=3)
|
||||
plt.scatter(start_ms, vals[:, 4], label="min", s=3 )
|
||||
|
||||
plt.xlabel("Time since start (s)")
|
||||
plt.ylabel("Voltage summary (V)")
|
||||
plt.title(f"Voltage Summary ({latest_file})")
|
||||
plt.grid(True)
|
||||
|
||||
# Format x-axis for readable time
|
||||
plt.gcf().autofmt_xdate()
|
||||
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
|
||||
plt.legend()
|
||||
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user