Forráskód Böngészése

auto create latex tables based on output files and cli for interaction

Snej69420 5 hónapja
szülő
commit
d45f3555db
1 módosított fájl, 214 hozzáadás és 32 törlés
  1. 214 32
      benchmarks/mean.py

+ 214 - 32
benchmarks/mean.py

@@ -1,48 +1,230 @@
-import pandas as pd
+import argparse
+from pathlib import Path
 import numpy as np
-# file_path = 'benchmarks/Results/Agents/Time/LI_baseline.csv'
-file_path = 'benchmarks/Results/PyPDEVS/Time/minimal/LI_baseline.csv'
-# file_path = 'benchmarks/Results/PyPDEVS/Time/parallel/HOmod_baseline.csv'
-target_width = 200
-target_depth = 200
+import pandas as pd
+
+BASE_DIR = Path(__file__).resolve().parent
+
+ENGINES = {
+    "agents": "Results/Agents/Time/{model}_{type}.csv",
+    "pypdevs-min": "Results/PyPDEVS/Time/minimal/{model}_{type}.csv",
+    "pypdevs-parallel": "Results/PyPDEVS/Time/parallel/{model}_{type}.csv",
+}
+
+# Configured dimensions per model scenario
+MODEL_DIMENSIONS = {
+    "LI": {"depth": 200, "width": 200},
+    "HI": {"depth": 200, "width": 200},
+    "HO": {"depth": 200, "width": 200},
+    "HOmod": {"depth": 43, "width": 43},
+}
+
+MODELS = ["LI", "HI", "HO", "HOmod"]
+TRANSLATIONS = ["baseline", "scaled", "accessed"]
+
 
 def format_to_latex(mean, std):
+    if pd.isna(mean) or pd.isna(std):
+        return "-"
     if std == 0:
         return f"${mean:.3f} \\pm 0.000$"
 
     std_magnitude = int(np.floor(np.log10(abs(std))))
-    decimal_places = -std_magnitude
+    decimal_places = -std_magnitude + 1
 
     if decimal_places >= 0:
-        formatted_mean = f"{mean:.{decimal_places}f}"
-        formatted_std = f"{std:.{decimal_places}f}"
+        return f"${mean:.{decimal_places}f} \\pm {std:.{decimal_places}f}$"
     else:
-        formatted_mean = f"{round(mean, decimal_places):.0f}"
-        formatted_std = f"{round(std, decimal_places):.0f}"
+        return f"${round(mean, decimal_places):.0f} \\pm {round(std, decimal_places):.0f}$"
 
-    return f"${formatted_mean} \\pm {formatted_std}$"
 
