feat: in-memory HII gap-fill in the ML data loader
CI/CD Pipeline - Northern Thailand Ping River Monitor / Test Suite (3.11) (push) Failing after 25s
CI/CD Pipeline - Northern Thailand Ping River Monitor / Build Docker Image (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Integration Test with Services (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Deploy to Staging (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Deploy to Production (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Performance Test (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Code Quality (push) Successful in 12s
Documentation / Generate API Documentation (push) Successful in 8s
Documentation / Build Sphinx Documentation (push) Successful in 15s
CI/CD Pipeline - Northern Thailand Ping River Monitor / Cleanup (push) Successful in 1s
Documentation / Documentation Summary (push) Successful in 2s
Documentation / Validate Documentation (push) Failing after 7s

load_measurements() (DB path) patches missing station-hours from the
hii_waterlevel mirror telemetry: exact mirrors (P.1/P.103/P.20/P.4A/P.67/
P.75/P.82/P.84/P.92) plus bias-corrected P.81 (+9,340 h). Per-station
MSL->gauge offset is derived from >=168 h of series overlap, which
reproduces the published offsets for exact mirrors and absorbs P.81's
bias; P.76/P.77/P.85/P.87 HII twins are different physical sensors and
stay excluded. Training and serving share the loader, so both sides see
identical filled series; water_measurements is never written.
This commit is contained in:
2026-08-13 10:29:54 +07:00
parent 6eafb353b1
commit ba781465a9
2 changed files with 252 additions and 1 deletions
+128 -1
View File
@@ -97,6 +97,128 @@ def _fetch_from_db(
return _normalize_long(df)
# Stations whose HII mirror is the SAME telemetry (corr ≈ 1.000, median diff
# == station offset exactly — validated 2026-08-11) plus P.81, where the HII
# twin reads the same river with a bias (corr 0.906, MAE 19 cm) that the
# dynamic overlap offset corrects. P.76/P.77/P.85/P.87 HII twins are DIFFERENT
# physical sensors (corr 0.25-0.62) and must never be merged into RID series.
HII_FILL_STATIONS = (
"P.1",
"P.103",
"P.20",
"P.4A",
"P.67",
"P.75",
"P.82",
"P.84",
"P.92",
"P.81",
)
_HII_EXACT_MIRRORS = frozenset(HII_FILL_STATIONS) - {"P.81"}
_HII_MIN_OVERLAP_HOURS = 168
def _fetch_hii_levels(
db_url: str,
stations: List[str],
start: Optional[datetime.datetime],
end: Optional[datetime.datetime],
) -> pd.DataFrame:
engine = create_engine(db_url, pool_pre_ping=True)
query = (
"SELECT m.timestamp, s.rid_code AS station_code, m.wl_msl, m.discharge "
"FROM hii_waterlevel m JOIN hii_wl_stations s ON s.id = m.station_id "
"WHERE s.rid_code IS NOT NULL"
)
params: Dict = {}
if start is not None:
query += " AND m.timestamp >= :start_time"
params["start_time"] = start
if end is not None:
query += " AND m.timestamp <= :end_time"
params["end_time"] = end
placeholders = ", ".join(f":station_{i}" for i in range(len(stations)))
query += f" AND s.rid_code IN ({placeholders})"
for i, code in enumerate(stations):
params[f"station_{i}"] = code
with engine.connect() as connection:
df = pd.read_sql(text(query), connection, params=params)
df = df.dropna(subset=["wl_msl"])
if df.empty:
return df
df["timestamp"] = pd.to_datetime(df["timestamp"]).dt.floor("h")
df["wl_msl"] = pd.to_numeric(df["wl_msl"], errors="coerce")
df["discharge"] = pd.to_numeric(df["discharge"], errors="coerce")
df = df.sort_values("timestamp").drop_duplicates(
subset=["station_code", "timestamp"], keep="last"
)
return df
def fill_from_hii(
df: pd.DataFrame,
db_url: str,
start: Optional[datetime.datetime] = None,
end: Optional[datetime.datetime] = None,
stations: Optional[List[str]] = None,
min_overlap_hours: int = _HII_MIN_OVERLAP_HOURS,
) -> pd.DataFrame:
"""Fill missing (station, hour) rows from the HII mirror telemetry.
In-memory only — water_measurements is never written. Each station's
MSL→gauge offset is derived from the overlap between the two series
(median of wl_msl water_level over ≥ `min_overlap_hours` shared hours),
which reproduces the published offset for exact mirrors and bias-corrects
P.81. Discharge is copied only for exact mirrors; P.81 fills get NaN
discharge (its discharge bias was never validated). Failures degrade to
returning `df` unchanged, so DBs without hii_* tables keep working.
"""
codes = [c for c in (stations or HII_FILL_STATIONS) if c in set(df["station_code"])]
if not codes:
return df
try:
hii = _fetch_hii_levels(db_url, codes, start, end)
except Exception as error:
logger.warning(f"HII gap-fill skipped (fetch failed): {error}")
return df
if hii.empty:
return df
fills = []
for code, mirror in hii.groupby("station_code"):
base = df[df["station_code"] == code]
overlap = base.merge(
mirror[["timestamp", "wl_msl"]], on="timestamp", how="inner"
).dropna(subset=["water_level", "wl_msl"])
if len(overlap) < min_overlap_hours:
continue
offset = (overlap["wl_msl"] - overlap["water_level"]).median()
# Hours the RID series lacks entirely OR carries only a NaN level;
# _normalize_long keeps the later (fill) row on collision.
present = base.loc[base["water_level"].notna(), "timestamp"]
missing = mirror[~mirror["timestamp"].isin(present)]
if missing.empty:
continue
fill = pd.DataFrame(
{
"timestamp": missing["timestamp"],
"station_code": code,
"water_level": missing["wl_msl"] - offset,
"discharge": missing["discharge"]
if code in _HII_EXACT_MIRRORS
else float("nan"),
}
)
fills.append(fill)
logger.info(
f"HII gap-fill {code}: +{len(fill)} hours (offset {offset:.3f} m)"
)
if not fills:
return df
return _normalize_long(pd.concat([df] + fills, ignore_index=True))
def _fetch_station_from_api(
api_url: str, station_code: str, hours: int, limit: int = 100000
) -> pd.DataFrame:
@@ -177,18 +299,23 @@ def load_measurements(
use_cache: bool = True,
cache_dir: Path = CACHE_DIR,
api_url: str = DEFAULT_API_URL,
hii_fill: bool = True,
) -> pd.DataFrame:
"""Load the long-format [timestamp, station_code, water_level, discharge] history.
Tries PostgreSQL first, then the HTTP API, then the on-disk cache as a last
resort. A successful DB/API fetch refreshes the cache; the cache itself is
never treated as a source of fresh data.
never treated as a source of fresh data. With `hii_fill` (DB path only),
gaps are patched in memory from the HII mirror telemetry — training and
serving both flow through here, so the two sides see identical series.
"""
resolved_db_url = resolve_db_url(db_url)
if resolved_db_url:
try:
df = _fetch_from_db(resolved_db_url, stations, start, end)
if hii_fill:
df = fill_from_hii(df, resolved_db_url, start=start, end=end)
if use_cache:
_write_cache(
df, cache_dir, source="postgres", discharge_maybe_synthetic=False