Skip to content

run_cell — the unit of work

worker

The cell runner — the unit of work that runs identically local / Spark / Ray.

run_cell fits, optionally backtests, and predicts ONE (ts_id, model) cell and returns a CellResult. Engines differ only in how they call it and collect its results — that symmetry is what makes "same code everywhere" real.

CellResult is defined here because it is the worker's output type; the registry writers (registry/) consume it. It carries plain data (frames + scalars), no behavior, so it is the clean seam between compute and lineage.

CellResult dataclass

CellResult(
    run_id,
    ts_id,
    model_type,
    compute_engine,
    model_hash,
    status,
    error,
    predictions,
    oof,
    metrics,
    best_params=dict(),
    fit_seconds=0.0,
    worker_id=None,
    cell_started_at=None,
    cell_ended_at=None,
    artifact_bytes=None,
    model_artifact_uri=None,
    cpu_seconds=None,
    process_rss_bytes=None,
    peak_gpu_bytes=None,
    intraop_threads=None,
    n_obs=None,
    device_requested=None,
    device_available=None,
    device_used=None,
    device_name=None,
    backtest_status=None,
    n_folds_achieved=None,
    backtest_note=None,
    backtest_refit=None,
    staleness_gap=None,
    achieved_step=None,
    achieved_min_train=None,
    first_val_date=None,
    last_val_date=None,
    error_class=None,
    interval_source=None,
    point_forecast_source=None,
    point_forecast_decision=None,
    interval_calibration=None,
    point_forecast_margin=None,
    hpo_scoring=None,
    diagnostics=dict(),
    demand_profile=None,
    explanations=None,
    n_fits=None,
    train_rows_total=None,
    n_hpo_fits=None,
)

The result of one (ts_id, model) cell.

A failing cell sets status="error" with error populated and empty predictions — it never raises out of run_cell, so one bad cell can't sink a 100k-series batch.

fit_diagnostics property

fit_diagnostics

Alias for self.diagnostics matching forecast_metadata.fit_diagnostics.

backtest_outcome

backtest_outcome(*args, **kwargs)

Shared internal helper delegating to _backtest_outcome.

Source code in src/scale_forecasting/worker.py
def backtest_outcome(*args: Any, **kwargs: Any) -> Any:
    """Shared internal helper delegating to `_backtest_outcome`."""
    return _backtest_outcome(*args, **kwargs)

classify_error

classify_error(exc)

Which ERROR_CLASSES token describes exc — "UNKNOWN" if none does (pure).

UNKNOWN is a real answer, not a failure of the table. A token invented per stack trace would make the column unqueryable, so the vocabulary stays fixed and the honest response to an unrecognised failure is to say so and leave the full text in error_detail. A rising UNKNOWN share is the signal that this table needs a row, and it is visible in one GROUP BY.

Source code in src/scale_forecasting/worker.py
def classify_error(exc: BaseException) -> str:
    """Which `ERROR_CLASSES` token describes ``exc`` — ``"UNKNOWN"`` if none does (pure).

    ``UNKNOWN`` is a real answer, not a failure of the table. A token invented per stack trace
    would make the column unqueryable, so the vocabulary stays fixed and the honest response to an
    unrecognised failure is to say so and leave the full text in ``error_detail``. A rising
    ``UNKNOWN`` share is the signal that this table needs a row, and it is visible in one GROUP BY.
    """
    names = {t.__name__ for t in type(exc).__mro__}
    text = f"{type(exc).__name__}: {exc}".lower()
    for token, types, needles in ERROR_CLASSES:
        if names.intersection(types) or any(needle in text for needle in needles):
            return token
    return "UNKNOWN"

hpo_scoring_basis

hpo_scoring_basis(n_obs, cfg, params)

Whether the search behind this cell's params reserved the newest fold (pure).

None when no search ran, which is most cells. Otherwise "holdout" when an inner fold existed for the search to score on, or "in_sample" when it did not and the search fell back to every fold — a distinction that decides whether the cell's own metrics are honest, since the search and the leaderboard would otherwise be reading the same window.

The two granularities are asked different questions. Per-series tuning searched this series, so the split exists only if this series is long enough for two folds. Fleetwide tuning searched a sample the cell never saw, so the only thing the cell can honestly report is the run's fold geometry; a short series inside that sample fell back on its own, and it is the run header's scoring block, not this column, that would show it.

Source code in src/scale_forecasting/worker.py
def hpo_scoring_basis(n_obs: int, cfg: RunConfig, params: dict[str, Any] | None) -> str | None:
    """Whether the search behind this cell's params reserved the newest fold (pure).

    ``None`` when no search ran, which is most cells. Otherwise ``"holdout"`` when an inner fold
    existed for the search to score on, or ``"in_sample"`` when it did not and the search fell
    back to every fold — a distinction that decides whether the cell's own metrics are honest,
    since the search and the leaderboard would otherwise be reading the same window.

    The two granularities are asked different questions. Per-series tuning searched *this* series,
    so the split exists only if this series is long enough for two folds. Fleetwide tuning searched
    a sample the cell never saw, so the only thing the cell can honestly report is the run's fold
    geometry; a short series inside that sample fell back on its own, and it is the run header's
    ``scoring`` block, not this column, that would show it.
    """
    if not cfg.hpo.enabled:
        return None
    if params is not None:
        return "holdout" if cfg.backtest.n_folds >= 2 else "in_sample"
    if cfg.hpo.granularity != "per_series":
        return None
    return "holdout" if achievable_folds(n_obs, cfg) >= 2 else "in_sample"

is_panel_model

is_panel_model(model_name, cfg, params=None)

Whether model_name is configured for panel training (global, hybrid, or similarity_global).

Source code in src/scale_forecasting/worker.py
def is_panel_model(model_name: str, cfg: RunConfig, params: dict[str, Any] | None = None) -> bool:
    """Whether ``model_name`` is configured for panel training (`global`, `hybrid`, or
    `similarity_global`)."""
    base_name = base_model_name(model_name)
    authored: dict[str, Any] = {}
    if base_name in cfg.model_params:
        authored.update(cfg.model_params[base_name])
    if model_name in cfg.model_params:
        authored.update(cfg.model_params[model_name])
    if params is not None:
        authored.update(params)
    if "training_mode" in authored:
        return str(authored["training_mode"]) in ("global", "hybrid", "similarity_global")
    if "@" in model_name:
        return resolve_model_variant(cfg, model_name).training_mode in (
            "global",
            "hybrid",
            "similarity_global",
        )
    return False

