"""Optional post-processing rules (reported as ablations).""" from __future__ import annotations from typing import Dict import numpy as np import pandas as pd def updrs4_fallback( pred_df: pd.DataFrame, train_df: pd.DataFrame, target_cols, mask_cols, mode: str = "none", ) -> pd.DataFrame: """Replace updrs_4 predictions with a population median or zero. This mirrors the observation from the top solutions that updrs_4 is mostly zero and is rarely measured in controls. It is a post-processing baseline, not a model. """ if mode in {None, "none", ""}: return pred_df if mode not in {"median", "zero"}: raise ValueError(f"Unknown updrs_4 fallback: {mode}") out = pred_df.copy() for tcol, mcol in zip(target_cols, mask_cols): if not tcol.startswith("y_updrs_4_"): continue horizon = int(tcol.rsplit("_", 1)[1]) if mode == "zero": value = 0.0 else: vals = train_df.loc[train_df[mcol] == 1, tcol].dropna() value = float(vals.median()) if len(vals) else 0.0 mask = (out["target"] == "updrs_4") & (out["horizon"] == horizon) out.loc[mask, "y_pred"] = value return out