-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpe_plot.py
More file actions
515 lines (364 loc) · 16.8 KB
/
Copy pathpe_plot.py
File metadata and controls
515 lines (364 loc) · 16.8 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
from typing import Optional
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import math
import pe_api
import pe_image
import pe_global_objects as pe_global
import pe_setup
import datetime
import pytz
import time
import os
import glob
import shutil
import asyncio
import locale
# Called when started
def graph_start():
pio.templates.default = "plotly"
def _github_grid_rows(total_days: int, target_ratio: float = 3.0) -> int:
"""Round the grid height up to the next multiple of 7 rows."""
base_rows = max(1, math.ceil(math.sqrt(total_days / target_ratio)))
return int(math.ceil(base_rows / 7) * 7)
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").replace(tzinfo=pytz.utc)
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}
plot_data = sorted(zip(map(datetime.datetime.fromisoformat, days_list), counts.values()))
plt.style.use("ggplot")
plt.cla()
plt.title("Solves versus time")
plt.plot(*zip(*plot_data))
plt.gcf().autofmt_xdate()
plt.savefig(save_location, bbox_inches="tight")
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
async 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 = (await 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
async def generate_graph_monthly(member: pe_api.Member) -> str:
r = await member.solves_by_csv(True)
username = await member.username_option()
# 1. Process dates
df = pd.DataFrame([{"date": datetime.datetime.fromtimestamp(s.unixtime())} for s in r]).sort_values(by="date")
df['month'] = df['date'].dt.to_period('M').dt.to_timestamp()
# 2. Fill empty months and calculate cumulative solves
df_months = pd.DataFrame({'month': pd.date_range(start=df['month'].min(), end=df['month'].max(), freq='MS')})
df_months = df_months.merge(df.groupby('month').size().reset_index(name='monthly'), on='month', how='left').fillna(0)
df_months['cumulative'] = df_months['monthly'].cumsum()
# 3. Build the plot
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Bar(x=df_months['month'], y=df_months['monthly'], marker_color='rgba(150,150,150,0.4)'), secondary_y=False)
fig.add_trace(go.Scatter(x=df_months['month'], y=df_months['cumulative'], mode='lines', line=dict(color='#d62728', width=4)), secondary_y=True)
fig.update_layout(
title=dict(text=f"Monthly and Cumulative Solves - {username}", font=dict(size=24)),
plot_bgcolor='#f0f0f0', # Inner graph area (light gray)
paper_bgcolor='white', # Outer margins (solid white)
showlegend=False,
bargap=0.1,
width=1200,
height=600,
margin=dict(l=20, r=20, t=60, b=20)
)
# Add white grid lines for the X-axis (vertical lines)
fig.update_xaxes(showgrid=True, gridcolor='white', gridwidth=1.5)
# Add white grid lines for the primary Y-axis (horizontal lines)
fig.update_yaxes(title_text="Monthly Solves", showgrid=True, gridcolor='white', gridwidth=1.5, secondary_y=False)
# Keep the secondary Y-axis grid off to prevent messy overlapping lines
fig.update_yaxes(title_text="Cumulative Solves", showgrid=False, secondary_y=True)
# 4. Save and return path
filename = f"images_saves/{member._username}_graph_monthly.png"
fig.write_image(filename, scale=2)
return filename
async def generate_graph_github(member: pe_api.Member) -> str:
r = await member.solves_by_csv(True)
username = await member.username_option()
# 1. Process dates and build continuous timeline
dates = [datetime.datetime.fromtimestamp(s.unixtime()).date() for s in r]
df = pd.DataFrame({'date': pd.date_range(start=min(dates), end=max(dates), freq='D')})
# Count solves and merge
solve_counts = pd.Series(dates).value_counts().reset_index()
solve_counts.columns = ['date', 'solves']
df['date'] = df['date'].dt.date
df = df.merge(solve_counts, on='date', how='left').fillna({'solves': 0})
# 2. Grid Math (Target Aspect Ratio 3:1)
num_rows = _github_grid_rows(len(df))
df['x_index'] = df.index // num_rows
df['y_index'] = df.index % num_rows
# 3. Locate years for X-axis labels
df['year'] = pd.to_datetime(df['date']).dt.year
years = df.drop_duplicates(subset=['year'])
# 4. Build the plot
heatmap = df.pivot(index='y_index', columns='x_index', values='solves')
heatmap = heatmap.reindex(index=range(num_rows), fill_value=0)
colors = [[0.0, '#ebedf0'], [0.01, '#9be9a8'], [0.33, '#40c463'], [0.66, '#30a14e'], [1.0, '#216e39']]
fig = go.Figure(data=go.Heatmap(
z=heatmap.values, x=heatmap.columns, y=heatmap.index,
colorscale=colors, xgap=2, ygap=2, showscale=False, hoverongaps=False
))
fig.update_layout(
title=dict(text=f"Solve Activity - {username}", font=dict(size=24)),
plot_bgcolor='white',
paper_bgcolor='white',
width=1200, height=500, # Adjusted height for the 3:1 ratio
margin=dict(l=20, r=20, t=60, b=20),
xaxis=dict(
showgrid=False, zeroline=False, side='top', ticks="", constrain="domain",
tickmode='array', tickvals=years['x_index'].tolist(), ticktext=years['year'].astype(str).tolist()
),
yaxis=dict(
autorange="reversed", showgrid=False, zeroline=False, showticklabels=False,
scaleanchor="x", scaleratio=1, constrain="domain" # Forces perfect squares
)
)
# 5. Save and return path
filename = f"images_saves/{member._username}_graph_github.png"
fig.write_image(filename, scale=2)
return filename
async def generate_graph_difficulty(member: pe_api.Member) -> str:
r = await member.solves_by_csv(True)
username = await member.username_option()
# 1. Process dates and fetch difficulties asynchronously
data = []
for s in r:
diff = await s.problem().difficulty()
data.append({
"date": datetime.datetime.fromtimestamp(s.unixtime()),
"difficulty": diff if diff is not None else 0
})
df = pd.DataFrame(data).sort_values(by="date").reset_index(drop=True)
# 2. Smooth by a rolling average of the last 20 solves
df['smoothed_diff'] = df['difficulty'].rolling(window=20, min_periods=1).mean()
# 3. Build the plot
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df['date'], y=df['smoothed_diff'],
mode='lines', line=dict(color='#ff7f0e', width=4) # Just the solid orange line
))
fig.update_layout(
title=dict(text=f"Average Difficulty Progression (Rolling 20 Solves) - {username}", font=dict(size=24)),
plot_bgcolor='#f0f0f0', # Inner graph area (light gray)
paper_bgcolor='white', # Outer margins (solid white)
width=1200, height=600,
margin=dict(l=20, r=20, t=60, b=20),
showlegend=False
)
# Add the white gridlines to match the monthly graph
fig.update_xaxes(showgrid=True, gridcolor='white', gridwidth=1.5)
fig.update_yaxes(title_text="Difficulty (%)", showgrid=True, gridcolor='white', gridwidth=1.5, rangemode='tozero')
# 4. Save and return path
filename = f"images_saves/{member._username}_graph_difficulty.png"
fig.write_image(filename, scale=2)
return filename
def _build_gol_gif(r, username: str) -> str:
# Process dates and build continuous timeline
dates = [datetime.datetime.fromtimestamp(s.unixtime()).date() for s in r]
df = pd.DataFrame({'date': pd.date_range(start=min(dates), end=max(dates), freq='D')})
solve_counts = pd.Series(dates).value_counts().reset_index()
solve_counts.columns = ['date', 'solves']
df['date'] = df['date'].dt.date
df = df.merge(solve_counts, on='date', how='left').fillna({'solves': 0})
# Grid Math (Target Aspect Ratio 3:1)
num_rows = _github_grid_rows(len(df))
df['x_index'] = df.index // num_rows
df['y_index'] = df.index % num_rows
# Create the initial state matrix
heatmap = df.pivot(index='y_index', columns='x_index', values='solves').fillna(0)
heatmap = heatmap.reindex(index=range(num_rows), fill_value=0)
grid = (heatmap.values > 0).astype(int)
h, w = grid.shape
# Drawing parameters
cell_size = 12
gap = 3
step_size = cell_size + gap
title_space = 52
img_h = h * step_size + gap + title_space
img_w = w * step_size + gap
color_dead = np.array([235, 237, 240], dtype=np.uint8)
color_alive = np.array([33, 110, 57], dtype=np.uint8)
color_bg = np.array([255, 255, 255], dtype=np.uint8)
color_text = (60, 60, 60)
title = f"Game of Life for member {username}"
try:
font = ImageFont.truetype("DejaVuSans.ttf", size=22)
except Exception:
font = ImageFont.load_default()
frames = []
# Timing model
accel_generations = 50
fastest_frame_duration_ms = 50
steady_phase_ms = 20_000
steady_generations = max(1, steady_phase_ms // fastest_frame_duration_ms)
num_generations = accel_generations + steady_generations
durations = [1500]
for i in range(1, accel_generations):
progress = i / (accel_generations - 1)
current_duration = fastest_frame_duration_ms + 750 * ((1 - progress) ** 2)
durations.append(int(current_duration))
durations.extend([fastest_frame_duration_ms] * steady_generations)
# Game of Life Simulation Loop
for _ in range(num_generations):
img_array = np.full((img_h, img_w, 3), color_bg, dtype=np.uint8)
for i in range(h):
for j in range(w):
color = color_alive if grid[i, j] else color_dead
y = gap + title_space + i * step_size
x = gap + j * step_size
img_array[y:y+cell_size, x:x+cell_size] = color
frame_img = Image.fromarray(img_array)
draw = ImageDraw.Draw(frame_img)
bbox = draw.textbbox((0, 0), title, font=font)
text_w = bbox[2] - bbox[0]
text_x = max(0, (img_w - text_w) // 2)
draw.text((text_x, 14), title, fill=color_text, font=font)
frames.append(frame_img)
# Calculate next generation
padded = np.pad(grid, 1, mode='constant')
neighbors = sum(np.roll(np.roll(padded, i, 0), j, 1)
for i in (-1, 0, 1) for j in (-1, 0, 1)
if (i != 0 or j != 0))
neighbors = neighbors[1:-1, 1:-1]
# Conway's Rules
grid = ((neighbors == 3) | (grid & (neighbors == 2))).astype(int)
# Save as GIF
filename = f"images_saves/{username}_gol_github.gif"
frames[0].save(
filename,
save_all=True,
append_images=frames[1:],
duration=durations,
loop=0
)
return filename
# 2. Keep the async wrapper for network calls and thread offloading
async def generate_graph_github_gol(member: pe_api.Member) -> str:
r = await member.solves_by_csv(True)
username = await member.username_option()
filename = await asyncio.to_thread(_build_gol_gif, r, username)
return filename
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)
pe_setup.setup()
m = pe_api.Member(_username="pacome_f")
r = asyncio.run(m.solves_by_csv())