File size: 11,849 Bytes
8b97eb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""eval_formula.py — the shared evaluation core.

Runs ONE formula module over the test set and returns raw metrics:
per-cluster (Type II) or on the flat test set (Type I). This is the common
engine called by evaluate_numeric.py (on each reference baseline and on a
submitted formula.py).

A formula module must expose the v2 contract:
  USED_INPUTS, LAW_CONSTANTS, OTHER_CONSTANTS, LOCAL_FITTABLE,
  predict(X, **params)   and (Type II) fit(X_fit, y_fit, **law_constants).

`run_formula` performs NO score normalisation and NO judging — it only
executes the formula and measures error. The reference-relative score
and the scoring channel lives in evaluate_numeric.py.
"""
from __future__ import annotations

import csv
import random
import signal
import time
from pathlib import Path

import numpy as np


# --------------------------------------------------------------------------
# data loading
# --------------------------------------------------------------------------

def load_csv(path: Path) -> tuple[list[str], list[list[str]]]:
    with path.open("r", newline="") as fh:
        reader = csv.reader(fh)
        header = next(reader)
        rows = [list(r) for r in reader]
    return header, rows


def group_by_cluster(header: list[str], rows: list[list[str]]) -> dict[int, list[list[str]]]:
    gid = header.index("group_id")
    out: dict[int, list[list[str]]] = {}
    for r in rows:
        out.setdefault(int(float(r[gid])), []).append(r)
    return out


def load_clusters(task_root: Path) -> dict:
    """Type II loader. Load test_fit.csv + test_test.csv, grouped by cluster.

    Returns a dict with: fit_header, test_header, fit_by_cluster,
    test_by_cluster, cluster_ids (sorted, present in both).
    """
    fit_header,  fit_rows  = load_csv(task_root / "data" / "test_fit.csv")
    test_header, test_rows = load_csv(task_root / "data" / "test_test.csv")
    fit_by_cluster  = group_by_cluster(fit_header,  fit_rows)
    test_by_cluster = group_by_cluster(test_header, test_rows)
    cluster_ids = sorted(set(fit_by_cluster) & set(test_by_cluster))
    return {
        "fit_header": fit_header, "test_header": test_header,
        "fit_by_cluster": fit_by_cluster, "test_by_cluster": test_by_cluster,
        "cluster_ids": cluster_ids,
    }


def load_flat(task_root: Path) -> dict:
    """Type I loader. Load train.csv + test.csv as flat tables (no clusters).

    The reference / submission formulas predict directly on test.csv.
    train.csv is carried for completeness (the SR system trains on it) but
    the v2 score does not use it — there is no naive baseline.
    """
    train_header, train_rows = load_csv(task_root / "data" / "train.csv")
    test_header,  test_rows  = load_csv(task_root / "data" / "test.csv")
    return {
        "train_header": train_header, "train_rows": train_rows,
        "test_header": test_header,   "test_rows": test_rows,
    }


# --------------------------------------------------------------------------
# helpers
# --------------------------------------------------------------------------

def _to_array(rows: list[list[str]], header: list[str], cols: list[str]) -> np.ndarray:
    idx = [header.index(c) for c in cols]
    return np.array([[float(row[i]) for i in idx] for row in rows], dtype=float)


def _col(rows: list[list[str]], header: list[str], name: str) -> np.ndarray:
    i = header.index(name)
    return np.array([float(r[i]) for r in rows], dtype=float)


# --------------------------------------------------------------------------
# metric registry
# --------------------------------------------------------------------------
# A task selects exactly ONE metric via metadata `metric:`. Each entry
# carries the compute fn, the optimisation direction, and the value a
# perfect prediction attains — the reference-relative score in evaluate_numeric.py
# needs all three. Higher-is-better metrics must be bounded above by
# `perfect`. Each compute fn receives finite-masked, equal-length
# (y_true, y_pred) with n >= 1, and returns a float or None (undefined for
# this data — e.g. mape when y_true has zeros).

def _mse(yt, yp):
    return float(np.mean((yp - yt) ** 2))


def _rmse(yt, yp):
    return float(np.sqrt(np.mean((yp - yt) ** 2)))


def _mae(yt, yp):
    return float(np.mean(np.abs(yp - yt)))


def _mdae(yt, yp):
    return float(np.median(np.abs(yp - yt)))


def _r2(yt, yp):
    ss_tot = float(np.sum((yt - yt.mean()) ** 2))
    if ss_tot <= 0:
        return None                               # constant truth — r2 undefined
    return float(1.0 - float(np.sum((yp - yt) ** 2)) / ss_tot)


def _smape(yt, yp):
    denom = (np.abs(yt) + np.abs(yp)) / 2.0
    safe = denom > 0
    if not safe.any():
        return None
    return float(np.mean(np.abs(yp[safe] - yt[safe]) / denom[safe]))


def _mape(yt, yp):
    nz = yt != 0
    if not nz.any():
        return None                               # all-zero truth — mape undefined
    return float(np.mean(np.abs((yp[nz] - yt[nz]) / yt[nz])))


def _log_mae(yt, yp):
    # log10-space error — for strictly-positive targets spanning decades.
    pos = yt > 0
    if not pos.any():
        return None
    yp_c = np.clip(yp[pos], 1e-300, None)         # non-positive prediction → huge log error
    return float(np.mean(np.abs(np.log10(yp_c) - np.log10(yt[pos]))))


METRICS: dict[str, dict] = {
    "rmse":    {"fn": _rmse,    "direction": "lower",  "perfect": 0.0},
    "mae":     {"fn": _mae,     "direction": "lower",  "perfect": 0.0},
    "mse":     {"fn": _mse,     "direction": "lower",  "perfect": 0.0},
    "mdae":    {"fn": _mdae,    "direction": "lower",  "perfect": 0.0},
    "smape":   {"fn": _smape,   "direction": "lower",  "perfect": 0.0},
    "mape":    {"fn": _mape,    "direction": "lower",  "perfect": 0.0},
    "log_mae": {"fn": _log_mae, "direction": "lower",  "perfect": 0.0},
    "r2":      {"fn": _r2,      "direction": "higher", "perfect": 1.0},
}


def metrics(y_true: np.ndarray, y_pred: np.ndarray) -> dict:
    """Compute the full metric registry on one (y_true, y_pred) pair.

    Returns {metric_name: value|None, ..., "n_finite": int}. A task's
    declared metric is one key; the rest are kept for diagnostics.
    """
    y_true = np.asarray(y_true, dtype=float)
    y_pred = np.asarray(y_pred, dtype=float)
    mask = np.isfinite(y_pred) & np.isfinite(y_true)
    n = int(mask.sum())
    if n == 0:
        return {**{name: None for name in METRICS}, "n_finite": 0}
    yt, yp = y_true[mask], y_pred[mask]
    out: dict = {}
    for name, spec in METRICS.items():
        try:
            out[name] = spec["fn"](yt, yp)
        except Exception:  # noqa: BLE001
            out[name] = None
    out["n_finite"] = n
    return out


class _Timeout(Exception):
    pass


def _timeout_handler(signum, frame):  # noqa: ARG001
    raise _Timeout()


# --------------------------------------------------------------------------
# core
# --------------------------------------------------------------------------

def run_formula(mod, clusters: dict, target_name: str,
                fit_timeout_seconds: int | None = None,
                seed: int | None = None) -> dict:
    """Execute one formula module over every test cluster.

    `seed`, if given, fixes the global NumPy / Python RNG before the run so
    a stochastic submission `fit()` is reproducible. evaluate_numeric.py runs each
    Type II submission under several seeds and reports mean / std.

    Returns:
      {
        "per_cluster": {cid: {"metrics": {...}, "failed": bool, "error": str|None}},
        "n_clusters_fitted": int,
        "n_clusters_failed": int,
        "max_fit_seconds": float,   # slowest per-cluster fit() wall-time
      }

    A cluster is `failed` if fit() raises / times out, or predict() returns
    non-finite. The score is computed per-cluster and averaged in
    evaluate_numeric.py — there is no cross-cluster pooling. `max_fit_seconds` lets
    evaluate_numeric.py derive the fit_timeout cap from the reference bank's
    measured fit cost.
    """
    fit_header  = clusters["fit_header"]
    test_header = clusters["test_header"]
    fit_by_cluster  = clusters["fit_by_cluster"]
    test_by_cluster = clusters["test_by_cluster"]
    cluster_ids = clusters["cluster_ids"]

    if seed is not None:
        np.random.seed(seed)
        random.seed(seed)

    used = list(mod.USED_INPUTS)
    LAW  = dict(mod.LAW_CONSTANTS)
    is_type_ii = bool(mod.LOCAL_FITTABLE)

    per_cluster: dict[int, dict] = {}
    n_failed = 0
    max_fit_seconds = 0.0

    for cid in cluster_ids:
        fr = fit_by_cluster[cid]
        tr = test_by_cluster[cid]
        try:
            if used:
                X_fit  = _to_array(fr, fit_header,  used)
                X_test = _to_array(tr, test_header, used)
            else:
                X_fit  = np.zeros((len(fr), 0), dtype=float)
                X_test = np.zeros((len(tr), 0), dtype=float)
            y_fit  = _col(fr, fit_header,  target_name)
            y_test = _col(tr, test_header, target_name)

            if is_type_ii:
                if fit_timeout_seconds:
                    signal.signal(signal.SIGALRM, _timeout_handler)
                    signal.alarm(int(fit_timeout_seconds))
                t0 = time.perf_counter()
                try:
                    local = mod.fit(X_fit, y_fit, **LAW)
                finally:
                    if fit_timeout_seconds:
                        signal.alarm(0)
                max_fit_seconds = max(max_fit_seconds, time.perf_counter() - t0)
            else:
                local = {}

            y_pred = np.asarray(mod.predict(X_test, **LAW, **local), dtype=float)
            if not np.all(np.isfinite(y_pred)):
                raise RuntimeError("predict returned non-finite values")

            m = metrics(y_test, y_pred)
            per_cluster[cid] = {"metrics": m, "failed": False, "error": None}
        except _Timeout:
            n_failed += 1
            per_cluster[cid] = {"metrics": None, "failed": True,
                                "error": f"fit() exceeded {fit_timeout_seconds}s"}
        except Exception as exc:  # noqa: BLE001
            n_failed += 1
            per_cluster[cid] = {"metrics": None, "failed": True,
                                "error": f"{type(exc).__name__}: {exc}"}

    return {
        "per_cluster": per_cluster,
        "n_clusters_fitted": len(cluster_ids) - n_failed,
        "n_clusters_failed": n_failed,
        "max_fit_seconds": max_fit_seconds,
    }


# --------------------------------------------------------------------------
# core — Type I (flat, no clusters, no fit)
# --------------------------------------------------------------------------

def run_formula_flat(mod, flat: dict, target_name: str) -> dict:
    """Execute one Type I formula on the flat test set.

    Type I: LOCAL_FITTABLE is empty, there is no fit() — predict() is called
    once on the whole test set with only LAW_CONSTANTS.

    Returns:
      {"metrics": {...} | None, "failed": bool, "error": str|None}
    """
    test_header = flat["test_header"]
    test_rows   = flat["test_rows"]
    used = list(mod.USED_INPUTS)
    LAW  = dict(mod.LAW_CONSTANTS)

    try:
        X_test = (_to_array(test_rows, test_header, used) if used
                  else np.zeros((len(test_rows), 0), dtype=float))
        y_test = _col(test_rows, test_header, target_name)
        y_pred = np.asarray(mod.predict(X_test, **LAW), dtype=float)
        if not np.all(np.isfinite(y_pred)):
            raise RuntimeError("predict returned non-finite values")
        return {"metrics": metrics(y_test, y_pred), "failed": False, "error": None}
    except Exception as exc:  # noqa: BLE001
        return {"metrics": None, "failed": True,
                "error": f"{type(exc).__name__}: {exc}"}