def attach_yaxis(self, axis_color=(0, 1, 1, 0.8)): ''' Provide y axis for the population rate ''' fg = axis_color # text show amplitude self.amp_text = scene.Text("", pos=(0, 0), italic=False, bold=False, anchor_x='left', anchor_y='center', color=axis_color, font_size=10, parent=self._view2) self.amp_text.pos = (0, 20) # x axis shows time and can be moved horizontally for clipping self._yaxis = scene.AxisWidget(orientation='left', text_color=fg, axis_color=fg, tick_color=fg) self._yaxis.stretch = (0, 1) self._grid.add_widget(self._yaxis, row=10, col=0, row_span=3) self._yaxis.link_view(self._view2)
def attach_xaxis(self, axis_color=(0, 1, 1, 0.8)): ''' Provide x axis. This method is optional for child class. ''' fg = axis_color # text show amplitude self.amp_text = scene.Text("", pos=(0, 0), italic=False, bold=False, anchor_x='left', anchor_y='center', color=axis_color, font_size=15, parent=self._view) self.amp_text.pos = (0, 12) # x axis shows time and can be moved horizontally for clipping self._xaxis = scene.AxisWidget(orientation='bottom', text_color=fg, axis_color=fg, tick_color=fg) self._xaxis.stretch = (0.9, 0.1) self._grid.add_widget(self._xaxis, row=7, col=0, row_span=2) self._xaxis.link_view(self._view) self.x_axis_lock = True
def _add_label(self, curve_index, new_color): new_label = scene.Text(f"{self.labels[curve_index]}", color=new_color, anchor_x='left', parent=self.canvas.scene) new_label.pos = self.LBL_POS_DEFAULTX, self.LBL_POS_DEFAULTY + self.LBL_SPACING * len( self.hl_labels) self.hl_labels.append((curve_index, new_label))
N = 200 pos = np.zeros((N, 2), dtype=np.float32) pos[:, 0] = np.linspace(10, 390, N) pos[:, 1] = np.random.normal(size=N, scale=20, loc=0) canvas = scene.SceneCanvas(keys='interactive', size=(400, 200), show=True) # Create a visual that updates the line with different colormaps color = next(colormaps) line = scene.Line(pos=pos, color=color, method='gl') line.transform = STTransform(translate=[0, 140]) line.parent = canvas.central_widget text = scene.Text(color, bold=True, font_size=24, color='w', pos=(200, 40), parent=canvas.central_widget) def on_timer(event): global colormaps, line, text, pos color = next(colormaps) line.set_data(pos=pos, color=color) text.text = color timer = app.Timer(.5, connect=on_timer, start=True) if __name__ == '__main__': canvas.app.run()
def __init__(self, data=None, fs=25e3, spks=None, chs=None, color=None, pagesize=20000, ncols=1, gap_value=0.8 * 0.95, ls='-', time_slice=0): scene.SceneCanvas.__init__(self, keys=None) self.unfreeze() assert data is not None self.grid1 = self.central_widget.add_grid(spacing=0, bgcolor='gray', border_color='k') self.view2 = self.grid1.add_view(row=0, col=1, col_span=36, margin=10, bgcolor=(0, 0, 0, 1), border_color=(0, 1, 0)) self.view2.camera = scene.cameras.PanZoomCamera() self.view2.camera.set_range() self.cursor_color = '#0FB6B6' self.cursor_text = scene.Text("", pos=(0, 0), italic=False, bold=True, anchor_x='left', anchor_y='center', color=self.cursor_color, font_size=14, parent=self.view2.scene) self.cursor_text_ref = scene.Text("", pos=(0, 0), italic=True, bold=False, anchor_x='left', anchor_y='center', color=(0, 1, 1, 1), font_size=14, parent=self.view2.scene) self.palette = palette self._gap_value = gap_value self.fs = fs wav_visual = scene.visuals.create_visual_node(MyWaveVisual) self.waves1 = wav_visual(ls=ls, parent=self.view2.scene, color=color, gap=self._gap_value) self.grid2 = self.view2.add_grid(spacing=0, bgcolor=(0, 0, 0, 0), border_color='k') self.cross = Cross(cursor_color=self.cursor_color) self.timer_cursor = app.Timer(connect=self.update_cursor, interval=0.01, start=False) self.view1 = self.grid1.add_view(row=0, col=0, col_span=1, margin=10, bgcolor=(0, 0, 0, 1), border_color=(1, 0, 0)) self.view1.camera = scene.cameras.PanZoomCamera() self.view1.camera.set_range() self._start_index = 0 self.flag = 0 self._pagesize = pagesize self.location = '' y_sync_cam = YSyncCamera() self.view1.camera.link(y_sync_cam) self.view2.camera.link(y_sync_cam) self.ch_no_text = scene.Text('', pos=(0, 0), italic=False, bold=True, color=self.cursor_color, font_size=12, parent=self.view1.scene) self.data = data if chs is not None: chs = np.array(chs) if chs.ndim != 1: chs = chs.ravel() self.data = self.data[:, chs] chs_labels = chs elif self.data.shape[1] <= 32: chs_labels = np.arange(self.data.shape[1]) else: chs_labels = np.arange(32) self._chs = list(zip(chs_labels, np.arange(len(chs_labels)))) self._chs_idx = sorted([j for _, j in self._chs], reverse=True) self.spikes = self._spkarray2dist(spks) self._render(self.data[0:self.pagesize, self._chs_idx]) self.attach_texts() self.highlight_ch() self.set_range() self.cross.attach(self.grid2) self.cross.link_view(self.view2) self._current_time = 0.0 self._sliding_time = 1.0 # for picker self.key_option = 0 self._picker = Picker(self.scene, self.view2.camera.transform) self.picked_data = []
def draw_axes(self): #First, calculate positions self.position_axes() #Draw x axis self._xax=scene.Axis( pos=[[self._xlim[0], self._ylim[self._axpos[0,1]], self._zlim[self._axpos[0,2]]], [self._xlim[1], self._ylim[self._axpos[0,1]], self._zlim[self._axpos[0,2]]]], domain=self._xlim, tick_direction=(0,-1,0), major_tick_length=10*self._cam_dist, font_size=10*self._cam_dist, font_face='FreeSerif', axis_color='k', tick_color='k', text_color='k', parent=self._view.scene) self._xlabel = scene.Text("x", pos=[(self._xlim[0]+self._xlim[1])/2, self._ylim[self._axpos[0,1]]-self._label_dist, self._zlim[self._axpos[0,2]]], font_size=10*self._cam_dist, face='FreeSerif', color='k', parent=self._view.scene) #Draw y axis self._yax=scene.Axis( pos=[[self._xlim[self._axpos[1,0]], self._ylim[0], self._zlim[self._axpos[1,2]]], [self._xlim[self._axpos[1,0]], self._ylim[1], self._zlim[self._axpos[1,2]]]], domain=self._ylim, tick_direction=(1,0,0), major_tick_length=10*self._cam_dist, font_size=10*self._cam_dist, font_face='FreeSerif', axis_color='k', tick_color='k', text_color='k', parent=self._view.scene) self._ylabel = scene.Text("y", pos=[self._xlim[self._axpos[1,0]]-self._label_dist, (self._ylim[0]+self._ylim[1])/2, self._zlim[self._axpos[1,2]]], font_size=10*self._cam_dist, face='FreeSerif', color='k', parent=self._view.scene) #Draw z axis self._zax=scene.Axis( pos=[[self._xlim[self._axpos[2,0]], self._ylim[self._axpos[2,1]], self._zlim[0]], [self._xlim[self._axpos[2,0]], self._ylim[self._axpos[2,1]], self._zlim[1]]], domain=self._zlim, tick_direction=(0,-1,0), major_tick_length=10*self._cam_dist, font_face='FreeSerif', font_size=10*self._cam_dist, axis_color='k', tick_color='k', text_color='k', parent=self._view.scene) self._zlabel = scene.Text("z", pos=[self._xlim[self._axpos[2,0]], self._ylim[self._axpos[2,1]]-self._label_dist, (self._zlim[0]+self._zlim[1])/2], font_size=10*self._cam_dist, face='FreeSerif', color='k', parent=self._view.scene) self._xax._update_subvisuals() self._yax._update_subvisuals() self._zax._update_subvisuals() #Add plot to scene self._view.add(self._volume)
def __init__(self, data, color=None, fs=25e3, spklen=19, ncols=1, gap_value=0.8 * 0.95, ls='-', time_slice=0): scene.SceneCanvas.__init__(self, keys=None) self.unfreeze() self.data = data self.fs = fs self.spklen = spklen self.grid1 = self.central_widget.add_grid(spacing=0, bgcolor='gray', border_color='k') self.view2 = self.grid1.add_view(row=0, col=0, col_span=36, margin=10, bgcolor=(0, 0, 0, 1), border_color=(0, 1, 0)) self.view2.camera = scene.cameras.PanZoomCamera() self.view2.camera.set_range() self.cursor_color = '#0FB6B6' self.cursor_text = scene.Text("", pos=(0, 0), italic=False, bold=True, anchor_x='left', anchor_y='center', color=self.cursor_color, font_size=24, parent=self.view2.scene) self.cursor_text_ref = scene.Text("", pos=(0, 0), italic=True, bold=False, anchor_x='left', anchor_y='center', color=(0, 1, 1, 1), font_size=24, parent=self.view2.scene) self.cursor_rect = scene.Rectangle(center=(0, 0, 0), height=1., width=1., radius=[0., 0., 0., 0.], color=(0.1, 0.3, 0.3, 0.5), border_width=0, border_color=(0, 0, 0, 0), parent=self.view2.scene) self.cursor_rect.visible = False self.palette = palette self._gap_value = gap_value self._locate_buffer = 200 self._picker = Picker(self.scene, self.view2.camera.transform) wav_visual = scene.visuals.create_visual_node(MyWaveVisual) self.waves1 = wav_visual(ls=ls, parent=self.view2.scene, color=color, gap=self._gap_value) self.grid2 = self.view2.add_grid(spacing=0, bgcolor=(0, 0, 0, 0), border_color='k') self.cross = Cross(cursor_color=self.cursor_color) self.timer_cursor = app.Timer(connect=self.update_cursor, interval=0.01, start=False) self.event = EventEmitter()
def plot_elements(elements, overlay_deformed=False, sel_nodes=None, sel_elements=None, canvas={}, hold_on=False, view=None, cam={}, tmat_scaling=1, plot_tmat_ax=None, plot_nodes=False, node_labels=False, element_labels=False, element_label_settings={}, node_label_settings={}, element_settings={}, node_settings={}, sel_node_settings={}, sel_element_settings={}, sel_node_label_settings={}, sel_element_label_settings={}, tmat_settings={}, deformed_element_settings={}, title='BEEF Element plot'): el_settings = dict(color='#008800') el_settings.update(**element_settings) def_el_settings = dict(color='#ff2288') def_el_settings.update(**deformed_element_settings) elsel_settings = dict(color='#ff0055', width=3) elsel_settings.update(**sel_element_settings) elsellab_settings = dict(color='#ff0055', bold=True, italic=False, face='OpenSans', font_size=10, anchor_x='left') elsellab_settings.update(**sel_element_label_settings) ellab_settings = dict(color='#008800', bold=False, italic=False, face='OpenSans', font_size=10, anchor_x='left') ellab_settings.update(**element_label_settings) n_settings = dict(face_color='#0000ff', edge_color=None, size=4) n_settings.update(**node_settings) nlab_settings = dict(color='#0000ff', bold=False, italic=False, face='OpenSans', font_size=10, anchor_x='left') nlab_settings.update(**node_label_settings) nsel_settings = dict(face_color='#ff0000', edge_color='#ff0000', size=8) nsel_settings.update(**sel_node_settings) nsellab_settings = dict(color='#ff0000', bold=True, italic=False, face='OpenSans', font_size=10, anchor_x='left') nsellab_settings.update(**sel_node_label_settings) tmat_colors = ['#0000ff', '#00ff00', '#ff0000'] tmatax_settings = dict(arrow_size=1) tmatax_settings.update(**tmat_settings) # Node coordinates nodes = list(set([a for b in [el.nodes for el in elements] for a in b])) #flat list of unique nodes node_pos = np.vstack([node.coordinates for node in nodes]) # Selected elements if sel_elements is None: sel_elements = [] unsel_elements = [el for el in elements if el.label not in sel_elements] sel_elements = [el for el in elements if el.label in sel_elements] if view is None: view, canvas, cam = initialize_plot(canvas=canvas, cam=cam, elements=elements, title=title) else: canvas = view.canvas cam = view.camera if not hold_on: rm_visuals(view) # Establish element lines if len(unsel_elements) > 0: element_lines = [None] * len(unsel_elements) for ix, el in enumerate(unsel_elements): element_lines[ix] = np.vstack( [node.coordinates for node in el.nodes]) element_visual = scene.Line(pos=np.vstack(element_lines), connect='segments', **el_settings) view.add(element_visual) # Establish selected element lines if len(sel_elements) > 0: element_lines = [None] * len(sel_elements) for ix, el in enumerate(sel_elements): element_lines[ix] = np.vstack( [node.coordinates for node in el.nodes]) element_visual = scene.Line(pos=np.vstack(element_lines), connect='segments', **elsel_settings) view.add(element_visual) # Overlay deformed plot if if overlay_deformed: element_lines = [None] * len(elements) for ix, el in enumerate(elements): element_lines[ix] = np.vstack([node.x[:3] for node in el.nodes]) element_visual = scene.Line(pos=np.vstack(element_lines), connect='segments', **def_el_settings) view.add(element_visual) # Establish element labels if element_labels and len(unsel_elements) > 0: el_cog = [el.get_cog() for el in unsel_elements] el_labels = [str(el.label) for el in unsel_elements] element_label_visual = scene.Text(text=el_labels, pos=el_cog, **ellab_settings) view.add(element_label_visual) if len(sel_elements) > 0: el_cog = [el.get_cog() for el in sel_elements] el_labels = [str(el.label) for el in sel_elements] element_label_visual = scene.Text(text=el_labels, pos=el_cog, **elsellab_settings) view.add(element_label_visual) # Node labels if node_labels: node_labels = [str(node.label) for node in nodes] element_label_visual = scene.Text(text=node_labels, pos=node_pos, **nlab_settings) view.add(element_label_visual) # Nodes if plot_nodes: node_visual = scene.visuals.Markers(pos=node_pos, **n_settings) view.add(node_visual) if sel_nodes is not None: sel_nodes = [node for node in nodes if node.label in sel_nodes] sel_node_labels = [str(node.label) for node in sel_nodes] if len(sel_nodes) >= 1: node_pos = np.vstack([node.coordinates for node in sel_nodes]) sel_node_label_visual = scene.Text(text=sel_node_labels, pos=node_pos, **nsellab_settings) sel_node_visual = scene.visuals.Markers(pos=node_pos, **nsel_settings) view.add(sel_node_label_visual) view.add(sel_node_visual) else: print('Requested nodes to highlight not found.') # Add transformation matrices if plot_tmat_ax is not None: for ax in plot_tmat_ax: el_vecs = np.vstack( [element.tmat[ax, 0:3] for element in elements]) * tmat_scaling el_cogs = np.vstack([element.get_cog() for element in elements]) # INTERTWINE TMAT AND ELCOGS arrow_data = np.hstack([el_cogs, el_cogs + el_vecs]) tmat_visual = scene.Arrow(pos=arrow_data.reshape( [el_vecs.shape[0] * 2, 3]), color=tmat_colors[ax], arrow_color=tmat_colors[ax], connect='segments', arrows=arrow_data, **tmatax_settings) view.add(tmat_visual) canvas.show() axis = scene.visuals.XYZAxis(parent=view.scene) return canvas, view