Suitability tables (#4)
Browse files- add a suitability table, (green = <3 x best effort and <20% failures) and remove "All" summary information (6aead36b149e5da6607fcf688fc057ba96f050a6)
- reorder suitability table (663f796aef4d9112b03264182f0ec6cea8d801f4)
Files changed (5)
46 unmodified lines474849505152535467 unmodified lines12212312412512612716 unmodified lines14414514614714814915015131 unmodified lines18318418546 unmodified lines│ ├── streamlit_app.py Entrypoint + Home/"Optimization Leaderboard"│ ├── data_store.py load_metric_store() / load_uq_store() / load_uq_budget_store()│ │ → @st.cache_data DataFrames; DATASET_FILES, UQ_BUDGET_FILES│ ├── common/│ │ ├── leaderboard.py render_leaderboard() — shared scored-table + chart pipeline│ │ │ optional budget_store param adds budget/iters chart section│ │ └── method_registry.py KNOWN_METHODS registry + name canonicalization│ └── pages/67 unmodified linesAll three stores are `@st.cache_data` — clear Streamlit cache to pick up new data files.## Method registry (`src/common/method_registry.py`)| Canonical | Abbreviation | Family | Aliases |16 unmodified lines3. Place result NetCDF(s) in `data/` and add paths to `DATASET_FILES` in `src/data_store.py`.### Add an optimization benchmark datasetAdd a `"BENCHMARK_NAME": [list_of_nc_paths]` entry to `DATASET_FILES` in `src/data_store.py`.Paths are relative to the `data/` directory.### Add UQ dataAdd an `(algorithm_type, nc_path)` tuple to the appropriate benchmark key in `UQ_BUDGET_FILES`31 unmodified linesnever reached (no `metric == -1` sentinel); `failure_rate` is computed differently.- **`render_leaderboard` budget_store param** — must be passed explicitly from`UQLeaderboard.py`; `leaderboard.py` is not aware of UQ-specific data paths.46 unmodified lines4748495051525354555667 unmodified lines12412512612712812913013113213313413513613713813914014114214314416 unmodified lines16116216316416516616716816917031 unmodified lines20220320420520620720820921021121246 unmodified lines│ ├── streamlit_app.py Entrypoint + Home/"Optimization Leaderboard"│ ├── data_store.py load_metric_store() / load_uq_store() / load_uq_budget_store()│ │ → @st.cache_data DataFrames; DATASET_FILES, UQ_BUDGET_FILES│ │ → BENCHMARK_DIMS — physical (param, state, output) dims per benchmark│ ├── common/│ │ ├── leaderboard.py render_leaderboard() — shared scored-table + chart pipeline│ │ │ _render_suitability_table() — method × benchmark suitability grid│ │ │ optional budget_store param adds budget/iters chart section│ │ └── method_registry.py KNOWN_METHODS registry + name canonicalization│ └── pages/67 unmodified linesAll three stores are `@st.cache_data` — clear Streamlit cache to pick up new data files.### Benchmark dimensions (`BENCHMARK_DIMS`)Physical dimensions of each benchmark exported from `src/data_store.py` as`BENCHMARK_DIMS: dict[str, tuple[int, int, int]]` — `(param_dim, state_dim, output_dim)`:| Benchmark | param | state | output ||---|---|---|---|| `L63` | 2 | 3 | 9 || `L96` | 1 | 40 | 80 || `L96_NN_FORCING` | 61 | 100 | 200 || `L96_SPATIAL_FORCING` | 40 | 40 | 80 |Passed as `benchmark_dims=BENCHMARK_DIMS` to `render_leaderboard()` for column-headerannotations in the suitability table. Update if a new benchmark is added.## Method registry (`src/common/method_registry.py`)| Canonical | Abbreviation | Family | Aliases |16 unmodified lines3. Place result NetCDF(s) in `data/` and add paths to `DATASET_FILES` in `src/data_store.py`.### Add an optimization benchmark dataset1. Add a `"BENCHMARK_NAME": [list_of_nc_paths]` entry to `DATASET_FILES` in `src/data_store.py`.Paths are relative to the `data/` directory.2. Add a `"BENCHMARK_NAME": (param_dim, state_dim, output_dim)` entry to `BENCHMARK_DIMS`in `src/data_store.py` for the suitability table column header.### Add UQ dataAdd an `(algorithm_type, nc_path)` tuple to the appropriate benchmark key in `UQ_BUDGET_FILES`31 unmodified linesnever reached (no `metric == -1` sentinel); `failure_rate` is computed differently.- **`render_leaderboard` budget_store param** — must be passed explicitly from`UQLeaderboard.py`; `leaderboard.py` is not aware of UQ-specific data paths.- **`render_leaderboard` requires `default_target`** — callers must pass the fixed target levelshown on first load (`1.1` for optimization, `1.5` for UQ). There is no "All targets" option.- **Suitability table is fixed at `default_target`** — `_render_suitability_table()` alwaysevaluates suitability at the `default_target` passed to `render_leaderboard()`, independentof what the user selects in the target radio control below it.- **Suitability criteria** — a method is green for a benchmark if there exists an ensemble sizewith `failure_rate < 20%` and `mean_budget ≤ 3× global_best` at `default_target`. Gray meansno data for that benchmark; the method may be suitable on other benchmarks.
13 unmodified lines14151617181911 unmodified lines3132333435362 unmodified lines394041424344454647484916 unmodified lines6667686970717273747576771 unmodified line798081828384858687881 unmodified line909192939495969798991001011021031041051061071081091101111121136 unmodified lines1201211221231241251261271281291301311321332 unmodified lines1361371381391401411421431441451461471481491501511521531541551561571 unmodified line15916016116216316416516616716816912 unmodified lines18218318418518618718827 unmodified lines21621721821922022122222322422552 unmodified lines27827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136235 unmodified lines3983994004014024032 unmodified lines4064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384399 unmodified lines44945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853963 unmodified lines6036046056066076086096106116126136141 unmodified line61661761861962062162262362462562662713 unmodified linestarget_label="RMSE Target Level",title="Optimization Leaderboard",state_prefix="opt",raw_page="pages/RawData.py",)"""11 unmodified lines"#eeca3b", "#b279a2", "#ff9da6", "#9d755d", "#bab0ac",]def render_leaderboard(metric_store: pd.DataFrame,2 unmodified linestarget_label: str,title: str,state_prefix: str,raw_page: str | None = None,show_failure_panel: bool = False,show_scoring_modes: bool = True,canonical_target_levels: list[float] | None = None,budget_store: pd.DataFrame | None = None,) -> None:"""Render a scored leaderboard backed by *metric_store*.16 unmodified linesShort string used to namespace ``st.session_state`` keys so multipleleaderboard pages keep independent control state. Use ``"opt"`` forthe Optimization leaderboard and ``"uq"`` for the UQ leaderboard.raw_page:Optional Streamlit page path for an "Open Raw Data" link shown at thebottom. Pass ``None`` to suppress the link.show_failure_panel:If ``True``, render a grouped-bar failure-rate chart below the mainperformance chart.canonical_target_levels:If provided, the target-level selector always offers exactly thesevalues (as strings) regardless of what is present in the data. Use1 unmodified linelevels even when some have 100 % failure.budget_store:Optional DataFrame produced by ``load_uq_budget_store()``. Whenprovided, an additional "Budget and Iterations for Coverage" section isrendered below the main performance chart, showing mean budget(N_ens·k_iter, solid lines) and mean iterations (k_iter, dashed lines)vs ensemble size, followed by a coverage-failure-rate bar chart."""st.header(title)1 unmodified linest.warning("No metric data found. Expected NetCDF files in `data/` with a `metric` variable.")return# Derived column name for the string version of the target coordinatetarget_str_col = f"{target_col}_str"benchmark_values = sorted(metric_store["benchmark"].unique().tolist())benchmark_options = ["All"] + benchmark_valuesselected_benchmark = st.selectbox("Benchmark", options=benchmark_options, index=0)filtered = (metric_store.copy()if selected_benchmark == "All"else 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 = ["All targets"] + [str(float(t)) for t in canonical_target_levels]else:target_options = ["All targets"] + sorted(metric_store[target_col].astype(str).unique().tolist())6 unmodified lines# Session-state keys namespaced by state_prefix so two leaderboard pages# don't share control state.k_target = f"{state_prefix}_selected_target"k_scoring = f"{state_prefix}_scoring_mode"k_weight = f"{state_prefix}_fwdruns_weight_percent"k_methods = f"{state_prefix}_selected_methods"current_target = st.session_state.get(k_target, "All targets")if current_target not in target_options:current_target = target_options[0]current_scoring_mode = st.session_state.get(k_scoring, "Mean Forward Model Runs")if current_scoring_mode not in scoring_options:2 unmodified linescurrent_fwdruns_weight_percent = int(st.session_state.get(k_weight, 80))current_fwdruns_weight_percent = max(0, min(100, current_fwdruns_weight_percent))selected_target = current_targetscoring_mode = current_scoring_modefwdruns_weight = current_fwdruns_weight_percent / 100.0ensemble_weight = 1.0 - fwdruns_weight# Available methods for the current benchmark selection; used to populate the# multiselect and to prune any stale saved selections when the benchmark changes.available_methods = sorted(filtered["abbreviation"].dropna().unique().tolist())saved_methods = st.session_state.get(k_methods, available_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 available_methods# Stable color scale: domain covers ALL methods so colors don't shift when a# subset is displayed.color_domain = available_methodscolor_range = [_METHOD_PALETTE[i % len(_METHOD_PALETTE)] for i in range(len(available_methods))]method_color = alt.Color("abbreviation:N",title="Method",1 unmodified line)def build_scored_table(input_df: pd.DataFrame, add_rank: bool = True) -> pd.DataFrame:ranking_source = (input_dfif selected_target == "All targets"else input_df[input_df[target_str_col] == selected_target])if ranking_source.empty:return ranking_source12 unmodified lines).agg(**{"Mean Forward Model Runs": ("metric", "mean")},**{"Minimum Forward Model Runs": ("metric", "min")},**{"Targets Used": (target_str_col, "nunique")},**{"Ensemble Sizes Used": ("ensemble_size", "nunique")},)27 unmodified lineshow="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["Minimum Forward Model Runs"] = scored_df["Minimum 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()52 unmodified linesreturn scored_dfif selected_benchmark == "All":benchmark_scores = []for benchmark_name in benchmark_values:benchmark_df = metric_store[metric_store["benchmark"] == benchmark_name].copy()benchmark_df[target_str_col] = benchmark_df[target_col].astype(str)scored = build_scored_table(benchmark_df, add_rank=False)if scored.empty:continuescored["benchmark"] = benchmark_namebenchmark_scores.append(scored)if benchmark_scores:combined_scores = (benchmark_scores[0].copy()if len(benchmark_scores) == 1else pd.concat(benchmark_scores, ignore_index=True))leaderboard_df = combined_scores.groupby(["algorithm_type", "abbreviation", "Method", "family"], as_index=False).agg(Score=("Score", "mean"),**{"Mean Forward Model Runs": ("Mean Forward Model Runs", "mean")},**{"Minimum Forward Model Runs": ("Minimum Forward Model Runs", "mean")},**{"Optimal Ensemble Size": ("Optimal Ensemble Size", "mean")},**{"Targets Used": ("Targets Used", "mean")},**{"Ensemble Sizes Used": ("Ensemble Sizes Used", "mean")},**{"Benchmarks Used": ("benchmark", "nunique")},**{"Mean Failure Rate (%)": ("Mean Failure Rate (%)", "mean")},)leaderboard_df["Mean Forward Model Runs"] = leaderboard_df["Mean Forward Model Runs"].round(4)leaderboard_df["Minimum Forward Model Runs"] = leaderboard_df["Minimum Forward Model Runs"].round(4)leaderboard_df["Optimal Ensemble Size"] = leaderboard_df["Optimal Ensemble Size"].round(2)leaderboard_df["Targets Used"] = leaderboard_df["Targets Used"].round().astype(int)leaderboard_df["Ensemble Sizes Used"] = leaderboard_df["Ensemble Sizes Used"].round().astype(int)leaderboard_df["Mean Failure Rate (%)"] = leaderboard_df["Mean Failure Rate (%)"].round(1)leaderboard_df = leaderboard_df.sort_values(["Score", "Mean Forward Model Runs", "abbreviation"], ascending=[False, True, True]).reset_index(drop=True)leaderboard_df["Rank"] = leaderboard_df.index + 1leaderboard_df["Placement"] = leaderboard_df["Rank"].apply(lambda rank: f"{ {1: '🥇', 2: '🥈', 3: '🥉'}.get(rank, '')} #{rank}".strip())else:leaderboard_df = pd.DataFrame(columns=["Placement","abbreviation","Method","family","Mean Forward Model Runs","Minimum Forward Model Runs","Score","Optimal Ensemble Size","Targets Used","Ensemble Sizes Used","Benchmarks Used","Mean Failure Rate (%)",])else:leaderboard_df = build_scored_table(filtered, add_rank=True)leaderboard_df["Benchmarks Used"] = 1if scoring_mode == "Mean Forward Model Runs":score_basis = "normalized mean of best forward model runs over selected target levels (lower is better)"elif scoring_mode == "Minimum Forward Model Runs":score_basis = "normalized minimum of forward model runs over selected targets and ensemble sizes (lower is better)"elif scoring_mode == "Smallest Optimal Ensemble Size":score_basis = "normalized mean optimal ensemble size over selected target levels (lower is better)"else:score_basis = ("weighted blend of normalized forward-model-runs score and normalized ensemble-size score "f"(forward-runs weight {fwdruns_weight:.0%}, ensemble-size weight {ensemble_weight:.0%})")if selected_benchmark == "All":score_basis = f"{score_basis}; in All mode, each method's final score is the mean of its per-benchmark scores"# Controls expander — always shown so users can change target even when the# current selection yields all failures.with st.expander("Scoring & Target Controls", expanded=False):35 unmodified linesif not selected_methods:selected_methods = available_methodsif leaderboard_df.empty:st.warning("All runs failed to reach the target at this selection. "2 unmodified lineselse "No rows available for the current benchmark/target selection.")else:if selected_benchmark == "All":table_column_order = ["Placement","abbreviation","Method","family","Score","Mean Failure Rate (%)","Targets Used","Ensemble Sizes Used","Benchmarks Used",]else:table_column_order = ["Placement","abbreviation","Method","family","Score","Mean Forward Model Runs","Minimum Forward Model Runs","Mean Failure Rate (%)","Optimal Ensemble Size","Targets Used","Ensemble Sizes Used","Benchmarks Used",]st.subheader(f"Ranked Leaderboard — {selected_benchmark}")st.dataframe(leaderboard_df,9 unmodified lines"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"),"Targets Used": st.column_config.NumberColumn("Targets Used", format="%d"),"Ensemble Sizes Used": st.column_config.NumberColumn("Ensemble Sizes Used", format="%d"),"Benchmarks Used": st.column_config.NumberColumn("Benchmarks Used", format="%d"),},column_order=table_column_order,)st.info(f"Score is a normalized 0–100 ranking based on **{score_basis}**. ""For Mean/Minimum forward-runs scoring, values are computed from all selected metric target levels ""and all ensemble sizes after averaging over random seeds.")if selected_benchmark != "All":st.subheader("Mean Forward Model Runs vs Ensemble Size")chart_source = (filteredif selected_target == "All targets"else 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 []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.emptychart_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=method_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_failedelse 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=method_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)"),],))if chart_layers:st.altair_chart(alt.layer(*chart_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 and selected_benchmark != "All":bf = budget_store[budget_store["benchmark"] == selected_benchmark].copy()bf[target_str_col] = bf[target_col].astype(str)if selected_target != "All targets":bf = bf[bf[target_str_col] == selected_target]bf = bf[bf["abbreviation"].isin(selected_methods)]iters_df = (63 unmodified linesst.altair_chart(alt.layer(*iters_layers), use_container_width=True)# Failure panel — rendered regardless of whether the scored table has rowsif show_failure_panel and selected_benchmark != "All":failure_source = (filteredif selected_target == "All targets"else 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(1 unmodified line).agg(mean_failure_rate=("failure_rate", "mean"))failure_df = failure_df.sort_values("ensemble_size")target_str = ("All Targets"if selected_target == "All targets"else f"Target {selected_target}")st.subheader(f"Failure Rate of Hitting {target_str}")ens_ticks_fail = sorted(failure_df["ensemble_size"].unique().tolist())failure_chart = (alt.Chart(failure_df)13 unmodified lines1415161718192011 unmodified lines32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281292 unmodified lines13213313413513613713813914014114214314416 unmodified lines1611621631641651661671681691701711721731741751761771 unmodified line1791801811821831841851861871881891 unmodified line1911921931941951961971981992002012022032042052062072082092102112122132142156 unmodified lines2222232242252262272282292302312322332342352362372382392402 unmodified lines2432442452462472482492502512522532542552562572582592602612622631 unmodified line26526626726826927027112 unmodified lines28428528628728828927 unmodified lines31731831932032132232332432532652 unmodified lines37938038138238338438538638738838939039139239339439539639739835 unmodified lines4344354364374384394404414424434444454464474484494504514522 unmodified lines4554564574584594609 unmodified lines47047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055163 unmodified lines6156166176186196206216221 unmodified line62462562662762862963013 unmodified linestarget_label="RMSE Target Level",title="Optimization Leaderboard",state_prefix="opt",default_target=1.1,raw_page="pages/RawData.py",)"""11 unmodified lines"#eeca3b", "#b279a2", "#ff9da6", "#9d755d", "#bab0ac",]_SUITABLE = "#009E73" # Okabe-Ito teal-green (colorblind-safe)_UNSUITABLE = "#C0392B" # dark red_UNTESTED = "#BDBDBD" # graydef _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 = 20.0,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,)methods = sorted(store["abbreviation"].dropna().unique().tolist())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 < thresholdglobal_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 Nonecell_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] = _UNTESTEDcontinuequalifying = sub[sub["failure_rate"] < failure_threshold].dropna(subset=["metric"])if qualifying.empty:cell_text[method][bm] = "failed"cell_color[method][bm] = _UNSUITABLEcontinuemethod_best = float(qualifying["metric"].min())gb = global_best.get(bm)ratio = (method_best / gb) if (gb is not None and gb > 0) else 1.0cell_text[method][bm] = f"{ratio:.1f}×"cell_color[method][bm] = _SUITABLE if ratio <= ratio_threshold else _UNSUITABLEcol_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] = bmdisplay_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"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 else "white"result.loc[row, col] = (f"background-color: {bg}; color: {fg}; ""text-align: center; font-weight: bold")return resultst.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**: does not meet criteria. ""**Gray**: no data for this experiment.")st.dataframe(display_df.style.apply(_style, axis=None), use_container_width=True)def render_leaderboard(metric_store: pd.DataFrame,2 unmodified linestarget_label: str,title: str,state_prefix: str,default_target: float,raw_page: str | None = None,show_failure_panel: bool = False,show_scoring_modes: bool = True,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*.16 unmodified linesShort string used to namespace ``st.session_state`` keys so multipleleaderboard pages keep independent control state. Use ``"opt"`` forthe 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 thebottom. Pass ``None`` to suppress the link.show_failure_panel:If ``True``, render a grouped-bar failure-rate chart below the mainperformance chart.show_scoring_modes:If ``True``, show scoring-mode radio controls in the expander.canonical_target_levels:If provided, the target-level selector always offers exactly thesevalues (as strings) regardless of what is present in the data. Use1 unmodified linelevels even when some have 100 % failure.budget_store:Optional DataFrame produced by ``load_uq_budget_store()``. Whenprovided, an additional "Mean Iterations for Coverage" section isrendered 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)1 unmodified linest.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 coordinatetarget_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())6 unmodified lines# Session-state keys namespaced by state_prefix so two leaderboard pages# don't share control state.k_target = f"{state_prefix}_selected_target"k_scoring = f"{state_prefix}_scoring_mode"k_weight = f"{state_prefix}_fwdruns_weight_percent"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]current_scoring_mode = st.session_state.get(k_scoring, "Mean Forward Model Runs")if current_scoring_mode not in scoring_options:2 unmodified linescurrent_fwdruns_weight_percent = int(st.session_state.get(k_weight, 80))current_fwdruns_weight_percent = max(0, min(100, current_fwdruns_weight_percent))scoring_mode = current_scoring_modefwdruns_weight = current_fwdruns_weight_percent / 100.0ensemble_weight = 1.0 - fwdruns_weight# Available methods for the current benchmark selection; used to populate the# multiselect and to prune any stale saved selections when the benchmark changes.available_methods = sorted(filtered["abbreviation"].dropna().unique().tolist())saved_methods = st.session_state.get(k_methods, available_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 available_methods# Stable color scale: domain covers ALL methods so colors don't shift when a# subset is displayed.color_domain = available_methodscolor_range = [_METHOD_PALETTE[i % len(_METHOD_PALETTE)] for i in range(len(available_methods))]method_color = alt.Color("abbreviation:N",title="Method",1 unmodified line)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_source12 unmodified lines).agg(**{"Mean Forward Model Runs": ("metric", "mean")},**{"Minimum Forward Model Runs": ("metric", "min")},**{"Ensemble Sizes Used": ("ensemble_size", "nunique")},)27 unmodified lineshow="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["Minimum Forward Model Runs"] = scored_df["Minimum 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()52 unmodified linesreturn scored_dfleaderboard_df = build_scored_table(filtered, add_rank=True)if scoring_mode == "Mean Forward Model Runs":score_basis = "mean forward-model runs at the selected target level (lower is better)"elif scoring_mode == "Minimum Forward Model Runs":score_basis = "minimum forward-model runs at the selected target level (lower is better)"elif scoring_mode == "Smallest Optimal Ensemble Size":score_basis = "mean optimal ensemble size at the selected target level (lower is better)"else:score_basis = (f"weighted blend of normalized forward-model-runs score ({fwdruns_weight:.0%}) "f"and normalized ensemble-size score ({ensemble_weight:.0%})")# Controls expander — always shown so users can change target even when the# current selection yields all failures.with st.expander("Scoring & Target Controls", expanded=False):35 unmodified linesif not selected_methods:selected_methods = available_methodstable_column_order = ["Placement","abbreviation","Method","family","Score","Mean Forward Model Runs","Minimum 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. "2 unmodified lineselse "No rows available for the current benchmark/target selection.")else:st.subheader(f"Ranked Leaderboard — {selected_benchmark}")st.dataframe(leaderboard_df,9 unmodified lines"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.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 []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.emptychart_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=method_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_failedelse 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=method_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)"),],))if chart_layers:st.altair_chart(alt.layer(*chart_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 = (63 unmodified linesst.altair_chart(alt.layer(*iters_layers), use_container_width=True)# Failure panel — rendered regardless of whether the scored table has rowsif 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(1 unmodified line).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)
86 unmodified lines87888990919286 unmodified lines# Budget = N_ens · k_iter where first k s.t. |S(q)−q| ≤ c·√(q(1−q)/N_y) for ALL q below.UQ_COVERAGE_QUANTILES: list[float] = [0.15, 0.5, 0.85]# ---------------------------------------------------------------------------86 unmodified lines8788899091929394959697989910010186 unmodified lines# Budget = N_ens · k_iter where first k s.t. |S(q)−q| ≤ c·√(q(1−q)/N_y) for ALL q below.UQ_COVERAGE_QUANTILES: list[float] = [0.15, 0.5, 0.85]# Physical dimensions of each benchmark: (param_dim, state_dim, output_dim).# Used only for column-header annotations in the suitability table.BENCHMARK_DIMS: dict[str, tuple[int, int, int]] = {"L63": (2, 3, 9),"L96": (1, 40, 80),"L96_NN_FORCING": (61, 100, 200),"L96_SPATIAL_FORCING": (40, 40, 80),}# ---------------------------------------------------------------------------
3 unmodified lines456789101112131416 unmodified lines31323334353 unmodified linesimport streamlit as sttry:from data_store import load_metric_storefrom common.leaderboard import render_leaderboardexcept ModuleNotFoundError:sys.path.append(str(Path(__file__).resolve().parents[1]))from data_store import load_metric_storefrom common.leaderboard import render_leaderboardst.set_page_config(page_title="Optimization Leaderboard", page_icon="📊", layout="wide")16 unmodified linestarget_label="RMSE Target Level",title="Optimization Leaderboard",state_prefix="opt",raw_page="pages/RawData.py",)3 unmodified lines456789101112131416 unmodified lines313233343536373 unmodified linesimport streamlit as sttry:from data_store import load_metric_store, BENCHMARK_DIMSfrom common.leaderboard import render_leaderboardexcept ModuleNotFoundError:sys.path.append(str(Path(__file__).resolve().parents[1]))from data_store import load_metric_store, BENCHMARK_DIMSfrom common.leaderboard import render_leaderboardst.set_page_config(page_title="Optimization Leaderboard", page_icon="📊", layout="wide")16 unmodified linestarget_label="RMSE Target Level",title="Optimization Leaderboard",state_prefix="opt",default_target=1.1,raw_page="pages/RawData.py",benchmark_dims=BENCHMARK_DIMS,)
3 unmodified lines456789101112131418 unmodified lines33343536373839403 unmodified linesimport streamlit as sttry:from data_store import load_uq_store, load_uq_budget_store, UQ_TARGET_LEVELSfrom common.leaderboard import render_leaderboardexcept ModuleNotFoundError:sys.path.append(str(Path(__file__).resolve().parents[1]))from data_store import load_uq_store, load_uq_budget_store, UQ_TARGET_LEVELSfrom common.leaderboard import render_leaderboardst.set_page_config(page_title="UQ Leaderboard", page_icon="🎯", layout="wide")18 unmodified linestarget_label="UQ Target (coverage tolerance scaling)",title="Uncertainty Quantification Leaderboard",state_prefix="uq",show_failure_panel=True,show_scoring_modes=False,canonical_target_levels=UQ_TARGET_LEVELS,budget_store=load_uq_budget_store(),)3 unmodified lines456789101112131418 unmodified lines333435363738394041423 unmodified linesimport streamlit as sttry:from data_store import load_uq_store, load_uq_budget_store, UQ_TARGET_LEVELS, BENCHMARK_DIMSfrom common.leaderboard import render_leaderboardexcept ModuleNotFoundError:sys.path.append(str(Path(__file__).resolve().parents[1]))from data_store import load_uq_store, load_uq_budget_store, UQ_TARGET_LEVELS, BENCHMARK_DIMSfrom common.leaderboard import render_leaderboardst.set_page_config(page_title="UQ Leaderboard", page_icon="🎯", layout="wide")18 unmodified linestarget_label="UQ Target (coverage tolerance scaling)",title="Uncertainty Quantification Leaderboard",state_prefix="uq",default_target=1.5,show_failure_panel=True,show_scoring_modes=False,canonical_target_levels=UQ_TARGET_LEVELS,budget_store=load_uq_budget_store(),benchmark_dims=BENCHMARK_DIMS,)
CLAUDE.md+29-2
src/common/leaderboard.py+216-213
src/data_store.py+9
src/pages/OptimizationLeaderboard.py+4-2
src/pages/UQLeaderboard.py+4-2