pooltool.events¶
Events, their detection, creation, and filtration.
See here to learn about events and why they matter.
Classes¶
- class Agent(id: str, agent_type: AgentType, initial: NullObject | Cue | Ball | Pocket | LinearCushionSegment | CircularCushionSegment | None = None, final: NullObject | Cue | Ball | Pocket | LinearCushionSegment | CircularCushionSegment | None = None)[source]¶
An event agent.
This class represents an agent involved in events. The agent can be in different states before and after an event, represented by
initialandfinalstates.Attributes:
- initial : pooltool.objects.datatypes.NullObject | Cue | Ball | Pocket | LinearCushionSegment | CircularCushionSegment | None¶
The state of the agent before an event.
- final : pooltool.objects.datatypes.NullObject | Cue | Ball | Pocket | LinearCushionSegment | CircularCushionSegment | None¶
The state of the agent after an event.
Methods:
- set_initial(obj: Object) None[source]¶
Sets the initial state of the agent (before event resolution).
This makes a copy of the passed object and sets it to
initial.In the case of a
pooltool.events.AgentType.BALLagent type, it drops history fields before copying to save time and memory.- Parameters:
obj : Object
The object from which
initialwill be set.
- set_final(obj: Object) None[source]¶
Sets the final state of the agent (after event resolution).
This makes a copy of the passed object and sets it to
final.In the case of a
pooltool.events.AgentType.BALLagent type, it drops history fields before copying to save time and memory.- Parameters:
obj : Object
The object from which
finalwill be set.
- class AgentType[source]¶
An Enum of event agents
Base Classes:
pooltool.utils.strenum.StrEnumAttributes:
- NULL¶
A null agent.
- CUE¶
A cue stick agent.
- BALL¶
A ball agent.
- POCKET¶
A pocket agent.
- LINEAR_CUSHION_SEGMENT¶
A linear cushion segment agent.
- CIRCULAR_CUSHION_SEGMENT¶
A circular cushion segment agent.
- class Event(event_type: EventType, agents: tuple[Agent, ...], time: float)[source]¶
Represents an event.
This class models an event characterized by its type, the agents involved, and the time at which the event occurs.
Agent states before and after event resolution are stored in the
pooltool.events.Agent.initialandpooltool.events.Agent.finalattributes of agents withinagents.Attributes:
- agents : tuple[Agent, ...]¶
A tuple containing one or two agents involved in the event.
Events that are collisions (
pooltool.events.EventType.is_collision()) have two agents, while events that are transitions (pooltool.events.EventType.is_transition()), or events with event typepooltool.events.EventType.NONE, have one agent.By convention, the order of the agents matches how the
pooltool.events.EventTypeattributes are named.
- property ids: tuple[str, ...]¶
Retrieves the IDs of the agents involved in the event.
This property provides access to a tuple of agent IDs, allowing identification of the agents involved in the event.
- Returns:
A tuple containing the IDs of the agents involved in the event.
- Return type:
Tuple[str, ...]
Methods:
- get_ball(ball_id: str, initial: bool = True) Ball[source]¶
Return the Ball object with the given ID, either final or initial.
- Parameters:
- Raises:
ValueError -- If the event does not involve a ball or if no matching ball is found.
- Return type:
- get_pocket(pocket_id: str, initial: bool = True) Pocket[source]¶
Return the Pocket object with the given ID, either final or initial.
- Return type:
- get_cushion(cushion_id: str) LinearCushionSegment | CircularCushionSegment[source]¶
Return the cushion segment with the given ID.
- Return type:
- class EventType[source]¶
An Enum of event types
Base Classes:
pooltool.utils.strenum.StrEnumAttributes:
- NONE¶
The null event.
- BALL_BALL¶
A ball-ball collision.
- BALL_LINEAR_CUSHION¶
A ball collision with a linear cushion segment.
- BALL_CIRCULAR_CUSHION¶
A ball collision with a circular cushion segment.
- BALL_POCKET¶
A ball pocket “collision”. This marks the point at which the ball crosses the point of no return.
- STICK_BALL¶
A cue-stick ball collision.
- SPINNING_STATIONARY¶
A ball transition from spinning to stationary.
- ROLLING_STATIONARY¶
A ball transition from rolling to stationary.
- ROLLING_SPINNING¶
A ball transition from rolling to spinning.
- SLIDING_ROLLING¶
A ball transition from sliding to rolling.
Methods:
Functions¶
- ball_ball_collision(ball1: Ball, ball2: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a ball-ball collision.
- Return type:
- ball_circular_cushion_collision(ball: Ball, cushion: CircularCushionSegment, time: float, set_initial: bool = False) Event[source]¶
Create a ball-circular-cushion collision.
- Return type:
- ball_linear_cushion_collision(ball: Ball, cushion: LinearCushionSegment, time: float, set_initial: bool = False) Event[source]¶
Create a ball-linear-cushion collision.
- Return type:
- ball_pocket_collision(ball: Ball, pocket: Pocket, time: float, set_initial: bool = False) Event[source]¶
Create a ball-pocket collision.
- Return type:
- rolling_spinning_transition(ball: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a rolling-spinning transition.
- Return type:
- rolling_stationary_transition(ball: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a rolling-stationary transition.
- Return type:
- sliding_rolling_transition(ball: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a sliding-rolling transition.
- Return type:
- spinning_stationary_transition(ball: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a spinning-stationary transition.
- Return type:
- stick_ball_collision(stick: Cue, ball: Ball, time: float, set_initial: bool = False) Event[source]¶
Create a cue stick-ball collision.
- Return type:
- by_ball(ball_ids: str | list[str], keep_nonevent: bool = False) FilterFunc[source]¶
Returns a function that filters events based on ball IDs.
- Parameters:
-
A collection of ball IDs.
keep_nonevent : bool
Retain non-events (
pooltool.events.EventType.NONE).
-
- Returns:
A function that when passed a list of events, returns a filtered list containing only events that involve balls matching the passed ball ID(s).
- Return type:
FilterFunc
- by_time(t: float, after: bool = True) FilterFunc[source]¶
Returns a function that filter events with respect to a time cutoff.
- Parameters:
- Returns:
A function that when passed a list of events, returns a filtered list containing only events before or after the cutoff time, non-inclusive.
- Return type:
FilterFunc
- by_type(types: EventType | list[EventType]) FilterFunc[source]¶
Returns a function that filters events based on event type.
- filter_ball(events: list[Event], ball_ids: str | list[str], keep_nonevent: bool = False) list[Event][source]¶
Filter events based on ball IDs.
- Parameters:
- Returns:
A filtered event list containing only events that involve balls matching the passed ball ID(s).
- Return type:
List[Event]
See also
If you’re filtering based on multiple criteria, you can (and should!) use
pooltool.events.filter_events().
- filter_events(events: list[Event], *funcs: FilterFunc) list[Event][source]¶
Filter events using multiple criteria.
A convenient way to filter based multiple filtering criteria.
- Parameters:
- Returns:
A filtered event list containing only events passing the supplied criteria.
- Return type:
List[Event]
Examples
Generate a list of events.
>>> import pooltool as pt >>> system = pt.System.example() >>> system.cue.set_state(a=0.68) >>> pt.simulate(system, inplace=True) >>> events = system.events
In this shot, both the cue-ball and the 1-ball are potted. We are interested in filtering for the cue-ball pocket event. Option 1 is to call
pooltool.events.filter_type()and thenpooltool.events.filter_ball():>>> filtered_events = pt.events.filter_type(events, pt.EventType.BALL_POCKET) >>> filtered_events = pt.events.filter_ball(filtered_events, "cue") >>> event_of_interest = filtered_events[0] >>> event_of_interest <Event object at 0x7fa855e7e6c0> ├── type : ball_pocket ├── time : 3.231130101576186 └── agents : ('cue', 'rt')
Option 2, the better option, is to use
pooltool.events.filter_events():>>> filtered_events = pt.events.filter_events( >>> events, >>> pt.events.by_type(pt.EventType.BALL_POCKET), >>> pt.events.by_ball("cue"), >>> ) >>> event_of_interest = filtered_events[0] >>> event_of_interest <Event object at 0x7fa855e7e6c0> ├── type : ball_pocket ├── time : 3.231130101576186 └── agents : ('cue', 'rt')
See also
If you’re filtering based on a single criterion, you can consider using
pooltool.events.filter_type(),pooltool.events.filter_ball(),pooltool.events.filter_time(), etc.
- filter_time(events: list[Event], t: float, after: bool = True) list[Event][source]¶
Filter events with respect to a time cutoff.
- Parameters:
- Returns:
A filtered event list containing only events before or after the cutoff time, non-inclusive.
- Return type:
List[Event]
See also
If you’re filtering based on multiple criteria, you can (and should!) use
pooltool.events.filter_events().
- filter_type(events: list[Event], types: EventType | list[EventType]) list[Event][source]¶
Filter events based on event type.
- Parameters:
- Returns:
A filtered event list containing only events matching the passed event type(s).
- Return type:
List[Event]
See also
If you’re filtering based on multiple criteria, you can (and should!) use
pooltool.events.filter_events().