wsba_hockey.tools package

Submodules

wsba_hockey.tools.agg module

wsba_hockey.tools.agg.apply_params(df, group_by, params, stage='before')[source]
wsba_hockey.tools.agg.apply_rosters(df, group, schedule_path, roster_path)[source]
wsba_hockey.tools.agg.calc_goalie(pbp, game_strength, second_group)[source]
wsba_hockey.tools.agg.calc_indv(pbp, game_strength, second_group)[source]
wsba_hockey.tools.agg.calc_onice(pbp, game_strength, second_group)[source]
wsba_hockey.tools.agg.calc_team(pbp, game_strength, second_group)[source]
wsba_hockey.tools.agg.extra_calc(df, metrics)[source]
wsba_hockey.tools.agg.process_stats(df, group, venue, game_strength, second_group)[source]
wsba_hockey.tools.agg.rank_stats(df, rates=True, comparison=True, group_by=None)[source]

wsba_hockey.tools.globals module

wsba_hockey.tools.globals.convert_to_seconds(minutes)[source]
wsba_hockey.tools.globals.get_contents(game_html)[source]
wsba_hockey.tools.globals.get_soup(shifts_html)[source]
wsba_hockey.tools.globals.get_team(team)[source]
wsba_hockey.tools.globals.get_teams(selector)[source]
wsba_hockey.tools.globals.random_game_is_valid(game_id, session=None)[source]

wsba_hockey.tools.http module

class wsba_hockey.tools.http.Response(response: Response)[source]

Bases: object

The response surface used by wsba_hockey’s synchronous functions.

property encoding
json()[source]
property text
class wsba_hockey.tools.http.ScrapySession[source]

Bases: object

Synchronous, reusable Scrapy-backed session for package requests.

close()[source]
get(url, **kwargs)[source]
wsba_hockey.tools.http.get(url, session=None, **kwargs)[source]

GET through Scrapy, preserving the package’s synchronous API.

wsba_hockey.tools.http.get_json(url, session=None, **kwargs)[source]

GET and decode JSON, preserving useful upstream error context.

wsba_hockey.tools.http.make_pooled_session() ScrapySession[source]

Create a reusable Scrapy-backed session.

The session is safe to pass to multiple package scraping functions and can be used as a context manager. Scrapy’s remote-control extension is disabled because this client does not use it.

wsba_hockey.tools.http.pooled_session() ScrapySession[source]

Return the session cached for the current thread.

wsba_hockey.tools.plotting module

class wsba_hockey.tools.plotting.WSBAPlot(display_range: str = 'full', rotation: int | None = 0, figsize=(10, 12), facecolor='w', edgecolor='k')[source]

Bases: object

flush()[source]
wsba_hockey.tools.plotting.apply_primary_colors(df: DataFrame, color_map: dict[str, str], *, team_abbr_col: str = 'event_team_abbr', season_col: str = 'season', out_col: str = 'color', fallback: str = '#1f77b4') DataFrame[source]
wsba_hockey.tools.plotting.load_teaminfo(info_path: str = '/home/runner/work/wsba_hockey/wsba_hockey/src/wsba_hockey/tools/teaminfo/nhl_teaminfo.csv') DataFrame[source]
wsba_hockey.tools.plotting.plot_events(pbp: DataFrame, events: list[str], title: str | None = None, marker_dict: dict | None = None, legend: bool = False, display_range: str = 'full', rotation: int | None = 0, figsize=(6.4, 4.8))[source]
wsba_hockey.tools.plotting.prep_plot_data(pbp, strengths, season_types=2, marker_dict={'blocked-shot': 'v', 'faceoff': 'X', 'giveaway': '1', 'goal': '*', 'hit': 'P', 'missed-shot': 'o', 'shot-on-goal': 'D', 'takeaway': '2'})[source]
wsba_hockey.tools.plotting.team_primary_color_map(teaminfo: DataFrame | None = None, *, info_path: str = '/home/runner/work/wsba_hockey/wsba_hockey/src/wsba_hockey/tools/teaminfo/nhl_teaminfo.csv') dict[str, str][source]
wsba_hockey.tools.plotting.wsba_rink(display_range: str = 'offense', rotation: int | None = 0, ax=None, figsize=(10, 12))[source]

wsba_hockey.tools.scraping module

wsba_hockey.tools.scraping.adjust_coords(pbp)[source]
wsba_hockey.tools.scraping.analyze_shifts(shift, id, name, pos, team)[source]
wsba_hockey.tools.scraping.apply_passing_imputation(pbp)[source]
wsba_hockey.tools.scraping.assign_target(data)[source]
wsba_hockey.tools.scraping.clean_html_pbp(info, session=None)[source]
wsba_hockey.tools.scraping.combine_data(info, sources, session=None)[source]
wsba_hockey.tools.scraping.combine_pbp(info, sources, session=None)[source]
wsba_hockey.tools.scraping.combine_shifts(info, sources, session=None)[source]
wsba_hockey.tools.scraping.edge_stat_entry(entry, season, season_type, type, session=None)[source]
wsba_hockey.tools.scraping.espn_game_id(date, away, home, session=None)[source]
wsba_hockey.tools.scraping.fix_players(pbp)[source]
wsba_hockey.tools.scraping.get_game_info(game_id, session=None)[source]
wsba_hockey.tools.scraping.get_game_roster(json)[source]
wsba_hockey.tools.scraping.logical_sort(df)[source]
wsba_hockey.tools.scraping.normalize_json_text(value)[source]

Return a JSON string for a JSON/nested value stored in a text column.

This also upgrades schedule files written by older releases, where a nested Polars value was persisted as its human-readable Series output.

wsba_hockey.tools.scraping.parse_espn(date, away, home, session=None)[source]
wsba_hockey.tools.scraping.parse_event_sprite(frames: list[dict], home_team_id, away_team_id) DataFrame[source]
wsba_hockey.tools.scraping.parse_game_roster(rost_df, game_id)[source]
wsba_hockey.tools.scraping.parse_html(info, session=None)[source]
wsba_hockey.tools.scraping.parse_json(info)[source]
wsba_hockey.tools.scraping.parse_shift_events(info, home, session=None)[source]
wsba_hockey.tools.scraping.parse_shifts_html(info, home, session=None)[source]
wsba_hockey.tools.scraping.parse_shifts_json(info)[source]
wsba_hockey.tools.scraping.seconds_expr(column)[source]
wsba_hockey.tools.scraping.stringify_nested(df: DataFrame) DataFrame[source]

Encode nested columns as JSON strings for portable table exports.

Polars cannot write list or struct columns to CSV, and some Parquet readers handle those types inconsistently. JSON preserves the complete value while keeping the returned frame usable by both formats. Scalar columns are returned unchanged.

wsba_hockey.tools.scraping.strip_html_pbp(td, rosters)[source]

wsba_hockey.tools.xg_model module

wsba_hockey.tools.xg_model.nhl_apply_xG(pbp: DataFrame, states: bool = False) DataFrame[source]

Given play-by-play data, return this data with xG-related columns.

Parameters:
  • pbp (pl.DataFrame) – A DataFrame containing play-by-play data generated within the WBSA Hockey package.

  • states (bool, optional) – If True, recalculate score, Corsi, Fenwick, and penalty states before applying xG. Defaults to False, matching the previous xG module behavior.

Returns:

A DataFrame containing input play-by-play data with xG column.

Return type:

pl.DataFrame

Module contents