| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #!/usr/bin/env python3
- import os
- import argparse
- import pandas as pd
- import matplotlib.pyplot as plt
- from pathlib import Path
- def clean_data(df):
- df["total_time"] = (
- df["simulation_time"]
- + df["creation_time"]
- + df["setup_time"]
- )
- possible_group_cols = ["depth", "width", "number", "total_time"]
- grouping_cols = [c for c in possible_group_cols if c in df.columns]
- def clean(group):
- Q1 = group["total_time"].quantile(0.25)
- Q3 = group["total_time"].quantile(0.75)
- IQR = Q3 - Q1
- lower = Q1 - 1.5 * IQR
- upper = Q3 + 1.5 * IQR
- return group[(group["total_time"] >= lower) & (group["total_time"] <= upper)]
- cleaned = (
- df.groupby(grouping_cols, group_keys=False)[grouping_cols]
- .apply(clean)
- )
- return cleaned
- def plot_time_vs_param(csv_files, param, fixed_param=None, fixed_value=None, max_value=0, name="DEVS"):
- results = {}
- for path in csv_files:
- df = pd.read_csv(path)
- df = clean_data(df)
- if fixed_param and fixed_value is not None:
- df = df[df[fixed_param] == fixed_value]
- if max_value > 0:
- df = df[df[param] <= max_value]
- grouped = df.groupby(param, as_index=False)["total_time"].mean()
- results[path.stem] = grouped
- plt.figure(figsize=(8,6))
- for label, df in results.items():
- plt.plot(df[param], df["total_time"], marker='o', label=label)
- title = f"Average Simulation Time vs {param.capitalize()}"
- if fixed_param:
- title += f" (fixed {fixed_param}={fixed_value})"
- plt.title(title)
- plt.xlabel(param.capitalize())
- plt.ylabel("Average Simulation Time (s)")
- plt.legend()
- plt.grid(True)
- output_dir = Path("../Plots")
- output_dir.mkdir(parents=True, exist_ok=True)
- output_file = output_dir / f"{name}_{param}.png"
- plt.tight_layout()
- plt.savefig(output_file, dpi=300)
- print(f"Saved plot as {output_file}")
- def main():
- parser = argparse.ArgumentParser(description="Compare simulation results from multiple CSV files.")
- subparsers = parser.add_subparsers(dest="command", required=True)
- # Subcommand: time-vs-depth/width
- parser_tvparam = subparsers.add_parser("param", help="Plot total time vs depth or width")
- parser_tvparam.add_argument("csv_files", nargs="+", type=Path, help="CSV files to process")
- parser_tvparam.add_argument("-n", "--name", default="DEVS", type=str, help="Name of output file")
- group = parser_tvparam.add_mutually_exclusive_group(required=True)
- group.add_argument("-w", "--width", type=int, help="Fixed width value")
- group.add_argument("-d", "--depth", type=int, help="Fixed depth value")
- parser_tvparam.add_argument('-m', '--max', type=int, default=0, help="Maximum value of varying parameter")
- # Subcommand: time-vs-number
- parser_tvn = subparsers.add_parser("number", help="Plot total time vs number")
- parser_tvn.add_argument("csv_files", nargs="+", type=Path, help="CSV files to process")
- parser_tvn.add_argument("-n", "--name", default="DEVS", type=str, help="Name of output file")
- args = parser.parse_args()
- if args.command == "param":
- if args.width is not None:
- fixed_param, fixed_value = "width", args.width
- varying_param = "depth"
- else:
- fixed_param, fixed_value = "depth", args.depth
- varying_param = "width"
- plot_time_vs_param(args.csv_files, param=varying_param,
- fixed_param=fixed_param, fixed_value=fixed_value,
- max_value=args.max, name=args.name)
- elif args.command == "number":
- plot_time_vs_param(args.csv_files, param="number", name=args.name)
- if __name__ == "__main__":
- main()
|