plot_time-models.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. import argparse
  3. from pathlib import Path
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. from copy import copy
  7. def remove_outliers_iqr(df):
  8. groups = ["model_type", "width", "depth"]
  9. q1 = df.groupby(groups)["simulation_time"].quantile(0.25)
  10. q3 = df.groupby(groups)["simulation_time"].quantile(0.75)
  11. iqr = q3 - q1
  12. lower = q1 - 1.5 * iqr
  13. upper = q3 + 1.5 * iqr
  14. # Join bounds back to original rows
  15. bounds = pd.concat([lower, upper], axis=1)
  16. bounds.columns = ["lower", "upper"]
  17. df = df.join(bounds, on=groups)
  18. return df[(df["simulation_time"] >= df["lower"]) &
  19. (df["simulation_time"] <= df["upper"])].drop(columns=["lower", "upper"])
  20. def process_file(path, max: int = 0):
  21. """Load file, remove outliers, compute atomic models, average per atomic count."""
  22. df = pd.read_csv(path).copy()
  23. if max > 0:
  24. df = df[df["width"] <= max].copy()
  25. df = df[df["depth"] <= max].copy()
  26. df = remove_outliers_iqr(df)
  27. df["atomic_models"] = (df["width"] - 1) * (df["depth"] - 1) + 1
  28. grouped = df.groupby("atomic_models", as_index=False)["simulation_time"].mean()
  29. label = path.parent.name + "-" + path.stem
  30. return label, grouped
  31. def plot_results(results_dict, name):
  32. plt.figure(figsize=(16, 9))
  33. for label, df in results_dict.items():
  34. plt.plot(
  35. df["atomic_models"],
  36. df["simulation_time"],
  37. marker="o",
  38. linestyle="-",
  39. label=label
  40. )
  41. plt.title("Mean Simulation Time vs Atomic Model Count")
  42. plt.xlabel("# Atomic Models")
  43. plt.ylabel("Mean Simulation Time (s)")
  44. plt.grid(True)
  45. plt.legend()
  46. plt.tight_layout()
  47. output_path = f"../Plots/{name}_NA.png"
  48. plt.savefig(output_path, dpi=300)
  49. print(f"Saved plot: {output_path}")
  50. def main():
  51. parser = argparse.ArgumentParser(description="Compare multiple model results by atomic-model count.")
  52. parser.add_argument("csv_files", nargs="+", type=Path, help="Input CSV files")
  53. parser.add_argument("-n", "--name", type=str, default="comparison", help="Output file name")
  54. parser.add_argument('-m', '--max', type=int, default=0, help='Maximum value of the varying parameter')
  55. args = parser.parse_args()
  56. results = {}
  57. for file in args.csv_files:
  58. label, grouped = process_file(file, args.max)
  59. results[label] = grouped
  60. plot_results(results, args.name)
  61. if __name__ == "__main__":
  62. main()