-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt_one_addpt_onclick.py
186 lines (164 loc) · 7.84 KB
/
plt_one_addpt_onclick.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
import time
import copy
from ipywidgets import Output
from matplotlib.widgets import Button, CheckButtons
from matplotlib.patches import FancyArrowPatch
from lab_utils_common import np, plt, dlblue, dlorange, sigmoid, dldarkred, gradient_descent
# for debug
#output = Output() # sends hidden error messages to display when using widgets
#display(output)
class plt_one_addpt_onclick:
""" class to run one interactive plot """
def __init__(self, x, y, w, b, logistic=True):
self.logistic=logistic
pos = y == 1
neg = y == 0
fig,ax = plt.subplots(1,1,figsize=(8,4))
fig.canvas.toolbar_visible = False
fig.canvas.header_visible = False
fig.canvas.footer_visible = False
plt.subplots_adjust(bottom=0.25)
ax.scatter(x[pos], y[pos], marker='x', s=80, c = 'red', label="malignant")
ax.scatter(x[neg], y[neg], marker='o', s=100, label="benign", facecolors='none', edgecolors=dlblue,lw=3)
ax.set_ylim(-0.05,1.1)
xlim = ax.get_xlim()
ax.set_xlim(xlim[0],xlim[1]*2)
ax.set_ylabel('y')
ax.set_xlabel('Tumor Size')
self.alegend = ax.legend(loc='lower right')
if self.logistic:
ax.set_title("Example of Logistic Regression on Categorical Data")
else:
ax.set_title("Example of Linear Regression on Categorical Data")
ax.text(0.65,0.8,"[Click to add data points]", size=10, transform=ax.transAxes)
axcalc = plt.axes([0.1, 0.05, 0.38, 0.075]) #l,b,w,h
axthresh = plt.axes([0.5, 0.05, 0.38, 0.075]) #l,b,w,h
self.tlist = []
self.fig = fig
self.ax = [ax,axcalc,axthresh]
self.x = x
self.y = y
self.w = copy.deepcopy(w)
self.b = b
f_wb = np.matmul(self.x.reshape(-1,1), self.w) + self.b
if self.logistic:
self.aline = self.ax[0].plot(self.x, sigmoid(f_wb), color=dlblue)
self.bline = self.ax[0].plot(self.x, f_wb, color=dlorange,lw=1)
else:
self.aline = self.ax[0].plot(self.x, sigmoid(f_wb), color=dlblue)
self.cid = fig.canvas.mpl_connect('button_press_event', self.add_data)
if self.logistic:
self.bcalc = Button(axcalc, 'Run Logistic Regression (click)', color=dlblue)
self.bcalc.on_clicked(self.calc_logistic)
else:
self.bcalc = Button(axcalc, 'Run Linear Regression (click)', color=dlblue)
self.bcalc.on_clicked(self.calc_linear)
self.bthresh = CheckButtons(axthresh, ('Toggle 0.5 threshold (after regression)',))
self.bthresh.on_clicked(self.thresh)
self.resize_sq(self.bthresh)
# @output.capture() # debug
def add_data(self, event):
#self.ax[0].text(0.1,0.1, f"in onclick")
if event.inaxes == self.ax[0]:
x_coord = event.xdata
y_coord = event.ydata
if y_coord > 0.5:
self.ax[0].scatter(x_coord, 1, marker='x', s=80, c = 'red' )
self.y = np.append(self.y,1)
else:
self.ax[0].scatter(x_coord, 0, marker='o', s=100, facecolors='none', edgecolors=dlblue,lw=3)
self.y = np.append(self.y,0)
self.x = np.append(self.x,x_coord)
self.fig.canvas.draw()
# @output.capture() # debug
def calc_linear(self, event):
if self.bthresh.get_status()[0]:
self.remove_thresh()
for it in [1,1,1,1,1,2,4,8,16,32,64,128,256]:
self.w, self.b, _ = gradient_descent(self.x.reshape(-1,1), self.y.reshape(-1,1),
self.w.reshape(-1,1), self.b, 0.01, it,
logistic=False, lambda_=0, verbose=False)
self.aline[0].remove()
self.alegend.remove()
y_hat = np.matmul(self.x.reshape(-1,1), self.w) + self.b
self.aline = self.ax[0].plot(self.x, y_hat, color=dlblue,
label=f"y = {np.squeeze(self.w):0.2f}x+({self.b:0.2f})")
self.alegend = self.ax[0].legend(loc='lower right')
time.sleep(0.3)
self.fig.canvas.draw()
if self.bthresh.get_status()[0]:
self.draw_thresh()
self.fig.canvas.draw()
def calc_logistic(self, event):
if self.bthresh.get_status()[0]:
self.remove_thresh()
for it in [1, 8,16,32,64,128,256,512,1024,2048,4096]:
self.w, self.b, _ = gradient_descent(self.x.reshape(-1,1), self.y.reshape(-1,1),
self.w.reshape(-1,1), self.b, 0.1, it,
logistic=True, lambda_=0, verbose=False)
self.aline[0].remove()
self.bline[0].remove()
self.alegend.remove()
xlim = self.ax[0].get_xlim()
x_hat = np.linspace(*xlim, 30)
y_hat = sigmoid(np.matmul(x_hat.reshape(-1,1), self.w) + self.b)
self.aline = self.ax[0].plot(x_hat, y_hat, color=dlblue,
label="y = sigmoid(z)")
f_wb = np.matmul(x_hat.reshape(-1,1), self.w) + self.b
self.bline = self.ax[0].plot(x_hat, f_wb, color=dlorange, lw=1,
label=f"z = {np.squeeze(self.w):0.2f}x+({self.b:0.2f})")
self.alegend = self.ax[0].legend(loc='lower right')
time.sleep(0.3)
self.fig.canvas.draw()
if self.bthresh.get_status()[0]:
self.draw_thresh()
self.fig.canvas.draw()
def thresh(self, event):
if self.bthresh.get_status()[0]:
#plt.figtext(0,0, f"in thresh {self.bthresh.get_status()}")
self.draw_thresh()
else:
#plt.figtext(0,0.3, f"in thresh {self.bthresh.get_status()}")
self.remove_thresh()
def draw_thresh(self):
ws = np.squeeze(self.w)
xp5 = -self.b/ws if self.logistic else (0.5 - self.b) / ws
ylim = self.ax[0].get_ylim()
xlim = self.ax[0].get_xlim()
a = self.ax[0].fill_between([xlim[0], xp5], [ylim[1], ylim[1]], alpha=0.2, color=dlblue)
b = self.ax[0].fill_between([xp5, xlim[1]], [ylim[1], ylim[1]], alpha=0.2, color=dldarkred)
c = self.ax[0].annotate("Malignant", xy= [xp5,0.5], xycoords='data',
xytext=[30,5],textcoords='offset points')
d = FancyArrowPatch(
posA=(xp5, 0.5), posB=(xp5+1.5, 0.5), color=dldarkred,
arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0',
)
self.ax[0].add_artist(d)
e = self.ax[0].annotate("Benign", xy= [xp5,0.5], xycoords='data',
xytext=[-70,5],textcoords='offset points', ha='left')
f = FancyArrowPatch(
posA=(xp5, 0.5), posB=(xp5-1.5, 0.5), color=dlblue,
arrowstyle='simple, head_width=5, head_length=10, tail_width=0.0',
)
self.ax[0].add_artist(f)
self.tlist = [a,b,c,d,e,f]
self.fig.canvas.draw()
def remove_thresh(self):
#plt.figtext(0.5,0.0, f"rem thresh {self.bthresh.get_status()}")
for artist in self.tlist:
artist.remove()
self.fig.canvas.draw()
def resize_sq(self, bcid):
""" resizes the check box """
#future reference
#print(f"width : {bcid.rectangles[0].get_width()}")
#print(f"height : {bcid.rectangles[0].get_height()}")
#print(f"xy : {bcid.rectangles[0].get_xy()}")
#print(f"bb : {bcid.rectangles[0].get_bbox()}")
#print(f"points : {bcid.rectangles[0].get_bbox().get_points()}") #[[xmin,ymin],[xmax,ymax]]
h = bcid.rectangles[0].get_height()
bcid.rectangles[0].set_height(3*h)
ymax = bcid.rectangles[0].get_bbox().y1
ymin = bcid.rectangles[0].get_bbox().y0
bcid.lines[0][0].set_ydata([ymax,ymin])
bcid.lines[0][1].set_ydata([ymin,ymax])