From acc751a725806a64f9c0cda58e9f65c2a6fa2962 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Fri, 2 Oct 2026 14:32:01 -0400 Subject: [PATCH 01/20] feasibility = full race and target soc >= target soc --- sim/planning/comparison.py | 4 ++++ sim/planning/report.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 949ab4d..fd37cba 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -31,8 +31,12 @@ def build_comparison_row( } if metadata: row["metadata"] = metadata + row["feasiblity"] = check_feasibility(row) return row +def check_feasibility(row: dict) -> bool: + return row["completed_full_window"] and row["final_soc_pct"] >= row["target_soc_pct"] + def sort_planning_rows(rows: list[dict]) -> list[dict]: return sorted( diff --git a/sim/planning/report.py b/sim/planning/report.py index e79bba4..01dbb0b 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -55,7 +55,7 @@ def print_planning_report( f" {row['target_soc_pct']:>5.1f}% target SoC" f" {row['final_soc_pct']:>5.1f}% final SoC" f" {row['soc_margin_pct']:>5.1f}% over/under target SoC%" - f" {status}" + f" {status}; {row['feasiblity'] and 'feasible' or 'infeasible'}" ) print() From 3426b9b24970533393cb13ef2c9fc4bedb9312a1 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sun, 27 Sep 2026 17:54:11 -0400 Subject: [PATCH 02/20] start --- sim/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sim/main.py b/sim/main.py index ab45645..c8e538e 100644 --- a/sim/main.py +++ b/sim/main.py @@ -99,8 +99,8 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: ) print("\nSpeed sweep summary") - print(" Speed (mph) | Final SoC (%) | Laps | Distance (mi) | 10% reached at") - print(" ----------- | ------------- | ---- | ------------- | --------------") + print(" Speed (mph) | Final SoC (%) | Target SoC | Laps | Distance (mi) | Target SoC% reached at") + print(" ----------- | ------------- | ---------- | ---- | ------------- | ----------------------") for row in rows: cutoff = ( "Full 8h" From 3691e9b3ca5a70850a77c424a5cf438d49dd67b1 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Fri, 2 Oct 2026 01:57:59 -0400 Subject: [PATCH 03/20] add missing stats --- sim/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sim/main.py b/sim/main.py index c8e538e..df654cc 100644 --- a/sim/main.py +++ b/sim/main.py @@ -98,9 +98,11 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: use_api_weather=True, ) + print(rows[0].keys()) + print("\nSpeed sweep summary") - print(" Speed (mph) | Final SoC (%) | Target SoC | Laps | Distance (mi) | Target SoC% reached at") - print(" ----------- | ------------- | ---------- | ---- | ------------- | ----------------------") + print(" Speed (mph) | Final SoC (%) | Target SoC% | SoC Margin (%) | Energy Margin (Wh) | Laps | Distance (mi) | Target SoC% reached at") + print(" ----------- | ------------- | ----------- | -------------- | ------------------ | ---- | ------------- | ----------------------") for row in rows: cutoff = ( "Full 8h" @@ -109,6 +111,7 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: ) print( f" {row['mph']:>11.0f} | {row['final_soc_pct']:>13.1f} |" + f" {row['target_soc_pct']:>10.1f}% | {row['soc_margin_pct']:>13.1f}% | {row['energy_margin_wh']:>18.1f} |" f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>11}" ) From 2f01e00437d831e0843324fd8889570c0ee44c23 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Fri, 2 Oct 2026 13:26:09 -0400 Subject: [PATCH 04/20] remove keys print --- sim/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sim/main.py b/sim/main.py index df654cc..429ec68 100644 --- a/sim/main.py +++ b/sim/main.py @@ -98,8 +98,6 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: use_api_weather=True, ) - print(rows[0].keys()) - print("\nSpeed sweep summary") print(" Speed (mph) | Final SoC (%) | Target SoC% | SoC Margin (%) | Energy Margin (Wh) | Laps | Distance (mi) | Target SoC% reached at") print(" ----------- | ------------- | ----------- | -------------- | ------------------ | ---- | ------------- | ----------------------") From 373916b04505eb6040d9465dd6d4289a719b4a22 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Fri, 2 Oct 2026 13:56:23 -0400 Subject: [PATCH 05/20] fdrmatting --- sim/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sim/main.py b/sim/main.py index 429ec68..57f1584 100644 --- a/sim/main.py +++ b/sim/main.py @@ -110,7 +110,7 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: print( f" {row['mph']:>11.0f} | {row['final_soc_pct']:>13.1f} |" f" {row['target_soc_pct']:>10.1f}% | {row['soc_margin_pct']:>13.1f}% | {row['energy_margin_wh']:>18.1f} |" - f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>11}" + f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>22}" ) From 1d935b2040227a662c2d4d3dab6899f90f539d54 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Fri, 2 Oct 2026 23:27:46 -0400 Subject: [PATCH 06/20] Add feasibility and fix typo --- sim/main.py | 6 +++--- sim/planning/comparison.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sim/main.py b/sim/main.py index 57f1584..d8148e7 100644 --- a/sim/main.py +++ b/sim/main.py @@ -99,8 +99,8 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: ) print("\nSpeed sweep summary") - print(" Speed (mph) | Final SoC (%) | Target SoC% | SoC Margin (%) | Energy Margin (Wh) | Laps | Distance (mi) | Target SoC% reached at") - print(" ----------- | ------------- | ----------- | -------------- | ------------------ | ---- | ------------- | ----------------------") + print(" Speed (mph) | Final SoC (%) | Target SoC% | SoC Margin (%) | Energy Margin (Wh) | Laps | Distance (mi) | Target SoC% reached at | Feasible?") + print(" ----------- | ------------- | ----------- | -------------- | ------------------ | ---- | ------------- | ---------------------- | ---------") for row in rows: cutoff = ( "Full 8h" @@ -110,7 +110,7 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: print( f" {row['mph']:>11.0f} | {row['final_soc_pct']:>13.1f} |" f" {row['target_soc_pct']:>10.1f}% | {row['soc_margin_pct']:>13.1f}% | {row['energy_margin_wh']:>18.1f} |" - f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>22}" + f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>22} | {'Yes' if row['feasibility'] else 'No'}" ) diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index fd37cba..0fbf9b9 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -31,7 +31,7 @@ def build_comparison_row( } if metadata: row["metadata"] = metadata - row["feasiblity"] = check_feasibility(row) + row["feasibility"] = check_feasibility(row) return row def check_feasibility(row: dict) -> bool: From 17d4a305f004058c323eb20c18db8770bf8c1c7b Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 00:03:03 -0400 Subject: [PATCH 07/20] Speed sweep range summary --- sim/main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sim/main.py b/sim/main.py index d8148e7..e6f0407 100644 --- a/sim/main.py +++ b/sim/main.py @@ -97,8 +97,11 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: speeds_mph=SPEED_SWEEP_MPH, use_api_weather=True, ) + rows.sort(key=lambda row: row["mph"]) - print("\nSpeed sweep summary") + print_speed_sweep_summary(rows) + + print("\nSpeed sweep table") print(" Speed (mph) | Final SoC (%) | Target SoC% | SoC Margin (%) | Energy Margin (Wh) | Laps | Distance (mi) | Target SoC% reached at | Feasible?") print(" ----------- | ------------- | ----------- | -------------- | ------------------ | ---- | ------------- | ---------------------- | ---------") for row in rows: @@ -113,6 +116,28 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>22} | {'Yes' if row['feasibility'] else 'No'}" ) +def print_speed_sweep_summary(rows): + print("\nSpeed sweep results:") + + # using a generator (lazy evaluation) to do this + slowest_feasible = next((row for row in rows if row["feasibility"]), None) + fastest_feasible = next((row for row in reversed(rows) if row["feasibility"]), None) + best_fixed_speed_row = max((row for row in rows if row["feasibility"]), key=lambda row: row["distance_miles"]) + + none_feasible = all(not row["feasibility"] for row in rows) + all_feasible = all(row["feasibility"] for row in rows) + + print(f"{len(rows)} speeds were tested") + if none_feasible: + print("No speeds were feasible!") + else: + print(f"Slowest feasible speed: {slowest_feasible['mph']} mph" if slowest_feasible else "None") + print(f"Fastest feasible speed: {fastest_feasible['mph']} mph" if fastest_feasible else "None") + print(f"Best feasible fixed speed (max distance): {best_fixed_speed_row['mph']} mph " + f"with {best_fixed_speed_row['distance_miles']:.1f} mi" + if best_fixed_speed_row else "None") + if all_feasible: + print("All speeds were feasible!") def main(): args = parse_args() From 37d4bd2769167aa743bf6de57b0135b50d6c757d Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 00:09:50 -0400 Subject: [PATCH 08/20] Have copilot generate tests --- tests/fixed_speed_sweep_test.py | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/fixed_speed_sweep_test.py diff --git a/tests/fixed_speed_sweep_test.py b/tests/fixed_speed_sweep_test.py new file mode 100644 index 0000000..b177c29 --- /dev/null +++ b/tests/fixed_speed_sweep_test.py @@ -0,0 +1,60 @@ +import unittest +from contextlib import redirect_stdout +from io import StringIO + +from sim.main import print_speed_sweep_summary + + +class TestFixedSpeedSweepSummary(unittest.TestCase): + def get_summary(self, rows): + output = StringIO() + with redirect_stdout(output): + print_speed_sweep_summary(rows) + return output.getvalue() + + def test_no_speeds_are_feasible(self): + rows = [ + {"mph": 20, "feasibility": False, "distance_miles": 120}, + {"mph": 30, "feasibility": False, "distance_miles": 150}, + ] + + summary = self.get_summary(rows) + + self.assertIn("2 speeds were tested", summary) + self.assertIn("No speeds were feasible!", summary) + self.assertNotIn("Slowest feasible speed:", summary) + self.assertNotIn("Best feasible fixed speed", summary) + + def test_some_speeds_are_feasible(self): + rows = [ + {"mph": 20, "feasibility": True, "distance_miles": 100}, + {"mph": 30, "feasibility": False, "distance_miles": 150}, + {"mph": 40, "feasibility": True, "distance_miles": 180}, + ] + + summary = self.get_summary(rows) + + self.assertIn("3 speeds were tested", summary) + self.assertIn("Slowest feasible speed: 20 mph", summary) + self.assertIn("Fastest feasible speed: 40 mph", summary) + self.assertIn("Best feasible fixed speed (max distance): 40 mph with 180.0 mi", summary) + self.assertNotIn("All speeds were feasible!", summary) + + def test_all_speeds_are_feasible(self): + rows = [ + {"mph": 20, "feasibility": True, "distance_miles": 100}, + {"mph": 30, "feasibility": True, "distance_miles": 200}, + {"mph": 40, "feasibility": True, "distance_miles": 180}, + ] + + summary = self.get_summary(rows) + + self.assertIn("3 speeds were tested", summary) + self.assertIn("Slowest feasible speed: 20 mph", summary) + self.assertIn("Fastest feasible speed: 40 mph", summary) + self.assertIn("Best feasible fixed speed (max distance): 30 mph with 200.0 mi", summary) + self.assertIn("All speeds were feasible!", summary) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From 9a98db2f30b35848b89bec2e1afd66d2a05aaae2 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 00:11:56 -0400 Subject: [PATCH 09/20] Fix crash when there are no feasible speeds --- sim/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sim/main.py b/sim/main.py index e6f0407..c608bbc 100644 --- a/sim/main.py +++ b/sim/main.py @@ -97,7 +97,6 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: speeds_mph=SPEED_SWEEP_MPH, use_api_weather=True, ) - rows.sort(key=lambda row: row["mph"]) print_speed_sweep_summary(rows) @@ -119,10 +118,11 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: def print_speed_sweep_summary(rows): print("\nSpeed sweep results:") - # using a generator (lazy evaluation) to do this - slowest_feasible = next((row for row in rows if row["feasibility"]), None) - fastest_feasible = next((row for row in reversed(rows) if row["feasibility"]), None) - best_fixed_speed_row = max((row for row in rows if row["feasibility"]), key=lambda row: row["distance_miles"]) + rows.sort(key=lambda row: row["mph"]) + feasible_rows = [row for row in rows if row["feasibility"]] + slowest_feasible = feasible_rows[0] if feasible_rows else None + fastest_feasible = feasible_rows[-1] if feasible_rows else None + best_fixed_speed_row = max(feasible_rows, key=lambda row: row["distance_miles"]) if feasible_rows else None none_feasible = all(not row["feasibility"] for row in rows) all_feasible = all(row["feasibility"] for row in rows) From 375176dea30e4a8b67ee10569b445249d8299379 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 00:40:15 -0400 Subject: [PATCH 10/20] Just write everything to CSV --- sim/planning/report.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/sim/planning/report.py b/sim/planning/report.py index 01dbb0b..7bfa9f2 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -55,7 +55,7 @@ def print_planning_report( f" {row['target_soc_pct']:>5.1f}% target SoC" f" {row['final_soc_pct']:>5.1f}% final SoC" f" {row['soc_margin_pct']:>5.1f}% over/under target SoC%" - f" {status}; {row['feasiblity'] and 'feasible' or 'infeasible'}" + f" {status}; {row['feasibility'] and 'feasible' or 'infeasible'}" ) print() @@ -77,3 +77,25 @@ def print_planning_report( f" Expected result: {best_row['laps']} laps, " f"{best_row['final_soc_pct']:.1f}% final SoC." ) + +def export_planning_report_to_csv( + *, + fixed_speed_rows: list[dict], + strategy_rows: list[dict], + output_file_path: str, +) -> None: + + import csv + + combined_rows = fixed_speed_rows + strategy_rows + if not combined_rows: + return + + fieldnames = combined_rows[0].keys() + with open(output_file_path, mode='w', newline='') as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + writer.writeheader() + for row in combined_rows: + writer.writerow(row) + + print(f"Planning report CSV exported to {output_file_path}") From 3d43ab39bd5a03b5611424217b2ad95cac0d0f23 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:02:25 -0400 Subject: [PATCH 11/20] Separate output file and directory --- sim/planning/report.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sim/planning/report.py b/sim/planning/report.py index 7bfa9f2..78392d6 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -82,20 +82,24 @@ def export_planning_report_to_csv( *, fixed_speed_rows: list[dict], strategy_rows: list[dict], - output_file_path: str, + output_dir: str, + file_name: str = "planning_report.csv", ) -> None: import csv + import os.path combined_rows = fixed_speed_rows + strategy_rows if not combined_rows: return + os.makedirs(output_dir, exist_ok=True) + fieldnames = combined_rows[0].keys() - with open(output_file_path, mode='w', newline='') as csvfile: + with open(os.path.join(output_dir, file_name), mode='w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in combined_rows: writer.writerow(row) - print(f"Planning report CSV exported to {output_file_path}") + print(f"Planning report CSV exported to {output_dir}") From d54115ac258bfc3c4dacc6d912a70a63291ccb00 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:02:55 -0400 Subject: [PATCH 12/20] Export CSV to csv directory --- sim/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sim/main.py b/sim/main.py index c608bbc..a58a4e6 100644 --- a/sim/main.py +++ b/sim/main.py @@ -12,7 +12,7 @@ run_fixed_speed_sweep, run_strategy_sweep, ) -from sim.planning.report import print_planning_report +from sim.planning.report import print_planning_report, export_planning_report_to_csv from sim.simulator import LapsRaceSimulator from sim.reporting import ResultsReporter from sim.plotting import SimulationPlotter, save_all_plots @@ -201,6 +201,13 @@ def main(): race_config=base_race, car_config=car ) + + output_path = os.path.join(os.getcwd(), "csv/") + export_planning_report_to_csv( + fixed_speed_rows=fixed_speed_rows, + strategy_rows=strategy_rows, + output_dir=output_path, + ) return if args.speed_sweep: From 4be389be8c9a084a7cbe95b72b6152f15ea868d9 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:03:16 -0400 Subject: [PATCH 13/20] Gitignore csv directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 82c33b8..f7aa4d3 100644 --- a/.gitignore +++ b/.gitignore @@ -220,6 +220,7 @@ plots/laps_modular/* !plots/laps_modular/.gitkeep lap_sim_plots/* plots/* +csv/* .idea venv. From 3cf7ac687367d9878ec18685bd7a098e8445b4f7 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 00:40:15 -0400 Subject: [PATCH 14/20] Just write everything to CSV --- sim/planning/report.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sim/planning/report.py b/sim/planning/report.py index eb238e4..7bfa9f2 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -77,3 +77,25 @@ def print_planning_report( f" Expected result: {best_row['laps']} laps, " f"{best_row['final_soc_pct']:.1f}% final SoC." ) + +def export_planning_report_to_csv( + *, + fixed_speed_rows: list[dict], + strategy_rows: list[dict], + output_file_path: str, +) -> None: + + import csv + + combined_rows = fixed_speed_rows + strategy_rows + if not combined_rows: + return + + fieldnames = combined_rows[0].keys() + with open(output_file_path, mode='w', newline='') as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + writer.writeheader() + for row in combined_rows: + writer.writerow(row) + + print(f"Planning report CSV exported to {output_file_path}") From 764c51f17bf86110e569873ee0220543280e647b Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:02:25 -0400 Subject: [PATCH 15/20] Separate output file and directory --- sim/planning/report.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sim/planning/report.py b/sim/planning/report.py index 7bfa9f2..78392d6 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -82,20 +82,24 @@ def export_planning_report_to_csv( *, fixed_speed_rows: list[dict], strategy_rows: list[dict], - output_file_path: str, + output_dir: str, + file_name: str = "planning_report.csv", ) -> None: import csv + import os.path combined_rows = fixed_speed_rows + strategy_rows if not combined_rows: return + os.makedirs(output_dir, exist_ok=True) + fieldnames = combined_rows[0].keys() - with open(output_file_path, mode='w', newline='') as csvfile: + with open(os.path.join(output_dir, file_name), mode='w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in combined_rows: writer.writerow(row) - print(f"Planning report CSV exported to {output_file_path}") + print(f"Planning report CSV exported to {output_dir}") From e241ee2b52504607447e1a866a905e2676002a6d Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:02:55 -0400 Subject: [PATCH 16/20] Export CSV to csv directory --- sim/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sim/main.py b/sim/main.py index a4b0a18..398452d 100644 --- a/sim/main.py +++ b/sim/main.py @@ -12,7 +12,7 @@ run_fixed_speed_sweep, run_strategy_sweep, ) -from sim.planning.report import print_planning_report +from sim.planning.report import print_planning_report, export_planning_report_to_csv from sim.simulator import LapsRaceSimulator from sim.reporting import ResultsReporter from sim.plotting import SimulationPlotter, save_all_plots @@ -205,6 +205,13 @@ def main(): race_config=base_race, car_config=car ) + + output_path = os.path.join(os.getcwd(), "csv/") + export_planning_report_to_csv( + fixed_speed_rows=fixed_speed_rows, + strategy_rows=strategy_rows, + output_dir=output_path, + ) return if args.speed_sweep: From 19c543d2b262eec8ed2e0059e7a2a1cd70b99446 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 3 Oct 2026 01:03:16 -0400 Subject: [PATCH 17/20] Gitignore csv directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 82c33b8..f7aa4d3 100644 --- a/.gitignore +++ b/.gitignore @@ -220,6 +220,7 @@ plots/laps_modular/* !plots/laps_modular/.gitkeep lap_sim_plots/* plots/* +csv/* .idea venv. From bf116449a119c9c2bd89e1311bfc0ac40c68b649 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 01:09:29 -0400 Subject: [PATCH 18/20] Only require one set of rows --- sim/main.py | 3 +-- sim/planning/report.py | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/sim/main.py b/sim/main.py index 398452d..bfb0f60 100644 --- a/sim/main.py +++ b/sim/main.py @@ -208,8 +208,7 @@ def main(): output_path = os.path.join(os.getcwd(), "csv/") export_planning_report_to_csv( - fixed_speed_rows=fixed_speed_rows, - strategy_rows=strategy_rows, + rows=fixed_speed_rows + strategy_rows, output_dir=output_path, ) return diff --git a/sim/planning/report.py b/sim/planning/report.py index 78392d6..a70c244 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -80,8 +80,7 @@ def print_planning_report( def export_planning_report_to_csv( *, - fixed_speed_rows: list[dict], - strategy_rows: list[dict], + rows: list[dict], output_dir: str, file_name: str = "planning_report.csv", ) -> None: @@ -89,17 +88,16 @@ def export_planning_report_to_csv( import csv import os.path - combined_rows = fixed_speed_rows + strategy_rows - if not combined_rows: + if not rows: return os.makedirs(output_dir, exist_ok=True) - fieldnames = combined_rows[0].keys() + fieldnames = rows[0].keys() with open(os.path.join(output_dir, file_name), mode='w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() - for row in combined_rows: + for row in rows: writer.writerow(row) print(f"Planning report CSV exported to {output_dir}") From e9d9391f1765f2525257f69319fd74b7888aa177 Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 01:38:52 -0400 Subject: [PATCH 19/20] Add csv results to speed sweep --- sim/main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sim/main.py b/sim/main.py index bfb0f60..34085ad 100644 --- a/sim/main.py +++ b/sim/main.py @@ -115,6 +115,9 @@ def fmt_elapsed(minute_of_day: float, start_time_hour: float) -> str: f" {row['laps']:>4} | {row['distance_miles']:>13.1f} | {cutoff:>22} | {'Yes' if row['feasibility'] else 'No'}" ) + output_path = os.path.join(os.getcwd(), "csv/") + export_planning_report_to_csv(rows=rows, output_dir=output_path) + def print_speed_sweep_summary(rows): print("\nSpeed sweep results:") From c0bde63e6a43e521eefedb99d74ff8d91b3bb92a Mon Sep 17 00:00:00 2001 From: Aidan Liu Date: Sat, 17 Oct 2026 01:39:07 -0400 Subject: [PATCH 20/20] Have copilot write tests again, then remove all the fluff --- tests/csv_test.py | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tests/csv_test.py diff --git a/tests/csv_test.py b/tests/csv_test.py new file mode 100644 index 0000000..1f341f9 --- /dev/null +++ b/tests/csv_test.py @@ -0,0 +1,132 @@ +import csv +import os +import tempfile +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +from sim.config import CarConfig, TrackConfig +from sim.main import main as run_main, run_speed_sweep + + +class TestCSV(unittest.TestCase): + def test_fixed_speed_sweep(self): + car = CarConfig() + track = TrackConfig( + name="Test Track", + file_path_name="test_track", + location="Test Location", + latitude=0.0, + longitude=0.0, + lap_distance_km=1.0, + timezone="UTC", + ) + simulator_args = [] + + class StubSimulator: + def __init__(self, *, track, car, race, use_api_weather): + simulator_args.append((track, car, use_api_weather)) + + def run(self): + return SimpleNamespace( + total_laps=1, + total_distance_miles=1.0, + final_soc_pct=80.0, + min_soc_pct=75.0, + avg_speed_mph=20.0, + max_speed_mph=20.0, + min_speed_mph=20.0, + completed_full_window=True, + hit_min_soc=False, + reached_min_soc_time_minutes=480.0, + ) + + with tempfile.TemporaryDirectory() as output_dir: + with patch("sim.planning.sweep.LapsRaceSimulator", StubSimulator): + with patch("sim.main.os.getcwd", return_value=output_dir): + run_speed_sweep(track, car, None) + + csv_path = os.path.join(output_dir, "csv", "planning_report.csv") + self.assertTrue(os.path.isfile(csv_path)) + + with open(csv_path, newline="") as csv_file: + rows = list(csv.DictReader(csv_file)) + + csv_columns = rows[0].keys() + self.assertIn("study", csv_columns) + self.assertIn("label", csv_columns) + self.assertIn("strategy", csv_columns) + self.assertIn("mph", csv_columns) + self.assertIn("laps", csv_columns) + self.assertIn("distance_miles", csv_columns) + self.assertIn("final_soc_pct", csv_columns) + self.assertIn("target_soc_pct", csv_columns) + self.assertIn("soc_margin_pct", csv_columns) + self.assertIn("energy_margin_wh", csv_columns) + self.assertIn("feasibility", csv_columns) + self.assertIn("avg_speed_mph", csv_columns) + self.assertTrue(len(rows) > 1) + + def test_planning_report(self): + car = CarConfig() + track = TrackConfig( + name="Test Track", + file_path_name="test_track", + location="Test Location", + latitude=0.0, + longitude=0.0, + lap_distance_km=1.0, + timezone="UTC", + ) + + class StubSimulator: + def __init__(self, *, track, car, race, use_api_weather): + pass + + def run(self): + return SimpleNamespace( + total_laps=1, + total_distance_miles=1.0, + final_soc_pct=80.0, + min_soc_pct=75.0, + avg_speed_mph=20.0, + max_speed_mph=20.0, + min_speed_mph=20.0, + completed_full_window=True, + hit_min_soc=False, + reached_min_soc_time_minutes=480.0, + ) + + args = SimpleNamespace( + plan=True, + speed_sweep=False, + location=track.file_path_name, + ) + with tempfile.TemporaryDirectory() as output_dir: + with patch("sim.main.parse_args", return_value=args): + with patch("sim.main.get_available_tracks", return_value=[track]): + with patch("sim.main.CarConfig.from_json", return_value=car): + with patch("sim.planning.sweep.LapsRaceSimulator", StubSimulator): + with patch("sim.main.os.getcwd", return_value=output_dir): + run_main() + + csv_path = os.path.join(output_dir, "csv", "planning_report.csv") + self.assertTrue(os.path.isfile(csv_path)) + + with open(csv_path, newline="") as csv_file: + rows = list(csv.DictReader(csv_file)) + + csv_columns = rows[0].keys() + self.assertIn("study", csv_columns) + self.assertIn("label", csv_columns) + self.assertIn("strategy", csv_columns) + self.assertIn("mph", csv_columns) + self.assertIn("laps", csv_columns) + self.assertIn("distance_miles", csv_columns) + self.assertIn("final_soc_pct", csv_columns) + self.assertIn("target_soc_pct", csv_columns) + self.assertIn("soc_margin_pct", csv_columns) + self.assertIn("energy_margin_wh", csv_columns) + self.assertIn("feasibility", csv_columns) + self.assertIn("avg_speed_mph", csv_columns) + self.assertTrue(len(rows) > 1) \ No newline at end of file