mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 15:26:15 +00:00
fix(pipeline): anchor features to target date
This commit is contained in:
1 parent
e14a9f2d3b
commit
1bc1e58781
1 file changed
+42
-3
@@ -204,6 +204,41 @@ def _load_prediction_feature_base(runtime_featured_df: pd.DataFrame) -> pd.DataF
|
||||
return feature_base_df
|
||||
|
||||
|
||||
def _select_feature_anchor_row(
|
||||
feature_base_df: pd.DataFrame,
|
||||
station_id: str,
|
||||
target_date: date,
|
||||
) -> pd.DataFrame | None:
|
||||
"""Return the best historical feature row to anchor inference.
|
||||
|
||||
The daily inference path must not blindly use the latest row in the
|
||||
historical dataset because that dataset can contain dates after the
|
||||
prediction target. Using a future-season row skews engineered features
|
||||
such as calendar fields and rolling weather summaries.
|
||||
|
||||
Preference order:
|
||||
1. Latest row with ``date <= target_date``.
|
||||
2. Earliest available row for the station as a fallback.
|
||||
"""
|
||||
station_featured = feature_base_df[feature_base_df["station_id"] == station_id].copy()
|
||||
if station_featured.empty:
|
||||
return None
|
||||
|
||||
station_featured["date"] = pd.to_datetime(station_featured["date"])
|
||||
eligible = station_featured[station_featured["date"] <= pd.Timestamp(target_date)]
|
||||
if eligible.empty:
|
||||
anchor = station_featured.sort_values("date").head(1).copy()
|
||||
else:
|
||||
anchor = eligible.sort_values("date").tail(1).copy()
|
||||
|
||||
logger.info(
|
||||
"Selected feature anchor for %s at %s.",
|
||||
station_id,
|
||||
pd.Timestamp(anchor.iloc[0]["date"]).date().isoformat(),
|
||||
)
|
||||
return anchor
|
||||
|
||||
|
||||
def generate_predictions(
|
||||
predictor: HybridPredictor,
|
||||
weather_by_station: dict[str, pd.DataFrame],
|
||||
@@ -237,10 +272,14 @@ def generate_predictions(
|
||||
# same-day or next-day inference.
|
||||
features_df = None
|
||||
if not feature_base_df.empty:
|
||||
station_featured = feature_base_df[feature_base_df["station_id"] == station_id]
|
||||
if not station_featured.empty:
|
||||
anchor_row = _select_feature_anchor_row(
|
||||
feature_base_df=feature_base_df,
|
||||
station_id=station_id,
|
||||
target_date=target_date,
|
||||
)
|
||||
if anchor_row is not None:
|
||||
# Start from the latest featured row (has rolling features)
|
||||
features_df = station_featured.tail(1).copy()
|
||||
features_df = anchor_row
|
||||
features_df["date"] = pd.Timestamp(target_date)
|
||||
# Overlay target-date weather
|
||||
for col in weather_data:
|
||||
|
||||
Reference in new issue
Block a user