-try:
-    df = pd.read_csv(file_path)
-    filtered_df = df[(df['depth'] == target_depth) & (df['width'] == target_width)]
-    if filtered_df.empty:
-        print(f"No rows found matching Depth={target_depth} and Width={target_width}.")
-    else:
-        combined_time = filtered_df['creation_time'] + filtered_df['setup_time'] + filtered_df['simulation_time']
+def get_metrics(engine, model, data_type, custom_depth=None, custom_width=None):
+    """Dynamically locates file, removes configuration-specific outliers using the IQR method
+
+    from compare.py, and computes the final mean/std.
+    """
+    template = ENGINES.get(engine)
+    if not template:
+        return None, None
+
+    file_path = BASE_DIR / template.format(model=model, type=data_type)
+    if not file_path.exists():
+        return None, None
+
+    depth = (
+        custom_depth
+        if custom_depth is not None
+        else MODEL_DIMENSIONS[model]["depth"]
+    )
+    width = (
+        custom_width
+        if custom_width is not None
+        else MODEL_DIMENSIONS[model]["width"]
+    )
+
+    try:
+        df = pd.read_csv(file_path)
+
+        # 1. Compute total_time exactly like clean_data(df) in compare.py
+        df["total_time"] = (
+            df["simulation_time"] + df["creation_time"] + df["setup_time"]
+        )
+
+        # 2. Identify outlier thresholds per unique simulation grouping parameter context
+        group_params = [
+            c for c in ["depth", "width", "number"] if c in df.columns
+        ]
+
+        if group_params:
+            q1 = df.groupby(group_params)["total_time"].transform(
+                lambda x: x.quantile(0.25)
+            )
+            q3 = df.groupby(group_params)["total_time"].transform(
+                lambda x: x.quantile(0.75)
+            )
+
+            iqr = q3 - q1
+            lower = q1 - 1.5 * iqr
+            upper = q3 + 1.5 * iqr
+
+            # 3. Filter out rows matching outlier bounds
+            df = df[(df["total_time"] >= lower) & (df["total_time"] <= upper)]
+
+        # 4. Extract specific dimension rows for the requested table metric cell
+        filtered_df = df[(df["depth"] == depth) & (df["width"] == width)]
+        if filtered_df.empty:
+            return None, None
+
+        combined_time = filtered_df["total_time"]
+        return combined_time.mean(), combined_time.std()
+    except Exception:
+        return None, None
+
+def handle_single(args):
+    """Computes a single measurement point using manual overrides or rules if omitted."""
+    depth = args.depth if args.depth is not None else MODEL_DIMENSIONS[args.model]["depth"]
+    width = args.width if args.width is not None else MODEL_DIMENSIONS[args.model]["width"]
+
+    mean_val, std_val = get_metrics(
+        args.engine, args.model, args.type, custom_depth=depth, custom_width=width
+    )
+    if mean_val is None:
+        print(f"❌ Data matching filters (Depth={depth}, Width={width}) could not be resolved.")
+        return
+
+    print(f"--- Matrix Point Verification ---")
+    print(f"Target: {args.engine} | {args.model}_{args.type}.csv")
+    print(f"Filter: Depth={depth} | Width={width}")
+    print(f"Raw Mean: {mean_val:.6f} | Raw Std: {std_val:.6f}")
+    print(f"LaTeX Output: {format_to_latex(mean_val, std_val)}")
+
+
+def handle_comparison(args):
+    """Generates the engine cross-comparison performance matrix table with fixed dimensions."""
+    print("Generating Engine Performance Matrix Table...\n")
+
+    table_data = {}
+    for engine in ["pypdevs-parallel", "pypdevs-min", "agents"]:
+        row_cells = []
+        for model in MODELS:
+            mean_val, std_val = get_metrics(engine, model, "baseline")
+            row_cells.append(format_to_latex(mean_val, std_val))
+        table_data[engine] = row_cells
+
+    labels = {
+        "pypdevs-parallel": "PyPDEVS (parallel)",
+        "pypdevs-min": "PyPDEVS (min)     ",
+        "agents": "Agents.jl          ",
+    }
+
+    latex_table = (
+        "\\begin{table}[htbp]\n"
+        "    \\centering\n"
+        "    \\begin{tabular}{lcccc}\n"
+        "        \\toprule\n"
+        "        \\textbf{Engine} & {\\textbf{LI (200x200)}} & {\\textbf{HI (200x200)}} & {\\textbf{HO (200x200)}} & {\\textbf{HOmod (43x43)}} \\\\\n"
+        "        \\midrule\n"
+    )
+    for eng in ["pypdevs-parallel", "pypdevs-min", "agents"]:
+        latex_table += f"        {labels[eng]} & {' & '.join(table_data[eng])} \\\\\n"
+
+    latex_table += (
+        "        \\bottomrule\n"
+        "    \\end{tabular}\n"
+        "    \\caption{Performance comparison of different DEVS simulation engines.}\n"
+        "    \\label{tab:devstone-performance}\n"
+        "\\end{table}"
+    )
+    print(latex_table)
+
+
+def handle_translation(args):
+    """Generates two separate cross-functional Translation strategy tables with fixed dimensions."""
+    print("Generating Comparative Translation Function Tables...\n")
+
+    engine_targets = ["pypdevs-parallel", "agents"]
+    engine_labels = {
+        "pypdevs-parallel": "PyPDEVS Parallel",
+        "agents": "Agents.jl",
+    }
+
+    labels = {
+        "baseline": "Identity (Baseline)",
+        "scaled": "Scaled             ",
+        "accessed": "Accessed           ",
+    }
+
+    for engine in engine_targets:
+        print(f"% --- TRANSLATION TABLE FOR {engine_labels[engine].upper()} ---")
+        latex_table = (
+            "\\begin{table}[htbp]\n"
+            "    \\centering\n"
+            "    \\begin{tabular}{lcccc}\n"
+            "        \\toprule\n"
+            "        \\textbf{Translation Function} & {\\textbf{LI (200x200)}} & {\\textbf{HI (200x200)}} & {\\textbf{HO (200x200)}} & {\\textbf{HOmod (43x43)}} \\\\\n"
+            "        \\midrule\n"
+        )
+        for tx in TRANSLATIONS:
+            row_cells = []
+            for model in MODELS:
+                mean_val, std_val = get_metrics(engine, model, tx)
+                row_cells.append(format_to_latex(mean_val, std_val))
+            latex_table += f"        {labels[tx]} & {' & '.join(row_cells)} \\\\\n"
+
+        latex_table += (
+            "        \\bottomrule\n"
+            "    \\end{tabular}\n"
+            f"    \\caption{{Performance comparison of translation functions under {engine_labels[engine]}.}}\n"
+            f"    \\label{{tab:translation-performance-{engine.replace('-', '_')}}}\n"
+            "\\end{table}\n"
+        )
+        print(latex_table)
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="DEVS Benchmarking Performance Statistics Aggregator CLI"
+    )
+    subparsers = parser.add_subparsers(dest="command", required=True, help="Available actions")
+
+    p_single = subparsers.add_parser("single", help="Calculate point result for individual matrix logs.")
+    p_single.add_argument("-e", "--engine", choices=["agents", "pypdevs-min", "pypdevs-parallel"], required=True)
+    p_single.add_argument("-m", "--model", choices=MODELS, required=True)
+    p_single.add_argument("-t", "--type", choices=TRANSLATIONS, default="baseline")
+    p_single.add_argument("-d", "--depth", type=int, default=None, help="Force override depth constraint")
+    p_single.add_argument("-w", "--width", type=int, default=None, help="Force override width constraint")
+
+    subparsers.add_parser("comparison", help="Generate Engine Comparison Table (Fixed sizes automatically applied).")
+    subparsers.add_parser("translation", help="Generate Translation Strategy Tables (Fixed sizes automatically applied).")
 
-        mean_val = combined_time.mean()
-        std_sample = combined_time.std()
+    args = parser.parse_args()
 
-        latex_code = format_to_latex(mean_val, std_sample)
+    if args.command == "single":
+        handle_single(args)
+    elif args.command == "comparison":
+        handle_comparison(args)
+    elif args.command == "translation":
+        handle_translation(args)
 
-        print(f"--- Results for Width: {target_width} | Height: {target_depth} ---")
-        print(f"Number of runs found: {len(filtered_df)}")
-        print(f"Mean Combined Time:   {mean_val:.6f} seconds")
-        print(f"Standard Deviation:    {std_sample:.6f} seconds")
-        print("\n--- Copyable LaTeX Code ---")
-        print(latex_code)
 
-except FileNotFoundError:
-    print(f"Error: The file '{file_path}' could not be found. Please check the path and try again.")
-except Exception as e:
-    print(f"An error occurred: {e}")
+if __name__ == "__main__":
+    main()