File size: 1,686 Bytes
536ba3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations

import numpy as np
import pandas as pd

from src.evaluation.recommendations import (
    Recommendation,
    _generate_explanation,
    grid_search_recommendations,
    recommendations_to_dataframe,
)


def _predict_fn(df: pd.DataFrame) -> np.ndarray:
    # Higher RUL for lower current and room temperature
    return (
        200
        - (df["avg_current"].to_numpy() * 20)
        - np.abs(df["ambient_temperature"].to_numpy() - 24.0) * 2
        + (df["min_voltage"].to_numpy() * 5)
    )


def test_grid_search_recommendations_sorted():
    base = {
        "ambient_temperature": 24.0,
        "avg_current": 2.0,
        "min_voltage": 2.5,
        "cycle_number": 100,
    }
    recs = grid_search_recommendations(_predict_fn, base, top_k=3)
    assert len(recs) == 3
    assert recs[0].predicted_rul >= recs[1].predicted_rul
    assert isinstance(recs[0], Recommendation)


def test_generate_explanation_contains_keywords():
    text = _generate_explanation(
        temp=4.0,
        current=0.5,
        cutoff=2.7,
        pred_rul=150,
        baseline_rul=100,
        improvement_pct=50.0,
    )
    assert "improve RUL" in text
    assert "Cold ambient" in text


def test_recommendations_to_dataframe():
    recs = [
        Recommendation(
            rank=1,
            ambient_temperature=24.0,
            discharge_current=1.0,
            cutoff_voltage=2.7,
            predicted_rul=200.0,
            rul_improvement=20.0,
            rul_improvement_pct=10.0,
            explanation="ok",
        )
    ]
    df = recommendations_to_dataframe(recs)
    assert df.shape[0] == 1
    assert "Predicted RUL" in df.columns