Skip to content

Commit

Permalink
rename to action value and give specific naming based on provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DriesDeprest committed Dec 18, 2024
1 parent cb4893e commit b8cb424
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
56 changes: 37 additions & 19 deletions kloppy/domain/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,44 +955,62 @@ def __post_init__(self):


@dataclass
class GameStateValue(Statistic):
"""Game state value"""
class ActionValue(Statistic):
"""Action value"""

gsv_scoring_before: Optional[float] = field(default=None)
gsv_scoring_after: Optional[float] = field(default=None)
gsv_conceding_before: Optional[float] = field(default=None)
gsv_conceding_after: Optional[float] = field(default=None)
provider: Provider
action_value_scoring_before: Optional[float] = field(default=None)
action_value_scoring_after: Optional[float] = field(default=None)
action_value_conceding_before: Optional[float] = field(default=None)
action_value_conceding_after: Optional[float] = field(default=None)

def __post_init__(self):
self.name = "GSV"
provider_action_value_mapping = {
Provider.STATSBOMB: "OBV",
Provider.STATSPERFORM: "PV",
}
self.name = provider_action_value_mapping.get(self.provider, "AV")

@property
def gsv_scoring_net(self) -> Optional[float]:
def offensive_value(self) -> Optional[float]:
return (
None
if None in (self.gsv_scoring_before, self.gsv_scoring_after)
else self.gsv_scoring_after - self.gsv_scoring_before
if None
in (
self.action_value_scoring_before,
self.action_value_scoring_after,
)
else self.action_value_scoring_after
- self.action_value_scoring_before
)

@property
def gsv_conceding_net(self) -> Optional[float]:
def defensive_value(self) -> Optional[float]:
return (
None
if None in (self.gsv_conceding_before, self.gsv_conceding_after)
else self.gsv_conceding_after - self.gsv_conceding_before
if None
in (
self.action_value_conceding_before,
self.action_value_conceding_after,
)
else self.action_value_conceding_after
- self.action_value_conceding_before
)

@property
def value(self) -> Optional[float]:
if None in (
self.gsv_scoring_before,
self.gsv_scoring_after,
self.gsv_conceding_before,
self.gsv_conceding_after,
self.action_value_scoring_before,
self.action_value_scoring_after,
self.action_value_conceding_before,
self.action_value_conceding_after,
):
return None
return (self.gsv_scoring_after - self.gsv_scoring_before) - (
self.gsv_conceding_after - self.gsv_conceding_before
return (
self.action_value_scoring_after - self.action_value_scoring_before
) - (
self.action_value_conceding_after
- self.action_value_conceding_before
)


Expand Down
17 changes: 9 additions & 8 deletions kloppy/infra/serializers/event/statsbomb/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Player,
PlayerData,
PositionType,
GameStateValue,
ActionValue,
Provider,
)
from kloppy.domain.services.frame_factory import create_frame
from kloppy.exceptions import DeserializationError
Expand All @@ -23,21 +24,21 @@ def parse_str_ts(timestamp: str) -> float:
return timedelta(seconds=int(h) * 3600 + int(m) * 60 + float(s))


def parse_obv_values(raw_event: dict) -> Optional[GameStateValue]:
game_state_values_data = {}
def parse_obv_values(raw_event: dict) -> Optional[ActionValue]:
game_state_values_data = {"provider": Provider.STATSBOMB}
obv_mapping = {
"obv_for_before": "gsv_scoring_before",
"obv_against_before": "gsv_conceding_before",
"obv_for_after": "gsv_scoring_after",
"obv_against_after": "gsv_conceding_after",
"obv_for_before": "action_value_scoring_before",
"obv_against_before": "action_value_conceding_before",
"obv_for_after": "action_value_scoring_after",
"obv_against_after": "action_value_conceding_after",
}
for sb_name, kloppy_name in obv_mapping.items():
obv_value = raw_event.get(sb_name)
if obv_value is not None:
game_state_values_data[kloppy_name] = obv_value

if game_state_values_data:
game_state_value = GameStateValue(**game_state_values_data)
game_state_value = ActionValue(**game_state_values_data)

return game_state_value

Expand Down

0 comments on commit b8cb424

Please sign in to comment.