-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
379 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Dict, Any, Hashable, List, NamedTuple | ||
|
||
from kloppy.domain import EventDataset | ||
|
||
|
||
class EventDatasetAggregator(ABC): | ||
@abstractmethod | ||
def aggregate(self, dataset: EventDataset) -> List[NamedTuple]: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from datetime import timedelta | ||
from typing import Dict, List, NamedTuple, Union, Optional | ||
|
||
from kloppy.domain import EventDataset, Player, Position, Time | ||
from kloppy.domain.services.aggregators.aggregator import ( | ||
EventDatasetAggregator, | ||
) | ||
|
||
|
||
class MinutesPlayed(NamedTuple): | ||
player: Player | ||
start_time: Time | ||
end_time: Time | ||
duration: timedelta | ||
|
||
|
||
class MinutesPlayedPerPosition(NamedTuple): | ||
player: Player | ||
position: Position | ||
start_time: Time | ||
end_time: Time | ||
duration: timedelta | ||
|
||
|
||
class MinutesPlayedAggregator(EventDatasetAggregator): | ||
def __init__(self, aggregate_position: bool = True): | ||
self.aggregate_position = aggregate_position | ||
|
||
def aggregate( | ||
self, dataset: EventDataset | ||
) -> List[Union[MinutesPlayedPerPosition, MinutesPlayed]]: | ||
items = [] | ||
|
||
for team in dataset.metadata.teams: | ||
for player in team.players: | ||
if self.aggregate_position: | ||
_start_time = None | ||
end_time = None | ||
for ( | ||
start_time, | ||
end_time, | ||
position, | ||
) in player.positions.ranges(): | ||
if not _start_time: | ||
_start_time = start_time | ||
|
||
if _start_time: | ||
items.append( | ||
MinutesPlayed( | ||
player=player, | ||
start_time=_start_time, | ||
end_time=_start_time, | ||
duration=end_time - _start_time, | ||
) | ||
) | ||
else: | ||
for ( | ||
start_time, | ||
end_time, | ||
position, | ||
) in player.positions.ranges(): | ||
items.append( | ||
MinutesPlayedPerPosition( | ||
player=player, | ||
position=position, | ||
start_time=start_time, | ||
end_time=end_time, | ||
duration=end_time - start_time, | ||
) | ||
) | ||
|
||
return items |
Oops, something went wrong.