wsba-hockey 1.0.1__py3-none-any.whl → 1.0.2__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.
- wsba_hockey/tools/plotting.py +12 -6
- wsba_hockey/tools/xg_model.py +1 -0
- wsba_hockey/workspace.py +74 -27
- wsba_hockey/wsba_main.py +8 -2
- {wsba_hockey-1.0.1.dist-info → wsba_hockey-1.0.2.dist-info}/METADATA +1 -1
- {wsba_hockey-1.0.1.dist-info → wsba_hockey-1.0.2.dist-info}/RECORD +9 -9
- {wsba_hockey-1.0.1.dist-info → wsba_hockey-1.0.2.dist-info}/WHEEL +0 -0
- {wsba_hockey-1.0.1.dist-info → wsba_hockey-1.0.2.dist-info}/licenses/LICENSE +0 -0
- {wsba_hockey-1.0.1.dist-info → wsba_hockey-1.0.2.dist-info}/top_level.txt +0 -0
wsba_hockey/tools/plotting.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
import matplotlib.pyplot as plt
|
2
|
-
import matplotlib.image as
|
2
|
+
import matplotlib.image as mpimg
|
3
3
|
import numpy as np
|
4
4
|
import pandas as pd
|
5
5
|
from scipy.interpolate import griddata
|
6
6
|
from scipy.ndimage import gaussian_filter
|
7
|
+
import urllib.request
|
8
|
+
import PIL
|
7
9
|
from .xg_model import *
|
8
10
|
from hockey_rink import NHLRink
|
9
11
|
from hockey_rink import CircularImage
|
@@ -109,7 +111,7 @@ def plot_skater_shots(pbp, player, season, team, strengths, title = None, marker
|
|
109
111
|
|
110
112
|
return fig
|
111
113
|
|
112
|
-
def plot_game_events(pbp,game_id,events,strengths,marker_dict=event_markers,legend=False,xg='moneypuck'):
|
114
|
+
def plot_game_events(pbp,game_id,events,strengths,marker_dict=event_markers,team_colors={'away':'secondary','home':'primary'},legend=False,xg='moneypuck'):
|
113
115
|
pbp = prep_plot_data(pbp,events,strengths,marker_dict,xg)
|
114
116
|
pbp = pbp.loc[pbp['game_id'].astype(str)==game_id]
|
115
117
|
|
@@ -119,10 +121,14 @@ def plot_game_events(pbp,game_id,events,strengths,marker_dict=event_markers,lege
|
|
119
121
|
season = f'{game_id[0:4]}{int(game_id[0:4])+1}'
|
120
122
|
|
121
123
|
team_data = pd.read_csv('teaminfo/nhl_teaminfo.csv')
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
124
|
+
team_info ={
|
125
|
+
'away_color':'#000000' if list(team_data.loc[team_data['WSBA']==f'{away_abbr}{season}','Secondary Color'])[0]=='#FFFFFF' else list(team_data.loc[team_data['WSBA']==f'{away_abbr}{season}',f'{team_colors['away'].capitalize()} Color'])[0],
|
126
|
+
'home_color': list(team_data.loc[team_data['WSBA']==f'{home_abbr}{season}',f'{team_colors['home'].capitalize()} Color'])[0],
|
127
|
+
'away_logo': f'tools/logos/png/{away_abbr}{season}.png',
|
128
|
+
'home_logo': f'tools/logos/png/{home_abbr}{season}.png',
|
129
|
+
}
|
130
|
+
|
131
|
+
pbp['color'] = np.where(pbp['event_team_abbr']==away_abbr,team_info['away_color'],team_info['home_color'])
|
126
132
|
|
127
133
|
fig, ax = plt.subplots()
|
128
134
|
wsba_rink(display_range='full')
|
wsba_hockey/tools/xg_model.py
CHANGED
@@ -38,6 +38,7 @@ def prep_xG_data(pbp):
|
|
38
38
|
data.sort_values(['season','game_id','period','seconds_elapsed','event_num'],inplace=True)
|
39
39
|
data['score_state'] = np.where(data['away_team_abbr']==data['event_team_abbr'],data['away_score']-data['home_score'],data['home_score']-data['away_score'])
|
40
40
|
data['strength_diff'] = np.where(data['away_team_abbr']==data['event_team_abbr'],data['away_skaters']-data['home_skaters'],data['home_skaters']-data['away_skaters'])
|
41
|
+
data['strength_state_venue'] = data['away_skaters'].astype(str)+'v'+data['home_skaters'].astype(str)
|
41
42
|
data['fenwick_state'] = np.where(data['away_team_abbr']==data['event_team_abbr'],data['away_fenwick']-data['home_fenwick'],data['home_fenwick']-data['away_fenwick'])
|
42
43
|
data['distance_from_last'] = np.sqrt((data['x_fixed'] - data['x_fixed_last'])**2 + (data['y_fixed'] - data['y_fixed_last'])**2)
|
43
44
|
|
wsba_hockey/workspace.py
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
import numpy as np
|
2
2
|
import pandas as pd
|
3
3
|
import matplotlib
|
4
|
+
matplotlib.use('agg')
|
4
5
|
import matplotlib.pyplot as plt
|
5
6
|
import wsba_hockey as wsba
|
7
|
+
from gspread_pandas import Spread, Client
|
8
|
+
import urllib.request
|
9
|
+
from wand.color import Color
|
10
|
+
from wand.image import Image
|
6
11
|
|
7
12
|
season_load = wsba.repo_load_seasons()
|
8
13
|
|
9
|
-
def workspace(seasons,type):
|
14
|
+
def workspace(seasons,type,arg = '',start='',end=''):
|
10
15
|
if type == 'pbp':
|
11
16
|
#Scrape pbp
|
12
17
|
errors=[]
|
@@ -17,51 +22,93 @@ def workspace(seasons,type):
|
|
17
22
|
print(f'Errors: {errors}')
|
18
23
|
|
19
24
|
elif type == 'convert':
|
20
|
-
for season in seasons
|
25
|
+
for season in seasons:
|
21
26
|
data = pd.read_csv(f"pbp/nhl_pbp_{season}.csv")
|
22
27
|
data = wsba.wsba_main.moneypuck_xG(data)
|
23
28
|
data.to_parquet(f'pbp/parquet/nhl_pbp_{season}.parquet',index=False)
|
24
29
|
|
25
|
-
elif type == '
|
26
|
-
#Scrape
|
27
|
-
stand = [wsba.nhl_scrape_standings(season) for season in
|
28
|
-
pd.concat(stand)
|
30
|
+
elif type == 'team_info':
|
31
|
+
#Scrape team info
|
32
|
+
stand = [wsba.nhl_scrape_standings(season) for season in seasons]
|
33
|
+
standings = pd.concat(stand)
|
34
|
+
|
35
|
+
colors = pd.read_csv('teaminfo/nhl_colors.csv')
|
36
|
+
data = pd.merge(colors,standings,how='right',left_on='triCode',right_on='teamAbbrev.default').sort_values(by=['seasonId','triCode'])
|
37
|
+
data['WSBA'] = data['teamAbbrev.default']+data['seasonId'].astype(str)
|
38
|
+
|
39
|
+
data.to_csv('teaminfo/nhl_teaminfo.csv',index=False)
|
29
40
|
|
30
41
|
elif type == 'stats':
|
31
42
|
#Stats building
|
32
43
|
stats = []
|
33
|
-
for season in seasons
|
44
|
+
for season in seasons:
|
34
45
|
for group in ['skater','team']:
|
35
46
|
pbp = pd.read_parquet(f'pbp/parquet/nhl_pbp_{season}.parquet')
|
36
47
|
stat = wsba.nhl_calculate_stats(pbp,group,[2],['5v5'],shot_impact=True)
|
37
48
|
stat.to_csv(f'stats/{group}/wsba_nhl_{season}_{group}.csv',index=False)
|
38
49
|
stats.append(stat)
|
39
50
|
pd.concat(stats).to_csv(f'stats/db/wsba_nhl_{group}_db.csv',index=False)
|
40
|
-
|
41
|
-
|
51
|
+
|
52
|
+
elif type == 'plot_game':
|
53
|
+
for season in seasons:
|
54
|
+
pbp = wsba.nhl_scrape_season(season,remove=[],start=start,end=end)
|
42
55
|
|
43
|
-
|
56
|
+
plots = wsba.nhl_plot_games(pbp,wsba.wsba_main.fenwick_events,['5v5'],'all',team_colors=arg,legend=True)
|
44
57
|
|
45
|
-
|
58
|
+
games = list(pbp['game_id'].astype(str).drop_duplicates())
|
59
|
+
i = 1
|
60
|
+
for plot, game_id in zip(plots,games):
|
61
|
+
plot.savefig(f'plots/games/{game_id[0:4]}{int(game_id[0:4])+1}/{game_id[5:6]}/{game_id}_shotplot.png',bbox_inches='tight',transparent=True)
|
62
|
+
i += 1
|
63
|
+
|
64
|
+
elif type == 'plot_skater':
|
65
|
+
for season in seasons:
|
66
|
+
pbp = pd.read_parquet(f'pbp/parquet/nhl_pbp_{season}.parquet')
|
67
|
+
|
68
|
+
skaters={}
|
69
|
+
|
70
|
+
for shooter,season,team in zip(pbp['event_player_1_name'],pbp['season'].astype(str),pbp['event_team_abbr']):
|
71
|
+
if shooter is None:
|
72
|
+
continue
|
73
|
+
else:
|
74
|
+
skaters.update({
|
75
|
+
shooter:[season,team]
|
76
|
+
})
|
46
77
|
|
47
|
-
|
48
|
-
|
49
|
-
|
78
|
+
plots = wsba.nhl_plot_skaters_shots(pbp,skaters,['5v5'],onice='indv',title=False,legend=True)
|
79
|
+
|
80
|
+
items = list(skaters.items())
|
81
|
+
for plot,skater in zip(plots,items):
|
82
|
+
plot.savefig(f'plots/{skater[1][0]}/{skater[0]}{skater[1][0]}{skater[1][1]}_indv.png',bbox_inches='tight',transparent=True)
|
83
|
+
|
84
|
+
elif type == 'logos':
|
85
|
+
data = pd.read_csv('teaminfo/nhl_teaminfo.csv')
|
86
|
+
for url, id in zip(data['teamLogo'],data['WSBA']):
|
87
|
+
print(url)
|
88
|
+
urllib.request.urlretrieve(url,f'tools/logos/svg/{id}.svg')
|
89
|
+
with Image(filename=f'tools/logos/svg/{id}.svg') as img:
|
90
|
+
img.format = 'png32'
|
91
|
+
img.background_color = Color('transparent')
|
92
|
+
img.alpha_channel = 'activate'
|
93
|
+
img.save(filename=f'tools/logos/png/{id}.png')
|
94
|
+
|
50
95
|
else:
|
51
|
-
|
52
|
-
shooter:[season,team]
|
53
|
-
})
|
96
|
+
print('Nothing here.')
|
54
97
|
|
55
|
-
|
98
|
+
def push_to_sheet():
|
99
|
+
spread = Spread('WSBA - NHL 5v5 Shooting Metrics Public v2.0')
|
56
100
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
101
|
+
#Tables
|
102
|
+
skater = pd.read_csv('stats/db/wsba_nhl_skater_db.csv')
|
103
|
+
team = pd.read_csv('stats/db/wsba_nhl_team_db.csv')
|
104
|
+
team_info = pd.read_csv('teaminfo/nhl_teaminfo.csv')
|
105
|
+
country = pd.read_csv('teaminfo/nhl_countryinfo.csv')
|
106
|
+
schedule = pd.read_csv('schedule/schedule.csv')
|
61
107
|
|
62
|
-
|
108
|
+
spread.df_to_sheet(skater,index=False,sheet='Skaters DB')
|
109
|
+
spread.df_to_sheet(team,index=False,sheet='Teams DB')
|
110
|
+
spread.df_to_sheet(team_info,index=False,sheet='Team Info')
|
111
|
+
spread.df_to_sheet(country,index=False,sheet='Country Info')
|
112
|
+
spread.df_to_sheet(schedule,index=False,sheet='Schedule')
|
63
113
|
|
64
|
-
|
65
|
-
for plot in plots:
|
66
|
-
plot.savefig(f'plots/20242025_03_{i}_shotplot.png',bbox_inches='tight',transparent=True)
|
67
|
-
i += 1
|
114
|
+
workspace(['20242025'],'plot_game',arg={'away':'secondary','home':'primary'}, start='04-20',end='04-20')
|
wsba_hockey/wsba_main.py
CHANGED
@@ -660,6 +660,12 @@ def nhl_calculate_stats(pbp,type,season_types,game_strength,roster_path="rosters
|
|
660
660
|
#Filter by season types and remove shootouts
|
661
661
|
pbp = pbp.loc[(pbp['season_type'].isin(season_types)) & (pbp['period'] < 5)]
|
662
662
|
|
663
|
+
#Convert all columns with player ids to float in order to avoid merging errors
|
664
|
+
for col in get_col():
|
665
|
+
if "_id" in col:
|
666
|
+
try: pbp[col] = pbp[col].astype(float)
|
667
|
+
except KeyError: continue
|
668
|
+
|
663
669
|
# Filter by game strength if not "all"
|
664
670
|
if game_strength != "all":
|
665
671
|
pbp = pbp.loc[pbp['strength_state'].isin(game_strength)]
|
@@ -794,7 +800,7 @@ def nhl_plot_skaters_shots(pbp,skater_dict,strengths,marker_dict=event_markers,o
|
|
794
800
|
#Return: list of plotted skater shot charts
|
795
801
|
return skater_plots
|
796
802
|
|
797
|
-
def nhl_plot_games(pbp,events,strengths,game_ids='all',marker_dict=event_markers,legend=False,xg='moneypuck'):
|
803
|
+
def nhl_plot_games(pbp,events,strengths,game_ids='all',marker_dict=event_markers,team_colors={'away':'primary','home':'primary'},legend=False,xg='moneypuck'):
|
798
804
|
#Returns list of plots for specified games
|
799
805
|
# param 'pbp' - pbp to plot data
|
800
806
|
# param 'events' - type of events to plot
|
@@ -811,7 +817,7 @@ def nhl_plot_games(pbp,events,strengths,game_ids='all',marker_dict=event_markers
|
|
811
817
|
print(f'Plotting the following games: {game_ids}...')
|
812
818
|
|
813
819
|
#Iterate through games, adding plot to list
|
814
|
-
game_plots = [plot_game_events(pbp,game,events,strengths,marker_dict,legend,xg) for game in game_ids]
|
820
|
+
game_plots = [plot_game_events(pbp,game,events,strengths,marker_dict,team_colors,legend,xg) for game in game_ids]
|
815
821
|
|
816
822
|
#Return: list of plotted game events
|
817
823
|
return game_plots
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: wsba_hockey
|
3
|
-
Version: 1.0.
|
3
|
+
Version: 1.0.2
|
4
4
|
Summary: WeakSide Breakout's complete Python package of access to hockey data, primairly including the scraping of National Hockey League schedule, play-by-play, and shifts information.
|
5
5
|
Author-email: Owen Singh <owenbksingh@gmail.com>
|
6
6
|
Project-URL: Homepage, https://github.com/owensingh38/wsba_hockey/
|
@@ -1,19 +1,19 @@
|
|
1
1
|
wsba_hockey/__init__.py,sha256=QXyc8FFlCDWQuECyyEbj80ASHEbTFj4R13DOFOY9nJg,353
|
2
|
-
wsba_hockey/workspace.py,sha256=
|
3
|
-
wsba_hockey/wsba_main.py,sha256=
|
2
|
+
wsba_hockey/workspace.py,sha256=HJ4ZJyL8OwrtknXKAqiptW8WxbJZp3kc0bobDwpKcLY,4875
|
3
|
+
wsba_hockey/wsba_main.py,sha256=ZYfYpqJeUBnVHAjGrDWkEjoYW_8qWCilYukGuKUkJA4,37073
|
4
4
|
wsba_hockey/stats/calculate_viz/shot_impact.py,sha256=7zxf64yt87YDucUBG31W75u951AUbMC7a3x5ClNIxYI,39
|
5
5
|
wsba_hockey/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
6
|
wsba_hockey/tools/agg.py,sha256=WRfuxJt0OgDSEkqHtuGql9nZrQnLGzkm6DUFVbDXayE,9560
|
7
|
-
wsba_hockey/tools/plotting.py,sha256=
|
7
|
+
wsba_hockey/tools/plotting.py,sha256=mOkXO-ZWFTblsGpYSOZFxFJFad4sZBr62prVaatoLEw,6003
|
8
8
|
wsba_hockey/tools/scraping.py,sha256=hZv1XMtQjsVSSVwN6Fzw7FuT94zSQGyX1WupTjUjUuU,48548
|
9
|
-
wsba_hockey/tools/xg_model.py,sha256=
|
9
|
+
wsba_hockey/tools/xg_model.py,sha256=vdmDq9CWFr-2AKUrnorjUyweUf9_NMdq5Xf3mbPserw,15158
|
10
10
|
wsba_hockey/tools/archive/old_scraping.py,sha256=hEjMI1RtfeZnf0RBiJFI38oXkLZ3WofeH5xqcF4pzgM,49585
|
11
11
|
wsba_hockey/tools/utils/__init__.py,sha256=vccXhOtzARoR99fmEWU1OEI3qCIdQ9Z42AlRA_BUhrs,114
|
12
12
|
wsba_hockey/tools/utils/config.py,sha256=D3Uk05-YTyrhfReMTTLfNI3HN_rON2uo_CDE9oER3Lg,351
|
13
13
|
wsba_hockey/tools/utils/save_pages.py,sha256=CsyL_0n-b-4pJoUauwU3HpnCO6n69-RlBMJQBd_qGDc,4979
|
14
14
|
wsba_hockey/tools/utils/shared.py,sha256=dH_JwZfia5fib8rksy5sW-mBp0pluBPvw37Vdr8Kap0,14211
|
15
|
-
wsba_hockey-1.0.
|
16
|
-
wsba_hockey-1.0.
|
17
|
-
wsba_hockey-1.0.
|
18
|
-
wsba_hockey-1.0.
|
19
|
-
wsba_hockey-1.0.
|
15
|
+
wsba_hockey-1.0.2.dist-info/licenses/LICENSE,sha256=Nr_Um1Pd5FQJTWWgm7maZArdtYMbDhzXYSwyJIZDGik,1114
|
16
|
+
wsba_hockey-1.0.2.dist-info/METADATA,sha256=fBFZblpMy7vFKcM9MN4JBjJYSIIljdfVNd4GusrpbKU,3547
|
17
|
+
wsba_hockey-1.0.2.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
|
18
|
+
wsba_hockey-1.0.2.dist-info/top_level.txt,sha256=acU7s3x-RZC1zGiqCOmO0g267iqCg34lzIfdmYxxGmQ,12
|
19
|
+
wsba_hockey-1.0.2.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|