mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 23:36:17 +00:00
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
"""Tests for scripts/build_full_dataset.py crowd-label logic."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pandas as pd
|
|
|
|
from scripts.build_full_dataset import _load_webcam_crowd_labels, load_station_data
|
|
|
|
|
|
class _FakeStore:
|
|
"""Minimal store stub for dataset-builder tests."""
|
|
|
|
def __init__(self, station_df: pd.DataFrame, crowd_df: pd.DataFrame):
|
|
self._station_df = station_df
|
|
self._crowd_df = crowd_df
|
|
|
|
def load_historical_station_raw(self) -> pd.DataFrame:
|
|
return self._station_df.copy()
|
|
|
|
def load_crowd_analyses(self, **_: object) -> pd.DataFrame:
|
|
return self._crowd_df.copy()
|
|
|
|
|
|
def _station_raw_row() -> pd.DataFrame:
|
|
return pd.DataFrame(
|
|
{
|
|
"date": ["2026-03-10"],
|
|
"station_id": ["pajares"],
|
|
"idestado": [1.0],
|
|
"kmopen": [20.0],
|
|
"pistasopen": [30.0],
|
|
"pistastotal": [40.0],
|
|
"espesormax": [120.0],
|
|
"calidadnieve": ["polvo"],
|
|
}
|
|
)
|
|
|
|
|
|
def test_crowd_level_is_sourced_from_webcam_labels():
|
|
"""Crowd labels must be read from crowd_analysis data."""
|
|
crowd_df = pd.DataFrame(
|
|
{
|
|
"analyzed_at": ["2026-03-10T10:00:00"],
|
|
"analysis_date": ["2026-03-10"],
|
|
"station_id": ["pajares"],
|
|
"crowd_density": ["low"],
|
|
}
|
|
)
|
|
store = _FakeStore(_station_raw_row(), crowd_df)
|
|
|
|
result = load_station_data(store)
|
|
|
|
assert result.iloc[0]["crowd_level"] == "low"
|
|
assert result.iloc[0]["crowd_level_source"] == "webcam"
|
|
|
|
|
|
def test_crowd_level_is_missing_when_webcam_label_is_missing():
|
|
"""Without webcam data, crowd labels must remain null."""
|
|
store = _FakeStore(_station_raw_row(), pd.DataFrame())
|
|
|
|
result = load_station_data(store)
|
|
|
|
assert pd.isna(result.iloc[0]["crowd_level"])
|
|
assert pd.isna(result.iloc[0]["crowd_level_source"])
|
|
|
|
|
|
def test_load_webcam_labels_keeps_latest_valid_label_per_day():
|
|
"""The latest valid label must be used when multiple rows exist per day."""
|
|
crowd_df = pd.DataFrame(
|
|
{
|
|
"analyzed_at": [
|
|
"2026-03-10T09:00:00",
|
|
"2026-03-10T10:00:00",
|
|
"2026-03-10T11:00:00",
|
|
],
|
|
"analysis_date": ["2026-03-10", "2026-03-10", "2026-03-10"],
|
|
"station_id": ["pajares", "pajares", "pajares"],
|
|
"crowd_density": ["low", "medium", "unknown"],
|
|
}
|
|
)
|
|
store = _FakeStore(_station_raw_row(), crowd_df)
|
|
|
|
labels = _load_webcam_crowd_labels(store)
|
|
|
|
assert len(labels) == 1
|
|
assert labels.iloc[0]["webcam_crowd_level"] == "medium"
|