def __create_iterator(self, data: Readable, sample_rate: float, frame_rate: int) -> Iterator: """ Notes: 1. the y-axis is flipped because Metrica use (y, -y) instead of (-y, y) """ team = None frame_idx = 0 frame_sample = 1 / sample_rate player_jersey_numbers = [] period = None for i, line in enumerate(data): line = line.strip().decode("ascii") columns = line.split(",") if i == 0: team = columns[3] elif i == 1: player_jersey_numbers = columns[3:-2:2] elif i == 2: # consider doing some validation on the columns pass else: period_id = int(columns[0]) frame_id = int(columns[1]) if period is None or period.id != period_id: period = Period( id=period_id, start_timestamp=frame_id / frame_rate, end_timestamp=frame_id / frame_rate, ) else: # consider not update this every frame for performance reasons period.end_timestamp = frame_id / frame_rate if frame_idx % frame_sample == 0: yield self.__PartialFrame( team=team, period=period, frame_id=frame_id, player_positions={ player_no: Point( x=float(columns[3 + i * 2]), y=1 - float(columns[3 + i * 2 + 1]), ) for i, player_no in enumerate( player_jersey_numbers) if columns[3 + i * 2] != "NaN" }, ball_position=Point(x=float(columns[-2]), y=1 - float(columns[-1])) if columns[-2] != "NaN" else None, ) frame_idx += 1
def deserialize(self, inputs: SportsCodeInputs) -> CodeDataset: all_instances = objectify.fromstring(inputs.data.read()) codes = [] period = Period(id=1, start_timestamp=0, end_timestamp=0) for instance in all_instances.ALL_INSTANCES.iterchildren(): end_timestamp = float(instance.end) code = Code( period=period, code_id=str(instance.ID), code=str(instance.code), timestamp=float(instance.start), end_timestamp=end_timestamp, labels={ str(label.find("group")): parse_value(str(label.find("text"))) for label in instance.iterchildren("label") }, ball_state=None, ball_owning_team=None, ) period.end_timestamp = end_timestamp codes.append(code) return CodeDataset( metadata=Metadata( teams=[], periods=[period], pitch_dimensions=None, score=Score(0, 0), frame_rate=0.0, orientation=Orientation.NOT_SET, flags=~(DatasetFlag.BALL_OWNING_TEAM | DatasetFlag.BALL_STATE), provider=Provider.OTHER, coordinate_system=None, ), records=codes, )
def deserialize( self, inputs: Dict[str, Readable], options: Dict = None ) -> EventDataset: """ Deserialize StatsBomb event data into a `EventDataset`. Parameters ---------- inputs : dict input `event_data` should point to a `Readable` object containing the 'json' formatted event data. input `lineup_data` should point to a `Readable` object containing the 'json' formatted lineup data. options : dict Options for deserialization of the StatsBomb file. Possible options are `event_types` (list of event types) to specify the event types that should be returned. Valid types: "shot", "pass", "carry", "take_on" and "generic". Generic is everything other than the first 4. Those events are barely parsed. This type of event can be used to do the parsing yourself. Every event has a 'raw_event' attribute which contains the original dictionary. Returns ------- dataset : EventDataset Raises ------ See Also -------- Examples -------- >>> serializer = StatsBombSerializer() >>> with open("events/12312312.json", "rb") as event_data, \ >>> open("lineups/123123123.json", "rb") as lineup_data: >>> >>> dataset = serializer.deserialize( >>> inputs={ >>> 'event_data': event_data, >>> 'lineup_data': lineup_data >>> }, >>> options={ >>> 'event_types': ["pass", "take_on", "carry", "shot"] >>> } >>> ) """ self.__validate_inputs(inputs) if not options: options = {} with performance_logging("load data", logger=logger): raw_events = json.load(inputs["event_data"]) home_lineup, away_lineup = json.load(inputs["lineup_data"]) ( shot_fidelity_version, xy_fidelity_version, ) = _determine_xy_fidelity_versions(raw_events) logger.info( f"Determined Fidelity versions: shot v{shot_fidelity_version} / XY v{xy_fidelity_version}" ) with performance_logging("parse data", logger=logger): home_team = Team( team_id=str(home_lineup["team_id"]), name=home_lineup["team_name"], ground=Ground.HOME, ) home_team.players = [ Player( player_id=str(player["player_id"]), team=home_team, name=player["player_name"], jersey_no=int(player["jersey_number"]), ) for player in home_lineup["lineup"] ] away_team = Team( team_id=str(away_lineup["team_id"]), name=away_lineup["team_name"], ground=Ground.AWAY, ) away_team.players = [ Player( player_id=str(player["player_id"]), team=away_team, name=player["player_name"], jersey_no=int(player["jersey_number"]), ) for player in away_lineup["lineup"] ] teams = [home_team, away_team] wanted_event_types = [ EventType[event_type.upper()] for event_type in options.get("event_types", []) ] periods = [] period = None events = [] for raw_event in raw_events: if raw_event["team"]["id"] == home_lineup["team_id"]: team = teams[0] elif raw_event["team"]["id"] == away_lineup["team_id"]: team = teams[1] else: raise Exception( f"Unknown team_id {raw_event['team']['id']}" ) if ( raw_event["possession_team"]["id"] == home_lineup["team_id"] ): possession_team = teams[0] elif ( raw_event["possession_team"]["id"] == away_lineup["team_id"] ): possession_team = teams[1] else: raise Exception( f"Unknown possession_team_id: {raw_event['possession_team']}" ) timestamp = parse_str_ts(raw_event["timestamp"]) period_id = int(raw_event["period"]) if not period or period.id != period_id: period = Period( id=period_id, start_timestamp=( timestamp if not period # period = [start, end], add millisecond to prevent overlapping else timestamp + period.end_timestamp + 0.001 ), end_timestamp=None, ) periods.append(period) else: period.end_timestamp = period.start_timestamp + timestamp player = None if "player" in raw_event: player = team.get_player_by_id(raw_event["player"]["id"]) event_type = raw_event["type"]["id"] if event_type == SB_EVENT_TYPE_SHOT: fidelity_version = shot_fidelity_version elif event_type in ( SB_EVENT_TYPE_CARRY, SB_EVENT_TYPE_DRIBBLE, SB_EVENT_TYPE_PASS, ): fidelity_version = xy_fidelity_version else: # TODO: Uh ohhhh.. don't know which one to pick fidelity_version = xy_fidelity_version generic_event_kwargs = dict( # from DataRecord period=period, timestamp=timestamp, ball_owning_team=possession_team, ball_state=BallState.ALIVE, # from Event event_id=raw_event["id"], team=team, player=player, coordinates=( _parse_coordinates( raw_event.get("location"), fidelity_version ) if "location" in raw_event else None ), raw_event=raw_event, ) if event_type == SB_EVENT_TYPE_PASS: pass_event_kwargs = _parse_pass( pass_dict=raw_event["pass"], team=team, fidelity_version=fidelity_version, ) event = PassEvent( # TODO: Consider moving this to _parse_pass receive_timestamp=timestamp + raw_event["duration"], **pass_event_kwargs, **generic_event_kwargs, ) elif event_type == SB_EVENT_TYPE_SHOT: shot_event_kwargs = _parse_shot( shot_dict=raw_event["shot"] ) event = ShotEvent( **shot_event_kwargs, **generic_event_kwargs ) # For dribble and carry the definitions # are flipped between Statsbomb and kloppy elif event_type == SB_EVENT_TYPE_DRIBBLE: take_on_event_kwargs = _parse_take_on( take_on_dict=raw_event["dribble"] ) event = TakeOnEvent( **take_on_event_kwargs, **generic_event_kwargs ) elif event_type == SB_EVENT_TYPE_CARRY: carry_event_kwargs = _parse_carry( carry_dict=raw_event["carry"], fidelity_version=fidelity_version, ) event = CarryEvent( # TODO: Consider moving this to _parse_carry end_timestamp=timestamp + raw_event["duration"], **carry_event_kwargs, **generic_event_kwargs, ) else: event = GenericEvent( result=None, event_name=raw_event["type"]["name"], **generic_event_kwargs, ) if ( not wanted_event_types or event.event_type in wanted_event_types ): events.append(event) metadata = Metadata( teams=teams, periods=periods, pitch_dimensions=PitchDimensions( x_dim=Dimension(0, 120), y_dim=Dimension(0, 80) ), frame_rate=None, orientation=Orientation.ACTION_EXECUTING_TEAM, flags=DatasetFlag.BALL_OWNING_TEAM, score=None, ) return EventDataset(metadata=metadata, records=events,)
def deserialize(self, inputs: Dict[str, Readable], options: Dict = None) -> EventDataset: """ Deserialize StatsBomb event data into a `EventDataset`. Parameters ---------- inputs : dict input `event_data` should point to a `Readable` object containing the 'json' formatted event data. input `lineup_data` should point to a `Readable` object containing the 'json' formatted lineup data. options : dict Options for deserialization of the StatsBomb file. Possible options are `event_types` (list of event types) to specify the event types that should be returned. Valid types: "shot", "pass", "carry", "take_on" and "generic". Generic is everything other than the first 4. Those events are barely parsed. This type of event can be used to do the parsing yourself. Every event has a 'raw_event' attribute which contains the original dictionary. Returns ------- dataset : EventDataset Raises ------ See Also -------- Examples -------- >>> serializer = StatsBombSerializer() >>> with open("events/12312312.json", "rb") as event_data, \ >>> open("lineups/123123123.json", "rb") as lineup_data: >>> >>> dataset = serializer.deserialize( >>> inputs={ >>> 'event_data': event_data, >>> 'lineup_data': lineup_data >>> }, >>> options={ >>> 'event_types': ["pass", "take_on", "carry", "shot"] >>> } >>> ) """ self.__validate_inputs(inputs) if not options: options = {} with performance_logging("load data", logger=logger): raw_events = json.load(inputs['event_data']) home_lineup, away_lineup = json.load(inputs['lineup_data']) shot_fidelity_version, xy_fidelity_version = _determine_xy_fidelity_versions( raw_events) logger.info( f"Determined Fidelity versions: shot v{shot_fidelity_version} / XY v{xy_fidelity_version}" ) with performance_logging("parse data", logger=logger): home_player_map = { player['player_id']: str(player['jersey_number']) for player in home_lineup['lineup'] } away_player_map = { player['player_id']: str(player['jersey_number']) for player in away_lineup['lineup'] } wanted_event_types = [ EventType[event_type.upper()] for event_type in options.get('event_types', []) ] periods = [] period = None events = [] for raw_event in raw_events: if raw_event['team']['id'] == home_lineup['team_id']: team = Team.HOME current_team_map = home_player_map elif raw_event['team']['id'] == away_lineup['team_id']: team = Team.AWAY current_team_map = away_player_map else: raise Exception( f"Unknown team_id {raw_event['team']['id']}") if raw_event['possession_team']['id'] == home_lineup[ 'team_id']: possession_team = Team.HOME elif raw_event['possession_team']['id'] == away_lineup[ 'team_id']: possession_team = Team.AWAY else: raise Exception( f"Unknown possession_team_id: {raw_event['possession_team']}" ) timestamp = parse_str_ts(raw_event['timestamp']) period_id = int(raw_event['period']) if not period or period.id != period_id: period = Period(id=period_id, start_timestamp=timestamp if not period else timestamp + period.end_timestamp, end_timestamp=None) periods.append(period) else: period.end_timestamp = period.start_timestamp + timestamp player_jersey_no = None if 'player' in raw_event: player_jersey_no = current_team_map[raw_event['player'] ['id']] event_type = raw_event['type']['id'] if event_type == SB_EVENT_TYPE_SHOT: fidelity_version = shot_fidelity_version elif event_type in (SB_EVENT_TYPE_CARRY, SB_EVENT_TYPE_DRIBBLE, SB_EVENT_TYPE_PASS): fidelity_version = xy_fidelity_version else: # TODO: Uh ohhhh.. don't know which one to pick fidelity_version = xy_fidelity_version generic_event_kwargs = dict( # from DataRecord period=period, timestamp=timestamp, ball_owning_team=possession_team, ball_state=BallState.ALIVE, # from Event event_id=raw_event['id'], team=team, player_jersey_no=player_jersey_no, position=(_parse_position(raw_event.get('location'), fidelity_version) if 'location' in raw_event else None), raw_event=raw_event) if event_type == SB_EVENT_TYPE_PASS: pass_event_kwargs = _parse_pass( pass_dict=raw_event['pass'], current_team_map=current_team_map, fidelity_version=fidelity_version) event = PassEvent( # TODO: Consider moving this to _parse_pass receive_timestamp=timestamp + raw_event['duration'], **pass_event_kwargs, **generic_event_kwargs) elif event_type == SB_EVENT_TYPE_SHOT: shot_event_kwargs = _parse_shot( shot_dict=raw_event['shot']) event = ShotEvent(**shot_event_kwargs, **generic_event_kwargs) # For dribble and carry the definitions # are flipped between Statsbomb and kloppy elif event_type == SB_EVENT_TYPE_DRIBBLE: take_on_event_kwargs = _parse_take_on( take_on_dict=raw_event['dribble']) event = TakeOnEvent(**take_on_event_kwargs, **generic_event_kwargs) elif event_type == SB_EVENT_TYPE_CARRY: carry_event_kwargs = _parse_carry( carry_dict=raw_event['carry'], fidelity_version=fidelity_version) event = CarryEvent( # TODO: Consider moving this to _parse_carry end_timestamp=timestamp + raw_event['duration'], **carry_event_kwargs, **generic_event_kwargs) else: event = GenericEvent(result=None, **generic_event_kwargs) if not wanted_event_types or event.event_type in wanted_event_types: events.append(event) return EventDataset(flags=DatasetFlag.BALL_OWNING_TEAM, orientation=Orientation.ACTION_EXECUTING_TEAM, pitch_dimensions=PitchDimensions( x_dim=Dimension(0, 120), y_dim=Dimension(0, 80)), periods=periods, records=events)
def deserialize(self, inputs: Dict[str, Readable], options: Dict = None) -> EventDataSet: self.__validate_inputs(inputs) periods = [] period = None events = [] game_state = self.__GameState(ball_state=BallState.DEAD, ball_owning_team=None) reader = csv.DictReader( map(lambda x: x.decode('utf-8'), inputs['raw_data'])) for event_id, record in enumerate(reader): event_type = event_type_map[record['Type']] subtypes = record['Subtype'].split('-') start_timestamp = float(record['Start Time [s]']) end_timestamp = float(record['End Time [s]']) period_id = int(record['Period']) if not period or period.id != period_id: period = Period(id=period_id, start_timestamp=start_timestamp, end_timestamp=end_timestamp) periods.append(period) else: period.end_timestamp = end_timestamp if record['Team'] == 'Home': team = Team.HOME elif record['Team'] == 'Away': team = Team.AWAY else: raise ValueError(f'Unknown team: {record["team"]}') event_kwargs = dict( # From DataRecord: timestamp=start_timestamp, ball_owning_team=None, ## todo ball_state=None, # todo period=period, # From Event: event_id=event_id, team=team, end_timestamp=end_timestamp, player_jersey_no=record['From'][6:], position=Point(x=float(record['Start X']), y=1 - float(record['Start Y'])) if record['Start X'] != 'NaN' else None, ) secondary_position = None if record['End X'] != 'NaN': secondary_position = Point(x=float(record['End X']), y=1 - float(record['End Y'])) secondary_jersey_no = None if record['To']: secondary_jersey_no = record['To'][6:] event = None if event_type == EventType.SET_PIECE: set_piece, fk_attempt, retaken = \ build_subtypes(subtypes, [SetPiece, FKAttempt, Retaken]) event = SetPieceEvent(**event_kwargs) elif event_type == EventType.RECOVERY: interference1, interference2 = \ build_subtypes(subtypes, [Interference1, Interference2]) event = RecoveryEvent(**event_kwargs) elif event_type == EventType.PASS: body_part, attempt, deflection, offside = \ build_subtypes(subtypes, [BodyPart, Attempt, Deflection, Offside]) event = PassEvent( receiver_position=secondary_position, receiver_player_jersey_no=secondary_jersey_no, **event_kwargs) elif event_type == EventType.BALL_LOST: body_part, attempt, interference1, intervention, deflection, offside = \ build_subtypes(subtypes, [ BodyPart, Attempt, Interference1, Intervention, Deflection, Offside ]) event = BallLossEvent(**event_kwargs) elif event_type == EventType.BALL_OUT: body_part, attempt, intervention, deflection, offside, own_goal = \ build_subtypes(subtypes, [ BodyPart, Attempt, Intervention, Deflection, Offside, OwnGoal ]) event = BallOutEvent(**event_kwargs) elif event_type == EventType.SHOT: body_part, deflection, shot_direction, shot_result, offside = \ build_subtypes(subtypes, [ BodyPart, Deflection, ShotDirection, ShotResult, Offside ]) event = ShotEvent(shot_result=shot_result, **event_kwargs) elif event_type == EventType.FAULT_RECEIVED: event = FaultReceivedEvent(**event_kwargs) elif event_type == EventType.CHALLENGE: challenge, fault, challenge_result = \ build_subtypes(subtypes, [Challenge, Fault, ChallengeResult]) event = ChallengeEvent(**event_kwargs) elif event_type == EventType.CARD: card, = build_subtypes(subtypes, [Card]) event = CardEvent(**event_kwargs) else: raise NotImplementedError( f"EventType {event_type} not implemented") # We want to attach the game_state after the event to the event game_state = self.__reduce_game_state(event=event, game_state=game_state) event.ball_state = game_state.ball_state event.ball_owning_team = game_state.ball_owning_team events.append(event) orientation = ( Orientation.FIXED_HOME_AWAY if periods[0].attacking_direction == AttackingDirection.HOME_AWAY else Orientation.FIXED_AWAY_HOME) return EventDataSet( flags=DataSetFlag.BALL_STATE | DataSetFlag.BALL_OWNING_TEAM, orientation=orientation, pitch_dimensions=PitchDimensions(x_dim=Dimension(0, 1), y_dim=Dimension(0, 1)), periods=periods, records=events)
def deserialize(self, inputs: StatsbombInputs) -> EventDataset: transformer = self.get_transformer(length=120, width=80) with performance_logging("load data", logger=logger): raw_events = json.load(inputs.event_data) home_lineup, away_lineup = json.load(inputs.lineup_data) ( shot_fidelity_version, xy_fidelity_version, ) = _determine_xy_fidelity_versions(raw_events) logger.info( f"Determined Fidelity versions: shot v{shot_fidelity_version} / XY v{xy_fidelity_version}" ) with performance_logging("parse data", logger=logger): starting_player_ids = { str(player["player"]["id"]) for raw_event in raw_events if raw_event["type"]["id"] == SB_EVENT_TYPE_STARTING_XI for player in raw_event["tactics"]["lineup"] } starting_formations = { raw_event["team"]["id"]: FormationType("-".join( list(str(raw_event["tactics"]["formation"])))) for raw_event in raw_events if raw_event["type"]["id"] == SB_EVENT_TYPE_STARTING_XI } home_team = Team( team_id=str(home_lineup["team_id"]), name=home_lineup["team_name"], ground=Ground.HOME, starting_formation=starting_formations[home_lineup["team_id"]], ) home_team.players = [ Player( player_id=str(player["player_id"]), team=home_team, name=player["player_name"], jersey_no=int(player["jersey_number"]), starting=str(player["player_id"]) in starting_player_ids, ) for player in home_lineup["lineup"] ] away_team = Team( team_id=str(away_lineup["team_id"]), name=away_lineup["team_name"], ground=Ground.AWAY, starting_formation=starting_formations[away_lineup["team_id"]], ) away_team.players = [ Player( player_id=str(player["player_id"]), team=away_team, name=player["player_name"], jersey_no=int(player["jersey_number"]), starting=str(player["player_id"]) in starting_player_ids, ) for player in away_lineup["lineup"] ] teams = [home_team, away_team] periods = [] period = None events = [] for raw_event in raw_events: if raw_event["team"]["id"] == home_lineup["team_id"]: team = home_team elif raw_event["team"]["id"] == away_lineup["team_id"]: team = away_team else: raise DeserializationError( f"Unknown team_id {raw_event['team']['id']}") if (raw_event["possession_team"]["id"] == home_lineup["team_id"]): possession_team = home_team elif (raw_event["possession_team"]["id"] == away_lineup["team_id"]): possession_team = away_team else: raise DeserializationError( f"Unknown possession_team_id: {raw_event['possession_team']}" ) timestamp = parse_str_ts(raw_event["timestamp"]) period_id = int(raw_event["period"]) if not period or period.id != period_id: period = Period( id=period_id, start_timestamp=( timestamp if not period # period = [start, end], add millisecond to prevent overlapping else timestamp + period.end_timestamp + 0.001), end_timestamp=None, ) periods.append(period) else: period.end_timestamp = period.start_timestamp + timestamp player = None if "player" in raw_event: player = team.get_player_by_id(raw_event["player"]["id"]) event_type = raw_event["type"]["id"] if event_type == SB_EVENT_TYPE_SHOT: fidelity_version = shot_fidelity_version elif event_type in ( SB_EVENT_TYPE_CARRY, SB_EVENT_TYPE_DRIBBLE, SB_EVENT_TYPE_PASS, ): fidelity_version = xy_fidelity_version else: # TODO: Uh ohhhh.. don't know which one to pick fidelity_version = xy_fidelity_version generic_event_kwargs = { # from DataRecord "period": period, "timestamp": timestamp, "ball_owning_team": possession_team, "ball_state": BallState.ALIVE, # from Event "event_id": raw_event["id"], "team": team, "player": player, "coordinates": (_parse_coordinates( raw_event.get("location"), fidelity_version, ) if "location" in raw_event else None), "related_event_ids": raw_event.get("related_events", []), "raw_event": raw_event, } new_events = [] if event_type == SB_EVENT_TYPE_PASS: pass_event_kwargs = _parse_pass( pass_dict=raw_event["pass"], team=team, fidelity_version=fidelity_version, ) pass_event = PassEvent.create( # TODO: Consider moving this to _parse_pass receive_timestamp=timestamp + raw_event["duration"], **pass_event_kwargs, **generic_event_kwargs, ) new_events.append(pass_event) elif event_type == SB_EVENT_TYPE_SHOT: shot_event_kwargs = _parse_shot( shot_dict=raw_event["shot"], ) shot_event = ShotEvent.create( **shot_event_kwargs, **generic_event_kwargs, ) new_events.append(shot_event) # For dribble and carry the definitions # are flipped between Statsbomb and kloppy elif event_type == SB_EVENT_TYPE_DRIBBLE: take_on_event_kwargs = _parse_take_on( take_on_dict=raw_event["dribble"], ) take_on_event = TakeOnEvent.create( qualifiers=None, **take_on_event_kwargs, **generic_event_kwargs, ) new_events.append(take_on_event) elif event_type == SB_EVENT_TYPE_CARRY: carry_event_kwargs = _parse_carry( carry_dict=raw_event["carry"], fidelity_version=fidelity_version, ) carry_event = CarryEvent.create( qualifiers=None, # TODO: Consider moving this to _parse_carry end_timestamp=timestamp + raw_event.get("duration", 0), **carry_event_kwargs, **generic_event_kwargs, ) new_events.append(carry_event) # lineup affecting events elif event_type == SB_EVENT_TYPE_SUBSTITUTION: substitution_event_kwargs = _parse_substitution( substitution_dict=raw_event["substitution"], team=team, ) substitution_event = SubstitutionEvent.create( result=None, qualifiers=None, **substitution_event_kwargs, **generic_event_kwargs, ) new_events.append(substitution_event) elif event_type == SB_EVENT_TYPE_BAD_BEHAVIOUR: bad_behaviour_kwargs = _parse_bad_behaviour( bad_behaviour_dict=raw_event.get("bad_behaviour", {}), ) if "card" in bad_behaviour_kwargs: card_kwargs = bad_behaviour_kwargs["card"] card_event = CardEvent.create( result=None, qualifiers=None, card_type=card_kwargs["card_type"], **generic_event_kwargs, ) new_events.append(card_event) elif event_type == SB_EVENT_TYPE_FOUL_COMMITTED: foul_committed_kwargs = _parse_foul_committed( foul_committed_dict=raw_event.get( "foul_committed", {}), ) foul_committed_event = FoulCommittedEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) new_events.append(foul_committed_event) if "card" in foul_committed_kwargs: card_kwargs = foul_committed_kwargs["card"] card_event = CardEvent.create( result=None, qualifiers=None, card_type=card_kwargs["card_type"], **generic_event_kwargs, ) new_events.append(card_event) elif event_type == SB_EVENT_TYPE_PLAYER_ON: player_on_event = PlayerOnEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) new_events.append(player_on_event) elif event_type == SB_EVENT_TYPE_PLAYER_OFF: player_off_event = PlayerOffEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) new_events.append(player_off_event) elif event_type == SB_EVENT_TYPE_RECOVERY: recovery_event = RecoveryEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) new_events.append(recovery_event) elif event_type == SB_EVENT_TYPE_FORMATION_CHANGE: formation_change_event_kwargs = _parse_formation_change( raw_event["tactics"]["formation"]) formation_change_event = FormationChangeEvent.create( result=None, qualifiers=None, **formation_change_event_kwargs, **generic_event_kwargs, ) new_events.append(formation_change_event) # rest: generic else: generic_event = GenericEvent.create( result=None, qualifiers=None, event_name=raw_event["type"]["name"], **generic_event_kwargs, ) new_events.append(generic_event) for event in new_events: if self.should_include_event(event): transformed_event = transformer.transform_event(event) events.append(transformed_event) # Checks if the event ended out of the field and adds a synthetic out event if event.result in OUT_EVENT_RESULTS: generic_event_kwargs["ball_state"] = BallState.DEAD if event.receiver_coordinates: generic_event_kwargs[ "coordinates"] = event.receiver_coordinates ball_out_event = BallOutEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) if self.should_include_event(ball_out_event): transformed_ball_out_event = ( transformer.transform_event(ball_out_event) ) events.append(transformed_ball_out_event) metadata = Metadata( teams=teams, periods=periods, pitch_dimensions=transformer.get_to_coordinate_system(). pitch_dimensions, frame_rate=None, orientation=Orientation.ACTION_EXECUTING_TEAM, flags=DatasetFlag.BALL_OWNING_TEAM, score=None, provider=Provider.STATSBOMB, coordinate_system=transformer.get_to_coordinate_system(), ) return EventDataset( metadata=metadata, records=events, )
def deserialize(self, inputs: SportecInputs) -> EventDataset: with performance_logging("load data", logger=logger): match_root = objectify.fromstring(inputs.meta_data.read()) event_root = objectify.fromstring(inputs.event_data.read()) with performance_logging("parse data", logger=logger): x_max = float( match_root.MatchInformation.Environment.attrib["PitchX"] ) y_max = float( match_root.MatchInformation.Environment.attrib["PitchY"] ) transformer = self.get_transformer(length=x_max, width=y_max) team_path = objectify.ObjectPath( "PutDataRequest.MatchInformation.Teams" ) team_elms = list(team_path.find(match_root).iterchildren("Team")) for team_elm in team_elms: if team_elm.attrib["Role"] == "home": home_team = _team_from_xml_elm(team_elm) elif team_elm.attrib["Role"] == "guest": away_team = _team_from_xml_elm(team_elm) else: raise DeserializationError( f"Unknown side: {team_elm.attrib['Role']}" ) ( home_score, away_score, ) = match_root.MatchInformation.General.attrib["Result"].split(":") score = Score(home=int(home_score), away=int(away_score)) teams = [home_team, away_team] if len(home_team.players) == 0 or len(away_team.players) == 0: raise DeserializationError("LineUp incomplete") periods = [] period_id = 0 events = [] for event_elm in event_root.iterchildren("Event"): event_chain = _event_chain_from_xml_elm(event_elm) timestamp = _parse_datetime(event_chain["Event"]["EventTime"]) if ( SPORTEC_EVENT_NAME_KICKOFF in event_chain and "GameSection" in event_chain[SPORTEC_EVENT_NAME_KICKOFF] ): period_id += 1 period = Period( id=period_id, start_timestamp=timestamp, end_timestamp=None, ) if period_id == 1: team_left = event_chain[SPORTEC_EVENT_NAME_KICKOFF][ "TeamLeft" ] if team_left == home_team.team_id: # goal of home team is on the left side. # this means they attack from left to right orientation = Orientation.FIXED_HOME_AWAY period.set_attacking_direction( AttackingDirection.HOME_AWAY ) else: orientation = Orientation.FIXED_AWAY_HOME period.set_attacking_direction( AttackingDirection.AWAY_HOME ) else: last_period = periods[-1] period.set_attacking_direction( AttackingDirection.AWAY_HOME if last_period.attacking_direction == AttackingDirection.HOME_AWAY else AttackingDirection.HOME_AWAY ) periods.append(period) elif SPORTEC_EVENT_NAME_FINAL_WHISTLE in event_chain: period.end_timestamp = timestamp continue team = None player = None flatten_attributes = dict() # reverse because top levels are more important for event_attributes in reversed(event_chain.values()): flatten_attributes.update(event_attributes) if "Team" in flatten_attributes: team = ( home_team if flatten_attributes["Team"] == home_team.team_id else away_team ) if "Player" in flatten_attributes: if not team: raise ValueError("Player set while team is not set") player = team.get_player_by_id( flatten_attributes["Player"] ) generic_event_kwargs = dict( # from DataRecord period=period, timestamp=timestamp - period.start_timestamp, ball_owning_team=None, ball_state=BallState.ALIVE, # from Event event_id=event_chain["Event"]["EventId"], coordinates=_parse_coordinates(event_chain["Event"]), raw_event=flatten_attributes, team=team, player=player, ) event_name, event_attributes = event_chain.popitem() if event_name in SPORTEC_SHOT_EVENT_NAMES: shot_event_kwargs = _parse_shot( event_name=event_name, event_chain=event_chain ) event = ShotEvent.create( **shot_event_kwargs, **generic_event_kwargs, ) elif event_name in SPORTEC_PASS_EVENT_NAMES: pass_event_kwargs = _parse_pass( event_chain=event_chain, team=team ) event = PassEvent.create( **pass_event_kwargs, **generic_event_kwargs, receive_timestamp=None, receiver_coordinates=None, ) elif event_name == SPORTEC_EVENT_NAME_BALL_CLAIMING: event = RecoveryEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) elif event_name == SPORTEC_EVENT_NAME_SUBSTITUTION: substitution_event_kwargs = _parse_substitution( event_attributes=event_attributes, team=team ) generic_event_kwargs["player"] = substitution_event_kwargs[ "player" ] del substitution_event_kwargs["player"] event = SubstitutionEvent.create( result=None, qualifiers=None, **substitution_event_kwargs, **generic_event_kwargs, ) elif event_name == SPORTEC_EVENT_NAME_CAUTION: card_kwargs = _parse_caution(event_attributes) event = CardEvent.create( result=None, qualifiers=None, **card_kwargs, **generic_event_kwargs, ) elif event_name == SPORTEC_EVENT_NAME_FOUL: foul_kwargs = _parse_foul(event_attributes, teams=teams) generic_event_kwargs.update(foul_kwargs) event = FoulCommittedEvent.create( result=None, qualifiers=None, **generic_event_kwargs, ) else: event = GenericEvent.create( result=None, qualifiers=None, event_name=event_name, **generic_event_kwargs, ) if events: previous_event = events[-1] if ( previous_event.event_type == EventType.PASS and previous_event.result == PassResult.COMPLETE ): if "X-Source-Position" in event_chain["Event"]: previous_event.receiver_coordinates = Point( x=float( event_chain["Event"]["X-Source-Position"] ), y=float( event_chain["Event"]["Y-Source-Position"] ), ) if ( event.event_type == EventType.PASS and event.get_qualifier_value(SetPieceQualifier) in ( SetPieceType.THROW_IN, SetPieceType.GOAL_KICK, SetPieceType.CORNER_KICK, ) ): # 1. update previous pass if events[-1].event_type == EventType.PASS: events[-1].result = PassResult.OUT # 2. add synthetic out event decision_timestamp = _parse_datetime( event_chain[list(event_chain.keys())[1]][ "DecisionTimestamp" ] ) out_event = BallOutEvent.create( period=period, timestamp=decision_timestamp - period.start_timestamp, ball_owning_team=None, ball_state=BallState.DEAD, # from Event event_id=event_chain["Event"]["EventId"] + "-ball-out", team=events[-1].team, player=events[-1].player, coordinates=None, raw_event={}, result=None, qualifiers=None, ) events.append(transformer.transform_event(out_event)) events.append(transformer.transform_event(event)) events = list( filter( self.should_include_event, events, ) ) metadata = Metadata( teams=teams, periods=periods, pitch_dimensions=transformer.get_to_coordinate_system().pitch_dimensions, score=score, frame_rate=None, orientation=orientation, flags=~(DatasetFlag.BALL_STATE | DatasetFlag.BALL_OWNING_TEAM), provider=Provider.SPORTEC, coordinate_system=transformer.get_to_coordinate_system(), ) return EventDataset( metadata=metadata, records=events, )