rpm_graph v1
This commit is contained in:
75
src/rpm_graph.py
Normal file
75
src/rpm_graph.py
Normal file
@@ -0,0 +1,75 @@
|
||||
# Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
# Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("directory", type=Path, help="Source data directory")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
directory: Path = args.directory
|
||||
|
||||
if not directory.is_dir():
|
||||
parser.error(f"{directory} is not a valid directory")
|
||||
|
||||
print(f"Processing data in: {directory}")
|
||||
|
||||
files: list[Path] = []
|
||||
|
||||
for path in directory.glob("*.csv"):
|
||||
stem = path.stem
|
||||
|
||||
try:
|
||||
base_name, channel = stem.rsplit("_", 1)
|
||||
except ValueError:
|
||||
print(f"Skipping badly named file: {path}")
|
||||
continue
|
||||
|
||||
if channel != "trimmed":
|
||||
print(f"Skipping unknown file: {path}")
|
||||
continue
|
||||
|
||||
files.append(path)
|
||||
|
||||
|
||||
for file in files:
|
||||
print(f"Processing {file.name}")
|
||||
df = pd.read_csv(file).set_index("time_us", drop=False)
|
||||
rows = []
|
||||
current_time_us: int = 0
|
||||
current_crank: int = 0
|
||||
last_crank: int = -1
|
||||
for _, row in tqdm(df.iterrows(), total=len(df)):
|
||||
current_time_us = row["time_us"]
|
||||
current_crank = row["crank"]
|
||||
|
||||
if current_crank == 1:
|
||||
if last_crank != -1:
|
||||
rpm = 30_000_000 / (current_time_us - last_crank)
|
||||
rows.append({"time_us": current_time_us, "rpm": rpm})
|
||||
last_crank = current_time_us
|
||||
|
||||
out_df = pd.DataFrame(rows)
|
||||
base_name, _ = file.stem.rsplit("_", 1)
|
||||
output = file.parent / f"{base_name}_rpm.csv"
|
||||
image_output = file.parent / f"{base_name}_rpm.png"
|
||||
out_df.to_csv(output)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
ax.plot(out_df["time_us"] / 1_000_000, out_df["rpm"])
|
||||
ax.set_xlabel("Time (s)")
|
||||
ax.set_ylabel("RPM")
|
||||
ax.set_title(f"{base_name} RPM over Time")
|
||||
ax.grid = True
|
||||
|
||||
fig.tight_layout()
|
||||
fig.savefig(image_output, dpi=300)
|
||||
|
||||
plt.close(fig)
|
||||
Reference in New Issue
Block a user