Datasets:
Download code/make_submission_v2.py from tchaudha/heat-seekers-protostar: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/datasets/tchaudha/heat-seekers-protostar/resolve/main/code/make_submission_v2.py
- Command line
-
hf download hf://datasets/tchaudha/heat-seekers-protostar/code/make_submission_v2.py
-
curl -L -o make_submission_v2.py https://huggingface.co/datasets/tchaudha/heat-seekers-protostar/resolve/main/code/make_submission_v2.py
2.58 kB
| """Write the 2019-2025 forecast from data <= 2018 using results/v2_selection.json. | |
| --variant strict : frozen-protocol selection (default) | |
| --variant v2b : documented deviation, CDD by daily shift with the selected temperature slope.""" | |
| import argparse, hashlib, json, sys | |
| import numpy as np, pandas as pd | |
| sys.path.insert(0, str(__import__('pathlib').Path(__file__).resolve().parent)) | |
| from v2lib import * | |
| RES = ROOT / 'results' | |
| def choice(variant): | |
| sel = json.loads((RES / 'v2_selection.json').read_text())['selection'] | |
| if variant == 'v2b': sel = {**sel, 'cdd': 'SHmean_' + sel['annual_avg_temp'][3:]} | |
| return sel | |
| def build(P, variant): | |
| sel = choice(variant) | |
| L, a, R = sel['annual_avg_temp'][3:].split('_'); cfg = (int(L[1:]), float(a[1:]), R) | |
| f = forecast_block(P, 2018, configs=[cfg]) | |
| parts = [f[(f.metric == m) & (f.method == sel[m])] for m in METRICS] | |
| p = pd.concat(parts).drop(columns='method') | |
| t = pd.read_csv(TEMPLATE).drop(columns='predicted_value') | |
| out = t.merge(p, on=KEY, how='left', validate='one_to_one', sort=False) | |
| assert out.predicted_value.notna().all(); validate(out) | |
| # fallback log: city-metrics without an official baseline (<10 valid years in 1989-2018) | |
| log = [] | |
| for m in METRICS: | |
| _, elig = clim30(P, m, 2018) | |
| for i in np.where(~elig)[0]: | |
| log.append(dict(city_id=P.ids[i], name=P.names[i], metric=m, method=sel[m], | |
| valid_years_1989_2018=int(P.valid[m][i, 1989 - Y0:].sum()), | |
| note='official baseline undefined; forecast uses last valid years at any time (see v2lib.clim30/level_years)')) | |
| return out, sel, pd.DataFrame(log) | |
| def main(): | |
| ap = argparse.ArgumentParser(); ap.add_argument('--variant', default='strict', choices=['strict', 'v2b']) | |
| ap.add_argument('--out', default=None); args = ap.parse_args() | |
| P = Panel(); out, sel, log = build(P, args.variant) | |
| path = RES / f'submission_v2_{args.variant}.csv' if args.out is None else ROOT / args.out | |
| out.to_csv(path, index=False, float_format='%.6f'); validate(pd.read_csv(path)) | |
| log.to_csv(RES / f'v2_fallback_rows_{args.variant}.csv', index=False) | |
| assert len(log) == 9 and len(log) * 7 == 63, len(log) | |
| h = hashlib.sha256(path.read_bytes()).hexdigest() | |
| (RES / f'submission_v2_{args.variant}.json').write_text(json.dumps(dict(variant=args.variant, selection=sel, sha256=h, rows=len(out)), indent=2)) | |
| print('VALID', path, h); print(out.groupby('metric').predicted_value.describe().round(3)) | |
| if __name__ == '__main__': main() | |