fix(setup): enforce webcam crowd labels

This commit is contained in:
dasemu committed 2026-03-13 12:09:42 +01:00
1 parent f6a65f41d9
commit f63913b23c
5 files changed
+261 -31

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
"""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"
+69
View File
@@ -0,0 +1,69 @@
"""Unit tests for scripts/collect_historical_webcams.py."""
from __future__ import annotations
from datetime import date
import pandas as pd
from scripts.collect_historical_webcams import HistoricalWebcamCollector
class _StoreStub:
"""Minimal store stub for historical webcam collector tests."""
def __init__(self, station_df: pd.DataFrame, crowd_df: pd.DataFrame):
self._station_df = station_df
self._crowd_df = crowd_df
def read_dataframe(self, *_: object, **__: object) -> pd.DataFrame:
return self._station_df.copy()
def load_crowd_analyses(self, **_: object) -> pd.DataFrame:
return self._crowd_df.copy()
def _collector_with_store(store: _StoreStub) -> HistoricalWebcamCollector:
collector = object.__new__(HistoricalWebcamCollector)
collector.store = store
collector.get_available_date_range = lambda _station_id: None
return collector
def test_get_missing_dates_uses_analysis_date_column():
"""Missing-date detection must use analysis_date instead of timestamp."""
station_df = pd.DataFrame(
{
"date": pd.to_datetime(["2026-03-10", "2026-03-11", "2026-03-12"]),
"station_id": ["pajares", "pajares", "pajares"],
}
)
crowd_df = pd.DataFrame(
{
"analysis_date": ["2026-03-10", "2026-03-11"],
"station_id": ["pajares", "pajares"],
}
)
collector = _collector_with_store(_StoreStub(station_df, crowd_df))
missing = collector.get_missing_dates("pajares")
assert missing == [date(2026, 3, 12)]
def test_is_already_processed_uses_analysis_date_column():
"""Processed-day checks must match against analysis_date values."""
collector = _collector_with_store(
_StoreStub(
station_df=pd.DataFrame(),
crowd_df=pd.DataFrame(
{
"analysis_date": ["2026-03-10"],
"station_id": ["pajares"],
}
),
)
)
assert collector._is_already_processed("pajares", date(2026, 3, 10))
assert not collector._is_already_processed("pajares", date(2026, 3, 11))