Skip to content

Commit

Permalink
Merge pull request #12 from ivd-git/naming-consistency
Browse files Browse the repository at this point in the history
Naming consistency
  • Loading branch information
koenvo authored May 27, 2020
2 parents 0311b57 + 1013f8d commit 20edb6a
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 178 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
__pycache__/
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ from kloppy import (
)

# metrica data
data_set = load_metrica_tracking_data('home_file.csv', 'away_file.csv')
dataset = load_metrica_tracking_data('home_file.csv', 'away_file.csv')
# or tracab
data_set = load_tracab_tracking_data('meta.xml', 'raw_data.txt')
dataset = load_tracab_tracking_data('meta.xml', 'raw_data.txt')
# or epts
data_set = load_epts_tracking_data('meta.xml', 'raw_data.txt')
dataset = load_epts_tracking_data('meta.xml', 'raw_data.txt')

data_set = transform(data_set, pitch_dimensions=[[0, 108], [-34, 34]])
pandas_data_frame = to_pandas(data_set)
dataset = transform(dataset, pitch_dimensions=[[0, 108], [-34, 34]])
pandas_data_frame = to_pandas(dataset)
```

### <a name="datasets"></a>Public datasets / Very quick start
Expand All @@ -63,7 +63,7 @@ we added a "dataset loader" which does all the heavy lifting for you: find urls,
```python
from kloppy import datasets

data_set = datasets.load("metrica_tracking", options={'sample_rate': 1./12, 'limit': 10})
dataset = datasets.load("metrica_tracking", options={'sample_rate': 1./12, 'limit': 10})
```

### <a name="models"></a>Standardized models
Expand All @@ -84,7 +84,7 @@ serializer = TRACABSerializer()
with open("tracab_data.dat", "rb") as raw, \
open("tracab_metadata.xml", "rb") as meta:

data_set = serializer.deserialize(
dataset = serializer.deserialize(
inputs={
'raw_data': raw,
'meta_data': meta
Expand All @@ -94,7 +94,7 @@ with open("tracab_data.dat", "rb") as raw, \
}
)

# start working with data_set
# start working with dataset
```

or Metrica data
Expand All @@ -106,7 +106,7 @@ serializer = MetricaTrackingSerializer()
with open("Sample_Game_1_RawTrackingData_Away_Team.csv", "rb") as raw_away, \
open("Sample_Game_1_RawTrackingData_Home_Team.csv", "rb") as raw_home:

