compare.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import os
  3. import argparse
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. from pathlib import Path
  7. def load_and_filter(csv_path, fixed_param, fixed_value):
  8. """Load a CSV file and filter rows by the fixed parameter."""
  9. df = pd.read_csv(csv_path)
  10. df = df[df[fixed_param] == fixed_value]
  11. return df
  12. def process_files(csv_files, fixed_param, fixed_value):
  13. """Combine and average simulation times by the varying parameter."""
  14. results = {}
  15. varying_param = "depth" if fixed_param == "width" else "width"
  16. for path in csv_files:
  17. df = load_and_filter(path, fixed_param, fixed_value)
  18. # Average simulation times for identical setups
  19. grouped = df.groupby(varying_param, as_index=False)["simulation_time"].mean()
  20. results[path.parent.name + "-" + path.stem] = grouped
  21. return results, varying_param
  22. def plot_results(results, varying_param, fixed_param, fixed_value, name):
  23. plt.figure(figsize=(8, 6))
  24. for label, df in results.items():
  25. plt.plot(df[varying_param], df["simulation_time"], marker='o', label=label)
  26. plt.title(f"Average Simulation Time vs {varying_param.capitalize()} (fixed {fixed_param}={fixed_value})")
  27. plt.xlabel(varying_param.capitalize())
  28. plt.ylabel("Average Simulation Time")
  29. plt.legend()
  30. plt.grid(True)
  31. path = "../Plots/"
  32. output_name = f"{name}_{fixed_param}_{fixed_value}.png"
  33. plt.tight_layout()
  34. plt.savefig(path + output_name, dpi=300)
  35. print(f"✅ Saved plot as {output_name}")
  36. def main():
  37. parser = argparse.ArgumentParser(description="Compare simulation results from multiple CSV files.")
  38. parser.add_argument("csv_files", nargs="+", type=Path, help="Paths to the result CSV files.")
  39. parser.add_argument("-n", "--name", default="DEVS", type=str, help="Name of the output file.")
  40. group = parser.add_mutually_exclusive_group(required=True)
  41. group.add_argument("-w", "--width", type=int, help="Fixed width value.")
  42. group.add_argument("-d", "--depth", type=int, help="Fixed depth value.")
  43. args = parser.parse_args()
  44. if args.width is not None:
  45. fixed_param, fixed_value = "width", args.width
  46. else:
  47. fixed_param, fixed_value = "depth", args.depth
  48. results, varying_param = process_files(args.csv_files, fixed_param, fixed_value)
  49. plot_results(results, varying_param, fixed_param, fixed_value, args.name)
  50. if __name__ == "__main__":
  51. main()