Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def emit_update_events(self):
"""Emit the pan and zoom events to update views after a pan zoom manual update."""
emit('pan', self, self.pan)
emit('zoom', self, self.zoom)
def zoom(self, value):
"""Zoom level."""
if isinstance(value, (int, float)):
value = (value, value)
assert len(value) == 2
old = tuple(self.zoom)
self._zoom = np.clip(value, self._zmin, self._zmax)
# Constrain bounding box.
self._constrain_pan()
self._constrain_zoom()
new = tuple(self.zoom)
if new != old:
emit('zoom', self, new)
self.update()
def on_mouse_click(self, e):
"""Select a cluster by clicking in the raster plot."""
b = e.button
if 'Control' in e.modifiers or 'Shift' in e.modifiers:
# Get mouse position in NDC.
cluster_idx, _ = self.canvas.stacked.box_map(e.pos)
cluster_id = self.all_cluster_ids[cluster_idx]
logger.debug("Click on cluster %d with button %s.", cluster_id, b)
if 'Shift' in e.modifiers:
emit('select_more', self, [cluster_id])
else:
emit('request_select', self, [cluster_id])
gui, name=self.name, menu='&View', submenu=self.name,
default_shortcuts=shortcuts, default_snippets=self.default_snippets)
# Freeze and unfreeze the view when selecting clusters.
self.actions.add(
self.toggle_auto_update, checkable=True, checked=self.auto_update, show_shortcut=False)
self.actions.add(self.screenshot, show_shortcut=False)
self.actions.add(self.close, show_shortcut=False)
self.actions.separator()
# Color scheme actions.
self.actions.add(self.next_color_scheme)
self.actions.add(self.previous_color_scheme)
self.actions.separator()
emit('view_actions_created', self)
on_select = partial(self.on_select_threaded, gui=gui)
connect(on_select, event='select')
# Save the view state in the GUI state.
@connect(sender=gui)
def on_close_view(sender, view):
if view != self:
return
logger.debug("Close view %s.", self.name)
self._closed = True
gui.remove_menu(self.name)
unconnect(on_select)
gui.state.update_view_state(self, self.state)
self.canvas.close()
gc.collect(0)
def set_interval(self, interval=None):
"""Display the traces and spikes in a given interval."""
if interval is None:
interval = self._interval
interval = self._restrict_interval(interval)
if interval != self._interval:
logger.debug("Redraw the entire trace view.")
self._interval = interval
emit('is_busy', self, True)
self.plot(update_traces=True, update_waveforms=True)
emit('is_busy', self, False)
emit('time_range_selected', self, interval)
self.update_status()
else:
self.plot(update_traces=False, update_waveforms=True)
def on_close_dock_widget(sender):
self._views.remove(view)
emit('close_view', self, view)
self.build(lambda html: emit('ready', self))
# Add the field if it doesn't exist.
if field not in self._fields:
self.add_field(field)
assert field in self._fields
clusters = _as_list(clusters)
for cluster in clusters:
if cluster not in self._data:
self._data[cluster] = {}
self._data[cluster][field] = value
up = UpdateInfo(description='metadata_' + field,
metadata_changed=clusters,
metadata_value=value,
)
undo_state = emit('request_undo_state', self, up)
if add_to_stack:
self._undo_stack.add((clusters, field, value, up, undo_state))
emit('cluster', self, up)
return up
# Find the spike and cluster closest to the mouse.
db = self.data_bounds
# Get the information about the displayed spikes.
wt = [(t, s, c, ch) for t, s, c, ch in self._waveform_times if channel_id in ch]
if not wt:
return
# Get the time coordinate of the mouse position.
mouse_pos = self.canvas.panzoom.window_to_ndc(e.pos)
mouse_time = Range(NDC, db).apply(mouse_pos)[0][0]
# Get the closest spike id.
times, spike_ids, spike_clusters, channel_ids = zip(*wt)
i = np.argmin(np.abs(np.array(times) - mouse_time))
# Raise the select_spike event.
spike_id = spike_ids[i]
cluster_id = spike_clusters[i]
emit('select_spike', self, channel_id=channel_id,
spike_id=spike_id, cluster_id=cluster_id)
if 'Shift' in e.modifiers:
# Get mouse position in NDC.
box_id, _ = self.canvas.stacked.box_map(e.pos)
channel_id = int(np.nonzero(self.channel_y_ranks == box_id)[0][0])
emit('select_channel', self, channel_id=channel_id, button=e.button)