-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACC_CustomCommands.py
More file actions
281 lines (218 loc) · 9.99 KB
/
Copy pathACC_CustomCommands.py
File metadata and controls
281 lines (218 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
### Import dependencies
import json
from typing import List
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
import numpy as np
### Classes
from pandas import DataFrame
class PullData:
def __init__(self, dir_file: str, sheetname: str, sheetnum: int = 0):
"""
:param dir: Directory to access the API tokens
:param sheetname: Name of Google Sheet to access.
:param sheetnum: The page number to access.
"""
self.sheetName = sheetname
self.sheetNum = sheetnum
self.SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
self.CREDS = ServiceAccountCredentials.from_json_keyfile_name(dir_file, self.SCOPES)
def authClient(self) -> None:
"""
:return: None. Prints success/failure to console.
Connects to Google Spreadsheets and accesses the named spreadsheet.
"""
try:
client = gspread.authorize(self.CREDS)
self.sheet = client.open(self.sheetName)
print(f'Successfully authorised {self.sheetName}')
except:
print('Error occured in authorisation')
return
def getDataFrame(self) -> pd.DataFrame:
"""
:return: Copy of records DataFrame
"""
records: pd.DataFrame = pd.DataFrame.from_dict(self.sheet.get_worksheet(self.sheetNum).get_all_records())
return records.copy()
class Standings:
def __init__(self, dataframe: pd.DataFrame):
self.leaderboard: pd.DataFrame = dataframe.copy()
self.leaderboard = self.leaderboard[['Drivers', 'Total']]
self.leaderboard.sort_values('Total', axis=0, inplace=True, ascending=False, ignore_index=True)
self.output: str = 'CurrentStandings.png'
def getTable(self) -> None:
"""
:return: Returns None on image creation
Method creates League Standings table. Saves to main directory as CurrentStandings.png
"""
colours = ['gold', 'lightsteelblue', 'peru']
for i in range(len(self.leaderboard.index.values) - 3):
colours.append('gainsboro')
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.box(on=None)
the_figure = plt.table(cellText=np.transpose([self.leaderboard.Drivers.values, self.leaderboard.Total.values]),
cellColours=np.transpose([colours, colours]), cellLoc='center',
rowLabels=[x + 1 for x in self.leaderboard.index.values],
colLabels=['Drivers', 'Total'],
loc='center',
colWidths=[0.2, 0.2])
the_figure.scale(1.5, 2)
the_figure.auto_set_font_size(False)
the_figure.set_fontsize(12)
the_figure.auto_set_column_width(col=list(range(len(self.leaderboard.columns))))
plt.gcf().canvas.draw()
points = the_figure.get_window_extent(plt.gcf()._cachedRenderer).get_points()
points[0,:] -= 4; points[1,:] += 4
nbbox = Bbox.from_extents(points / plt.gcf().dpi)
plt.savefig(self.output,
bbox_inches=nbbox,
pad_inches=3,
dpi=100)
plt.clf()
return None
class RaceResults:
def __init__(self, dataframe: pd.DataFrame):
"""
:param dataframe: Input DataFrame containing the record of race results.
:type dataframe: pd.DataFrame
"""
self.output: str = 'RaceResults.png'
self.results = dataframe.copy()
self.results.replace("", np.nan, inplace=True)
self.results.dropna(how='all', axis=1, inplace=True)
self.results.drop(['Points', 'Result', 'Total'], axis=1, inplace=True)
rightpart = self.results.drop('Drivers', axis=1).to_numpy()
self.results.set_index('Drivers', inplace=True)
# Need to create colour map
colours = ['gold', 'lightsteelblue', 'peru', 'lightcoral', 'lightskyblue', 'gainsboro']
self.colormap = np.zeros_like(rightpart)
places = [25, 18, 15, 'DNF', 'DNS']
for i in range(len(places)):
self.colormap[rightpart == places[i]] = colours[i]
self.colormap[self.colormap == 0] = colours[-1]
def getTable(self) -> None:
"""
:return: Returns None on image creation
Method creates table of race results. Saves to main directory as RaceResults.png
"""
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.box(on=None)
the_figure = plt.table(cellText=self.results.to_numpy(),
cellColours=self.colormap, cellLoc='center',
rowLabels=self.results.index.values,
colLabels=self.results.columns.values,
loc='center')
the_figure.scale(1, 1.5)
the_figure.auto_set_font_size(False)
the_figure.set_fontsize(12)
the_figure.auto_set_column_width(col=list(range(len(self.results.columns))))
plt.gcf().canvas.draw()
points = the_figure.get_window_extent(plt.gcf()._cachedRenderer).get_points()
points[0, :] -= 4
points[1, :] += 4
nbbox = Bbox.from_extents(points / plt.gcf().dpi)
plt.savefig(self.output,
bbox_inches=nbbox,
pad_inches=3,
dpi=100)
plt.clf()
return None
class BestTimes:
def __init__(self, dataframe: pd.DataFrame) -> None:
"""
:param dataframe: Input DataFrame
:type dataframe: pd.DataFrame
"""
self.times: pd.DataFrame = dataframe.dropna(axis=1)
self.output: str = 'BestTimes.png'
tracks = self.times.columns.values
tracks = tracks[~self.times.columns.str.contains('Track')]
new_cols = []
for i in np.arange(0, len(tracks), 2):
new_cols.append(f'{tracks[i]}: {self.times.iat[0, i + 1]}')
new_cols.append(f'{tracks[i]}: {self.times.iat[0, i + 2]}')
self.times = self.times[1:]
self.times.set_index('Track', inplace=True)
self.times.columns = new_cols
self.rightpart = self.times.to_numpy()
def getColours(self):
"""
:return: colourmap, column colours and row colours
"""
colours = ['deeppink', 'gainsboro']
colormap = np.zeros_like(self.rightpart)
colormap[colormap == 0] = colours[-1]
colcolours = np.zeros_like(self.times.columns.values)
colcolours[colcolours == 0] = 'bisque'
rowcolours = np.zeros_like(self.times.index.values)
rowcolours[rowcolours == 0] = 'bisque'
return colormap, colcolours, rowcolours
def getTable(self) -> None:
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.box(on=None)
colormap, colcolours, rowcolours = self.getColours()
the_figure = plt.table(cellText=self.times.to_numpy(),
cellColours=colormap, cellLoc='center',
rowLabels=self.times.index.values,
colLabels=self.times.columns.values,
loc='center',
colColours=colcolours,
rowColours=rowcolours,
fontsize = 26)
the_figure.scale(1, 1.5)
the_figure.auto_set_font_size(False)
the_figure.set_fontsize(12)
the_figure.auto_set_column_width(col=list(range(len(self.times.columns))))
plt.gcf().canvas.draw()
points = the_figure.get_window_extent(plt.gcf()._cachedRenderer).get_points()
points[0, :] -= 4
points[1, :] += 4
nbbox = Bbox.from_extents(points / plt.gcf().dpi)
plt.savefig(self.output,
bbox_inches=nbbox,
pad_inches=3,
dpi=100)
plt.clf()
return None
class UpdateFigures:
"""
The UpdateFigures class concatenates the classes into one admin command.
This reduces the number of requests to the Google Spreadsheet, thus allowing for faster returns.
Since updates mainly occur after races, no need to poll the spreadsheet many times.
Introducing a poll for the times command may be beneficial.
This might implemented in the future
"""
def __init__(self, dir_file, spreadsheet: str, section: str = 'all'):
options = ['all', 'standings', 'results', 'times']
self.updated_images: List[str] = []
if self.section not in options:
raise Exception('Argument not in list')
self.section = section
self.googledata = PullData(dir_file, spreadsheet)
self.googledata.authClient()
if self.section == "standings" or "all":
standings_command = Standings(self.googledata.getDataFrame())
standings_command.getTable()
self.updated_images.append(standings_command.output)
plt.clf()
if self.section == "results" or "all":
results_command = RaceResults(self.googledata.getDataFrame())
results_command.getTable()
self.updated_images.append(results_command.output)
plt.clf()
if self.section == "times" or "all":
self.googledata.sheetNum = 1
times_command = BestTimes(self.googledata.getDataFrame())
times_command.getTable()
self.updated_images.append(times_command.output)
plt.clf()