-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagrams.py
131 lines (95 loc) · 3.78 KB
/
diagrams.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
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
# modules to create diagrames
import flask
import io
import json
from flask import send_file, render_template
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import bokeh
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
from bokeh.embed import json_item
from bokeh.palettes import Spectral6
from bokeh.transform import factor_cmap
from bokeh.sampledata.iris import flowers
import pandas as pd
from util import df_from_sql
uri_prefix = '/diagrams'
diagrams = flask.Blueprint('diagrams', __name__)
colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]
def make_plot(x, y):
p = figure(title = "Iris Morphology", sizing_mode="fixed", plot_width=800, plot_height=400)
p.xaxis.axis_label = x
p.yaxis.axis_label = y
p.circle(flowers[x], flowers[y], color=colors, fill_alpha=0.2, size=10)
return p
@diagrams.route(f'{uri_prefix}/')
def root():
print (render_template('diagrams/root.html',resources=CDN.render(), prefix=uri_prefix))
return render_template('diagrams/root.html',resources=CDN.render(), prefix=uri_prefix)
@diagrams.route(f'{uri_prefix}/plot')
def plot():
sql= """SELECT * FROM items
LEFT JOIN
(SELECT item_id, college_id, AVG(mlss.age) as avg_age, AVG(mlss.serieux) as avg_serieux, COUNT(id) as nb_item FROM
(SELECT id, item_id, college_id, serieux, DATE_DIFF(CURRENT_DATE(), `date`, DAY) as age from lss) as mlss
GROUP BY item_id, college_id) AS item_synt
ON items.item_id = item_synt.item_id
LEFT JOIN colleges
ON item_synt.college_id = colleges.college_id ;
"""
df = df_from_sql(sql)
df = df.loc[:,~df.columns.duplicated()]
df['size']=df['nb_item']*12
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
TOOLTIPS = [
("item", "@item_name"),
("College","@college_name"),
("Nb essions", "@nb_item"),
]
p = figure(tools=TOOLS, title = "Items", sizing_mode="scale_both", plot_width=100, plot_height=50, tooltips=TOOLTIPS)
p.xaxis.axis_label = 'Serieux'
p.yaxis.axis_label = 'Age'
p.y_range.flipped = True
mapper = factor_cmap(field_name='college_id', factors=df['college_id'].dropna().values, palette=Spectral6)
print(df['college_id'].dropna().unique())
p.circle('avg_serieux', 'avg_age',source=df, color=mapper, size="size", fill_alpha=0.4, legend_field="college_name")
p.legend.location = "top_left"
p.legend.click_policy="hide"
return json.dumps(json_item(p, "myplot"))
@diagrams.route(f'{uri_prefix}/basic.png')
def basic():
plot = figure()
plot.circle([1,2], [3,4])
return file_html(plot, CDN, "my plot")
@diagrams.route(f'{uri_prefix}/plot.png')
def plot_png():
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
# Fixing random state for reproducibility
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.xlim(40, 160)
plt.ylim(0, 0.03)
plt.grid(True)
fig = plt.figure(1)
print(fig)
# draw(ax)
return fig_response(fig)
def fig_response(fig):
"""Turn a matplotlib Figure into Flask response"""
img_bytes = io.BytesIO()
fig.savefig(img_bytes)
img_bytes.seek(0)
return send_file(img_bytes, mimetype='image/png')