34
tests/ml/test_explanation.py
Normal file
34
tests/ml/test_explanation.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.ml.explanation import explain_feature
|
||||
from app.ml.training import FeatureModel
|
||||
|
||||
|
||||
def _model(slope: float) -> FeatureModel:
|
||||
return FeatureModel(
|
||||
sample_count=4,
|
||||
mean=20.0,
|
||||
standard_deviation=1.0,
|
||||
minimum=18.0,
|
||||
maximum=22.0,
|
||||
slope=slope,
|
||||
intercept=18.5,
|
||||
)
|
||||
|
||||
|
||||
def test_explain_feature_describes_rising_forecast() -> None:
|
||||
explanation = explain_feature("temperature", 21.0, 21.5, _model(0.5))
|
||||
|
||||
assert explanation.direction == "steigend"
|
||||
assert explanation.change == 0.5
|
||||
assert explanation.historical_range == (18.0, 22.0)
|
||||
assert "4 Messwerte" in explanation.summary
|
||||
assert "Trend +0.500" in explanation.summary
|
||||
|
||||
|
||||
def test_explain_feature_describes_stable_and_falling_forecasts() -> None:
|
||||
stable = explain_feature("humidity", 50.0, 50.0, _model(0.0))
|
||||
falling = explain_feature("temperature", 21.0, 20.5, _model(-0.5))
|
||||
|
||||
assert stable.direction == "stabil"
|
||||
assert falling.direction == "fallend"
|
||||
@@ -33,6 +33,11 @@ def test_predict_returns_statistical_forecast() -> None:
|
||||
assert result.predictions == {"temperature": 22.0}
|
||||
assert 0.0 < result.confidence <= 1.0
|
||||
assert result.model_type == "statistical_baseline"
|
||||
explanation = result.explanations["temperature"]
|
||||
assert explanation.direction == "steigend"
|
||||
assert explanation.current_value == 21.0
|
||||
assert explanation.predicted_value == 22.0
|
||||
assert explanation.sample_count == 2
|
||||
|
||||
|
||||
def test_predict_rejects_unknown_sensor() -> None:
|
||||
|
||||
Reference in New Issue
Block a user