-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpe_plot.py
More file actions
227 lines (145 loc) · 6.22 KB
/
pe_plot.py
File metadata and controls
227 lines (145 loc) · 6.22 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
from typing import Optional
import plotly.express as px
import plotly.io as pio
import matplotlib.pyplot as plt
import pe_api
import pe_image
import pe_global_objects as pe_global
import datetime
import pytz
import time
import os
import glob
import shutil
import locale
# Called when started
def graph_start():
pio.templates.default = "plotly"
def project_euler_date_converter(s: str):
minimal_date = datetime.datetime(1980, 1, 1, 0, 0, 0)
project_euler_time_format = "%d %b %y (%H:%M)"
# print(datetime.datetime.strftime(datetime.datetime.now(), project_euler_time_format))
if "date" in s:
return minimal_date
else:
try:
return datetime.datetime.strptime(s, project_euler_time_format)
except:
changes = [(m, m.lower() + ".") for m in ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]]
for change in changes:
s = s.replace(change[0], change[1])
return datetime.datetime.strptime(s, project_euler_time_format)
# Return a graph of the last solves during the last 'day_counts' days.
def graph_solves(day_counts: int, local: bool, smoothing = 1):
save_location = "graphs/solves_figure.png"
database_format = "%Y-%m-%d"
output_format = "%Y-%m-%d"
if local is True:
data = pe_api.get_solves_in_database()
data_len = day_counts + 1
current_day = datetime.datetime.now(pytz.utc)
days_list = [(current_day - datetime.timedelta(days=x)).strftime(output_format) for x in range(data_len)]
counts = {day: 0 for day in days_list}
for element in data:
day_as_key = datetime.datetime.strptime(element["solve_date"].split()[0], database_format).strftime(output_format)
if day_as_key in counts:
counts[day_as_key] += 1
else:
data: list = pe_api.get_global_solves_in_database()
minimum_day = datetime.datetime.now(pytz.utc) - datetime.timedelta(days=day_counts)
filtered_data = []
for element in data:
d = datetime.datetime.strptime(element["date_stat"], "%Y-%m-%d %H:%M:%S")
if d >= minimum_day:
filtered_data.append({"date_stat": element["date_stat"], "solves": element["solves"]})
data_len = len(filtered_data)
days_list = [element["date_stat"] for element in filtered_data]
counts = {element["date_stat"]: element["solves"] for element in filtered_data}
data_df = {"DATE": days_list, "SOLVES": list(counts.values())}
figure = px.line(data_df, x="DATE", y="SOLVES")
figure.write_image(save_location)
return save_location
def format_data_for_individual_graph(file_content: str, username: str) -> list:
seperator = ","
path = f"graphs/{username}/"
try:
locale.setlocale(locale.LC_TIME, "en_US")
except Exception as e:
pass
try:
os.mkdir(path)
except:
files = glob.glob(path + "*")
for f in files:
os.remove(f)
new_file_content = file_content.split("\n")
# Remove \r at end of lines
lines = list(map(lambda line: line.replace("\r", ""), new_file_content))
solves = list(map(lambda l: l.split(seperator), lines))
# Remove blank lines
solves = list(filter(lambda element: len(element) > 1, solves))
# Not showing up bonus problems for now
solves = list(filter(lambda element: element[0][0] != "B", solves))
for i in range(len(solves)):
solves[i][1] = str(solves[i][1])
# print(solves[i])
solves[i] = [int(solves[i][0]), project_euler_date_converter(solves[i][-1])]
solves = solves[::-1]
return solves
def generate_individual_graph(file_content: str, username: str) -> Optional[str]:
minimal_date = datetime.datetime(1980, 1, 1, 0, 0, 0)
solves = format_data_for_individual_graph(file_content, username)
frame_count = 100
additional_frame_count = 25
temp_epsilon = 1000
starting_timestamp = list(filter(
lambda el: el[1].timestamp() - temp_epsilon > minimal_date.timestamp(),
solves
))[0][1].timestamp()
difference = solves[-1][1].timestamp() - starting_timestamp + temp_epsilon
try:
problems = pe_api.problems_list()[1:-1]
except Exception as _:
return None
for percentage in range(frame_count + 1):
current_timestamp = starting_timestamp + difference * percentage / frame_count
last_pb = len(list(filter(lambda el: pe_global.pe_unix_from_time(el[2]) < current_timestamp, problems)))
pe_image.image_for_timestamp_user_solve(
solves, current_timestamp, username, percentage,
frame_count, percentage, last_pb
)
for addition in range(1, additional_frame_count + 1):
current_timestamp = starting_timestamp + difference
last_pb = len(problems)
pe_image.image_for_timestamp_user_solve(
solves, current_timestamp, username, frame_count,
frame_count, frame_count + addition, last_pb
)
pe_image.concatenate_image_gif(username)
return f"graphs/{username}/{username}.gif"
def generate_simple_individual_graph(solves, username):
# solves = format_data_for_individual_graph(file_content, username)
solve_times = []
solve_count = 0
minimal_date = datetime.datetime(1980, 1, 1, 0, 0, 0)
temp_epsilon = 1000
for solve in solves:
solve_count += 1
if solve[1].timestamp() - temp_epsilon > minimal_date.timestamp():
solve_times.append([solve_count, solve[1]])
counts = [s[0] for s in solve_times]
times = [s[1] for s in solve_times]
save_path = f"graphs/{username}/{username}-linear-plot.png"
plt.cla()
plt.style.use('ggplot')
plt.title("Solves versus time")
plt.plot(times, counts)
plt.gcf().autofmt_xdate()
plt.savefig(save_path, bbox_inches='tight')
return save_path
if __name__ == "__main__":
with open("pjt33_history_2023_04_25_2325.csv", "r") as f:
content = "".join(f.readlines())
tic = time.time()
generate_individual_graph(content, "Teyzer18")
print(time.time() - tic)