calibration_benchmark / src /common /leaderboard.py
odunbar's picture
Separate GBOED from IES and rerun (#19)
221faf8
Raw History Blame
38.5 kB
"""Shared leaderboard renderer for the Calibration Benchmark dashboard.
Extracts the scored-table pipeline from the home page so both the Optimization
leaderboard (home) and the UQ leaderboard (pages/UQLeaderboard.py) can reuse
it without duplicating code.
Usage::
from common.leaderboard import render_leaderboard
render_leaderboard(
metric_store,
target_col="rmse_target",
target_label="RMSE Target Level",
title="Optimization Leaderboard",
state_prefix="opt",
default_target=1.1,
raw_page="pages/RawData.py",
)
"""
from __future__ import annotations
import altair as alt
import pandas as pd
import streamlit as st
try:
from common.method_registry import METHOD_COLORS, SUITABILITY_FAMILIES, family_full_name
except ModuleNotFoundError:
from src.common.method_registry import METHOD_COLORS, SUITABILITY_FAMILIES, family_full_name
_SUITABLE = "#009E73" # Okabe-Ito teal-green (colorblind-safe)
_UNSUITABLE = "#C0392B" # dark red
_UNTESTED = "#BDBDBD" # gray
# Alternating light grays for the family-label column of the suitability table, so
# consecutive family blocks are visually distinct.
_FAMILY_BGS = ("#E9ECEF", "#CED4DA")
def _method_color(present_abbrevs: list[str]) -> alt.Color:
"""Color encoding for the ``abbreviation`` field.
The scale domain/range is always the full method registry so a given
method keeps the same color across every chart and page. The legend,
however, is restricted to *present_abbrevs* so it only lists methods
actually plotted in this chart section rather than every known method.
"""
return alt.Color(
"abbreviation:N",
title="Method",
scale=alt.Scale(domain=list(METHOD_COLORS.keys()), range=list(METHOD_COLORS.values())),
legend=alt.Legend(values=sorted(present_abbrevs)),
)
# update_type display order in the leaderboard table (lower = earlier).
# Unknown/unmapped update types fall back to 99 and appear at the end.
# See method_registry.py for the full four-axis taxonomy.
_UPDATE_TYPE_ORDER: dict[str, int] = {
"gradient (Kalman)": 0,
"gradient (exact)": 1,
"gradient (score)": 2,
"general": 3,
}
# Failure-rate threshold (%) below which a method's runs at a given target are
# considered reliable. Shared by the suitability table and the default
# "Methods to display in charts" selection so both surfaces agree on what
# counts as trustworthy β€” a method that only reaches a fast Score on the rare
# seed that didn't fail shouldn't outrank a slower method that never fails.
_RELIABLE_FAILURE_THRESHOLD = 20.0
def _group_by_family(methods: list[str]) -> list[tuple[str, list[str]]]:
"""Group method abbreviations into (family key, methods) in SUITABILITY_FAMILIES order.
Methods not listed in any family go into a trailing "Other" group.
"""
groups: list[tuple[str, list[str]]] = []
placed: set[str] = set()
for fam, members in SUITABILITY_FAMILIES.items():
present = [m for m in members if m in methods]
if present:
groups.append((fam, present))
placed.update(present)
rest = [m for m in methods if m not in placed]
if rest:
groups.append(("Other", sorted(rest)))
return groups
def _render_suitability_table(
store: pd.DataFrame,
target_col: str,
suitability_target: float,
benchmark_dims: dict[str, tuple[int, int, int]] | None = None,
failure_threshold: float = _RELIABLE_FAILURE_THRESHOLD,
ratio_threshold: float = 3.0,
) -> None:
"""Render the method Γ— benchmark suitability grid above the leaderboard controls."""
benchmarks = sorted(
store["benchmark"].unique().tolist(),
key=lambda bm: benchmark_dims[bm][0] if (benchmark_dims and bm in benchmark_dims) else bm,
)
# Rows are grouped into method families (SUITABILITY_FAMILIES); methods not listed
# there fall into a trailing "Other" family.
_family_of = {m: fam for fam, ms in SUITABILITY_FAMILIES.items() for m in ms}
_family_rank = {fam: i for i, fam in enumerate(SUITABILITY_FAMILIES)}
_row_rank = {m: i for ms in SUITABILITY_FAMILIES.values() for i, m in enumerate(ms)}
methods = sorted(
store["abbreviation"].dropna().unique().tolist(),
key=lambda a: (
_family_rank.get(_family_of.get(a, ""), len(_family_rank)),
_row_rank.get(a, 0),
a,
),
)
family_label = {m: family_full_name(_family_of.get(m, "Other")) for m in methods}
target_str = str(float(suitability_target))
target_str_col = f"{target_col}_str"
target_df = store.copy()
target_df[target_str_col] = target_df[target_col].astype(str)
target_df = target_df[target_df[target_str_col] == target_str]
# Global best per benchmark: min metric across all methods with failure_rate < threshold
global_best: dict[str, float | None] = {}
for bm in benchmarks:
bm_df = target_df[target_df["benchmark"] == bm]
qualifying = bm_df[bm_df["failure_rate"] < failure_threshold].dropna(subset=["metric"])
global_best[bm] = float(qualifying["metric"].min()) if not qualifying.empty else None
cell_text: dict[str, dict[str, str]] = {}
cell_color: dict[str, dict[str, str]] = {}
for method in methods:
cell_text[method] = {}
cell_color[method] = {}
for bm in benchmarks:
sub = target_df[
(target_df["abbreviation"] == method) & (target_df["benchmark"] == bm)
]
if sub.empty:
cell_text[method][bm] = "β€”"
cell_color[method][bm] = _UNTESTED
continue
qualifying = sub[sub["failure_rate"] < failure_threshold].dropna(subset=["metric"])
if qualifying.empty:
# Report the best (lowest) failure rate over ensemble sizes
best_fail = sub["failure_rate"].min()
cell_text[method][bm] = (
f"failed ({best_fail:.0f}%)" if pd.notna(best_fail) else "failed"
)
cell_color[method][bm] = _UNSUITABLE
continue
method_best = float(qualifying["metric"].min())
gb = global_best.get(bm)
ratio = (method_best / gb) if (gb is not None and gb > 0) else 1.0
cell_text[method][bm] = f"{ratio:.1f}Γ—"
cell_color[method][bm] = _SUITABLE if ratio <= ratio_threshold else _UNSUITABLE
col_labels: dict[str, str] = {}
for bm in benchmarks:
if benchmark_dims and bm in benchmark_dims:
p, s, o = benchmark_dims[bm]
col_labels[bm] = f"{bm} (p={p}, s={s}, o={o})"
else:
col_labels[bm] = bm
display_df = pd.DataFrame(cell_text).T.rename(columns=col_labels)
color_df = pd.DataFrame(cell_color).T.rename(columns=col_labels)
display_df.index.name = "Method"
# Leading "Family" column, labelled only on the first row of each family block so
# the families read as blocks (st.dataframe cannot draw rules between row groups).
shown_family = [
fam if i == 0 or fam != family_label[methods[i - 1]] else ""
for i, fam in enumerate(family_label[m] for m in methods)
]
display_df.insert(0, "Family", shown_family)
block_idx, prev = -1, None
family_bg = []
for m in methods:
if family_label[m] != prev:
block_idx, prev = block_idx + 1, family_label[m]
family_bg.append(_FAMILY_BGS[block_idx % 2])
color_df.insert(0, "Family", family_bg)
def _style(df: pd.DataFrame) -> pd.DataFrame:
result = pd.DataFrame("", index=df.index, columns=df.columns)
for row in df.index:
for col in df.columns:
bg = color_df.loc[row, col]
fg = "#212529" if bg == _UNTESTED or bg in _FAMILY_BGS else "white"
result.loc[row, col] = (
f"background-color: {bg}; color: {fg}; "
"text-align: center; font-weight: bold"
)
return result
st.subheader("Method Suitability Overview")
st.caption(
f"Evaluated at target = {suitability_target}. "
f"**Green**: at some ensemble size, failure rate < {failure_threshold:.0f}% "
f"and mean budget ≀ {ratio_threshold:.0f}Γ— the best method (ratio shown). "
"**Red** (failed): data present but no ensemble size met the criteria; "
"the percentage is the lowest failure rate (share of runs that did not reach the target) "
"across ensemble sizes. "
"**Gray (β€”)**: no data for this benchmark. "
"Rows are grouped into method families of similar construction (left column)."
)
st.dataframe(
display_df.style.apply(_style, axis=None),
use_container_width=True,
height=(len(methods) + 1) * 35 + 3,
)
def render_leaderboard(
metric_store: pd.DataFrame,
*,
target_col: str,
target_label: str,
title: str,
state_prefix: str,
default_target: float,
raw_page: str | None = None,
show_failure_panel: bool = False,
canonical_target_levels: list[float] | None = None,
budget_store: pd.DataFrame | None = None,
benchmark_dims: dict[str, tuple[int, int, int]] | None = None,
) -> None:
"""Render a scored leaderboard backed by *metric_store*.
Parameters
----------
metric_store:
DataFrame produced by ``load_metric_store()`` or ``load_uq_store()``.
Must contain at least the columns ``benchmark``, ``algorithm_type``,
``abbreviation``, ``Method``, the four taxonomy tag columns
(``parallelism``, ``update_type``, ``method_goal``, ``emulator_use``),
``ensemble_size``, ``metric``, ``failure_rate``, and *target_col*.
target_col:
Name of the target-coordinate column, e.g. ``"rmse_target"`` or
``"uq_target"``.
target_label:
Human-readable label for the target-level radio control,
e.g. ``"RMSE Target Level"`` or ``"UQ Target Level"``.
title:
Leaderboard section header text.
state_prefix:
Short string used to namespace ``st.session_state`` keys so multiple
leaderboard pages keep independent control state. Use ``"opt"`` for
the Optimization leaderboard and ``"uq"`` for the UQ leaderboard.
default_target:
Target level shown on first load (e.g. ``1.1`` for optimization,
``1.5`` for UQ).
raw_page:
Optional Streamlit page path for an "Open Raw Data" link shown at the
bottom. Pass ``None`` to suppress the link.
show_failure_panel:
If ``True``, render a grouped-bar failure-rate chart below the main
performance chart.
canonical_target_levels:
If provided, the target-level selector always offers exactly these
values (as strings) regardless of what is present in the data. Use
this to pin the UQ leaderboard to its fixed set of target-scaling
levels even when some have 100 % failure.
budget_store:
Optional DataFrame produced by ``load_uq_budget_store()``. When
provided, an additional "Mean Iterations for Coverage" section is
rendered below the main performance chart.
benchmark_dims:
Optional mapping of benchmark name β†’ (param_dim, state_dim, output_dim)
used to annotate column headers in the suitability table.
"""
st.header(title)
if metric_store.empty:
st.warning("No metric data found. Expected NetCDF files in `data/` with a `metric` variable.")
return
_render_suitability_table(
metric_store, target_col, default_target, benchmark_dims=benchmark_dims
)
st.divider()
# Derived column name for the string version of the target coordinate
target_str_col = f"{target_col}_str"
benchmark_values = sorted(metric_store["benchmark"].unique().tolist())
selected_benchmark = st.selectbox("Benchmark", options=benchmark_values, index=0)
filtered = metric_store[metric_store["benchmark"] == selected_benchmark].copy()
filtered[target_str_col] = filtered[target_col].astype(str)
if canonical_target_levels is not None:
target_options = [str(float(t)) for t in canonical_target_levels]
else:
target_options = sorted(
metric_store[target_col].astype(str).unique().tolist()
)
# Session-state keys namespaced by state_prefix so two leaderboard pages
# don't share control state.
k_target = f"{state_prefix}_selected_target"
k_methods = f"{state_prefix}_selected_methods"
default_target_str = str(float(default_target))
# Pre-populate session state so the radio widget and the filter agree on first load.
if k_target not in st.session_state or st.session_state[k_target] not in target_options:
st.session_state[k_target] = (
default_target_str if default_target_str in target_options else target_options[0]
)
selected_target = st.session_state[k_target]
def build_scored_table(input_df: pd.DataFrame, add_rank: bool = True) -> pd.DataFrame:
ranking_source = input_df[input_df[target_str_col] == selected_target]
if ranking_source.empty:
return ranking_source
# Failure rate from every row (NaN metric rows carry failure_rate=100)
failure_agg = ranking_source.groupby(
["algorithm_type", "abbreviation", "Method"], as_index=False
).agg(**{"Mean Failure Rate (%)": ("failure_rate", "mean")})
# Metric stats only from runs that reached the target (non-NaN metric)
valid_rows = ranking_source.dropna(subset=["metric"])
if valid_rows.empty:
return pd.DataFrame()
scored_df = valid_rows.groupby(
["algorithm_type", "abbreviation", "Method"], as_index=False
).agg(
**{"Mean Forward Model Runs": ("metric", "mean")},
**{"Ensemble Sizes Used": ("ensemble_size", "nunique")},
)
best_per_target = (
valid_rows.sort_values(["algorithm_type", target_str_col, "metric", "ensemble_size"])
.groupby(["algorithm_type", "abbreviation", "Method", target_str_col], as_index=False)
.first()[
[
"algorithm_type",
"abbreviation",
"Method",
target_str_col,
"ensemble_size",
]
]
)
optimal_ensemble = best_per_target.groupby(
["algorithm_type", "abbreviation", "Method"], as_index=False
).agg(**{"Optimal Ensemble Size": ("ensemble_size", "mean")})
scored_df = scored_df.merge(
optimal_ensemble,
on=["algorithm_type", "abbreviation", "Method"],
how="left",
)
scored_df = scored_df.merge(
failure_agg,
on=["algorithm_type", "abbreviation", "Method"],
how="left",
)
scored_df["Optimal Ensemble Size"] = scored_df["Optimal Ensemble Size"].round(2)
scored_df["Mean Forward Model Runs"] = scored_df["Mean Forward Model Runs"].round(4)
scored_df["Mean Failure Rate (%)"] = scored_df["Mean Failure Rate (%)"].round(1)
mean_runs_min = scored_df["Mean Forward Model Runs"].min()
mean_runs_max = scored_df["Mean Forward Model Runs"].max()
if mean_runs_max > mean_runs_min:
scored_df["mean_runs_score"] = (
100.0 * (mean_runs_max - scored_df["Mean Forward Model Runs"]) / (mean_runs_max - mean_runs_min)
)
else:
scored_df["mean_runs_score"] = 100.0
scored_df["_unreliable"] = (
scored_df["Mean Failure Rate (%)"].fillna(0.0) >= _RELIABLE_FAILURE_THRESHOLD
)
scored_df["Score"] = scored_df["mean_runs_score"]
sort_columns = ["_unreliable", "Mean Forward Model Runs", "Optimal Ensemble Size", "abbreviation"]
ascending = [True, True, True, True]
scored_df = (
scored_df.sort_values(sort_columns, ascending=ascending)
.drop(columns="_unreliable")
.reset_index(drop=True)
)
if add_rank:
scored_df["Rank"] = scored_df.index + 1
scored_df["Placement"] = scored_df["Rank"].apply(
lambda rank: f"{ {1: 'πŸ₯‡', 2: 'πŸ₯ˆ', 3: 'πŸ₯‰'}.get(rank, '')} #{rank}".strip()
)
# Append methods that had data for this target but every seed failed (all-NaN metric).
# They appear at the bottom of the table as "DNF" so users can distinguish
# "tried and failed" from "not tested on this benchmark".
tried_abbrevs = set(ranking_source["abbreviation"].dropna().unique())
ranked_abbrevs = set(scored_df["abbreviation"].dropna().unique())
dnf_abbrevs = tried_abbrevs - ranked_abbrevs
if dnf_abbrevs:
dnf_rows = failure_agg[failure_agg["abbreviation"].isin(dnf_abbrevs)].copy()
for col in [
"Score", "mean_runs_score",
"Mean Forward Model Runs",
"Optimal Ensemble Size", "Ensemble Sizes Used",
]:
dnf_rows[col] = float("nan")
if add_rank:
dnf_rows["Rank"] = float("nan")
dnf_rows["Placement"] = "DNF"
scored_df = pd.concat([scored_df, dnf_rows], ignore_index=True)
return scored_df
leaderboard_df = build_scored_table(filtered, add_rank=True)
# Methods available for the current benchmark; used to populate the pills
# selector and to prune stale saved selections when the benchmark changes. The
# default selection (first load, or once a prior selection becomes entirely
# invalid) is the top 5 methods, not every method β€” the "Methods to display in
# charts" pills selector lets a user opt into seeing more.
#
# Ranked primarily by reliability (failure rate below _RELIABLE_FAILURE_THRESHOLD)
# and only then by performance Rank within each reliability group. Ranking by
# Rank/Score alone would let a method that only succeeds on a rare seed (and is
# fast on that seed) outrank a method that reliably succeeds but is slower β€”
# e.g. on the UQ leaderboard, a mostly-failing method can post a great mean
# budget from its few converged seeds while a method with zero failures scores
# worse and gets dropped from the default view.
available_methods = sorted(filtered["abbreviation"].dropna().unique().tolist())
if not leaderboard_df.empty and leaderboard_df["Rank"].notna().any():
ranked_methods = leaderboard_df.dropna(subset=["Rank"]).copy()
ranked_methods["_unreliable"] = (
ranked_methods["Mean Failure Rate (%)"].fillna(0.0) >= _RELIABLE_FAILURE_THRESHOLD
)
default_methods = (
ranked_methods.sort_values(["_unreliable", "Rank"])["abbreviation"].head(5).tolist()
)
else:
# All runs failed at this target (or no data) β€” show every method so the
# all-DNF warning/chart-suppression path still has something to work with.
default_methods = available_methods
# The selector is one pills widget per method family (one row each); the
# selection is the union of the family widgets, mirrored into k_methods.
method_groups = _group_by_family(available_methods)
family_keys = {fam: f"{k_methods}__{fam}" for fam, _ in method_groups}
if any(wkey in st.session_state for wkey in family_keys.values()):
saved_methods = [
m for wkey in family_keys.values() for m in st.session_state.get(wkey) or []
]
else:
saved_methods = default_methods
valid_saved = [m for m in saved_methods if m in available_methods]
st.session_state[k_methods] = valid_saved if valid_saved else default_methods
for fam, members in method_groups:
st.session_state[family_keys[fam]] = [
m for m in st.session_state[k_methods] if m in members
]
# Attach the four taxonomy tags (constant per abbreviation, so this is a
# plain lookup merge rather than a groupby key threaded through every
# aggregation above).
if not leaderboard_df.empty:
tag_cols = ["abbreviation", "parallelism", "update_type", "method_goal", "emulator_use"]
tag_lookup = filtered[tag_cols].drop_duplicates("abbreviation")
leaderboard_df = leaderboard_df.merge(tag_lookup, on="abbreviation", how="left")
# Re-sort by update_type group, then by performance within each group.
# Placement numbers still reflect overall performance rank.
if not leaderboard_df.empty:
leaderboard_df = leaderboard_df.assign(
_update_type_sort=leaderboard_df["update_type"].map(_UPDATE_TYPE_ORDER).fillna(99),
_is_dnf=leaderboard_df["Score"].isna(),
).sort_values(
["_update_type_sort", "_is_dnf", "Score"],
ascending=[True, True, False],
na_position="last",
).drop(columns=["_update_type_sort", "_is_dnf"]).reset_index(drop=True)
score_basis = "mean forward-model runs at the selected target level (lower is better)"
# Controls β€” always visible (not collapsed behind a dropdown) so users can
# change target/methods even when the current selection yields all
# failures.
st.subheader("Target & Method Controls")
st.radio(
target_label,
options=target_options,
horizontal=True,
key=k_target,
)
st.markdown("**Methods to display in charts**")
st.caption("Defaults to the top 5 methods by current rank β€” click to add or remove. Rows are method families.")
for fam, members in method_groups:
st.pills(
family_full_name(fam),
options=members,
selection_mode="multi",
key=family_keys[fam],
)
selected_methods = [
m for fam, _ in method_groups for m in st.session_state.get(family_keys[fam]) or []
]
st.session_state[k_methods] = selected_methods
if not selected_methods:
selected_methods = available_methods
st.divider()
# --- Charts (rendered above the leaderboard table, below the controls) ---
# Failure panel β€” shown first so failures are visible before the budget/
# iterations charts below. Rendered regardless of whether the scored table
# has rows (own emptiness check on failure_source).
if show_failure_panel:
failure_source = filtered[filtered[target_str_col] == selected_target]
failure_source = failure_source[failure_source["abbreviation"].isin(selected_methods)]
if not failure_source.empty:
failure_df = failure_source.groupby(
["abbreviation", "ensemble_size"], as_index=False
).agg(mean_failure_rate=("failure_rate", "mean"))
failure_df = failure_df.sort_values("ensemble_size")
st.subheader(f"Failure Rate of Hitting Target {selected_target}")
ens_ticks_fail = sorted(failure_df["ensemble_size"].unique().tolist())
failure_chart = (
alt.Chart(failure_df)
.mark_bar()
.encode(
x=alt.X(
"ensemble_size:O",
title="Ensemble Size",
sort=[str(e) for e in ens_ticks_fail],
axis=alt.Axis(labelAngle=0),
),
xOffset=alt.XOffset("abbreviation:N"),
y=alt.Y(
"mean_failure_rate:Q",
title="Failure Rate (%)",
scale=alt.Scale(domain=[0, 100]),
),
color=_method_color(failure_df["abbreviation"].unique().tolist()),
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:O", title="Ensemble Size"),
alt.Tooltip("mean_failure_rate:Q", format=".1f", title="Failure Rate (%)"),
],
)
)
ceiling_line = (
alt.Chart(alt.Data(values=[{}]))
.mark_rule(color="grey", strokeDash=[4, 4])
.encode(y=alt.datum(100))
)
st.altair_chart(failure_chart + ceiling_line, use_container_width=True)
st.divider()
if not leaderboard_df.empty:
st.subheader("Mean Forward Model Runs vs Ensemble Size")
chart_source = filtered[filtered[target_str_col] == selected_target]
chart_source = chart_source[chart_source["abbreviation"].isin(selected_methods)]
chart_df = chart_source.dropna(subset=["metric"]).groupby(
["abbreviation", "ensemble_size"], as_index=False
).agg(mean_forward_runs=("metric", "mean"))
all_ens_combos = chart_source[["abbreviation", "ensemble_size"]].drop_duplicates()
ens_ticks = sorted(all_ens_combos["ensemble_size"].unique().tolist()) if not all_ens_combos.empty else []
main_color = _method_color(all_ens_combos["abbreviation"].unique().tolist())
if not chart_df.empty:
_ok = chart_df[["abbreviation", "ensemble_size"]].assign(_ok=True)
fail_df = all_ens_combos.merge(_ok, on=["abbreviation", "ensemble_size"], how="left")
fail_df = fail_df[fail_df["_ok"].isna()].drop(columns="_ok").assign(mean_forward_runs=0.0)
else:
fail_df = all_ens_combos.assign(mean_forward_runs=0.0)
all_failed = chart_df.empty
chart_layers = []
if not chart_df.empty:
chart_layers.append(
alt.Chart(chart_df)
.mark_line(point=True)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=ens_ticks, format="d"),
),
y=alt.Y("mean_forward_runs:Q", title="Mean Forward Model Runs"),
color=main_color,
tooltip=["abbreviation", "ensemble_size", alt.Tooltip("mean_forward_runs:Q", format=".4f")],
)
)
if not fail_df.empty:
y_fwd = (
alt.Y("mean_forward_runs:Q", title="Mean Forward Model Runs", scale=alt.Scale(domain=[0, 1]))
if all_failed
else alt.Y("mean_forward_runs:Q", title="Mean Forward Model Runs")
)
chart_layers.append(
alt.Chart(fail_df)
.mark_point(shape="cross", angle=45, size=200, filled=True, opacity=1.0)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=ens_ticks, format="d"),
),
y=y_fwd,
color=main_color,
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:Q", title="Ensemble Size"),
alt.Tooltip("mean_forward_runs:Q", title="Value (all failed)"),
],
)
)
# Dashed vertical rule for methods with only one ensemble size so they
# remain easy to spot when multi-ensemble methods dominate the x-axis.
if not all_ens_combos.empty:
single_ens_abbrevs = (
all_ens_combos.groupby("abbreviation")["ensemble_size"]
.nunique()
.pipe(lambda s: s[s == 1].index.tolist())
)
if single_ens_abbrevs:
rule_df = (
all_ens_combos[all_ens_combos["abbreviation"].isin(single_ens_abbrevs)]
.drop_duplicates()
)
chart_layers.append(
alt.Chart(rule_df)
.mark_rule(strokeDash=[4, 4], opacity=0.5)
.encode(
x=alt.X(
"ensemble_size:Q",
axis=alt.Axis(values=ens_ticks, format="d"),
),
color=main_color,
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:Q", title="Ensemble Size"),
],
)
)
if chart_layers:
st.altair_chart(alt.layer(*chart_layers), use_container_width=True)
st.caption(
"Note: plotted means here and in the charts below are averaged over "
"successful seeds only β€” runs that never reached the target are excluded. "
"An βœ• at zero marks ensemble sizes where every seed failed."
+ (
" Failure frequency is shown in the failure-rate chart further down."
if show_failure_panel
else ""
)
)
# Mean-iterations section for the Optimization board: n_iterations =
# metric / ensemble_size, since every registered method's forward-model
# call count scales as ensemble_size (or restart count) per outer-loop
# iteration β€” see method_registry.py taxonomy. The UQ board instead
# renders its own iterations chart below from real k_iter data via
# budget_store, so this section only applies when none is supplied.
if budget_store is None and not all_ens_combos.empty:
iters_chart_df = chart_df.assign(
mean_iterations=lambda d: d["mean_forward_runs"] / d["ensemble_size"]
)
iters_fail_df = fail_df.assign(mean_iterations=0.0)
st.subheader("Mean Iterations to Reach Target vs Ensemble Size")
opt_iters_layers = []
if not iters_chart_df.empty:
opt_iters_layers.append(
alt.Chart(iters_chart_df)
.mark_line(point=True)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=ens_ticks, format="d"),
),
y=alt.Y("mean_iterations:Q", title="Mean Iterations"),
color=main_color,
tooltip=["abbreviation", "ensemble_size", alt.Tooltip("mean_iterations:Q", format=".2f")],
)
)
if not iters_fail_df.empty:
y_iters_opt = (
alt.Y("mean_iterations:Q", title="Mean Iterations", scale=alt.Scale(domain=[0, 1]))
if all_failed
else alt.Y("mean_iterations:Q", title="Mean Iterations")
)
opt_iters_layers.append(
alt.Chart(iters_fail_df)
.mark_point(shape="cross", angle=45, size=200, filled=True, opacity=1.0)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=ens_ticks, format="d"),
),
y=y_iters_opt,
color=main_color,
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:Q", title="Ensemble Size"),
alt.Tooltip("mean_iterations:Q", title="Value (all failed)"),
],
)
)
if opt_iters_layers:
st.altair_chart(alt.layer(*opt_iters_layers), use_container_width=True)
# Mean-iterations-for-coverage section (UQ only, when budget_store provided)
if budget_store is not None and not budget_store.empty:
bf = budget_store[budget_store["benchmark"] == selected_benchmark].copy()
bf[target_str_col] = bf[target_col].astype(str)
bf = bf[bf[target_str_col] == selected_target]
bf = bf[bf["abbreviation"].isin(selected_methods)]
iters_df = (
bf[["abbreviation", "ensemble_size", "mean_iters"]]
.dropna(subset=["mean_iters"])
.groupby(["abbreviation", "ensemble_size"], as_index=False)
.agg(mean_iters=("mean_iters", "mean"))
)
all_ens_combos_iters = bf[["abbreviation", "ensemble_size"]].drop_duplicates()
if not all_ens_combos_iters.empty:
iters_ticks = sorted(all_ens_combos_iters["ensemble_size"].unique().tolist())
iters_color = _method_color(all_ens_combos_iters["abbreviation"].unique().tolist())
if not iters_df.empty:
_ok_iters = iters_df[["abbreviation", "ensemble_size"]].assign(_ok=True)
fail_df_iters = all_ens_combos_iters.merge(_ok_iters, on=["abbreviation", "ensemble_size"], how="left")
fail_df_iters = fail_df_iters[fail_df_iters["_ok"].isna()].drop(columns="_ok").assign(mean_iters=0.0)
else:
fail_df_iters = all_ens_combos_iters.assign(mean_iters=0.0)
all_failed_iters = iters_df.empty
st.subheader("Mean Iterations for Coverage vs Ensemble Size")
iters_layers = []
if not iters_df.empty:
iters_layers.append(
alt.Chart(iters_df)
.mark_line(point=True)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=iters_ticks, format="d"),
),
y=alt.Y("mean_iters:Q", title="Mean Iterations"),
color=iters_color,
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:Q", title="Ensemble Size"),
alt.Tooltip("mean_iters:Q", format=".2f", title="Mean Iterations"),
],
)
)
if not fail_df_iters.empty:
y_iters = (
alt.Y("mean_iters:Q", title="Mean Iterations", scale=alt.Scale(domain=[0, 1]))
if all_failed_iters
else alt.Y("mean_iters:Q", title="Mean Iterations")
)
iters_layers.append(
alt.Chart(fail_df_iters)
.mark_point(shape="cross", angle=45, size=200, filled=True, opacity=1.0)
.encode(
x=alt.X(
"ensemble_size:Q",
title="Ensemble Size",
axis=alt.Axis(values=iters_ticks, format="d"),
),
y=y_iters,
color=iters_color,
tooltip=[
alt.Tooltip("abbreviation:N", title="Method"),
alt.Tooltip("ensemble_size:Q", title="Ensemble Size"),
alt.Tooltip("mean_iters:Q", title="Value (all failed)"),
],
)
)
if iters_layers:
st.altair_chart(alt.layer(*iters_layers), use_container_width=True)
st.divider()
# --- Leaderboard table (rendered below the charts) ---
table_column_order = [
"Placement",
"abbreviation",
"Method",
"update_type",
"parallelism",
"method_goal",
"emulator_use",
"Score",
"Mean Forward Model Runs",
"Mean Failure Rate (%)",
"Optimal Ensemble Size",
"Ensemble Sizes Used",
]
if leaderboard_df.empty:
st.warning(
"All runs failed to reach the target at this selection. "
"See the failure rate chart above."
if show_failure_panel
else "No rows available for the current benchmark/target selection."
)
else:
st.subheader(f"Ranked Leaderboard β€” {selected_benchmark}")
st.dataframe(
leaderboard_df,
hide_index=True,
use_container_width=True,
column_config={
"Placement": st.column_config.TextColumn("Placement"),
"update_type": st.column_config.TextColumn("Update Type"),
"parallelism": st.column_config.TextColumn("Parallelism"),
"method_goal": st.column_config.TextColumn("Method Goal"),
"emulator_use": st.column_config.TextColumn("Emulator Use"),
"Method": st.column_config.TextColumn("Method"),
"abbreviation": st.column_config.TextColumn("Abbrev."),
"Mean Forward Model Runs": st.column_config.NumberColumn("Mean Forward Model Runs", format="%.4f"),
"Score": st.column_config.ProgressColumn("Score (0-100)", min_value=0.0, max_value=100.0, format="%.1f"),
"Optimal Ensemble Size": st.column_config.NumberColumn("Mean Optimal Ensemble Size", format="%.2f"),
"Mean Failure Rate (%)": st.column_config.NumberColumn("Mean Failure Rate (%)", format="%.1f"),
"Ensemble Sizes Used": st.column_config.NumberColumn("Ensemble Sizes Used", format="%d"),
},
column_order=table_column_order,
)
st.info(
f"Score is a normalized 0–100 ranking based on **{score_basis}**. "
"Values are computed from all ensemble sizes after averaging over random seeds."
)
st.caption("Top 3 are shown as podium spots; remaining methods are directly comparable via normalized score.")
if raw_page is not None:
st.page_link(raw_page, label="Open Raw Data & CSV Export", icon="🧾")