model_context

model_context(*args, **kwargs)

Shared internal helper delegating to _model_context.

Source code in src/scale_forecasting/worker.py
def model_context(*args: Any, **kwargs: Any) -> Any:
    """Shared internal helper delegating to `_model_context`."""
    return _model_context(*args, **kwargs)

rollup_metrics

rollup_metrics(fold_metrics)

Average the per-fold metric panels into one panel.

NaNs are ignored per metric (a metric undefined on one fold shouldn't sink the mean); a metric NaN on every fold stays NaN. Always returns the full panel.

Source code in src/scale_forecasting/worker.py
def rollup_metrics(fold_metrics: list[dict[str, float]]) -> dict[str, float]:
    """Average the per-fold metric panels into one panel.

    NaNs are ignored per metric (a metric undefined on one fold shouldn't sink the mean);
    a metric NaN on every fold stays NaN. Always returns the full panel.
    """
    out: dict[str, float] = {}
    for name in METRIC_NAMES:
        vals = np.array([fm.get(name, np.nan) for fm in fold_metrics], dtype=float)
        finite = vals[~np.isnan(vals)]
        out[name] = float(finite.mean()) if finite.size else float("nan")
    return out

run_cell

run_cell(
    series,
    model_name,
    cfg,
    params=None,
    _extra_params=None,
    *,
    future_df=None,
    cold_start_donor_pool=None,
)

Fit + (optional backtest) + predict ONE (ts_id, model) cell.

Pure-ish and deterministic: reads nothing global, writes nothing (no BQ), returns a CellResult carrying plain data. A failing cell returns status="error" and never raises, so one bad series can't sink a batch.

params are the hyperparameters this cell's model is built with. Resolution order:

  • params given (not None) → use them. This is the fleetwide path: the driver tuned the model once on a sample (resolve_fleetwide) and threads the winning params here — never through cfg (the config is the run_id identity key).
  • else if cfg.hpo.enabled and granularity == "per_series" → tune on this series now.
  • else → {} (the default: today's untuned behavior).

The resolved params drive both the backtest folds and the final fit, so best_params = model.get_params() reflects what actually ran.

Source code in src/scale_forecasting/worker.py
def run_cell(
    series: pd.DataFrame | str,
    model_name: str | pd.DataFrame,
    cfg: RunConfig | str,
    params: dict[str, Any] | RunConfig | None = None,
    _extra_params: dict[str, Any] | None = None,
    *,
    future_df: pd.DataFrame | None = None,
    cold_start_donor_pool: Any | None = None,
) -> CellResult:
    """Fit + (optional backtest) + predict ONE ``(ts_id, model)`` cell.

    Pure-ish and deterministic: reads nothing global, writes nothing (no BQ), returns a
    `CellResult` carrying plain data. A failing cell returns ``status="error"`` and
    never raises, so one bad series can't sink a batch.

    ``params`` are the hyperparameters this cell's model is built with. Resolution order:

    * ``params`` given (not None) → use them. This is the **fleetwide** path: the driver tuned the
      model once on a sample (`resolve_fleetwide`) and threads the
      winning params here — never through ``cfg`` (the config is the run_id identity key).
    * else if ``cfg.hpo.enabled`` and ``granularity == "per_series"`` → tune on *this* series now.
    * else → ``{}`` (the default: today's untuned behavior).

    The resolved params drive **both** the backtest folds and the final fit, so
    ``best_params = model.get_params()`` reflects what actually ran.
    """
    explicit_ts_id: str | None = None
    if isinstance(series, str) and isinstance(model_name, pd.DataFrame):
        explicit_ts_id = series
        series = model_name
        model_name = str(cfg)
        assert isinstance(params, RunConfig)
        cfg = params
        params = _extra_params
    assert isinstance(series, pd.DataFrame)
    assert isinstance(model_name, str)
    assert isinstance(cfg, RunConfig)
    assert params is None or isinstance(params, dict)
    if explicit_ts_id is not None:
        if not series.empty and cfg.data.ts_id_col not in series.columns:
            series = series.assign(**{cfg.data.ts_id_col: explicit_ts_id})
        elif series.empty and cfg.data.ts_id_col not in series.columns:
            series = series.copy()
            series[cfg.data.ts_id_col] = pd.Series(dtype=object)
        series.attrs["ts_id"] = explicit_ts_id
    ts_id = explicit_ts_id if explicit_ts_id is not None else _ts_id(series, cfg)
    run_id = make_run_id(cfg)
    model_hash = make_model_hash(run_id, ts_id, model_name, cfg)
    # Wall-clock lane + worker identity for the trace, captured for every return path (ok or error).
    cell_started_at = datetime.now(UTC)
    worker_id = _worker_id()
    # What this worker can see, memoized per process. Read up here so an error cell carries it too:
    # a cell that failed *because* the accelerator was missing is the row most worth the evidence.
    available, device_name = visible_device()
    # Two tallies, one question each: what the published forecast cost, and what a per-series
    # search cost on top of it. Created up here so the error path below can report them — a cell
    # that died after forty fits burned forty fits, and that is the row worth finding.
    fits, hpo_fits = FitTally(), FitTally()

    def _error(exc: BaseException, engine: str) -> CellResult:
        return CellResult(
            run_id=run_id,
            ts_id=ts_id,
            model_type=model_name,
            compute_engine=engine,
            model_hash=model_hash,
            status="error",
            error=repr(exc),
            # Classified here rather than by a reader after the fact, because this is the only
            # place the exception object still exists: `error` is a string by the time it leaves
            # the worker, and the type is the strongest signal the table has.
            error_class=classify_error(exc),
            predictions=_empty_predictions(),
            oof=None,
            metrics={name: float("nan") for name in METRIC_NAMES},
            worker_id=worker_id,
            cell_started_at=cell_started_at,
            cell_ended_at=datetime.now(UTC),
            device_available=available,
            device_name=device_name,
            n_fits=fits.n_fits,
            train_rows_total=fits.train_rows,
            n_hpo_fits=hpo_fits.n_fits,
        )

    try:
        model_cls = get_model(model_name)
    except Exception as e:  # noqa: BLE001 - unknown model name → error cell, engine unknown
        return _error(e, cfg.python_runtime)

    engine = _compute_engine(model_cls, cfg)
    # Harvest: record what this fit costs so a later run can be sized from it. Three cheap probes
    # around work the run was doing anyway — no sample, no pre-pass. Neither RSS nor the CUDA
    # high-water is reset first: the absolute peak is the number that sizes a slot (see
    # `CellResult.process_rss_bytes`), and resetting would also perturb whatever else shares this
    # worker. `intraop_threads` is captured before the fit because that is when it is in force.
    measuring = cfg.compute.profile.records_measurements
    intraop_threads = _intraop_threads() if measuring else None
    cpu_started = time.process_time()
    started = time.perf_counter()
    donor_pool = (
        cold_start_donor_pool
        if cold_start_donor_pool is not None
        else series.attrs.get("cold_start_donor_pool")
    )
    raw_input_series = series
    try:
        try:
            series = regularize_series_grid(series, cfg)
        except Exception:
            eff_pre = effective_config_for_model(cfg, model_cls, model_name)
            if not eff_pre.features.cold_start_transfer:
                raise
            series = raw_input_series.copy()
        fit_model_name = model_name
        if cfg.features.regime_routing and not series.empty:
            compat = select_models_for_series(series, cfg)
            if compat and model_name not in compat:
                fit_model_name = compat[0]
                model_cls = get_model(fit_model_name)
        model_cfg = effective_config_for_model(cfg, model_cls, fit_model_name)
        raw_target_series = (
            _target(series, model_cfg)
            if (not series.empty and model_cfg.data.target_col in series.columns)
            else pd.Series([], dtype=float)
        )
        n_valid_obs = int(raw_target_series.notna().sum())
        is_cold_start_cell = bool(
            model_cfg.features.cold_start_transfer
            and n_valid_obs < model_cfg.features.cold_start_min_obs
        )
        demand_prof = compute_demand_profile(
            raw_target_series.dropna().to_numpy(dtype=float),
            min_cold_start_obs=model_cfg.features.cold_start_min_obs,
        )

        oof: pd.DataFrame | None = None
        metrics = {name: float("nan") for name in METRIC_NAMES}
        backtest_status: str | None = None
        n_folds_achieved: int | None = None
        backtest_note: str | None = None
        backtest_refit: str | None = None
        staleness_gap: float | None = None
        achieved_step: int | None = None
        achieved_min_train: int | None = None
        first_val_date: date | None = None
        last_val_date: date | None = None
        eff_qs = cfg.output.effective_quantiles
        metric = cfg.backtest.decision_metric
        corrected = corrected_arm_for(metric)
        arm = corrected if cfg.output.point_forecast is None else str(cfg.output.point_forecast)
        if arm == "auto":
            arm = corrected
        arm_decision = "configured"
        interval_calibration = "in-sample"
        arm_comparison: dict[str, Any] = {}
        artifact_bytes: bytes | None = None
        explanations: list[dict[str, Any] | None] | None = None
        best_params_out: dict[str, Any] = {}
        dev_used: str | None = "cpu"
        diag: dict[str, Any] = {}
        future_exog: pd.DataFrame | None = None
        predictions: pd.DataFrame | None = None

        if n_valid_obs >= 2 or not is_cold_start_cell:
            try:
                lam = fit_transform_lambda(
                    raw_target_series,
                    model_cfg.features.transform,
                    simplex_sum=model_cfg.features.simplex_sum,
                )
                static_covs = extract_static_covariates(series, model_cfg) or None
                ctx = _model_context(
                    model_cfg,
                    transform_lambda=lam,
                    family=model_cls.family,
                    static_covariates=static_covs,
                )
                _require_device(ctx.device, model_cls.family, engine)
                resolved = _resolve_params(series, fit_model_name, model_cfg, ctx, params, hpo_fits)

                if model_cfg.backtest.enabled:
                    try:
                        oof, fold_metrics, bt = backtest_cell(
                            series,
                            lambda: model_cls(resolved, ctx),
                            model_cfg,
                            lam,
                            fits,
                            model_cls.lags_covariates_internally,
                        )
                        metrics = rollup_metrics(fold_metrics)
                        n_folds_achieved = len(fold_metrics)
                        backtest_status, backtest_note = _backtest_outcome(
                            n_folds_achieved, model_cfg, series
                        )
                        backtest_refit, staleness_gap = bt.refit_mode, bt.staleness_gap
                        achieved_step, achieved_min_train = bt.achieved_step, bt.achieved_min_train
                        first_val_date, last_val_date = bt.first_val_date, bt.last_val_date
                        if n_folds_achieved == 0:
                            oof = None
                    except Exception as e:  # noqa: BLE001 - scoring is not the forecast; degrade
                        _log.warning(
                            "backtest failed for %s/%s (forecast unaffected): %r",
                            ts_id,
                            model_name,
                            e,
                        )
                        oof = None
                        metrics = {name: float("nan") for name in METRIC_NAMES}
                        backtest_status, n_folds_achieved, backtest_note = "failed", 0, repr(e)
                        backtest_refit, staleness_gap = None, None
                        achieved_step, achieved_min_train = None, None
                        first_val_date, last_val_date = None, None

                y, X = build_features(series, model_cfg, lam, model_cls.lags_covariates_internally)
                model = model_cls(resolved, ctx)
                model.fit(y, X)
                fits.record(len(y))
                future_exog = build_future_features(y, X, model_cfg, future_covariates_df=future_df)
                predictions = model.predict(model_cfg.data.horizon, future_exog, quantiles=eff_qs)
                explanations = _collect_explanations(
                    model, model_cfg.data.horizon, future_exog, ts_id, model_name
                )
                arm, arm_decision = resolve_arm(cfg.output.point_forecast, oof, metric, corrected)
                cal = (
                    calibrate_from_oof(
                        oof,
                        eff_qs,
                        method=model_cfg.output.calibration_method,
                        aci_gamma=model_cfg.output.aci_gamma,
                    )
                    if oof is not None
                    else None
                )
                predictions, interval_calibration = apply_calibration(predictions, cal, arm)
                arm_comparison = compare_arms(oof, metric, corrected) if oof is not None else {}
                if cfg.compute.persist_models:
                    try:
                        artifact_bytes = model.serialize()
                    except Exception as e:  # noqa: BLE001
                        _log.warning("serialize failed for %s/%s: %r", ts_id, model_name, e)
                best_params_out = model.get_params()
                dev_used = model.device_used()
                diag = _collect_diagnostics(model, ts_id, model_name)
            except Exception:
                if not is_cold_start_cell:
                    raise
                predictions = None
        else:
            ctx = _model_context(model_cfg, family=model_cls.family)

        if is_cold_start_cell:
            from .cold_start import forecast_cold_start_series

            hier_cols_cs = (
                [c for lvl in model_cfg.hierarchy.levels for c in lvl]
                if model_cfg.hierarchy.enabled
                else []
            )
            cs_res = forecast_cold_start_series(
                raw_input_series if series.empty else series,
                donor_pool,
                horizon=model_cfg.data.horizon,
                freq=model_cfg.data.freq,
                method=model_cfg.features.cold_start_method,
                matching=model_cfg.features.cold_start_matching,
                similarity_method=model_cfg.features.similarity_method,
                min_obs=model_cfg.features.cold_start_min_obs,
                ts_id_col=model_cfg.data.ts_id_col,
                date_col=model_cfg.data.date_col,
                target_col=model_cfg.data.target_col,
                static_covariates=model_cfg.features.static_covariates,
                hierarchy_cols=hier_cols_cs,
                quantiles=eff_qs,
                base_predictions=predictions,
            )
            predictions = cs_res.predictions
            interval_calibration = "cold-start-pooled"
            cs_diag = dict(cs_res.diagnostics)
            cs_diag.setdefault("is_cold_start", True)
            diag["cold_start"] = cs_diag
            diag["cold_start_transfer"] = cs_diag
            if model_cfg.features.cold_start_method == "arps_decline" and cs_res.lifecycle_params:
                diag["arps_decline"] = dict(cs_res.lifecycle_params)
        elif model_cfg.features.cold_start_method == "arps_decline" and n_valid_obs >= 2:
            from .cold_start import fit_arps_decline

            _, arps_p = fit_arps_decline(
                raw_target_series.dropna().to_numpy(dtype=float),
                model_cfg.data.horizon,
            )
            diag["arps_decline"] = arps_p

        if raw_input_series.attrs.get("supersession_info"):
            diag["supersession"] = dict(raw_input_series.attrs["supersession_info"])

        assert predictions is not None
        predictions, arm_override = apply_quantile_and_cost_rules(predictions, cfg.output)
        if arm_override is not None:
            arm = arm_override
            arm_decision = "configured"
        if has_output_constraints(cfg.output):
            bounds_src = (
                future_exog if future_exog is not None and not future_exog.empty else series
            )
            predictions = apply_output_constraints(predictions, cfg.output, bounds_df=bounds_src)

        if should_stamp_variant_diagnostics(model_name, cfg):
            diag.update(variant_diagnostics_for(model_name, cfg, model_cls=model_cls))
        if cfg.features.regime_routing:
            diag["demand_profile"] = demand_prof
            if fit_model_name != model_name:
                diag["regime_routing"] = {
                    "demand_class": demand_prof["demand_class"],
                    "routed_to": fit_model_name,
                }

        fit_seconds = time.perf_counter() - started
        cpu_seconds = time.process_time() - cpu_started
        return CellResult(
            run_id=run_id,
            ts_id=ts_id,
            model_type=model_name,
            compute_engine=engine,
            model_hash=model_hash,
            status="ok",
            error=None,
            predictions=predictions,
            oof=oof,
            metrics=metrics,
            best_params=best_params_out,
            fit_seconds=fit_seconds,
            worker_id=worker_id,
            cell_started_at=cell_started_at,
            cell_ended_at=datetime.now(UTC),
            artifact_bytes=artifact_bytes,
            cpu_seconds=cpu_seconds if measuring else None,
            process_rss_bytes=_process_rss_bytes() if measuring else None,
            peak_gpu_bytes=_peak_gpu_bytes(),
            intraop_threads=intraop_threads,
            n_obs=len(series) if measuring else None,
            device_requested=ctx.device,
            device_available=available,
            device_used=dev_used,
            device_name=device_name,
            backtest_status=backtest_status,
            n_folds_achieved=n_folds_achieved,
            backtest_note=backtest_note,
            backtest_refit=backtest_refit,
            staleness_gap=staleness_gap,
            achieved_step=achieved_step,
            achieved_min_train=achieved_min_train,
            first_val_date=first_val_date,
            last_val_date=last_val_date,
            interval_source="native" if model_cls.supports_native_intervals else "residual",
            point_forecast_source=arm,
            point_forecast_decision=arm_decision,
            interval_calibration=interval_calibration,
            point_forecast_margin=arm_comparison.get("margin"),
            hpo_scoring=hpo_scoring_basis(len(series), cfg, params),
            diagnostics=diag,
            demand_profile=demand_prof,
            explanations=explanations,
            n_fits=fits.n_fits,
            train_rows_total=fits.train_rows,
            n_hpo_fits=hpo_fits.n_fits,
        )
    except Exception as e:  # noqa: BLE001 - any failure → error cell, batch survives
        return _error(e, engine)

run_panel

run_panel(
    panel, model_name, cfg, params=None, *, future_df=None
)

Fit + (optional panel backtest) + predict ONE global/hybrid/similarity_global model across a panel.

Returns one CellResult per distinct ts_id in panel, preserving the exact per-series schema and lineage contract (forecasts, backtest_oof, forecast_metadata) as run_cell. Never raises out of run_panel — an unhandled failure produces status="error" cells.

Source code in src/scale_forecasting/worker.py
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
def run_panel(
    panel: pd.DataFrame,
    model_name: str,
    cfg: RunConfig,
    params: dict[str, Any] | None = None,
    *,
    future_df: pd.DataFrame | None = None,
) -> list[CellResult]:
    """Fit + (optional panel backtest) + predict ONE global/hybrid/similarity_global model across
    a panel.

    Returns one `CellResult` per distinct ``ts_id`` in ``panel``, preserving the exact per-series
    schema and lineage contract (`forecasts`, `backtest_oof`, `forecast_metadata`) as `run_cell`.
    Never raises out of `run_panel` — an unhandled failure produces `status="error"` cells.
    """
    from .backtest import OOF_COLUMNS, make_folds
    from .metrics import compute_metrics
    from .seasonality import seasonal_period

    id_col = cfg.data.ts_id_col
    date_col = cfg.data.date_col
    target_col = cfg.data.target_col
    run_id = make_run_id(cfg)
    cell_started_at = datetime.now(UTC)
    worker_id = _worker_id()
    available, device_name = visible_device()
    supersession_chains: dict[str, Any] = {}
    if cfg.features.supersession_map and not panel.empty and id_col in panel.columns:
        from .cold_start import apply_supersession_map

        panel = apply_supersession_map(
            panel,
            cfg.features.supersession_map,
            ts_id_col=id_col,
            date_col=date_col,
            target_col=target_col,
        )
        supersession_chains = dict(panel.attrs.get("supersession_chains") or {})
    series_frames: dict[str, pd.DataFrame] = {}
    for raw_uid, sub in panel.groupby(id_col, sort=True):
        reg_sub = regularize_series_grid(sub, cfg).sort_values(date_col).reset_index(drop=True)
        reg_sub.attrs = {}
        series_frames[str(raw_uid)] = reg_sub
    future_frames: dict[str, pd.DataFrame] = {}
    if future_df is not None and not future_df.empty and id_col in future_df.columns:
        for raw_uid, f_sub in future_df.groupby(id_col, sort=False):
            future_frames[str(raw_uid)] = f_sub.sort_values(date_col).reset_index(drop=True)
    uids = sorted(series_frames.keys())
    if not uids:
        return []

    def _panel_errors(exc: BaseException, engine: str, n_fits: int = 0) -> list[CellResult]:
        ended = datetime.now(UTC)
        return [
            CellResult(
                run_id=run_id,
                ts_id=uid,
                model_type=model_name,
                compute_engine=engine,
                model_hash=make_model_hash(run_id, uid, model_name, cfg),
                status="error",
                error=repr(exc),
                error_class=classify_error(exc),
                predictions=_empty_predictions(),
                oof=None,
                metrics={name: float("nan") for name in METRIC_NAMES},
                worker_id=worker_id,
                cell_started_at=cell_started_at,
                cell_ended_at=ended,
                device_available=available,
                device_name=device_name,
                n_fits=n_fits,
                train_rows_total=0,
                n_hpo_fits=0,
            )
            for uid in uids
        ]

    try:
        model_cls = get_model(model_name)
    except Exception as e:  # noqa: BLE001
        return _panel_errors(e, cfg.python_runtime)

    engine = _compute_engine(model_cls, cfg)
    measuring = cfg.compute.profile.records_measurements
    intraop_threads = _intraop_threads() if measuring else None
    cpu_started = time.process_time()
    started = time.perf_counter()
    n_fits = 0
    train_rows_total = 0

    try:
        model_cfg = effective_config_for_model(cfg, model_cls, model_name)
        ctx = _model_context(model_cfg, family=model_cls.family)
        _require_device(ctx.device, model_cls.family, engine)
        variant_spec = resolve_model_variant(cfg, model_name, model_cls=model_cls)
        authored: dict[str, Any] = dict(variant_spec.clean_params)
        clean_override = clean_model_params(params) if params is not None else {}
        resolved = {**authored, **clean_override} if params is not None else authored

        is_similarity_global = variant_spec.training_mode == "similarity_global"
        if is_similarity_global:
            resolved["training_mode"] = "global"

        cluster_info_by_uid: dict[str, dict[str, Any]] = {}
        clusters_uids: dict[str, list[str]] = {"__all__": list(uids)}
        if is_similarity_global or model_cfg.features.similarity_cluster_feature:
            from .similarity import cluster_by_similarity_df

            reg_panel = pd.concat([series_frames[u] for u in uids], ignore_index=True)
            clusters_df = cluster_by_similarity_df(
                reg_panel,
                cfg=model_cfg,
                n_clusters=model_cfg.features.similarity_n_clusters,
                method=model_cfg.features.similarity_method,
                min_cluster_size=2,
                ts_id_col=id_col,
                date_col=date_col,
                target_col=target_col,
            )
            if is_similarity_global:
                clusters_uids = {}
            for _, crow in clusters_df.iterrows():
                u = str(crow["ts_id"])
                cid = str(crow["similarity_cluster_id"])
                c_int = int(crow.get("cluster_int_id", 0))
                sim_med = float(crow.get("similarity_to_medoid", 1.0))
                cluster_info_by_uid[u] = {
                    "similarity_cluster_id": cid,
                    "cluster_id": cid,
                    "cluster_int_id": c_int,
                    "cluster_medoid_ts_id": str(crow.get("cluster_medoid_ts_id", u)),
                    "similarity_to_medoid": sim_med,
                    "cluster_silhouette": float(crow.get("cluster_silhouette", 0.0)),
                    "cluster_size": int(crow.get("cluster_size", 1)),
                    "cluster_archetype": str(crow.get("cluster_archetype", "smooth_regular")),
                    "method": model_cfg.features.similarity_method,
                }
                if is_similarity_global and u in series_frames:
                    clusters_uids.setdefault(cid, []).append(u)
                if model_cfg.features.similarity_cluster_feature and u in series_frames:
                    sub_u = series_frames[u].copy()
                    sub_u["similarity_cluster_id"] = c_int
                    sub_u["similarity_to_medoid"] = sim_med
                    series_frames[u] = sub_u
            if is_similarity_global:
                for u in uids:
                    if u not in cluster_info_by_uid:
                        clusters_uids.setdefault("cluster_0", []).append(u)
                n_clust_total = len(clusters_uids)
                for u_info in cluster_info_by_uid.values():
                    u_info["n_clusters"] = n_clust_total

        lams: dict[str, float | None] = {
            uid: fit_transform_lambda(
                _target(series_frames[uid], model_cfg),
                model_cfg.features.transform,
                simplex_sum=model_cfg.features.simplex_sum,
            )
            for uid in uids
        }
        static_map: dict[str, dict[str, Any]] = {}
        for uid in uids:
            sc = extract_static_covariates(series_frames[uid], model_cfg)
            if sc:
                static_map[uid] = sc

        # Optional panel-wide backtest across folds.
        oof_by_uid: dict[str, pd.DataFrame | None] = dict.fromkeys(uids, None)
        metrics_by_uid: dict[str, dict[str, float]] = {
            uid: {name: float("nan") for name in METRIC_NAMES} for uid in uids
        }
        bt_status_by_uid: dict[str, str | None] = dict.fromkeys(uids, None)
        bt_note_by_uid: dict[str, str | None] = dict.fromkeys(uids, None)
        n_folds_by_uid: dict[str, int | None] = dict.fromkeys(uids, None)
        bt_refit_by_uid: dict[str, str | None] = dict.fromkeys(uids, None)
        ach_step_by_uid: dict[str, int | None] = dict.fromkeys(uids, None)
        ach_min_by_uid: dict[str, int | None] = dict.fromkeys(uids, None)
        first_val_by_uid: dict[str, date | None] = dict.fromkeys(uids, None)
        last_val_by_uid: dict[str, date | None] = dict.fromkeys(uids, None)

        if model_cfg.backtest.enabled:
            try:
                folds_by_uid = {uid: make_folds(len(series_frames[uid]), model_cfg) for uid in uids}
                max_folds = max((len(fl) for fl in folds_by_uid.values()), default=0)
                oof_chunks_by_uid: dict[str, list[pd.DataFrame]] = {u: [] for u in uids}
                fmetrics_by_uid: dict[str, list[dict[str, float]]] = {u: [] for u in uids}
                m_period = seasonal_period(model_cfg.data.freq)
                gap = model_cfg.backtest.gap
                bt_h = model_cfg.backtest.horizon

                for fold_idx in range(max_folds):
                    active_uids = [u for u in uids if fold_idx < len(folds_by_uid[u])]
                    if not active_uids:
                        continue
                    fold_cluster_int: dict[str, int] = {}
                    fold_sim_med: dict[str, float] = {}
                    if model_cfg.features.similarity_cluster_feature:
                        from .similarity import cluster_by_similarity_df

                        fold_train_frames = [
                            series_frames[u].iloc[: folds_by_uid[u][fold_idx].train_end]
                            for u in active_uids
                        ]
                        fold_panel = pd.concat(fold_train_frames, ignore_index=True)
                        fold_clusters = cluster_by_similarity_df(
                            fold_panel,
                            cfg=model_cfg,
                            n_clusters=model_cfg.features.similarity_n_clusters,
                            method=model_cfg.features.similarity_method,
                            min_cluster_size=2,
                            ts_id_col=id_col,
                            date_col=date_col,
                            target_col=target_col,
                        )
                        for _, frow in fold_clusters.iterrows():
                            fu = str(frow["ts_id"])
                            fold_cluster_int[fu] = int(frow.get("cluster_int_id", 0))
                            fold_sim_med[fu] = float(frow.get("similarity_to_medoid", 1.0))

                    fold_series_map: dict[str, tuple[pd.Series, pd.DataFrame | None]] = {}
                    fold_futr_map: dict[str, pd.DataFrame | None] = {}
                    fold_lams: dict[str, float | None] = {}
                    for uid in active_uids:
                        fold = folds_by_uid[uid][fold_idx]
                        sub = series_frames[uid]
                        train_slice = sub.iloc[: fold.train_end]
                        if model_cfg.features.similarity_cluster_feature:
                            train_slice = train_slice.copy()
                            train_slice["similarity_cluster_id"] = fold_cluster_int.get(uid, 0)
                            train_slice["similarity_to_medoid"] = fold_sim_med.get(uid, 1.0)
                        val_future = sub.iloc[fold.train_end : fold.val_end]
                        if model_cfg.features.past_covariates:
                            val_future = val_future.drop(
                                columns=[
                                    c
                                    for c in model_cfg.features.past_covariates
                                    if c in val_future.columns
                                ]
                            )
                        y_tr, X_tr = build_features(
                            train_slice, model_cfg, lams[uid], model_cls.lags_covariates_internally
                        )
                        fold_series_map[uid] = (y_tr, X_tr)
                        fold_futr_map[uid] = build_future_features(
                            y_tr,
                            X_tr,
                            model_cfg,
                            horizon=gap + bt_h,
                            future_covariates_df=val_future,
                        )
                        fold_lams[uid] = lams[uid]
                        train_rows_total += len(y_tr)

                    preds_map: dict[str, pd.DataFrame] = {}
                    for _cid, c_uids in sorted(clusters_uids.items()):
                        c_active = [u for u in c_uids if u in fold_series_map]
                        if not c_active:
                            continue
                        fold_model = model_cls(resolved, ctx)
                        c_static = (
                            {u: static_map[u] for u in c_active if u in static_map}
                            if static_map
                            else None
                        )
                        fold_model.fit_panel(
                            {u: fold_series_map[u] for u in c_active},
                            static_map=c_static if c_static else None,
                        )
                        n_fits += 1
                        c_preds = fold_model.predict_panel(
                            gap + bt_h,
                            {u: fold_futr_map[u] for u in c_active},
                            transform_lambdas={u: fold_lams[u] for u in c_active},
                        )
                        preds_map.update(c_preds)

                    for uid in active_uids:
                        fold = folds_by_uid[uid][fold_idx]
                        sub = series_frames[uid]
                        train_slice = sub.iloc[: fold.train_end]
                        val_slice = sub.iloc[fold.val_start : fold.val_end]
                        pred = preds_map[uid].iloc[gap:].reset_index(drop=True)
                        y_train_raw = train_slice[target_col].to_numpy(dtype=float)
                        y_val = val_slice[target_col].to_numpy(dtype=float)
                        yhat_raw = pred["yhat_raw"].to_numpy(dtype=float)
                        yhat_adjusted = pred["yhat"].to_numpy(dtype=float)
                        yhat = (
                            yhat_raw if model_cfg.output.point_forecast == "raw" else yhat_adjusted
                        )
                        lower = pred["yhat_lower"].to_numpy(dtype=float)
                        upper = pred["yhat_upper"].to_numpy(dtype=float)
                        cutoff = pd.Timestamp(train_slice[date_col].iloc[-1]).normalize()
                        fold_oof = pd.DataFrame(
                            {
                                "ds": pd.to_datetime(val_slice[date_col]).to_numpy(),
                                "fold_id": fold.fold_id,
                                "y_true": y_val,
                                "yhat": yhat,
                                "yhat_raw": yhat_raw,
                                "yhat_adjusted": yhat_adjusted,
                                "yhat_lower": lower,
                                "yhat_upper": upper,
                                "cutoff_date": cutoff,
                                "horizon_step": np.arange(1, len(y_val) + 1),
                                "yhat_stale": np.full(len(y_val), np.nan),
                            },
                            columns=list(OOF_COLUMNS),
                        )
                        if has_output_constraints(model_cfg.output):
                            fold_oof = apply_output_constraints(
                                fold_oof, model_cfg.output, bounds_df=val_slice
                            )
                            yhat = fold_oof["yhat"].to_numpy(dtype=float)
                            lower = fold_oof["yhat_lower"].to_numpy(dtype=float)
                            upper = fold_oof["yhat_upper"].to_numpy(dtype=float)
                        fmetrics_by_uid[uid].append(
                            compute_metrics(
                                y_val,
                                yhat,
                                y_train=y_train_raw,
                                lower=lower,
                                upper=upper,
                                seasonal_period=m_period,
                            )
                        )
                        oof_chunks_by_uid[uid].append(fold_oof)

                for uid in uids:
                    sub = series_frames[uid]
                    flist = folds_by_uid[uid]
                    n_ach = len(fmetrics_by_uid[uid])
                    n_folds_by_uid[uid] = n_ach
                    metrics_by_uid[uid] = rollup_metrics(fmetrics_by_uid[uid])
                    bt_status_by_uid[uid], bt_note_by_uid[uid] = _backtest_outcome(
                        n_ach, model_cfg, sub
                    )
                    if n_ach > 0:
                        oof_by_uid[uid] = pd.concat(oof_chunks_by_uid[uid], ignore_index=True)
                        bt_refit_by_uid[uid] = "per_fold"
                        geom = resolve_geometry(len(sub), model_cfg)
                        ach_step_by_uid[uid] = geom.step
                        ach_min_by_uid[uid] = geom.min_train
                        first_val_by_uid[uid] = pd.Timestamp(
                            sub[date_col].iloc[flist[0].val_start]
                        ).date()
                        last_val_by_uid[uid] = pd.Timestamp(
                            sub[date_col].iloc[flist[-1].val_end - 1]
                        ).date()
            except Exception as e:  # noqa: BLE001
                _log.warning("panel backtest failed for %s: %r", model_name, e)
                for uid in uids:
                    bt_status_by_uid[uid] = "failed"
                    n_folds_by_uid[uid] = 0
                    bt_note_by_uid[uid] = repr(e)

        # Final fit across the full panel (or per similarity cluster).
        full_series_map: dict[str, tuple[pd.Series, pd.DataFrame | None]] = {}
        full_futr_map: dict[str, pd.DataFrame | None] = {}
        for uid in uids:
            sub = series_frames[uid]
            y_full, X_full = build_features(
                sub, model_cfg, lams[uid], model_cls.lags_covariates_internally
            )
            full_series_map[uid] = (y_full, X_full)
            full_futr_map[uid] = build_future_features(
                y_full,
                X_full,
                model_cfg,
                future_covariates_df=future_frames.get(uid),
            )
            train_rows_total += len(y_full)

        eff_qs = cfg.output.effective_quantiles
        raw_preds_map: dict[str, pd.DataFrame] = {}
        models_by_uid: dict[str, BaseModel] = {}
        first_model: BaseModel | None = None
        for _cid, c_uids in sorted(clusters_uids.items()):
            c_active = [u for u in c_uids if u in full_series_map]
            if not c_active:
                continue
            c_model = model_cls(resolved, ctx)
            c_static = (
                {u: static_map[u] for u in c_active if u in static_map} if static_map else None
            )
            c_model.fit_panel(
                {u: full_series_map[u] for u in c_active},
                static_map=c_static if c_static else None,
            )
            n_fits += 1
            c_preds = c_model.predict_panel(
                model_cfg.data.horizon,
                {u: full_futr_map[u] for u in c_active},
                quantiles=eff_qs,
                transform_lambdas={u: lams[u] for u in c_active},
            )
            raw_preds_map.update(c_preds)
            for u in c_active:
                models_by_uid[u] = c_model
            if first_model is None:
                first_model = c_model

        assert first_model is not None
        model = first_model
        total_fit_seconds = time.perf_counter() - started
        total_cpu_seconds = time.process_time() - cpu_started
        per_series_fit_s = total_fit_seconds / max(1, len(uids))
        per_series_cpu_s = total_cpu_seconds / max(1, len(uids))
        rss_bytes = _process_rss_bytes() if measuring else None
        peak_gpu = _peak_gpu_bytes()
        dev_used = model.device_used()
        best_params = model.get_params()

        metric = cfg.backtest.decision_metric
        corrected = corrected_arm_for(metric)
        ended_at = datetime.now(UTC)

        donor_pool_panel = panel.attrs.get("cold_start_donor_pool")
        if donor_pool_panel is None and model_cfg.features.cold_start_transfer:
            from .cold_start import build_cold_start_donor_pool

            donor_pool_panel = build_cold_start_donor_pool(panel, model_cfg)

        results: list[CellResult] = []
        for idx, uid in enumerate(uids):
            uid_model = models_by_uid.get(uid, model)
            oof = oof_by_uid[uid]
            arm, arm_decision = resolve_arm(cfg.output.point_forecast, oof, metric, corrected)
            cal = (
                calibrate_from_oof(
                    oof,
                    eff_qs,
                    method=model_cfg.output.calibration_method,
                    aci_gamma=model_cfg.output.aci_gamma,
                )
                if oof is not None
                else None
            )
            predictions, interval_calibration = apply_calibration(raw_preds_map[uid], cal, arm)
            uid_diag = _collect_diagnostics(uid_model, uid, model_name)
            if uid in cluster_info_by_uid:
                c_info = dict(cluster_info_by_uid[uid])
                uid_diag["similarity_clusters"] = c_info
                uid_diag["similarity_cluster_id"] = c_info["similarity_cluster_id"]
                uid_diag["similarity_to_medoid"] = c_info["similarity_to_medoid"]
            if uid in supersession_chains:
                uid_diag["supersession"] = dict(supersession_chains[uid])
            if (
                model_cfg.features.cold_start_transfer
                and len(series_frames[uid]) < model_cfg.features.cold_start_min_obs
            ):
                from .cold_start import forecast_cold_start_series

                hier_cols_cs = (
                    [c for lvl in model_cfg.hierarchy.levels for c in lvl]
                    if model_cfg.hierarchy.enabled
                    else []
                )
                cs_res = forecast_cold_start_series(
                    series_frames[uid],
                    donor_pool_panel,
                    horizon=model_cfg.data.horizon,
                    freq=model_cfg.data.freq,
                    method=model_cfg.features.cold_start_method,
                    matching=model_cfg.features.cold_start_matching,
                    similarity_method=model_cfg.features.similarity_method,
                    min_obs=model_cfg.features.cold_start_min_obs,
                    ts_id_col=model_cfg.data.ts_id_col,
                    date_col=model_cfg.data.date_col,
                    target_col=model_cfg.data.target_col,
                    static_covariates=model_cfg.features.static_covariates,
                    hierarchy_cols=hier_cols_cs,
                    quantiles=eff_qs,
                    base_predictions=predictions,
                )
                predictions = cs_res.predictions
                interval_calibration = "cold-start-pooled"
                cs_diag_panel = dict(cs_res.diagnostics)
                cs_diag_panel.setdefault("is_cold_start", True)
                uid_diag["cold_start"] = cs_diag_panel
                uid_diag["cold_start_transfer"] = cs_diag_panel
            predictions, arm_override = apply_quantile_and_cost_rules(predictions, cfg.output)
            if arm_override is not None:
                arm = arm_override
                arm_decision = "configured"
            if has_output_constraints(cfg.output):
                fut_df = full_futr_map.get(uid)
                bounds_src = (
                    fut_df if fut_df is not None and not fut_df.empty else series_frames[uid]
                )
                predictions = apply_output_constraints(
                    predictions, cfg.output, bounds_df=bounds_src
                )
            arm_comparison = compare_arms(oof, metric, corrected) if oof is not None else {}

            artifact_bytes: bytes | None = None
            if cfg.compute.persist_models and idx == 0:
                try:
                    artifact_bytes = uid_model.serialize()
                except Exception as e:  # noqa: BLE001
                    _log.warning("serialize failed for %s/%s: %r", uid, model_name, e)

            uid_demand_prof = compute_demand_profile(
                _target(series_frames[uid], model_cfg).to_numpy(dtype=float),
                min_cold_start_obs=model_cfg.features.cold_start_min_obs,
            )
            if should_stamp_variant_diagnostics(model_name, cfg):
                uid_diag.update(variant_diagnostics_for(model_name, cfg, model_cls=model_cls))
            if cfg.features.regime_routing:
                uid_diag["demand_profile"] = uid_demand_prof

            results.append(
                CellResult(
                    run_id=run_id,
                    ts_id=uid,
                    model_type=model_name,
                    compute_engine=engine,
                    model_hash=make_model_hash(run_id, uid, model_name, cfg),
                    status="ok",
                    error=None,
                    predictions=predictions,
                    oof=oof,
                    metrics=metrics_by_uid[uid],
                    best_params=uid_model.get_params() if is_similarity_global else best_params,
                    fit_seconds=per_series_fit_s,
                    worker_id=worker_id,
                    cell_started_at=cell_started_at,
                    cell_ended_at=ended_at,
                    artifact_bytes=artifact_bytes,
                    cpu_seconds=per_series_cpu_s if measuring else None,
                    process_rss_bytes=rss_bytes,
                    peak_gpu_bytes=peak_gpu,
                    intraop_threads=intraop_threads,
                    n_obs=len(series_frames[uid]) if measuring else None,
                    device_requested=ctx.device,
                    device_available=available,
                    device_used=dev_used,
                    device_name=device_name,
                    backtest_status=bt_status_by_uid[uid],
                    n_folds_achieved=n_folds_by_uid[uid],
                    backtest_note=bt_note_by_uid[uid],
                    backtest_refit=bt_refit_by_uid[uid],
                    staleness_gap=None,
                    achieved_step=ach_step_by_uid[uid],
                    achieved_min_train=ach_min_by_uid[uid],
                    first_val_date=first_val_by_uid[uid],
                    last_val_date=last_val_by_uid[uid],
                    interval_source="native" if model_cls.supports_native_intervals else "residual",
                    point_forecast_source=arm,
                    point_forecast_decision=arm_decision,
                    interval_calibration=interval_calibration,
                    point_forecast_margin=arm_comparison.get("margin"),
                    hpo_scoring=hpo_scoring_basis(len(series_frames[uid]), cfg, params),
                    diagnostics=uid_diag,
                    demand_profile=uid_demand_prof,
                    n_fits=n_fits if idx == 0 else 0,
                    train_rows_total=train_rows_total if idx == 0 else 0,
                    n_hpo_fits=0,
                )
            )
        if (
            model_cfg.features.transform in ("clr", "ilr")
            or model_cfg.features.simplex_sum is not None
        ):
            from .simplex import close_simplex_cells

            results = close_simplex_cells(results, model_cfg, history_df=panel)
        return results
    except Exception as e:  # noqa: BLE001
        return _panel_errors(e, engine, n_fits=n_fits)

worker_id

worker_id(*args, **kwargs)

Shared internal helper delegating to _worker_id.

Source code in src/scale_forecasting/worker.py
def worker_id(*args: Any, **kwargs: Any) -> Any:
    """Shared internal helper delegating to `_worker_id`."""
    return _worker_id(*args, **kwargs)