-
Notifications
You must be signed in to change notification settings - Fork 1
/
charts.py
97 lines (77 loc) · 2.75 KB
/
charts.py
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
"""A set of functions to generate, store and retrieve charts."""
import random
import os
from typing import Callable
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
import numpy as np
from dataFormat import Image
def load_chart(name:str)->bytes:
"""Load a chart from a file."""
with open(f'charts/{name}.png', 'rb') as file:
data = file.read()
#This is cursed IDK if it wil deploy properly but prevents file buildup
os.remove(f'charts/{name}.png')
return data
def save_chart(func)->Callable[[],Image]:
"""A decorator to save the chart to a file."""
def wrapper(*args, **kwargs)-> Image:
"""Save the chart to a file with a hashed name."""
axes = func(*args, **kwargs)
name = abs(hash(axes))
plt.savefig(f'charts/{name}')
plt.close()
return Image(str(name))
return wrapper
@save_chart
def test_chart()->Axes:
"""Generate a test chart"""
x_list:list[float] =[random.random() for i in range(10)]
y_list:list[float] =[random.random() for i in range(10)]
_, axes = plt.subplots()
axes.scatter(x_list, y_list)
axes.set_title('Test Chart')
axes.set_xlabel('x')
axes.set_ylabel('y')
return axes
@save_chart
def graph_before_deadline(deadlines) -> Axes:
deadlines.sort(key=lambda x: x[4])
time_deltas = [(ded[3] - ded[4]).total_seconds()/86400 for ded in deadlines] # time_delta in days
_, axes = plt.subplots()
axes.scatter(range(len(time_deltas)), time_deltas)
axes.set_title('Time before deadline for each submission')
axes.set_xlabel('Submission number')
axes.set_ylabel('Time before deadline (days)')
axes.set_ylim(-5,14)
axes.plot(np.poly1d(time_deltas))
return axes
@save_chart
def submission_hour_histogram(deadlines) -> Axes:
bins = [i for i in range(24)]
hours = [i[4].hour for i in deadlines]
_ , axes = plt.subplots()
default_x_ticks = range(len(bins)+1)
x = ["Midnight","","","","","","6am","","","","","","12pm","","","","","","6pm","","","","","","Midnight"]
axes.hist(hours, bins)
axes.set_title('Your submission time')
axes.set_xlabel('Hour')
axes.set_ylabel('Number of submissions')
plt.xticks(default_x_ticks, x)
return axes
@save_chart
def module_grade_histogram(modules) -> Axes:
bins = [i*3 for i in range(34)]
marks = [i[4] for i in modules]
_, axes = plt.subplots()
axes.hist(marks, bins)
axes.set_title('Your module grades distribution')
axes.set_xlabel('Mark')
axes.set_ylabel('Number of modules')
sigma = np.std(marks)
mu = np.mean(marks)
y = 34 * ((1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
axes.plot(bins, y)
return axes