-
Notifications
You must be signed in to change notification settings - Fork 0
/
SW_Cursor.py
executable file
·391 lines (301 loc) · 13.8 KB
/
SW_Cursor.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import math
import matplotlib.pyplot as plt
import numpy as np
class Cursor(object):
#def __init__(self, ax1, ax2, ax3, ax4):
def __init__(self, ax1, ax2, ax4):
self.clicked=False
self.second_click = False
self.ax1 = ax1
self.ax2 = ax2
#self.ax3 = ax3
self.ax4 = ax4
self.movie_mode = False
self.bins = []
self.change_bins = False
self.movie_bin = 0
self.DONE = False
self.STATE = []
# Replot flags
self.replot = False
self.replotx = 0
self.horizontal_line = ax2.axhline(color='k', lw=0.8, ls='--')
self.vertical_line = ax2.axvline(color='k', lw=0.8, ls='--')
self.text = ax2.text(0.72, 0.9, '', transform=ax2.transAxes)
# initializing the lines
self.ylims_ax1 = ax1.get_ylim()
self.ylims_ax2 = ax2.get_ylim()
#self.ylims_ax3 = ax3.get_ylim()
line1 = ax1.plot([0,0], [self.ylims_ax1[0], self.ylims_ax1[1]], linewidth = 0.5, color = 'k')
ml1 = line1.pop(0)
line2 = ax2.plot([0,0], [self.ylims_ax2[0], self.ylims_ax2[1]], linewidth = 0.5, color = 'k')
ml2 = line2.pop(0)
#line3 = ax3.plot([0,0], [self.ylims_ax3[0], self.ylims_ax3[1]], linewidth = 0.5, color = 'k')
#ml3 = line3.pop(0)
self.movement_x_axis = np.linspace(0,60,900)
self.spect_x_axis = np.linspace(199,1442, 900)
#self.lines = [ml1, ml2, ml3]
self.toggle_line = False
print('making a cursor')
def on_move(self, event):
print('on move')
def on_press(self, event):
if event.key == 'd':
print('DONE SCORING')
self.DONE = True
elif event.key in [1,2,3,4]:
self.STATE.append(event.key)
elif event.key == 'l':
print(f'toggling line!! xdata: {event.xdata} ydata: {event.ydata}')
for line in self.lines:
line.remove()
line1 = self.ax1.plot([self.spect_x_axis[int(event.xdata)],self.spect_x_axis[int(event.xdata)]], [self.ylims_ax1[0], self.ylims_ax1[1]], linewidth = 0.5, color = 'k')
line2 = self.ax2.plot([int(event.xdata), int(event.xdata)], [self.ylims_ax2[0], self.ylims_ax2[1]], linewidth = 0.5, color = 'k')
#line3 = self.ax3.plot([self.movement_x_axis[int(event.xdata)],self.movement_x_axis[int(event.xdata)]], [self.ylims_ax3[0], self.ylims_ax3[1]], linewidth = 0.5, color = 'k')
self.lines[0] = line1.pop(0)
self.lines[1] = line2.pop(0)
#self.lines[2] = line3.pop(0)
# This works, but doesn't refresh fast enough. I think this is a limit of matplotlib however and out of my control
def set_cross_hair_visible(self, visible):
need_redraw = self.horizontal_line.get_visible() != visible
self.horizontal_line.set_visible(visible)
self.vertical_line.set_visible(visible)
self.text.set_visible(visible)
return need_redraw
def on_mouse_move(self, event):
if not event.inaxes:
need_redraw = self.set_cross_hair_visible(False)
if need_redraw:
self.ax2.figure.canvas.draw()
else:
self.set_cross_hair_visible(True)
x, y = event.xdata, event.ydata
# update the line positions
self.horizontal_line.set_ydata(y)
self.vertical_line.set_xdata(x)
self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
self.ax2.figure.canvas.draw()
def in_axes(self, event):
# Add the crosshair here? TODO: put in priint statements to see when this triggers
print('scanning axes')
#Stashing cursor thread here: https://stackoverflow.com/questions/63195460/how-to-have-a-fast-crosshair-mouse-cursor-for-subplots-in-matplotlib
#this would be so much chooler if I could use a switch statement but fuck it
if event.inaxes == self.ax2:
print('Second bins')
if event.inaxes == self.ax4:
print('EMG bin!!')
#Should we call a graph refresh here?
# x, y2 = sel.target
# y1 = np.interp( sel.target[0], plot1.get_xdata(), plot1.get_ydata() )
# sel.annotation.set_text(f'x: {x:.2f}\ny1: {y1:.2f}\ny2: {y2:.2f}')
# # sel.annotation.set_visible(False)
# hline1 = ax1.axhline(y1, color='k', ls=':')
# vline1 = ax1.axvline(x, color='k', ls=':')
# vline2 = ax2.axvline(x, color='k', ls=':')
# hline2 = ax2.axhline(y2, color='k', ls=':')
# sel.extras.append(hline1)
# sel.extras.append(vline1)
# sel.extras.append(hline2)
# sel.extras.append(vline2)
#
# fig = plt.figure(figsize=(15, 10))
# ax1 = plt.subplot(2, 1, 1)
# ax2 = plt.subplot(2, 1, 2, sharex=ax1)
#
# plot1, = ax1.plot(np.array(np.random.uniform(-1, 1, 100).cumsum()))
# plot2, = ax2.plot(np.array(np.random.uniform(-1, 1, 100).cumsum()))
#
# cursor = mplcursors.cursor(plot2, hover=True)
# cursor.connect('add', crosshair)
# Movie mode triggers when you hover over the bottom axis. Duh ax3 I guess
# if event.inaxes == self.ax3:
# self.movie_mode = True
# print('MOVIE MODE!')
# else:
# self.movie_mode = False
def pull_up_movie(self, event):
# I don't think we call movies there TODO: See if this was a stub for something else?
print('gon pull up some movies')
##
def crosshair():
plt.show()
##
def on_click(self, event):
print("self.clicked = " + str(self.clicked))
if self.movie_mode:
self.movie_bin = event.xdata
print(f'video bin (xdata): {event.xdata}')
print(f'x: {event.x}')
elif self.clicked:
print('click registered')
if event.inaxes == self.ax2:
if self.clicked == True:
print(F'SECOND CLICK ---- xdata:{event.xdata} x:{event.x} axes: {event.inaxes}')
self.bins.append(math.floor(event.xdata))
self.clicked = False
self.change_bins = True
else:
if event.inaxes == self.ax1:
print('Inside Spectrogram')
#Set this bool to true, and then have it get flipped back to false in New_SWS
self.replot = True
print(f'event.x: {event.x}')
print(f'event.xdata {event.xdata}')
#Set a replot start
self.replotx = event.xdata
#Log and store the xpos
# Replot the graph here
else:
print('Clicked outside of any bins')
if event.inaxes != self.ax2:
print('please click in the second figure to select bins')
else:
self.bins.append(math.floor(event.xdata))
print(f'FIRST CLICK ----- xdata:{event.xdata} x:{event.x} axes: {event.inaxes}')
self.clicked = True
class ScoringCursor(object):
#def __init__(self, ax1, ax2, ax3, ax4):
def __init__(self, ax1, ax2, ax4):
self.clicked=False
self.second_click = False
self.ax1 = ax1
self.ax2 = ax2
#self.ax3 = ax3
self.ax4 = ax4
self.movie_mode = False
self.bins = []
self.change_bins = False
self.movie_bin = 0
self.DONE = False
self.STATE = []
# Replot flags
self.replot = False
self.replotx = 0
self.horizontal_line = ax2.axhline(color='k', lw=0.8, ls='--')
self.vertical_line = ax2.axvline(color='k', lw=0.8, ls='--')
self.text = ax2.text(0.72, 0.9, '', transform=ax2.transAxes)
# initializing the lines
self.ylims_ax1 = ax1.get_ylim()
self.ylims_ax2 = ax2.get_ylim()
#self.ylims_ax3 = ax3.get_ylim()
line1 = ax1.plot([0,0], [self.ylims_ax1[0], self.ylims_ax1[1]], linewidth = 0.5, color = 'k')
ml1 = line1.pop(0)
line2 = ax2.plot([0,0], [self.ylims_ax2[0], self.ylims_ax2[1]], linewidth = 0.5, color = 'k')
ml2 = line2.pop(0)
# line3 = ax3.plot([0,0], [self.ylims_ax3[0], self.ylims_ax3[1]], linewidth = 0.5, color = 'k')
# ml3 = line3.pop(0)
self.movement_x_axis = np.linspace(0,60,900)
self.spect_x_axis = np.linspace(199,1442, 900)
#self.lines = [ml1, ml2, ml3]
self.lines = [ml1, ml2]
self.toggle_line = False
print('making a cursor')
def on_move(self, event):
print('on move')
# def on_press(self, event):
# if event.key == 'd':
# print('DONE SCORING')
# self.DONE = True
# elif event.key in [1,2,3,4]:
# self.STATE.append(event.key)
# elif event.key == 'l':
# print(f'toggling line!! xdata: {event.xdata} ydata: {event.ydata}')
# for line in self.lines:
# line.remove()
# line1 = self.ax1.plot([self.spect_x_axis[int(event.xdata)],self.spect_x_axis[int(event.xdata)]], [self.ylims_ax1[0], self.ylims_ax1[1]], linewidth = 0.5, color = 'k')
# line2 = self.ax2.plot([int(event.xdata), int(event.xdata)], [self.ylims_ax2[0], self.ylims_ax2[1]], linewidth = 0.5, color = 'k')
# line3 = self.ax3.plot([self.movement_x_axis[int(event.xdata)],self.movement_x_axis[int(event.xdata)]], [self.ylims_ax3[0], self.ylims_ax3[1]], linewidth = 0.5, color = 'k')
# self.lines[0] = line1.pop(0)
# self.lines[1] = line2.pop(0)
# self.lines[2] = line3.pop(0)
# def on_mouse_move(self, event):
# if not event.inaxes:
# need_redraw = self.set_cross_hair_visible(False)
# if need_redraw:
# self.ax2.figure.canvas.draw()
# else:
# self.set_cross_hair_visible(True)
# x, y = event.xdata, event.ydata
# # update the line positions
# self.horizontal_line.set_ydata(y)
# self.vertical_line.set_xdata(x)
# self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
# self.ax2.figure.canvas.draw()
def in_axes(self, event):
# Add the crosshair here? TODO: put in priint statements to see when this triggers
print('scanning axes')
#Stashing cursor thread here: https://stackoverflow.com/questions/63195460/how-to-have-a-fast-crosshair-mouse-cursor-for-subplots-in-matplotlib
# #this would be so much chooler if I could use a switch statement but fuck it
# if event.inaxes == self.ax2:
# print('Second bins')
# if event.inaxes == self.ax4:
# print('EMG bin!!')
#Should we call a graph refresh here?
# x, y2 = sel.target
# y1 = np.interp( sel.target[0], plot1.get_xdata(), plot1.get_ydata() )
# sel.annotation.set_text(f'x: {x:.2f}\ny1: {y1:.2f}\ny2: {y2:.2f}')
# # sel.annotation.set_visible(False)
# hline1 = ax1.axhline(y1, color='k', ls=':')
# vline1 = ax1.axvline(x, color='k', ls=':')
# vline2 = ax2.axvline(x, color='k', ls=':')
# hline2 = ax2.axhline(y2, color='k', ls=':')
# sel.extras.append(hline1)
# sel.extras.append(vline1)
# sel.extras.append(hline2)
# sel.extras.append(vline2)
#
# fig = plt.figure(figsize=(15, 10))
# ax1 = plt.subplot(2, 1, 1)
# ax2 = plt.subplot(2, 1, 2, sharex=ax1)
#
# plot1, = ax1.plot(np.array(np.random.uniform(-1, 1, 100).cumsum()))
# plot2, = ax2.plot(np.array(np.random.uniform(-1, 1, 100).cumsum()))
#
# cursor = mplcursors.cursor(plot2, hover=True)
# cursor.connect('add', crosshair)
# Movie mode triggers when you hover over the bottom axis. Duh ax3 I guess
# if event.inaxes == self.ax3:
# self.movie_mode = True
# print('MOVIE MODE!')
# else:
# self.movie_mode = False
# def pull_up_movie(self, event):
# # I don't think we call movies there TODO: See if this was a stub for something else?
# print('gon pull up some movies')
# ##
# def crosshair():
# plt.show()
# ##
def on_click(self, event):
print("self.clicked = " + str(self.clicked))
# if self.movie_mode:
# self.movie_bin = event.xdata
# print(f'video bin (xdata): {event.xdata}')
# print(f'x: {event.x}')
# elif self.clicked:
# print('click registered')
# if event.inaxes == self.ax2:
# if self.clicked == True:
# print(F'SECOND CLICK ---- xdata:{event.xdata} x:{event.x} axes: {event.inaxes}')
# self.bins.append(math.floor(event.xdata))
# self.clicked = False
# self.change_bins = True
# else:
if event.inaxes == self.ax1:
print('Inside Spectrogram')
#Set this bool to true, and then have it get flipped back to false in New_SWS
self.replot = True
print(f'event.x: {event.x}')
print(f'event.xdata {event.xdata}')
#Set a replot start
self.replotx = event.xdata
#Log and sto re the xpos
# Replot the graph here
# else:
# print('Clicked outside of any bins')
# if event.inaxes != self.ax2:
# print('please click in the second figure to select bins')
# else:
# self.bins.append(math.floor(event.xdata))
# print(f'FIRST CLICK ----- xdata:{event.xdata} x:{event.x} axes: {event.inaxes}')
# self.clicked = True