mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 15:26:15 +00:00
fix(core): clean predictor debt
This commit is contained in:
1 parent
e952cea417
commit
e6add1b727
10 files changed
+268
-58
No files matched your search
@@ -104,7 +104,7 @@ def collect_station_status(
|
||||
"""Scrape current operational status for all stations."""
|
||||
logger.info("Step 2/5 - Collecting station status.")
|
||||
records = []
|
||||
for station_id in scraper._stations:
|
||||
for station_id in scraper.station_ids:
|
||||
try:
|
||||
record = scraper.scrape_station(station_id)
|
||||
records.append(record)
|
||||
@@ -176,6 +176,59 @@ def run_feature_engineering(
|
||||
return featured
|
||||
|
||||
|
||||
def _available_prediction_dates(
|
||||
weather_by_station: dict[str, pd.DataFrame],
|
||||
*,
|
||||
base_date: date,
|
||||
include_today: bool,
|
||||
) -> list[date]:
|
||||
"""Return the real prediction dates available in the collected forecasts."""
|
||||
available_dates: set[date] = set()
|
||||
for weather_df in weather_by_station.values():
|
||||
if weather_df.empty or "date" not in weather_df.columns:
|
||||
continue
|
||||
station_dates = pd.to_datetime(weather_df["date"], format="mixed").dt.date
|
||||
available_dates.update(station_dates.tolist())
|
||||
|
||||
if include_today:
|
||||
return sorted(d for d in available_dates if d >= base_date)
|
||||
return sorted(d for d in available_dates if d > base_date)
|
||||
|
||||
|
||||
def _select_forecast_row_for_date(
|
||||
weather_df: pd.DataFrame,
|
||||
*,
|
||||
station_id: str,
|
||||
target_date: date,
|
||||
) -> dict[str, object] | None:
|
||||
"""Return the exact forecast row for *target_date*, or None when unavailable."""
|
||||
if weather_df.empty or "date" not in weather_df.columns:
|
||||
logger.warning("No forecast rows available for %s.", station_id)
|
||||
return None
|
||||
|
||||
weather_dates = pd.to_datetime(weather_df["date"], format="mixed").dt.date
|
||||
target_rows = weather_df.loc[weather_dates == target_date]
|
||||
if not target_rows.empty:
|
||||
return target_rows.iloc[0].to_dict()
|
||||
|
||||
available_dates = sorted(set(weather_dates.tolist()))
|
||||
if available_dates:
|
||||
logger.warning(
|
||||
"Forecast for %s on %s is unavailable. Available range: %s -> %s.",
|
||||
station_id,
|
||||
target_date.isoformat(),
|
||||
available_dates[0].isoformat(),
|
||||
available_dates[-1].isoformat(),
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"Forecast for %s on %s is unavailable.",
|
||||
station_id,
|
||||
target_date.isoformat(),
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def _load_prediction_feature_base(
|
||||
store: SQLiteStore,
|
||||
runtime_featured_df: pd.DataFrame,
|
||||
@@ -267,22 +320,17 @@ def generate_predictions(
|
||||
logger.info("Step 5/5 - Generating predictions for %s.", target_date.isoformat())
|
||||
predictions = {}
|
||||
station_map = {r["station_id"]: r for r in station_records}
|
||||
base_date = date.today()
|
||||
days_ahead = (target_date - base_date).days
|
||||
feature_base_df = _load_prediction_feature_base(store, featured_df)
|
||||
|
||||
for station_id, weather_df in weather_by_station.items():
|
||||
# Get target-date forecast row as weather_data dict.
|
||||
target_rows = weather_df[weather_df["date"] == target_date]
|
||||
if target_rows.empty:
|
||||
fallback_index = max(0, min(days_ahead, len(weather_df) - 1))
|
||||
target_rows = weather_df.iloc[fallback_index:fallback_index + 1]
|
||||
if target_rows.empty:
|
||||
logger.warning("No forecast for %s for %s.", station_id, target_date.isoformat())
|
||||
weather_data = _select_forecast_row_for_date(
|
||||
weather_df,
|
||||
station_id=station_id,
|
||||
target_date=target_date,
|
||||
)
|
||||
if weather_data is None:
|
||||
continue
|
||||
|
||||
weather_data = target_rows.iloc[0].to_dict()
|
||||
|
||||
# Build a feature row: target-date weather + today's station data.
|
||||
# Current station observations are still the best proxy available for
|
||||
# same-day or next-day inference.
|
||||
@@ -327,6 +375,14 @@ def generate_predictions(
|
||||
except Exception as exc:
|
||||
logger.error("Prediction failed for %s: %s", station_id, exc)
|
||||
|
||||
if not predictions:
|
||||
logger.error(
|
||||
"No predictions generated for %s; the requested horizon is outside the "
|
||||
"available forecast window or every station failed.",
|
||||
target_date.isoformat(),
|
||||
)
|
||||
return
|
||||
|
||||
output = {
|
||||
"generated_at": datetime.now(timezone.utc).isoformat(),
|
||||
"prediction_date": target_date.isoformat(),
|
||||
|
||||
Reference in new issue
Block a user