wsba-hockey 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ import matplotlib.pyplot as plt
2
+ import matplotlib.image as img
3
+ import numpy as np
4
+ import pandas as pd
5
+ pd.options.mode.chained_assignment = None
6
+ from scipy.interpolate import griddata
7
+ from scipy.ndimage import gaussian_filter
8
+ from tools.xg_model import *
9
+ from hockey_rink import NHLRink
10
+ from hockey_rink import CircularImage
11
+
12
+ event_colors = {
13
+ 'faceoff':'black',
14
+ 'hit':'yellow',
15
+ 'blocked-shot':'pink',
16
+ 'missed-shot':'red',
17
+ 'shot-on-goal':'purple',
18
+ 'goal':'blue',
19
+ 'giveaway':'orange',
20
+ 'takeaway':'green',
21
+ }
22
+
23
+ def wsba_rink(display_range='offense',rotation = 0):
24
+ img = 'tools/utils/wsba.png'
25
+ rink = NHLRink(center_logo={
26
+ "feature_class": CircularImage,
27
+ "image_path": img,
28
+ "length": 25, "width": 25,
29
+ "x": 0, "y": 0,
30
+ "radius": 14,
31
+ "zorder": 11,
32
+ }
33
+ )
34
+ rink.draw(
35
+ display_range=display_range,
36
+ rotation=rotation,
37
+ despine=True
38
+ )
39
+
40
+ def prep_plot_data(pbp,events,strengths,color_dict=event_colors,xg='moneypuck'):
41
+ try: pbp['xG']
42
+ except:
43
+ if xg == 'wsba':
44
+ pbp = wsba_xG(pbp)
45
+ else:
46
+ pbp = moneypuck_xG(pbp)
47
+ pbp['xG'] = np.where(pbp['xG'].isna(),0,pbp['xG'])
48
+
49
+ pbp['WSBA'] = pbp['event_player_1_name']+pbp['season'].astype(str)+pbp['event_team_abbr']
50
+
51
+ pbp['x_plot'] = pbp['y_fixed']*-1
52
+ pbp['y_plot'] = pbp['x_fixed']
53
+
54
+ pbp['size'] = np.where(pbp['xG']<=0,100,pbp['xG']*1000)
55
+ pbp['color'] = pbp['event_type'].replace(color_dict)
56
+
57
+ pbp = pbp.loc[(pbp['event_type'].isin(events))&
58
+ (pbp['event_distance']<=89)&
59
+ (pbp['x_fixed']<=89)&
60
+ (pbp['strength_state'].isin(strengths))]
61
+
62
+ return pbp
63
+
64
+ def league_shots(pbp,cord = 'fixed'):
65
+ [x,y] = np.round(np.meshgrid(np.linspace(0,100,100),np.linspace(-42.5,42.5,85)))
66
+ xgoals = griddata((pbp[f'x_{cord}'],pbp[f'y_{cord}']),pbp['xG'],(x,y),method='cubic',fill_value=0)
67
+ xgoals_smooth = gaussian_filter(xgoals,sigma = 3)
68
+
69
+ fig = plt.figure(figsize=(10,12), facecolor='w', edgecolor='k')
70
+ plt.imshow(xgoals_smooth,origin = 'lower')
71
+ plt.colorbar(orientation = 'horizontal', pad = 0.05)
72
+ plt.title('xGoal Array',fontdict={'fontsize': 15})
73
+ plt.show()
74
+
75
+ return xgoals_smooth
76
+
77
+
78
+ def plot_skater_shots(pbp, player, season, team, strengths, title = None, color_dict=event_colors, legend=False,xg='moneypuck'):
79
+ shots = ['missed-shot','shot-on-goal','goal']
80
+ pbp = prep_plot_data(pbp,shots,strengths,color_dict,xg)
81
+ skater = pbp.loc[pbp['WSBA'] == f'{player.upper()}{season}{team}']
82
+
83
+ fig, ax = plt.subplots()
84
+ wsba_rink(rotation=90)
85
+
86
+ for event in shots:
87
+ plays = skater.loc[skater['event_type']==event]
88
+ ax.scatter(plays['x_plot'],plays['y_plot'],plays['size'],plays['color'],label=event)
89
+
90
+ ax.set_title(title) if title else ''
91
+ ax.legend().set_visible(legend)
92
+
93
+ return fig
94
+
95
+ def plot_game_events(pbp,game_id,events,strengths,color_dict=event_colors,legend=False,xg='moneypuck'):
96
+ pbp = prep_plot_data(pbp,events,strengths,color_dict,xg)
97
+ pbp = pbp.loc[pbp['game_id'].astype(str)==game_id]
98
+
99
+ away_abbr = list(pbp['away_team_abbr'])[0]
100
+ home_abbr = list(pbp['home_team_abbr'])[0]
101
+ date = list(pbp['game_date'])[0]
102
+
103
+ fig, ax = plt.subplots()
104
+ wsba_rink(display_range='full')
105
+
106
+ for event in events:
107
+ plays = pbp.loc[pbp['event_type']==event]
108
+ ax.scatter(plays['x_adj'],plays['y_adj'],plays['size'],plays['color'],label=event)
109
+
110
+ ax.set_title(f'{away_abbr} @ {home_abbr} - {date}')
111
+ ax.legend(bbox_to_anchor =(0.5,-0.4), loc='lower center',ncol=1).set_visible(legend)
112
+
113
+ return fig