-
Notifications
You must be signed in to change notification settings - Fork 2
/
ml_section.py
129 lines (111 loc) · 4.44 KB
/
ml_section.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
import pickle
from ipywidgets import (Tab, SelectMultiple, Accordion, ToggleButton,
VBox, HBox, HTML, Image, Button, Text, Dropdown)
import plotly.graph_objs as go
import numpy as np
class MLSection:
def __init__(self):
self.data = pickle.load(open('./data/ml_data.pkl', 'rb'))
self.setup_widgets()
self.refresh_plot()
def setup_widgets(self):
amine_list = sorted(list(self.data.keys()))
self.select_amine = Dropdown(
options=amine_list,
description='Amine: ',
disabled=False,
)
self.select_metric = Dropdown(
options=['Accuracy', 'Precision', 'Recall', 'F1'],
description='Metric: ',
disabled=False,
)
self.figure = go.FigureWidget()
self.select_amine.observe(self.select_amine_callback, 'value')
self.select_metric.observe(self.select_amine_callback, 'value')
self.full_widget = VBox([self.figure,
HBox([self.select_amine, self.select_metric])])
def select_amine_callback(self, state):
self.refresh_plot()
def refresh_plot(self):
color_list = ['rgba(31, 118, 180, 1)', 'rgba(255, 127, 14, 1)',
'rgba(44, 160, 44, 1)', 'rgba(214, 39, 39, 1)',
'rgba(147, 103, 189, 1)', 'rgba(140, 86, 75, 1)',
'rgba(227, 119, 195, 1)', 'rgba(127, 127, 127, 1)',
'rgba(189, 189, 34, 1)', 'rgba(23, 189, 207, 1)']
amine = self.select_amine.value
metric = self.select_metric.value
x_values = self.data[amine]['learn_rate']
# y_values = self.data[amine]
layout = go.Layout(
hovermode='closest',
showlegend=True,
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text="Number of training experiments",
),
),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text=metric,
)
),
)
trace_list = []
for i, model in enumerate(sorted(self.data[amine]['model'].keys())):
y_mean = [np.mean(y_data.flatten()) for y_data in
self.data[amine]['model'][model][metric.lower()]]
x = x_values
trace = go.Scatter(
name=model,
x=x,
y=y_mean,
marker=dict(size=5, color=color_list[i % len(color_list)],),
opacity=1.0,
)
trace_list.append(trace)
y_std_dev = [np.std(y_data.flatten()) for y_data in
self.data[amine]['model'][model][metric.lower()]]
y_upper = np.array(y_mean) + np.array(y_std_dev)
y_lower = np.array(y_mean) - np.array(y_std_dev)
trace2 = go.Scatter(
x=x+x[::-1],
y=list(y_upper)+list(reversed(y_lower)),
fill='tozerox',
fillcolor=self.change_alpha(
color_list[i % len(color_list)], 0.3),
line=dict(color='rgba(255,255,255,0)'),
name=model,
showlegend=False,
)
trace_list.append(trace2)
"""
if std_dev[model]:
x_rev = x[::-1]
y_upper = np.array(data[model]) + np.array(std_dev[model])
y_lower = np.array(data[model]) - np.array(std_dev[model])
trace2 = go.Scatter(
x=x+x_rev,
y=list(y_upper)+list(y_lower)[::-1],
fill='tozerox',
fillcolor=self.change_alpha(
color_list[i % len(color_list)], 0.3),
line=dict(color='rgba(255,255,255,0)'),
name=model,
showlegend=False,
)
trace_list.append(trace2)
"""
# self.figure = go.FigureWidget(data=trace_list, layout=layout)
with self.figure.batch_update():
self.figure.data = []
# for trace in trace_list:
self.figure.add_traces(trace_list)
self.figure.layout = layout
def change_alpha(self, color, alpha):
color = color.split(',')
color[-1] = '{})'.format(alpha)
return ','.join(color)
@property
def plot(self):
return self.full_widget