#!/usr/bin/env python3 """Reduce the L73 per-tile capture logs into reviewable CSV tables.""" from __future__ import annotations import csv import json import statistics from pathlib import Path ROOT = Path(__file__).resolve().parents[4] OUT = ROOT / "Saved/Linux/l73/breakdown" SUMMARY = OUT / "summary.json" EXPECTED_TILES = {0: 2850, 1: 2249, 2: 2233, 3: 2329} LODS = range(9) def median(values: list[float]) -> float: return statistics.median(values) if values else 0.0 def metric(rows: list[dict], section: str, lod: int, key: str) -> float: return median([float(row["details"].get(section, {}).get(str(lod), {}).get(key, 0) or 0) for row in rows]) def sum_metric(details: dict, section: str, key: str) -> float: return sum(float(row.get(key, 0) or 0) for row in details.get(section, {}).values()) def write_csv(path: Path, rows: list[dict], fields: list[str]) -> None: with path.open("w", newline="", encoding="utf-8") as stream: writer = csv.DictWriter(stream, fieldnames=fields) writer.writeheader() writer.writerows(rows) def main() -> int: runs = json.loads(SUMMARY.read_text(encoding="utf-8")) if len(runs) != 24 or any(row["exit_code"] != 0 for row in runs): raise SystemExit("L73 matrix is incomplete or contains a failed run") signatures = {(row["plan_hash"], row["layout_identity"]) for row in runs} if len(signatures) != 1: raise SystemExit(f"plan/layout changed across runs: {sorted(signatures)}") for strate, expected in EXPECTED_TILES.items(): if {row["whole_tiles"] for row in runs if row["strate"] == strate} != {expected}: raise SystemExit(f"strate {strate} tile count differs from {expected}") lod_rows = [] strategy_rows = [] for strate in range(4): all_rows = [row for row in runs if row["strate"] == strate] clean = [row for row in all_rows if row["mode"] == "clean"] profile = [row for row in all_rows if row["mode"] == "profile"] paired_wall_delta = [ row["whole_view_ready_s"] - next(other["whole_view_ready_s"] for other in all_rows if other["pair"] == row["pair"] and other["mode"] == "clean") for row in profile] paired_worker_delta = [ row["worker_seconds"] - next(other["worker_seconds"] for other in all_rows if other["pair"] == row["pair"] and other["mode"] == "clean") for row in profile] stage_keys = ( ("classify_ms", "stages_by_lod", "classify_ms"), ("field_eval_ms_est", "classification_by_lod", "field_eval_ms_est"), ("mc_total_ms", "worker_by_lod", "mc_exclusive_ms"), ("mc_grid_ms", "cost_by_lod", "mc_grid_ms"), ("mc_cell_class_ms", "cost_by_lod", "mc_cell_class_ms"), ("mc_gradient_ms", "cost_by_lod", "mc_gradient_ms"), ("mc_interpolation_ms", "cost_by_lod", "mc_interpolation_ms"), ("mc_dedup_stream_ms", "cost_by_lod", "mc_dedup_stream_ms"), ("mc_other_ms", "cost_by_lod", "mc_other_ms"), ("skirt_ms", "worker_by_lod", "skirt_ms"), ("ground_refine_ms", "worker_by_lod", "ground_refine_exclusive_ms"), ("cap_refine_ms", "worker_by_lod", "cap_refine_exclusive_ms"), ("connector_shell_ms", "cost_by_lod", "connector_shell_ms"), ("mesh_assembly_ms", "worker_by_lod", "mesh_assembly_ms"), ("payload_build_ms", "classification_by_lod", "payload_build_ms"), ) profile_stage_sums = [] for row in profile: details = row["details"] profile_stage_sums.append({name: sum_metric(details, section, key) for name, section, key in stage_keys}) stage_totals = {name: median([item[name] for item in profile_stage_sums]) for name, _, _ in stage_keys} strategy_row = { "strate": strate, "tiles": EXPECTED_TILES[strate], "clean_wall_median_s": round(median([row["whole_view_ready_s"] for row in clean]), 3), "clean_worker_median_s": round(median([row["worker_seconds"] for row in clean]), 3), "profile_wall_median_s": round(median([row["whole_view_ready_s"] for row in profile]), 3), "profile_worker_median_s": round(median([row["worker_seconds"] for row in profile]), 3), "paired_profile_wall_delta_median_s": round(median(paired_wall_delta), 3), "paired_profile_worker_delta_median_s": round(median(paired_worker_delta), 3), "profile_all_air_tiles_median": 0, "profile_all_solid_tiles_median": 0, "profile_whole_tile_skips_median": 0, "profile_density_point_calls_median": 0, "profile_tile_lod_visits_median": 0, "profile_density_points_per_tile": 0.0, **{name: round(value, 3) for name, value in stage_totals.items()}, } for lod in LODS: cls = [row["details"]["classification_by_lod"].get(str(lod), {}) for row in profile] cost = [row["details"]["cost_by_lod"].get(str(lod), {}) for row in profile] worker = [row["details"]["worker_by_lod"].get(str(lod), {}) for row in profile] stages = [row["details"]["stages_by_lod"].get(str(lod), {}) for row in profile] island = [row["details"]["floating_island_attribution_by_lod"].get(str(lod), {}) for row in profile] passage = [row["details"]["passage_composer_by_lod"].get(str(lod), {}) for row in profile] first = cls[0] if cls else {} if not first: continue row = { "strate": strate, "lod": lod, "tiles": int(median([float(item.get("tiles", 0)) for item in cls])), "all_air_tiles": int(median([float(item.get("all_air", 0)) for item in cls])), "all_solid_tiles": int(median([float(item.get("all_solid", 0)) for item in cls])), "mixed_tiles": int(median([float(item.get("mixed", 0)) for item in cls])), "unclassified_tiles": int(median([float(item.get("unclassified", 0)) for item in cls])), "whole_tile_skips": int(median([float(item.get("tile_skips", 0)) for item in cls])), "density_grid_tiles": int(median([float(item.get("density_grid_tiles", 0)) for item in cls])), "density_grid_sample_points": int(median([float(item.get("density_samples", 0)) for item in cost])), "density_point_calls": int(median([float(item.get("density_point_calls", 0)) for item in cls])), "density_points_per_tile": round(median([float(item.get("density_points_per_tile", 0)) for item in cls]), 1), "field_eval_sample_points": int(median([float(item.get("field_eval_sample_points", 0)) for item in cls])), "proof_air_cells": int(median([float(item.get("proof_air_cells", 0)) for item in cls])), "proof_solid_cells": int(median([float(item.get("proof_solid_cells", 0)) for item in cls])), "classify_worker_ms": round(median([float(item.get("classify_ms", 0)) for item in stages]), 3), "field_eval_worker_ms_est": round(median([float(item.get("field_eval_ms_est", 0)) for item in cls]), 3), "mc_total_worker_ms": round(median([float(item.get("mc_exclusive_ms", 0)) for item in worker]), 3), "mc_grid_worker_ms": round(median([float(item.get("mc_grid_ms", 0)) for item in cost]), 3), "mc_cell_class_worker_ms": round(median([float(item.get("mc_cell_class_ms", 0)) for item in cost]), 3), "mc_normals_worker_ms": round(median([float(item.get("mc_gradient_ms", 0)) for item in cost]), 3), "mc_interpolation_worker_ms": round(median([float(item.get("mc_interpolation_ms", 0)) for item in cost]), 3), "mc_dedup_worker_ms": round(median([float(item.get("mc_dedup_stream_ms", 0)) for item in cost]), 3), "mc_other_worker_ms": round(median([float(item.get("mc_other_ms", 0)) for item in cost]), 3), "skirt_worker_ms": round(median([float(item.get("skirt_ms", 0)) for item in worker]), 3), "ground_refine_worker_ms": round(median([float(item.get("ground_refine_exclusive_ms", 0)) for item in worker]), 3), "cap_refine_worker_ms": round(median([float(item.get("cap_refine_exclusive_ms", 0)) for item in worker]), 3), "connector_shell_worker_ms": round(median([float(item.get("connector_shell_ms", 0)) for item in cost]), 3), "mesh_assembly_worker_ms": round(median([float(item.get("mesh_assembly_ms", 0)) for item in worker]), 3), "payload_build_worker_ms": round(median([float(item.get("payload_build_ms", 0)) for item in cls]), 3), "passage_queries": int(median([float(item.get("queries", 0)) for item in passage])), "passage_relief_field_calls": int(median([float(item.get("relief_field_calls", 0)) for item in passage])), "passage_carve_segment_visits": int(median([float(item.get("carve_segment_visits", 0)) for item in passage])), "passage_support_segment_visits": int(median([float(item.get("support_segment_visits", 0)) for item in passage])), "passage_mouth_route_checks": int(median([float(item.get("mouth_route_segment_checks", 0)) for item in passage])), "passage_smoothstep_evaluations": int(median([float(item.get("smoothstep_evaluations", 0)) for item in passage])), "fi_component_field_ns_est": int(median([float(item.get("comp_field_ns_est", 0)) for item in island])), "fi_component_island_ns_est": int(median([float(item.get("comp_island_ns_est", 0)) for item in island])), "fi_component_roots_ns_est": int(median([float(item.get("comp_roots_ns_est", 0)) for item in island])), } lod_rows.append(row) for name in ("all_air_tiles", "all_solid_tiles", "whole_tile_skips", "density_point_calls"): strategy_row["profile_" + name + "_median"] += row[name] strategy_row["profile_tile_lod_visits_median"] += row["tiles"] strategy_row["profile_density_points_per_tile"] = round( strategy_row["profile_density_point_calls_median"] / strategy_row["profile_tile_lod_visits_median"], 1) strategy_rows.append(strategy_row) write_csv(OUT / "lod-breakdown.csv", lod_rows, list(lod_rows[0])) write_csv(OUT / "strate-summary.csv", strategy_rows, list(strategy_rows[0])) print(f"[L73Analysis] lod_rows={len(lod_rows)} strategy_rows={len(strategy_rows)} " f"plan_hash={next(iter(signatures))[0]} layout={next(iter(signatures))[1]}") for row in strategy_rows: print("[L73Summary] " + json.dumps(row, sort_keys=True)) return 0 if __name__ == "__main__": raise SystemExit(main())