|
| 1 | +import warnings |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from kloppy.config import get_config |
| 5 | +from kloppy.infra.serializers.event.impect import ( |
| 6 | + ImpectDeserializer, |
| 7 | + ImpectInputs, |
| 8 | +) |
| 9 | +from kloppy.domain import EventDataset, Optional, List, EventFactory |
| 10 | +from kloppy.io import open_as_file, FileLike, Source |
| 11 | + |
| 12 | + |
| 13 | +def load( |
| 14 | + event_data: FileLike, |
| 15 | + lineup_data: FileLike, |
| 16 | + squads_data: Optional[FileLike] = None, |
| 17 | + players_data: Optional[FileLike] = None, |
| 18 | + event_types: Optional[List[str]] = None, |
| 19 | + coordinates: Optional[str] = None, |
| 20 | + event_factory: Optional[EventFactory] = None, |
| 21 | +) -> EventDataset: |
| 22 | + """ |
| 23 | + Load Impect event data into a [`EventDataset`][kloppy.domain.models.event.EventDataset] |
| 24 | +
|
| 25 | + Args: |
| 26 | + event_data: JSON feed with the raw event data of a game. |
| 27 | + lineup_data: JSON feed with the corresponding lineup information of the game. |
| 28 | + squads_data: Optional JSON feed with squad information for team names. |
| 29 | + players_data: Optional JSON feed with player information for player names. |
| 30 | + event_types: A list of event types to load. When set, only the specified event types will be loaded. |
| 31 | + coordinates: The coordinate system to use. Defaults to "impect". See [`kloppy.domain.models.common.Provider`][kloppy.domain.models.common.Provider] for available options. |
| 32 | + event_factory: A custom event factory. When set, the factory is used to create event instances. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + The parsed event data. |
| 36 | + """ |
| 37 | + deserializer = ImpectDeserializer( |
| 38 | + event_types=event_types, |
| 39 | + coordinate_system=coordinates, |
| 40 | + event_factory=event_factory or get_config("event_factory"), |
| 41 | + ) |
| 42 | + with open_as_file(event_data) as event_data_fp, open_as_file( |
| 43 | + lineup_data |
| 44 | + ) as lineup_data_fp, open_as_file( |
| 45 | + Source.create(squads_data, optional=True) |
| 46 | + ) as squads_data_fp, open_as_file( |
| 47 | + Source.create(players_data, optional=True) |
| 48 | + ) as players_data_fp: |
| 49 | + return deserializer.deserialize( |
| 50 | + inputs=ImpectInputs( |
| 51 | + event_data=event_data_fp, |
| 52 | + meta_data=lineup_data_fp, |
| 53 | + squads_data=squads_data_fp, |
| 54 | + players_data=players_data_fp, |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def load_open_data( |
| 60 | + match_id: Union[str, int] = "122838", |
| 61 | + competition_id: Union[str, int] = "743", |
| 62 | + event_types: Optional[List[str]] = None, |
| 63 | + coordinates: Optional[str] = None, |
| 64 | + event_factory: Optional[EventFactory] = None, |
| 65 | +) -> EventDataset: |
| 66 | + """ |
| 67 | + Load Impect open data. |
| 68 | +
|
| 69 | + This function loads event data directly from the ImpectAPI open-data |
| 70 | + GitHub repository. |
| 71 | +
|
| 72 | + Parameters: |
| 73 | + match_id: The id of the match to load data for. Defaults to "100214". |
| 74 | + competition_id: The competition id to load squad and player names from. Defaults to "743". |
| 75 | + event_types: A list of event types to load. |
| 76 | + coordinates: The coordinate system to use. |
| 77 | + event_factory: A custom event factory. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + The parsed event data. |
| 81 | +
|
| 82 | + Examples: |
| 83 | + >>> from kloppy import impect |
| 84 | + >>> dataset = impect.load_open_data(match_id="100214") |
| 85 | + >>> df = dataset.to_df(engine="pandas") |
| 86 | + """ |
| 87 | + |
| 88 | + warnings.warn( |
| 89 | + "\n\nYou are about to use IMPECT public data." |
| 90 | + "\nBy using this data, you are agreeing to the user agreement. " |
| 91 | + "\nThe user agreement can be found here: https://github.com/ImpectAPI/open-data/blob/main/LICENSE.pdf" |
| 92 | + "\n" |
| 93 | + ) |
| 94 | + |
| 95 | + base_url = ( |
| 96 | + "https://raw.githubusercontent.com/ImpectAPI/open-data/main/data" |
| 97 | + ) |
| 98 | + |
| 99 | + return load( |
| 100 | + event_data=f"{base_url}/events/events_{match_id}.json", |
| 101 | + lineup_data=f"{base_url}/lineups/lineups_{match_id}.json", |
| 102 | + squads_data=f"{base_url}/squads/squads_{competition_id}.json", |
| 103 | + players_data=f"{base_url}/players/players_{competition_id}.json", |
| 104 | + event_types=event_types, |
| 105 | + coordinates=coordinates, |
| 106 | + event_factory=event_factory, |
| 107 | + ) |
0 commit comments