Download scripts/preprocess.py from AtmosIQ/atmosiq-processed: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/datasets/AtmosIQ/atmosiq-processed/resolve/main/scripts/preprocess.py
- Command line
-
hf download hf://datasets/AtmosIQ/atmosiq-processed/scripts/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/datasets/AtmosIQ/atmosiq-processed/resolve/main/scripts/preprocess.py
3.52 kB
| """ | |
| preprocess.py -> data\ingest\ (run: python data\ingest\preprocess.py) | |
| Turns the raw master into a TRAINING-READY dataset. Per month partition: | |
| 1. CLIP each feature to a physical envelope (winsorises bad-pixel tails — | |
| e.g. modis_aod 11 -> 5, s5p_aer_ai 151 -> 20 — without touching real highs). | |
| 2. Add <source>_present missingness flags (blueprint Group A) — model must | |
| know which sparse satellite inputs are present (fill ranges 21-89%). | |
| 3. LEFT-JOIN CPCB labels (cpcb_labels_daily.parquet) -> 7 targets + `labeled`. | |
| Output: data\ingest\train_ready\<YYYY-MM>.parquet (features + flags + targets) | |
| After this, only training is left. | |
| """ | |
| from __future__ import annotations | |
| import os, sys, pathlib | |
| import numpy as np, pandas as pd | |
| OUT = pathlib.Path(os.environ.get("ATMOS_OUT", "data/ingest")) | |
| MASTER = OUT / "node_observations_daily" | |
| DEST = OUT / "train_ready"; DEST.mkdir(exist_ok=True) | |
| # physical clip envelopes (keep real extremes, cap absurd sensor spikes) | |
| CLIP = { | |
| "s5p_no2_column":(-5e-4,5e-3), "s5p_so2_column":(-0.05,0.5), "s5p_co_column":(0,1.0), | |
| "s5p_o3_column":(0,0.3), "s5p_hcho_column":(-0.05,0.05), "s5p_aer_ai":(-15,20), | |
| "s5p_ch4_column":(1400,2200), "s5p_cloud_fraction":(0,1), | |
| "modis_aod":(0,5), "viirs_aod":(0,5), "fire_count":(0,None), | |
| "era5_temperature":(220,330), "era5_boundary_layer_height":(0,5000), | |
| "era5_relative_humidity":(0,100), "era5_surface_pressure":(40000,105000), | |
| "era5_cloud_cover":(0,1), "era5_wind_u":(-60,60), "era5_wind_v":(-60,60), | |
| "era5_rainfall":(0,None), "u_1330ist":(-60,60), "v_1330ist":(-60,60), | |
| } | |
| # sparse satellite sources -> missingness flags | |
| FLAG_COLS = ["s5p_no2_column","s5p_co_column","s5p_so2_column","s5p_o3_column", | |
| "s5p_hcho_column","s5p_aer_ai","s5p_ch4_column","s5p_cloud_fraction", | |
| "modis_aod","viirs_aod","fire_count"] | |
| TARGETS = ["pm2_5","pm10","no2","so2","co","o3","nh3"] | |
| def main(): | |
| cpcb_path = OUT / "cpcb_labels_daily.parquet" | |
| cpcb = pd.read_parquet(cpcb_path) if cpcb_path.exists() else None | |
| if cpcb is not None: | |
| cpcb = cpcb[["node_id","date"] + TARGETS + ["n_stations"]] | |
| print(f"CPCB labels: {len(cpcb):,} labeled node-days joined") | |
| else: | |
| print("!! cpcb_labels_daily.parquet not found — writing features only (run ingest_cpcb.py first)") | |
| parts = sorted(MASTER.glob("*.parquet")) | |
| print(f"{len(parts)} partitions -> {DEST}") | |
| tot=0; labeled=0 | |
| for pf in parts: | |
| df = pd.read_parquet(pf) | |
| # 1. clip | |
| for col,(lo,hi) in CLIP.items(): | |
| if col in df: | |
| df[col] = df[col].clip(lower=lo, upper=hi) | |
| # 2. missingness flags | |
| for col in FLAG_COLS: | |
| if col in df: | |
| df[col+"_present"] = df[col].notna().astype("int8") | |
| # 3. join labels | |
| if cpcb is not None: | |
| df = df.merge(cpcb, on=["node_id","date"], how="left") | |
| df["labeled"] = df[TARGETS].notna().any(axis=1).astype("int8") | |
| labeled += int(df["labeled"].sum()) | |
| df.to_parquet(DEST / pf.name, index=False) | |
| tot += len(df) | |
| print(f" {pf.stem}: {len(df):,} rows", end="\r") | |
| print(f"\ndone. {tot:,} node-days -> {DEST}") | |
| if cpcb is not None: | |
| print(f"labeled node-days (have >=1 CPCB target): {labeled:,} ({100*labeled/tot:.2f}%)") | |
| print(" -> supervised loss uses labeled rows; SSL pretrain uses ALL rows.") | |
| if __name__ == "__main__": | |
| main() |