1+ # -*- coding: utf-8 -*-
2+ """
3+ Created on Wed Sep 18 14:33:57 2019
4+
5+ @author: Mason
6+ """
7+
8+ import pandas as pd
9+ import numpy as np
10+ import seaborn as sb
11+ from tkinter import filedialog
12+ from pprint import pprint
13+ import matplotlib .pyplot as plt
14+ import matplotlib .gridspec as gridspec
15+ import matplotlib .ticker as ticker
16+
17+ pd .set_option ('display.max_columns' , 200 )
18+
19+ def combineColumn (scoutData ):
20+ '''
21+ This combines columns and creates columns from adding other columns. Specifics:
22+ Total/low/high/outer/inner goal makes/misses/attempts across game phase(overall)
23+ Percent of shots are makes, percent of makes in low/high/outer goals, accuracy
24+ in low and high goal, autonomous score, teleop score.
25+ '''
26+
27+ scoutData ['totalAttempts' ]= scoutData ['lowGoalMissesAuto' ]+ scoutData ['highGoalMissesAuto' ]
28+ scoutData ['totalAttempts' ]+= scoutData ['lowGoalMakesAuto' ]+ scoutData ['outerGoalMakesAuto' ]
29+ scoutData ['totalAttempts' ]+= scoutData ['innerGoalMakesAuto' ]+ scoutData ['lowGoalMissesTele' ]
30+ scoutData ['totalAttempts' ]+= scoutData ['lowGoalMakesTele' ]+ scoutData ['outerGoalMakesTele' ]
31+ scoutData ['totalAttempts' ]+= scoutData ['innerGoalMakesTele' ]
32+
33+
34+ scoutData ['lowGoalAttemptsAuto' ]= scoutData ['lowGoalMissesAuto' ]+ scoutData ['lowGoalMakesAuto' ]
35+
36+ scoutData ['lowGoalAttemptsTele' ]= scoutData ['lowGoalMissesTele' ]+ scoutData ['lowGoalMakesTele' ]
37+
38+ scoutData ['lowGoalAttempts' ]= scoutData ['lowGoalAttemptsAuto' ]+ scoutData ['lowGoalAttemptsTele' ]
39+
40+ scoutData ['lowGoalMakes' ]= scoutData ['lowGoalMakesAuto' ]+ scoutData ['lowGoalMakesTele' ]
41+
42+ scoutData ['highGoalMakesAuto' ] = (scoutData ['outerGoalMakesAuto' ]+ scoutData ['innerGoalMakesAuto' ])
43+
44+ scoutData ['highGoalAttemptsAuto' ]= (scoutData ['outerGoalMakesAuto' ]+ scoutData ['innerGoalMakesAuto' ])+ scoutData ['highGoalMissesAuto' ]
45+
46+ scoutData ['highGoalAttemptsTele' ]= (scoutData ['outerGoalMakesTele' ]+ scoutData ['innerGoalMakesTele' ])+ scoutData ['highGoalMissesTele' ]
47+
48+ scoutData ['highGoalMakesTele' ]= scoutData ['outerGoalMakesTele' ]+ scoutData ['innerGoalMakesTele' ]
49+
50+ scoutData ['highGoalAttempts' ]= scoutData ['highGoalAttemptsAuto' ]+ scoutData ['highGoalAttemptsTele' ]
51+
52+ scoutData ['highGoalMakes' ]= scoutData ['outerGoalMakesTele' ]+ scoutData ['outerGoalMakesAuto' ]+ scoutData ['innerGoalMakesTele' ]+ scoutData ['innerGoalMakesAuto' ]
53+
54+
55+ scoutData ['outerGoalMakes' ]= scoutData ['outerGoalMakesTele' ]+ scoutData ['outerGoalMakesAuto' ]
56+
57+
58+ scoutData ['innerGoalMakes' ]= scoutData ['innerGoalMakesAuto' ]+ scoutData ['innerGoalMakesTele' ]
59+
60+
61+ scoutData ['totalMakes' ]= scoutData ['innerGoalMakes' ]+ scoutData ['outerGoalMakes' ]+ scoutData ['lowGoalMakes' ]
62+
63+
64+ scoutData ['autoMakes' ]= scoutData ['innerGoalMakesAuto' ]+ scoutData ['outerGoalMakesAuto' ]+ scoutData ['lowGoalMakesAuto' ]
65+
66+
67+ scoutData ['totalAccuracy' ]= (scoutData ['totalMakes' ]/ scoutData ['totalAttempts' ])* 100
68+
69+ scoutData ['lowGoalMakesAccuracy' ]= (scoutData ['lowGoalMakes' ]/ scoutData ['lowGoalAttempts' ])* 100
70+
71+ scoutData ['lowGoalMakesAccuracyAuto' ]= (scoutData ['lowGoalMakesAuto' ]/ scoutData ['lowGoalAttemptsAuto' ])* 100
72+
73+ scoutData ['lowGoalMakesAccuracyTele' ]= (scoutData ['lowGoalMakesTele' ]/ scoutData ['lowGoalAttemptsTele' ])* 100
74+
75+ scoutData ['highGoalMakesAccuracy' ]= (scoutData ['highGoalMakes' ]/ scoutData ['highGoalAttempts' ])* 100
76+
77+ scoutData ['highGoalMakesAccuracyAuto' ]= ((scoutData ['outerGoalMakesAuto' ]+ scoutData ['innerGoalMakesAuto' ])/ scoutData ['highGoalAttemptsAuto' ])* 100
78+
79+ scoutData ['highGoalMakesAccuracyTele' ]= ((scoutData ['outerGoalMakesTele' ]+ scoutData ['innerGoalMakesTele' ])/ scoutData ['highGoalAttemptsTele' ])* 100
80+
81+ scoutData ['percentOfLowGoal' ]= (scoutData ['lowGoalMakes' ]/ scoutData ['totalMakes' ])* 100
82+
83+ scoutData ['percentOfOuterGoal' ]= (scoutData ['outerGoalMakes' ]/ scoutData ['totalMakes' ])* 100
84+
85+ scoutData ['percentOfInnerGoal' ]= (scoutData ['innerGoalMakes' ]/ scoutData ['totalMakes' ])* 100
86+
87+
88+ scoutData ['teleopScore' ]= scoutData ['lowGoalMakesTele' ]+ 2 * scoutData ['outerGoalMakesTele' ]
89+ scoutData ['teleopScore' ]+= 3 * scoutData ['innerGoalMakesTele' ]
90+
91+
92+ scoutData ['autoScore' ]= 2 * scoutData ['lowGoalMakesAuto' ]+ 4 * scoutData ['outerGoalMakesAuto' ]
93+ scoutData ['autoScore' ]+= 6 * scoutData ['innerGoalMakesAuto' ]
94+
95+
96+ scoutData = scoutData .fillna (0 )
97+
98+
99+ # print(scoutData.head())
100+
101+ return scoutData
102+
103+ def getPicklistHeatmap (mainDf , df , ax , graphVar ):
104+ df ['highGoalMakes' ] = df ['innerGoalMakes' ] + df ['outerGoalMakes' ]
105+ pprint (df )
106+ ''' FIX ME VICTORIA
107+ THE APPEND DOESN'T WORK'''
108+ mainteams = mainDf ['teamNo' ].drop_duplicates ().to_numpy ()
109+ teams = df ['teamNo' ].drop_duplicates ().to_numpy ()
110+
111+ for team in mainteams :
112+ if team not in teams :
113+ #df.append([0, 0, team, 0, 'A', 0, 0, 0, 0, 0, 0])
114+
115+ newdfentry = pd .DataFrame ({'id' : [df ['id' ].max ()],'matchNo' :[0 ], 'teamNo' :[team ], 'cycle' :[0 ], 'shooterPosition' :['A' ], 'lowGoalMisses' :[0 ],
116+ 'highGoalMisses' :[0 ], 'lowGoalMakes' :[0 ], 'outerGoalMakes' :[0 ], 'innerGoalMakes' :[0 ],
117+ 'gamePhase' :[0 ], 'highGoalMakes' :[0 ]})
118+
119+ print ('\n ' , newdfentry , '\n ' )
120+
121+ df = pd .concat ([df , newdfentry ], ignore_index = True )
122+ print (team )
123+
124+ print ()
125+ print (df .tail ())
126+ print (df ['teamNo' ].drop_duplicates ())
127+
128+ df = df .sort_values ('teamNo' , ascending = True )
129+ highGoalMakesbyMatchDf = getHeatMapPivot (df .loc [:,['matchNo' ,'teamNo' ,'cycle' ,'shooterPosition' , graphVar ]])
130+ cookedDf = pd .pivot_table (highGoalMakesbyMatchDf .reset_index ().drop (['matchNo' ], axis = 1 ), index = 'teamNo' )
131+ print (cookedDf .stack (1 ).unstack (level = 0 ))
132+ yLabels = ['A' ]
133+ for position in df ['shooterPosition' ].sort_values ().values :
134+ # print(position)
135+ passer = False
136+ for label in yLabels :
137+ if label == position :
138+ passer = True
139+ if passer == False :
140+ yLabels .append (position )
141+ teamList = df ['teamNo' ].drop_duplicates ().to_numpy ()
142+ ax .set_title (graphVar )
143+ sb .heatmap (cookedDf .stack (1 ).unstack (level = 0 ).to_numpy (), cmap = "YlGn" , ax = ax , annot = True , vmin = 0 , vmax = 55 , yticklabels = yLabels , xticklabels = teamList )
144+
145+ def getPicklistBoxplotData (df , graphVar , title , ax ):
146+
147+ df = df .sort_values ('teamNo' , ascending = True )
148+ df = combineColumn (df )
149+ teamList = df ['teamNo' ].drop_duplicates ()
150+ df .set_index ('teamNo' , inplace = True )
151+ data = []
152+ dataArr = []
153+ k = 0
154+ for team in teamList :
155+ data .append (df .loc [[team ], [graphVar ]].get_values ())
156+ for i in data :
157+ dataArr .append ([])
158+
159+ for j in i :
160+ dataArr [k ].append (j [0 ])
161+ k += 1
162+ ax .set_title (title , fontsize = 14 )
163+ ax .set_xticklabels (teamList .get_values ())
164+ ax .boxplot (dataArr )
165+
166+ def getClimbHeatMap (mainData , ax ):
167+ mainData ['climbCounter' ] = 1
168+ climbData = mainData .set_index ('teamNo' ).loc [:, ['climbLevel' , 'climbCounter' ]]
169+ climbData = pd .pivot_table (climbData .reset_index (), index = ['teamNo' , 'climbLevel' ], aggfunc = sum )
170+ climbSums = pd .pivot_table (climbData .reset_index (), index = 'teamNo' , columns = 'climbLevel' , aggfunc = sum , margins = True )
171+ climbSums .fillna (0 , inplace = True )
172+ climbSums .drop ('All' , inplace = True )
173+ xLabels = climbSums .index .to_series ().values
174+ climbSums = climbSums .stack (1 ).unstack (level = 0 )
175+ climbSums .drop ('No Climb' , inplace = True )
176+ yLabels = climbSums .index .to_series ().values
177+ sb .heatmap (climbSums .to_numpy (), cmap = "YlGn" , ax = ax , annot = True , yticklabels = yLabels , xticklabels = xLabels )
178+
179+
180+ def getTeamList (df ):
181+ teamList = df ['teamNo' ].drop_duplicates ()
182+ return (teamList )
183+
184+ def initPicklistGraph (teamList ):
185+ fig = plt .figure (tight_layout = True , figsize = (len (teamList ), 10 ))
186+ gs = gridspec .GridSpec (5 , 1 )
187+ return fig , gs
188+
189+ def initMatchReportGraph ():
190+ fig = plt .figure (tight_layout = True , figsize = (14 , 7 ))
191+ gs = gridspec .GridSpec (2 , 4 )
192+ return fig , gs
193+
194+
195+ def picklistGraphs (df , cycleDf ):
196+ fig , gs = initPicklistGraph (getTeamList (df ))
197+ ax1 = fig .add_subplot (gs [0 , 0 ])
198+ ax2 = fig .add_subplot (gs [1 , 0 ])
199+ ax3 = fig .add_subplot (gs [2 , 0 ])
200+ ax4 = fig .add_subplot (gs [3 , 0 ])
201+ ax5 = fig .add_subplot (gs [4 , 0 ])
202+ getPicklistBoxplotData (df , 'totalMakes' , 'Total Shots Made' , ax1 )
203+ getPicklistBoxplotData (df , 'highGoalMakes' , 'Total High Shots Made' , ax2 )
204+ getPicklistBoxplotData (df , 'autoMakes' , 'Total Auto Shots Made' , ax3 )
205+ getPicklistHeatmap (df , cycleDf , ax4 , 'highGoalMakes' )
206+ getClimbHeatMap (df , ax5 )
207+ plt .savefig (input ('Event name: ' ) + ' Picklist Graphs' )
208+ plt .show ()
209+
210+
211+ def getPrematchScatterPlot (df , team , graphVar , ax ):
212+ ax .scatter (df .loc [[team ], ["matchNo" ]], df .loc [[team ], [graphVar ]], color = "red" )
213+ ax .set_ylim (top = 55 , bottom = 0 )
214+ ax .xaxis .set_major_locator (ticker .MaxNLocator (integer = True ))
215+ if graphVar == 'autoMakes' :
216+ ax .set_ylim (top = 15 , bottom = 0 )
217+
218+ ax .set_title (graphVar )
219+ ax .set_xlabel ('Matches' )
220+ ax .set_ylabel (graphVar )
221+
222+
223+ def getHeatMapPivot (df ):
224+ firstmove = pd .pivot_table (df , index = ['matchNo' ,'teamNo' ,'cycle' ], columns = 'shooterPosition' ,aggfunc = np .sum ).fillna (0 )
225+ print (firstmove )
226+ secondmove = firstmove .groupby (axis = 0 ,level = ['matchNo' ,'teamNo' ])
227+ thirdmove = secondmove .sum ()
228+ # print(thirdmove)
229+ fourthmove = thirdmove .swaplevel (i = 'matchNo' ,j = 'teamNo' ).sort_index ()
230+ # print(fourthmove)
231+ return fourthmove
232+
233+ def getHeatMap (df , mainDf , team , graphVar , ax ):
234+ maxShot = 55
235+ df ['highGoalMakes' ] = df ['innerGoalMakes' ] + df ['outerGoalMakes' ]
236+ df ['lowGoalAttempts' ] = df ['lowGoalMisses' ] + df ['lowGoalMakes' ]
237+ df ['highGoalAttempts' ] = df ['highGoalMisses' ] + df ['innerGoalMakes' ] + df ['outerGoalMakes' ]
238+ highGoalMakesbyMatchDf = getHeatMapPivot (df .loc [:,['matchNo' ,'teamNo' ,'cycle' ,'shooterPosition' , graphVar ]])
239+ yLabels = ['A' ]
240+ matchNum = []
241+ for position in df ['shooterPosition' ].sort_values ().values :
242+ passer = False
243+ for label in yLabels :
244+ if label == position :
245+ passer = True
246+ if passer == False :
247+ yLabels .append (position )
248+ for match in mainDf .set_index ('teamNo' ).loc [[team ], ["matchNo" ]].values :
249+ matchNum .append (str (match [0 ]))
250+ print (matchNum )
251+ print (yLabels )
252+ ax .set_title (graphVar )
253+ ax .set_xlabel ('Matches' )
254+ ax .set_ylabel ('Position' )
255+ try : sb .heatmap (highGoalMakesbyMatchDf .loc [[team ], :].unstack ().stack (1 ).to_numpy (), cmap = "YlGn" , ax = ax , annot = True , yticklabels = yLabels , xticklabels = matchNum , vmin = 0 , vmax = maxShot )
256+ except :print ('data not available' )
257+
258+
259+
260+ def prematchGraphs (maindf , cycledf , team ):
261+ df = combineColumn (maindf )
262+ df .set_index ("teamNo" , inplace = True )
263+ fig , gs = initMatchReportGraph ()
264+ ax1 = fig .add_subplot (gs [0 , 0 ])
265+ ax2 = fig .add_subplot (gs [0 , 1 ])
266+ ax3 = fig .add_subplot (gs [0 , 2 ])
267+ ax4 = fig .add_subplot (gs [0 , 3 ])
268+ ax5 = fig .add_subplot (gs [1 , 1 ])
269+ ax6 = fig .add_subplot (gs [1 , 2 ])
270+ ax7 = fig .add_subplot (gs [1 , 3 ])
271+ getPrematchScatterPlot (df , team , 'autoMakes' , ax1 )
272+ getPrematchScatterPlot (df , team , 'totalMakes' , ax2 )
273+ getHeatMap (cycledf , maindf , team , 'highGoalAttempts' , ax3 )
274+ getHeatMap (cycledf , maindf , team , 'lowGoalAttempts' , ax4 )
275+ getHeatMap (cycledf , maindf , team , 'innerGoalMakes' , ax5 )
276+ getHeatMap (cycledf , maindf , team , 'outerGoalMakes' , ax6 )
277+ getHeatMap (cycledf , maindf , team , 'lowGoalMakes' , ax7 )
278+ ax3 .title .set_position ([.5 , 1.2 ])
279+ ax4 .title .set_position ([.5 , 1.2 ])
280+ ax5 .title .set_position ([.5 , 1.2 ])
281+ ax6 .title .set_position ([.5 , 1.2 ])
282+ ax7 .title .set_position ([.5 , 1.2 ])
283+ plt .savefig (str (team ) + ' Prematch Graphs' )
284+ plt .show ()
285+
286+ maindf = pd .read_csv (filedialog .askopenfilename (title = 'select unfiltered main data file' ), sep = '|' )
287+ cycledf = pd .read_csv (filedialog .askopenfilename (title = 'select unfiltered cycle data file' ), sep = '|' )
288+ picklistGraphs (maindf , cycledf )
289+ #prematchGraphs(maindf, cycledf, 1939)
290+ #getPicklistHeatmapPivot(cycledf, 'outerGoalMakes')
0 commit comments