protostar-placeholders-final / code /prepare_warm_days.py
DragonDare's picture
Upload folder using huggingface_hub
b5e8582 verified
Raw History Blame Contribute Delete
2.08 kB
"""Write data/daily_warm_days.csv: the daily-data input of Placeholder_v9-S (plan v9).
uv run python code/prepare_warm_days.py # reads data/raw/daily if present, else the Hub
uv run python code/prepare_warm_days.py --daily PATH # an existing copy of the daily subset
Source: the `daily` subset of CMU-OSPO/protostar-climate-train-to-2018 at the pinned revision (CC0), 1851-01-01 to
2018-12-31. One row per city and year: n_both = days with both Tmax and Tmin (the days the dataset's `cdd` uses) and
n_warm = those days with (Tmax + Tmin) / 2 > 65 F. Nothing else is derived. DataRuleViolation is raised on any row
after 2018. The model uses only the 30 years up to its origin and only years whose official `cdd` is valid.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import pandas as pd
from common import DATA_DIR, TRAIN_REPO, TRAIN_REVISION, assert_no_post_cutoff
RAW = DATA_DIR / "raw/daily/train-00000-of-00001.parquet"
OUT = DATA_DIR / "daily_warm_days.csv"
def main(argv=None) -> None:
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
ap.add_argument("--daily", type=Path, default=None)
args = ap.parse_args(argv)
src = args.daily or (RAW if RAW.is_file() else f"hf://datasets/{TRAIN_REPO}@{TRAIN_REVISION}/daily")
d = pd.read_parquet(src, columns=["ID", "Date", "tmax", "tmin"])
d["year"] = pd.to_datetime(d.Date).dt.year.astype(int)
assert_no_post_cutoff(d, "year", "daily subset")
both = d.tmax.notna() & d.tmin.notna()
d["n_both"] = both.astype(int)
d["n_warm"] = (both & ((d.tmax + d.tmin) / 2 > 65)).astype(int)
out = (d.groupby(["ID", "year"], as_index=False)[["n_both", "n_warm"]].sum()
.rename(columns={"ID": "city_id"}).sort_values(["city_id", "year"]))
assert_no_post_cutoff(out, "year", "warm-day counts")
out.to_csv(OUT, index=False, lineterminator="\n")
print(f"Wrote {len(out):,} city-years ({out.city_id.nunique()} cities, {out.year.min()}-{out.year.max()}) to {OUT}")
if __name__ == "__main__":
main()