compare.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 clean_data(df):
  8. df["total_time"] = (
  9. df["simulation_time"]
  10. + df["creation_time"]
  11. + df["setup_time"]
  12. )
  13. possible_group_cols = ["depth", "width", "number", "total_time"]
  14. grouping_cols = [c for c in possible_group_cols if c in df.columns]
  15. def clean(group):
  16. Q1 = group["total_time"].quantile(0.25)
  17. Q3 = group["total_time"].quantile(0.75)
  18. IQR = Q3 - Q1
  19. lower = Q1 - 1.5 * IQR
  20. upper = Q3 + 1.5 * IQR
  21. return group[(group["total_time"] >= lower) & (group["total_time"] <= upper)]
  22. cleaned = (
  23. df.groupby(grouping_cols, group_keys=False)[grouping_cols]
  24. .apply(clean)
  25. )
  26. return cleaned
  27. def plot_time_vs_param(csv_files, param, fixed_param=None, fixed_value=None, max_value=0, name="DEVS"):
  28. results = {}
  29. for path in csv_files:
  30. df = pd.read_csv(path)
  31. df = clean_data(df)
  32. if fixed_param and fixed_value is not None:
  33. df = df[df[fixed_param] == fixed_value]
  34. if max_value > 0:
  35. df = df[df[param] <= max_value]
  36. grouped = df.groupby(param, as_index=False)["total_time"].mean()
  37. results[path.stem] = grouped
  38. plt.figure(figsize=(8,6))
  39. for label, df in results.items():
  40. plt.plot(df[param], df["total_time"], marker='o', label=label)
  41. title = f"Average Simulation Time vs {param.capitalize()}"
  42. if fixed_param:
  43. title += f" (fixed {fixed_param}={fixed_value})"
  44. plt.title(title)
  45. plt.xlabel(param.capitalize())
  46. plt.ylabel("Average Simulation Time (s)")
  47. plt.legend()
  48. plt.grid(True)
  49. output_dir = Path("../Plots")
  50. output_dir.mkdir(parents=True, exist_ok=True)
  51. output_file = output_dir / f"{name}_{param}.png"
  52. plt.tight_layout()
  53. plt.savefig(output_file, dpi=300)
  54. print(f"Saved plot as {output_file}")
  55. def main():
  56. parser = argparse.ArgumentParser(description="Compare simulation results from multiple CSV files.")
  57. subparsers = parser.add_subparsers(dest="command", required=True)
  58. # Subcommand: time-vs-depth/width
  59. parser_tvparam = subparsers.add_parser("param", help="Plot total time vs depth or width")
  60. parser_tvparam.add_argument("csv_files", nargs="+", type=Path, help="CSV files to process")
  61. parser_tvparam.add_argument("-n", "--name", default="DEVS", type=str, help="Name of output file")
  62. group = parser_tvparam.add_mutually_exclusive_group(required=True)
  63. group.add_argument("-w", "--width", type=int, help="Fixed width value")
  64. group.add_argument("-d", "--depth", type=int, help="Fixed depth value")
  65. parser_tvparam.add_argument('-m', '--max', type=int, default=0, help="Maximum value of varying parameter")
  66. # Subcommand: time-vs-number
  67. parser_tvn = subparsers.add_parser("number", help="Plot total time vs number")
  68. parser_tvn.add_argument("csv_files", nargs="+", type=Path, help="CSV files to process")
  69. parser_tvn.add_argument("-n", "--name", default="DEVS", type=str, help="Name of output file")
  70. args = parser.parse_args()
  71. if args.command == "param":
  72. if args.width is not None:
  73. fixed_param, fixed_value = "width", args.width
  74. varying_param = "depth"
  75. else:
  76. fixed_param, fixed_value = "depth", args.depth
  77. varying_param = "width"
  78. plot_time_vs_param(args.csv_files, param=varying_param,
  79. fixed_param=fixed_param, fixed_value=fixed_value,
  80. max_value=args.max, name=args.name)
  81. elif args.command == "number":
  82. plot_time_vs_param(args.csv_files, param="number", name=args.name)
  83. if __name__ == "__main__":
  84. main()