Datasets:
Download code/v4lib.py from tchaudha/heat-seekers-protostar: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/datasets/tchaudha/heat-seekers-protostar/resolve/main/code/v4lib.py
- Command line
-
hf download hf://datasets/tchaudha/heat-seekers-protostar/code/v4lib.py
-
curl -L -o v4lib.py https://huggingface.co/datasets/tchaudha/heat-seekers-protostar/resolve/main/code/v4lib.py
4.26 kB
| """Heat Seekers v4: five fixed-parameter ideas (research/V4_PROTOCOL.md). Data <= origin only.""" | |
| import numpy as np, pandas as pd | |
| from v3lib import * | |
| from v3lib import _win | |
| def hinge_slope(P, origin, fallback): | |
| m = 'annual_avg_temp'; start = max(origin - 44, 1973) | |
| if origin - start + 1 < 15: return fallback, False | |
| a, b = start - Y0, origin - Y0 + 1; v = P.value[m][:, a:b]; ok = P.valid[m][:, a:b]; seg = P.seg[:, a:b] | |
| an = np.full(v.shape, np.nan); ncity = 0 | |
| for i in range(len(P.ids)): | |
| used = False | |
| for g in np.unique(seg[i][ok[i]]): | |
| k = ok[i] & (seg[i] == g) | |
| if k.sum() >= 15: an[i, k] = v[i, k] - v[i, k].mean(); used = True | |
| ncity += used | |
| if ncity < 30: return fallback, False | |
| cnt = np.isfinite(an).sum(0); yrs = np.arange(start, origin + 1); use = cnt >= 20 | |
| nat = np.nanmean(np.where(np.isfinite(an), an, np.nan)[:, use], axis=0) | |
| return float(np.polyfit(yrs[use], nat, 1)[0]), True | |
| def cal_years(P, m, origin, i): | |
| """Valid years in [T-14, T] within the current station segment; v2 level years if fewer than 5.""" | |
| k1 = origin - Y0 + 1; idx = np.where(P.valid[m][i, :k1])[0] | |
| cur = P.seg[i, idx[-1]]; w = idx[(idx >= k1 - 15) & (P.seg[i, idx] == cur)] | |
| return w if len(w) >= 5 else level_years(P, m, origin, i, 15) | |
| def trim1(x, axis=0): | |
| if x.shape[axis] < 10: return x.mean(axis) | |
| s = np.sort(x, axis=axis); return np.take(s, range(1, x.shape[axis] - 1), axis=axis).mean(axis) | |
| def temp_det(P, origin, beta, trim, H=7): | |
| m = 'annual_avg_temp'; hs = np.arange(1, H + 1); out = np.empty((len(P.ids), H)) | |
| for i in range(len(P.ids)): | |
| V = cal_years(P, m, origin, i); d = P.value[m][i, V] + beta * (origin - YEARS[V]) | |
| L = trim1(d) if trim else d.mean(); out[i] = L + beta * hs | |
| return out | |
| def cdd_trim(P, origin, beta, H=7): | |
| m = 'cdd'; hs = np.arange(1, H + 1); out = np.empty((len(P.ids), H)) | |
| for i in range(len(P.ids)): | |
| V = cal_years(P, m, origin, i); vals = np.empty((len(V), H)) | |
| for r, t in enumerate(V): vals[r] = P.value[m][i, t] + interp_inc(P.inc[i, t], beta * (origin + hs - YEARS[t])) | |
| out[i] = trim1(vals, 0) | |
| return np.maximum(out, 0) | |
| def v4_block(P, origin, H=7): | |
| ids = P.ids; hs = np.arange(1, H + 1); rows = []; info = {} | |
| def add(metric, method, mat): | |
| mat = np.asarray(mat, float) | |
| if mat.ndim == 1: mat = np.repeat(mat[:, None], H, 1) | |
| if metric in ('cdd', 'total_precip_in'): mat = np.maximum(mat, 0) | |
| for j, h in enumerate(hs): | |
| rows.append(pd.DataFrame({'city_id': ids, 'year': origin + h, 'metric': metric, 'method': method, 'predicted_value': mat[:, j]})) | |
| T = 'annual_avg_temp' | |
| for m in METRICS: add(m, 'clim30', clim30(P, m, origin)[0]) | |
| s2 = temp_slope(P, origin, 'v2')[0]; sh, used = hinge_slope(P, origin, s2); info.update(origin=origin, s_v2=s2, s_hinge=sh, hinge_used=used) | |
| S2 = np.full(len(ids), s2); SH = np.full(len(ids), sh) | |
| lv, cv = level_v2(P, origin) | |
| add(T, 'v2', temp_forecast(P, origin, S2, lv, cv, H)); add(T, 't_hinge', temp_forecast(P, origin, SH, lv, cv, H)) | |
| for nm, b in (('v2', s2), ('hinge', sh)): | |
| add(T, f't_det_{nm}', temp_det(P, origin, b, False, H)); add(T, f't_dettrim_{nm}', temp_det(P, origin, b, True, H)) | |
| add('cdd', 'v2b', cdd_shift(P, origin, 15, S2)); add('cdd', 'c_v2b_hinge', cdd_shift(P, origin, 15, SH)) | |
| add('cdd', 'c_trim_v2', cdd_trim(P, origin, s2)); add('cdd', 'c_trim_hinge', cdd_trim(P, origin, sh)) | |
| p = 'total_precip_in'; base = clim30(P, p, origin)[0] | |
| tm = window_stat(P, p, origin, 30, lambda x: np.sort(x)[2:-2].mean()) | |
| add(p, 'p_trim', 0.85 * base + 0.15 * tm) | |
| q = 'hottest_day_temp'; hb = clim30(P, q, origin)[0] | |
| for nm, b in (('v2', s2), ('hinge', sh)): add(q, f'h_tether_{nm}', hb[:, None] + 0.15 * max(0.0, b) * (hs[None] + 14.5)) | |
| r15 = hb.copy() | |
| for i in range(len(ids)): | |
| k1 = origin - Y0 + 1; idx = np.where(P.valid[q][i, :k1])[0] | |
| if len(idx): | |
| w = idx[(idx >= k1 - 15) & (P.seg[i, idx] == P.seg[i, idx[-1]])] | |
| if len(w) >= 5: r15[i] = P.value[q][i, w].mean() | |
| add(q, 'h_blend15', 0.9 * hb + 0.1 * r15) | |
| return pd.concat(rows, ignore_index=True), info | |