-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_plot.py
191 lines (147 loc) · 5.17 KB
/
make_plot.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
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
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import column, row
from bokeh.models import ColumnDataSource, Slider, Button, Toggle, glyphs
from bokeh.plotting import figure
import utils
# Initializations
np.random.seed(40)
num_points = 100
m = 3.1415
x_line = np.arange(-1, 5.1, 0.1)
x = np.random.rand(num_points) * 5
y = m * x + np.random.randn(num_points) * 2
y_line = m * x_line
scatter_data = ColumnDataSource(data=dict(x=x, y=y))
line_data = ColumnDataSource(data=dict(x=x_line, y=y_line))
indexes = np.random.randint(num_points, size=(5,))
start_m = 0
end_m = 6
# Plot scatter plot
pair_plot = figure(
plot_height=400,
plot_width=400,
title="Linear Regression",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[-1, 5.5],
y_range=[-4, 20],
)
pair_plot.circle('x', 'y', source=scatter_data, color='blue')
pair_plot.line('x', 'y', source=line_data, color='red')
axis_source = ColumnDataSource(data=dict(
xs=[[-1., 6.], [0., 0.]],
ys=[[0., 0.], [-4., 20]],
))
axis_line = glyphs.MultiLine(
xs='xs', ys='ys', line_color='black',
)
pair_plot.add_glyph(axis_source, axis_line)
line_x = [[i, i] for i in scatter_data.data['x']]
line_y = [
[j, m * scatter_data.data['x'][i]]
for i, j in enumerate(scatter_data.data['y'])
]
error_points = ColumnDataSource(data=dict(x=line_x, y=line_y))
error_glyph = glyphs.MultiLine(
xs='x', ys='y', line_color='green', line_dash='dashed', line_alpha=0.0,
)
pair_plot.add_glyph(error_points, error_glyph)
# Plot error landscape
error_plot = figure(
plot_height=400,
plot_width=400,
title="Error Plots",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[start_m - 0.5, end_m + 0.5],
y_range=[0, 50],
)
error_land_data = ColumnDataSource(data=dict(
x=[m], y=[utils.compute_error(x, y, m)],
))
error_plot.line('x', 'y', source=error_land_data, color='red')
# Set up widgets
set_m = Slider(title='m', value=m, start=start_m, step=0.1, end=end_m)
animate_button = Button(label='► Play', button_type='primary')
cluttered_button = Toggle(
label='Filter points', button_type='primary', width=200
)
button_draw_error = Toggle(
label='Draw Errors', button_type='primary', width=200
)
reset_button = Button(label='Reset errors')
# Setup callbacks
def change_m(attr, old, new):
new_m = set_m.value
x_line = np.arange(-1, 5.1, 0.1)
y_line = new_m * x_line
line_data.data = dict(x=x_line, y=y_line)
if cluttered_button.active:
new_x = x[indexes]
new_y = y[indexes]
else:
new_x = x
new_y = y
new_y_line = set_m.value * new_x
new_line_x = [[i, i] for i in new_x]
new_line_y = [[j, new_y_line[i]] for i, j in enumerate(new_y)]
error_points.data = dict(x=new_line_x, y=new_line_y)
error_param = np.append(error_land_data.data['x'], new_m)
error_val = np.append(
error_land_data.data['y'], utils.compute_error(new_x, new_y, new_m),
)
sort_index = np.argsort(error_param)
error_param = error_param[sort_index]
error_val = error_val[sort_index]
error_land_data.data = dict(x=error_param, y=error_val)
set_m.on_change('value', change_m)
# Animate
def animate_update():
new_m = set_m.value + .1
if new_m > set_m.end:
new_m = 0
set_m.value = new_m
def animate_button_callback():
global callback_id
if animate_button.label == '► Play':
animate_button.label = '❚❚ Pause'
set_m.value = 0
error_land_data.data = {'x': [], 'y': []}
callback_id = curdoc().add_periodic_callback(animate_update, 50)
else:
animate_button.label = '► Play'
curdoc().remove_periodic_callback(callback_id)
animate_button.on_click(animate_button_callback)
# Clutter button
def clutter_button_callback(attr):
if cluttered_button.active:
cluttered_button.button_type = 'success'
scatter_data.data = {'x': x[indexes], 'y': y[indexes]}
new_x = x[indexes]
new_y = y[indexes]
new_y_line = set_m.value * new_x
new_line_x = [[i, i] for i in new_x]
new_line_y = [[j, new_y_line[i]] for i, j in enumerate(new_y)]
error_points.data = dict(x=new_line_x, y=new_line_y)
error_param = error_land_data.data['x']
error_val = [utils.compute_error(new_x, new_y, i) for i in error_param]
error_land_data.data = dict(x=error_param, y=error_val)
else:
cluttered_button.button_type = 'primary'
scatter_data.data = {'x': x, 'y': y}
error_param = error_land_data.data['x']
error_val = [utils.compute_error(x, y, i) for i in error_param]
error_land_data.data = dict(x=error_param, y=error_val)
cluttered_button.on_click(clutter_button_callback)
def button_draw_error_callback(attr):
if button_draw_error.active:
button_draw_error.button_type = 'success'
error_glyph.line_alpha = 1.0
else:
button_draw_error.button_type = 'primary'
error_glyph.line_alpha = 0.0
button_draw_error.on_click(button_draw_error_callback)
button_row = row(cluttered_button, button_draw_error)
plots = row(pair_plot, error_plot)
layout = column(animate_button, set_m, plots, button_row)
curdoc().add_root(layout)
curdoc().title = "LR"