Skip to content

AttackingDirection

kloppy.domain.AttackingDirection

Bases: Enum

The direction of play for the home team.

ATTRIBUTE DESCRIPTION
LTR

Home team is playing from left to right

TYPE: AttackingDirection

RTL

Home team is playing from right to left

TYPE: AttackingDirection

NOT_SET

not set yet

TYPE: AttackingDirection

LTR class-attribute instance-attribute

LTR = 'left-to-right'

RTL class-attribute instance-attribute

RTL = 'right-to-left'

NOT_SET class-attribute instance-attribute

NOT_SET = 'not-set'

from_orientation staticmethod

1
2
3
4
5
6
from_orientation(
    orientation,
    period=None,
    ball_owning_team=None,
    action_executing_team=None,
)

Determines the attacking direction for a specific data record.

PARAMETER DESCRIPTION
orientation

The orientation of the dataset.

TYPE: Orientation

period

The period of the data record.

TYPE: Optional[Period] DEFAULT: None

ball_owning_team

The team that is in possession of the ball.

TYPE: Optional[Team] DEFAULT: None

action_executing_team

The team that executes the action.

TYPE: Optional[Team] DEFAULT: None

RAISES DESCRIPTION
OrientationError

If the attacking direction cannot be determined from the given data.

RETURNS DESCRIPTION
'AttackingDirection'

The attacking direction for the given data record.

Source code in kloppy/domain/models/common.py
@staticmethod
def from_orientation(
    orientation: Orientation,
    period: Optional[Period] = None,
    ball_owning_team: Optional[Team] = None,
    action_executing_team: Optional[Team] = None,
) -> "AttackingDirection":
    """Determines the attacking direction for a specific data record.

    Args:
        orientation: The orientation of the dataset.
        period: The period of the data record.
        ball_owning_team: The team that is in possession of the ball.
        action_executing_team: The team that executes the action.

    Raises:
        OrientationError: If the attacking direction cannot be determined
            from the given data.

    Returns:
        The attacking direction for the given data record.
    """
    if orientation == Orientation.STATIC_HOME_AWAY:
        return AttackingDirection.LTR
    if orientation == Orientation.STATIC_AWAY_HOME:
        return AttackingDirection.RTL
    if orientation == Orientation.HOME_AWAY:
        if period is None:
            raise OrientationError(
                "You must provide a period to determine the attacking direction"
            )
        dirmap = {
            1: AttackingDirection.LTR,
            2: AttackingDirection.RTL,
            3: AttackingDirection.LTR,
            4: AttackingDirection.RTL,
        }
        if period.id in dirmap:
            return dirmap[period.id]
        raise OrientationError(
            "This orientation is not defined for period %s" % period.id
        )
    if orientation == Orientation.AWAY_HOME:
        if period is None:
            raise OrientationError(
                "You must provide a period to determine the attacking direction"
            )
        dirmap = {
            1: AttackingDirection.RTL,
            2: AttackingDirection.LTR,
            3: AttackingDirection.RTL,
            4: AttackingDirection.LTR,
        }
        if period.id in dirmap:
            return dirmap[period.id]
        raise OrientationError(
            "This orientation is not defined for period %s" % period.id
        )
    if orientation == Orientation.BALL_OWNING_TEAM:
        if ball_owning_team is None:
            raise OrientationError(
                "You must provide the ball owning team to determine the attacking direction"
            )
        if ball_owning_team is not None:
            if ball_owning_team.ground == Ground.HOME:
                return AttackingDirection.LTR
            if ball_owning_team.ground == Ground.AWAY:
                return AttackingDirection.RTL
            raise OrientationError(
                "Invalid ball_owning_team: %s", ball_owning_team
            )
        return AttackingDirection.NOT_SET
    if orientation == Orientation.ACTION_EXECUTING_TEAM:
        if action_executing_team is None:
            raise ValueError(
                "You must provide the action executing team to determine the attacking direction"
            )
        if action_executing_team.ground == Ground.HOME:
            return AttackingDirection.LTR
        if action_executing_team.ground == Ground.AWAY:
            return AttackingDirection.RTL
        raise OrientationError(
            "Invalid action_executing_team: %s", action_executing_team
        )
    raise OrientationError("Unknown orientation: %s", orientation)