Download sunstar_plus/obsfeats.py from summerAIO/sunstar-plus: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/summerAIO/sunstar-plus/resolve/main/sunstar_plus/obsfeats.py
- Command line
-
hf download hf://summerAIO/sunstar-plus/sunstar_plus/obsfeats.py
-
curl -L -o obsfeats.py https://huggingface.co/summerAIO/sunstar-plus/resolve/main/sunstar_plus/obsfeats.py
3.1 kB
| """チャンク(10日)単位の特徴行列生成 (学習行サンプリング / 評価ストリーミング共通)""" | |
| import numpy as np, pandas as pd | |
| from .features import * | |
| LEADS_B = np.array([1, 3, 6, 12, 24]) | |
| def obsfeats(O, base): | |
| """V2 と同一 (feats.obsfeats): 観測履歴特徴 G (T,S,ng)。O: (T,S,7) [temp,u,v,rh,precip,speed,pressure]""" | |
| T_, U_, V_, RH_, P_, SP_, PS_ = [O[:, :, i] for i in range(7)] | |
| spd = np.hypot(U_, V_) | |
| res = {"T": T_ - base["t2m"], "U": U_ - base["u10"], "V": V_ - base["v10"], "RH": RH_ - base["rh"], "SPD": spd - base["spd10"]} | |
| obsv = {"T": T_, "U": U_, "V": V_, "RH": RH_, "SPD": spd, "P": P_} | |
| names, cols = [], [] | |
| for k, a in obsv.items(): | |
| names.append(f"o_{k}0"); cols.append(a) | |
| for lag in (1, 2, 3, 6, 12, 24): names.append(f"o_{k}_l{lag}"); cols.append(shift(a, -lag)) | |
| for k, a in res.items(): | |
| names.append(f"r_{k}0"); cols.append(a) | |
| for lag in (1, 2, 3, 6, 12, 24): names.append(f"r_{k}_l{lag}"); cols.append(shift(a, -lag)) | |
| for win in (3, 6, 24, 72, 168): names.append(f"r_{k}_m{win}"); cols.append(roll_mean(a, -win + 1, 0)) | |
| names.append(f"r_{k}_sd24"); cols.append(np.sqrt(np.maximum(roll_mean(a ** 2, -23, 0) - roll_mean(a, -23, 0) ** 2, 0))) | |
| names.append("o_T_trend3"); cols.append(T_ - shift(T_, -3)) | |
| names.append("o_P_sum3"); cols.append(sum(np.nan_to_num(shift(P_, -k)) for k in range(0, 3))) | |
| names.append("o_P_sum24"); cols.append(sum(np.nan_to_num(shift(P_, -k)) for k in range(0, 24))) | |
| G = np.stack([np.asarray(c, np.float32) for c in cols], 2) | |
| return G, names, res | |
| NB_NAMES = ["nb_T0", "nb_U0", "nb_V0", "nb_RH0", "nb_SPD0", "nb_T_m6", "nb_U_m6", "nb_V_m6", "nb_RH_m6", "nb_SPD_m6", "nb_P0", "nb_P_m3", "nb_Pmax3", "nb_w"] | |
| DB_KEYS = ("t2m", "u10", "v10", "rh", "spd10", "d2m") | |
| def hav_km(lat1, lon1, lat2, lon2): | |
| p = np.pi / 180; a = np.sin((lat2 - lat1) * p / 2) ** 2 + np.cos(lat1 * p) * np.cos(lat2 * p) * np.sin((lon2 - lon1) * p / 2) ** 2 | |
| return 12742 * np.arcsin(np.sqrt(a)) | |
| def neighbor_table(st, pool_mask, K=8, L=30.0, Z=250.0, rmax=90.0): | |
| """各地点の近傍 K 局 (pool の他局): index (S,K), weight (S,K). 自局は除外。""" | |
| S = len(st); lat, lon, el = st.lat.values, st.lon.values, st.elevation_m.values | |
| pool = np.where(pool_mask)[0]; idx = np.zeros((S, K), np.int32); w = np.zeros((S, K), np.float32) | |
| for s in range(S): | |
| d = hav_km(lat[s], lon[s], lat[pool], lon[pool]); d[pool == s] = 1e9 | |
| o = np.argsort(d)[:K]; j = pool[o]; dd = d[o] | |
| ww = np.exp(-(dd / L) ** 2 - ((el[s] - el[j]) / Z) ** 2); ww[dd > rmax] = 0 | |
| idx[s, :len(j)] = j; w[s, :len(j)] = ww | |
| return idx, w | |
| def nb_mean(a, idx, w): | |
| """a (T,S) -> weighted nan-mean over neighbours (T,S), and weight sum""" | |
| v = a[:, idx] # (T,S,K) | |
| fin = np.isfinite(v); ww = w[None] * fin | |
| num = np.where(fin, v, 0) * w[None] | |
| den = ww.sum(2) | |
| return np.where(den > 1e-3, num.sum(2) / np.maximum(den, 1e-6), np.nan).astype(np.float32), den.astype(np.float32) | |