compare.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 process_files(csv_files, fixed_param, fixed_value, max_value=0):
  8. """Combine and average simulation times by the varying parameter."""
  9. results = {}
  10. varying_param = "depth" if fixed_param == "width" else "width"
  11. for path in csv_files:
  12. df = pd.read_csv(path)
  13. df = df[df[fixed_param] == fixed_value].copy()
  14. if max_value > 0:
  15. df = df[df[varying_param] <= max_value].copy()
  16. df["total_time"] = (
  17. df["simulation_time"]
  18. + df["creation_time"]
  19. + df["setup_time"]
  20. )
  21. Q1 = df["total_time"].quantile(0.25)
  22. Q3 = df["total_time"].quantile(0.75)
  23. IQR = Q3 - Q1
  24. lower_bound = Q1 - 1.5 * IQR
  25. upper_bound = Q3 + 1.5 * IQR
  26. df = df[(df["total_time"] >= lower_bound) & (df["total_time"] <= upper_bound)]
  27. grouped = df.groupby(varying_param, as_index=False)["total_time"].mean()
  28. results[path.parent.name + "-" + path.stem] = grouped
  29. return results, varying_param
  30. def plot_results(results, varying_param, fixed_param, fixed_value, name):
  31. plt.figure(figsize=(8, 6))
  32. for label, df in results.items():
  33. plt.plot(df[varying_param], df["total_time"], marker='o', label=label)
  34. plt.title(f"Average Simulation Time vs {varying_param.capitalize()} (fixed {fixed_param}={fixed_value})")
  35. plt.xlabel(varying_param.capitalize())
  36. plt.ylabel("Average Simulation Time (s)")
  37. plt.legend()
  38. plt.grid(True)
  39. path = "../Plots/"
  40. output_name = f"{name}_{fixed_param}_{fixed_value}.png"
  41. plt.tight_layout()
  42. plt.savefig(path + output_name, dpi=300)
  43. print(f"Saved plot as {output_name}")
  44. def main():
  45. parser = argparse.ArgumentParser(description="Compare simulation results from multiple CSV files.")
  46. parser.add_argument("csv_files", nargs="+", type=Path, help="Paths to the result CSV files.")
  47. parser.add_argument("-n", "--name", default="DEVS", type=str, help="Name of the output file.")
  48. group = parser.add_mutually_exclusive_group(required=True)
  49. group.add_argument("-w", "--width", type=int, help="Fixed width value.")
  50. group.add_argument("-d", "--depth", type=int, help="Fixed depth value.")
  51. parser.add_argument('-m', '--max', type=int, default=0, help='Maximum value of the varying parameter')
  52. args = parser.parse_args()
  53. if args.width is not None:
  54. fixed_param, fixed_value = "width", args.width
  55. else:
  56. fixed_param, fixed_value = "depth", args.depth
  57. results, varying_param = process_files(args.csv_files, fixed_param, fixed_value, args.max)
  58. plot_results(results, varying_param, fixed_param, fixed_value, args.name)
  59. if __name__ == "__main__":
  60. main()