File size: 1,915 Bytes
d6ea71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import pytest
from socceraction.vaep import VAEP
from socceraction.vaep import features as fs


@pytest.fixture(scope="session")
def vaep_model(sb_worldcup_data: pd.HDFStore) -> VAEP:
    # Test the vAEP framework on the StatsBomb World Cup data
    model = VAEP(nb_prev_actions=1)
    # comppute features and labels
    games = sb_worldcup_data["games"]
    features = pd.concat(
        [
            model.compute_features(game, sb_worldcup_data[f"actions/game_{game.game_id}"])
            for game in games.iloc[:-1].itertuples()
        ]
    )
    expected_features = set(fs.feature_column_names(model.xfns, model.nb_prev_actions))
    assert set(features.columns) == expected_features
    labels = pd.concat(
        [
            model.compute_labels(game, sb_worldcup_data[f"actions/game_{game.game_id}"])
            for game in games.iloc[:-1].itertuples()
        ]
    )
    expected_labels = {"scores", "concedes"}
    assert set(labels.columns) == expected_labels
    assert len(features) == len(labels)
    # fit the model
    model.fit(features, labels)
    return model


@pytest.mark.e2e
def test_predict(sb_worldcup_data: pd.HDFStore, vaep_model: VAEP) -> None:
    games = sb_worldcup_data["games"]
    game = games.iloc[-1]
    actions = sb_worldcup_data[f"actions/game_{game.game_id}"]
    ratings = vaep_model.rate(game, actions)
    expected_rating_columns = {"offensive_value", "defensive_value", "vaep_value"}
    assert set(ratings.columns) == expected_rating_columns


@pytest.mark.e2e
def test_predict_with_missing_features(sb_worldcup_data: pd.HDFStore, vaep_model: VAEP) -> None:
    games = sb_worldcup_data["games"]
    game = games.iloc[-1]
    actions = sb_worldcup_data[f"actions/game_{game.game_id}"]
    X = vaep_model.compute_features(game, actions)
    del X["period_id_a0"]
    with pytest.raises(ValueError):
        vaep_model.rate(game, actions, X)