data_set = serializer.deserialize(
dataset = serializer.deserialize(
inputs={
'raw_data_home': raw_home,
'raw_data_away': raw_away
Expand All @@ -116,7 +116,7 @@ with open("Sample_Game_1_RawTrackingData_Away_Team.csv", "rb") as raw_away, \
}
)

# start working with data_set
# start working with dataset
```


Expand All @@ -129,7 +129,7 @@ serializer = EPTSSerializer()
with open("raw_data.txt", "rb") as raw, \
open("metadata.xml", "rb") as meta:

data_set = serializer.deserialize(
dataset = serializer.deserialize(
inputs={
'raw_data': raw,
'meta_data': meta
Expand All @@ -139,7 +139,7 @@ with open("raw_data.txt", "rb") as raw, \
}
)

# start working with data_set
# start working with dataset
```


Expand All @@ -148,9 +148,9 @@ Data providers use their own pitch dimensions. Some use actual meters while othe
```python
from kloppy.domain import Transformer, PitchDimensions, Dimension

# use deserialized `data_set`
new_data_set = Transformer.transform_data_set(
data_set,
# use deserialized `dataset`
new_dataset = Transformer.transform_dataset(
dataset,
to_pitch_dimensions=PitchDimensions(
x_dim=Dimension(0, 100),
y_dim=Dimension(0, 100)
Expand All @@ -166,8 +166,8 @@ Data providers can use different orientations. Some use a fixed orientation and
```python
from kloppy.domain import Transformer, Orientation

new_data_set = Transformer.transform_data_set(
data_set,
new_dataset = Transformer.transform_dataset(
dataset,
to_orientation=Orientation.BALL_OWNING_TEAM
)
```
Expand All @@ -176,9 +176,9 @@ new_data_set = Transformer.transform_data_set(
```python
from kloppy.domain import Transformer, PitchDimensions, Dimension, Orientation

# use deserialized `data_set`
new_data_set = Transformer.transform_data_set(
data_set,
# use deserialized `dataset`
new_dataset = Transformer.transform_dataset(
dataset,
to_pitch_dimensions=PitchDimensions(
x_dim=Dimension(0, 100),
y_dim=Dimension(0, 100)
Expand Down
8 changes: 4 additions & 4 deletions examples/datasets/metrica.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def main():
"""

# The metrica dataset loader loads by default the 'game1' dataset
data_set = datasets.load("metrica_tracking", options={'sample_rate': 1./12, 'limit': 10})
print(len(data_set.frames))
dataset = datasets.load("metrica_tracking", options={'sample_rate': 1./12, 'limit': 10})
print(len(dataset.frames))

# We can pass additional keyword arguments to the loaders to specify a different dataset
data_set = datasets.load("metrica_tracking", options={'limit': 1000}, game='game2')
dataset = datasets.load("metrica_tracking", options={'limit': 1000}, game='game2')

data_frame = to_pandas(data_set)
data_frame = to_pandas(dataset)
print(data_frame)


Expand Down
4 changes: 2 additions & 2 deletions examples/playing_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def main():
This example shows how to determine playing time
"""

data_set = datasets.load("metrica_tracking", options={'sample_rate': 1./25})
dataset = datasets.load("metrica_tracking", options={'sample_rate': 1./25})

playing_seconds_per_player = Counter()
for frame in data_set.frames:
for frame in dataset.frames:
playing_seconds_per_player.update([int(jersey_no) for jersey_no in frame.home_team_player_positions.keys()])

x = range(len(playing_seconds_per_player))
Expand Down
6 changes: 3 additions & 3 deletions kloppy/domain/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def set_attacking_direction(self, attacking_direction: AttackingDirection):
self.attacking_direction = attacking_direction


class DataSetFlag(Flag):
class DatasetFlag(Flag):
BALL_OWNING_TEAM = 1
BALL_STATE = 2

Expand All @@ -108,8 +108,8 @@ class DataRecord(ABC):


@dataclass
class DataSet(ABC):
flags: DataSetFlag
class Dataset(ABC):
flags: DatasetFlag
pitch_dimensions: PitchDimensions
orientation: Orientation
periods: List[Period]
Expand Down
4 changes: 2 additions & 2 deletions kloppy/domain/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Union

from .pitch import Point
from .common import DataRecord, DataSet, Team
from .common import DataRecord, Dataset, Team


class SubType(Enum):
Expand Down Expand Up @@ -226,7 +226,7 @@ def event_type(self) -> EventType:


@dataclass
class EventDataSet(DataSet):
class EventDataset(Dataset):
records: List[Union[
SetPieceEvent, ShotEvent
]]
Expand Down
4 changes: 2 additions & 2 deletions kloppy/domain/models/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Dict

from .common import (
DataSet,
Dataset,
DataRecord
)
from .pitch import Point
Expand All @@ -17,7 +17,7 @@ class Frame(DataRecord):


@dataclass
class TrackingDataSet(DataSet):
class TrackingDataset(Dataset):
frame_rate: int
records: List[Frame]

Expand Down
20 changes: 10 additions & 10 deletions kloppy/domain/services/enrichers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from dataclasses import dataclass

from ...models.tracking import DataSet as TrackingDataSet
from ...models.event import DataSet as EventDataSet
from ...models.common import DataSetFlag, Team, BallState
from ...models.tracking import Dataset as TrackingDataset
from ...models.event import Dataset as EventDataset
from ...models.common import DatasetFlag, Team, BallState


class TrackingPossessionEnricher:
def enrich_inplace(self, tracking_data_set: TrackingDataSet, event_data_set: EventDataSet) -> None:
def enrich_inplace(self, tracking_dataset: TrackingDataset, event_dataset: EventDataset) -> None:
"""
Return an enriched tracking data set.
Expand All @@ -16,20 +16,20 @@ def enrich_inplace(self, tracking_data_set: TrackingDataSet, event_data_set: Eve
they happen.
"""
if tracking_data_set.flags & (DataSetFlag.BALL_OWNING_TEAM | DataSetFlag.BALL_STATE):
if tracking_dataset.flags & (DatasetFlag.BALL_OWNING_TEAM | DatasetFlag.BALL_STATE):
return

if not event_data_set.flags & (DataSetFlag.BALL_OWNING_TEAM | DataSetFlag.BALL_STATE):
raise Exception("Event DataSet does not contain ball owning team or ball state information")
if not event_dataset.flags & (DatasetFlag.BALL_OWNING_TEAM | DatasetFlag.BALL_STATE):
raise Exception("Event Dataset does not contain ball owning team or ball state information")

# set some defaults
next_event_idx = 0
current_ball_owning_team = None
current_ball_state = None

for frame in tracking_data_set.records:
if next_event_idx < len(event_data_set.records):
event = event_data_set.records[next_event_idx]
for frame in tracking_dataset.records:
if next_event_idx < len(event_dataset.records):
event = event_dataset.records[next_event_idx]
if frame.period.id == event.period.id and frame.timestamp >= event.timestamp:
current_ball_owning_team = event.ball_owning_team
current_ball_state = event.ball_state
Expand Down
40 changes: 20 additions & 20 deletions kloppy/domain/services/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Frame,
Team, AttackingDirection,

TrackingDataSet, DataSetFlag, DataSet, # NOT YET: EventDataSet
TrackingDataset, DatasetFlag, Dataset, # NOT YET: EventDataset
)


Expand Down Expand Up @@ -78,43 +78,43 @@ def transform_frame(self, frame: Frame) -> Frame:
}
)

DataSetType = TypeVar('DataSetType')
DatasetType = TypeVar('DatasetType')

@classmethod
def transform_data_set(cls,
data_set: DataSetType,
def transform_dataset(cls,
dataset: DatasetType,
to_pitch_dimensions: PitchDimensions = None,
to_orientation: Orientation = None) -> DataSetType:
to_orientation: Orientation = None) -> DatasetType:
if not to_pitch_dimensions and not to_orientation:
return data_set
return dataset
elif not to_orientation:
to_orientation = data_set.orientation
to_orientation = dataset.orientation
elif not to_pitch_dimensions:
to_pitch_dimensions = data_set.pitch_dimensions
to_pitch_dimensions = dataset.pitch_dimensions

if to_orientation == Orientation.BALL_OWNING_TEAM:
if not data_set.flags & DataSetFlag.BALL_OWNING_TEAM:
if not dataset.flags & DatasetFlag.BALL_OWNING_TEAM:
raise ValueError("Cannot transform to BALL_OWNING_TEAM orientation when dataset doesn't contain "
"ball owning team data")

transformer = cls(
from_pitch_dimensions=data_set.pitch_dimensions,
from_orientation=data_set.orientation,
from_pitch_dimensions=dataset.pitch_dimensions,
from_orientation=dataset.orientation,
to_pitch_dimensions=to_pitch_dimensions,
to_orientation=to_orientation
)
if isinstance(data_set, TrackingDataSet):
frames = list(map(transformer.transform_frame, data_set.records))
if isinstance(dataset, TrackingDataset):
frames = list(map(transformer.transform_frame, dataset.records))

return TrackingDataSet(
flags=data_set.flags,
frame_rate=data_set.frame_rate,
periods=data_set.periods,
return TrackingDataset(
flags=dataset.flags,
frame_rate=dataset.frame_rate,
periods=dataset.periods,
pitch_dimensions=to_pitch_dimensions,
orientation=to_orientation,
records=frames
)
#elif isinstance(data_set, EventDataSet):
# raise Exception("EventDataSet transformer not implemented yet")
#elif isinstance(dataset, EventDataset):
# raise Exception("EventDataset transformer not implemented yet")
else:
raise Exception("Unknown DataSet type")
raise Exception("Unknown Dataset type")
Loading

0 comments on commit 20edb6a

Please sign in to comment.