def __init__(self, view=None, transform=None, **kwargs): self.view = view # Add a 3D cube to show us the unit cube. The 1.001 factor is to make # sure that the grid lines are not 'hidden' by volume renderings on the # front side due to numerical precision. vertices, filled_indices, outline_indices = create_cube() self.axis = scene.visuals.Mesh(vertices['position'], outline_indices, parent=self.view.scene, color=kwargs['axis_color'], mode='lines') self.axis.transform = transform self.xax = Axis(pos=[[-1.0, 0], [1.0, 0]], tick_direction=(0, -1), parent=self.view.scene, axis_label='X', anchors=['center', 'middle'], **kwargs) self.yax = Axis(pos=[[0, -1.0], [0, 1.0]], tick_direction=(-1, 0), parent=self.view.scene, axis_label='Y', anchors=['center', 'middle'], **kwargs) self.zax = Axis(pos=[[0, -1.0], [0, 1.0]], tick_direction=(-1, 0), parent=self.view.scene, axis_label='Z', anchors=['center', 'middle'], **kwargs) self.xtr = STTransform() self.xtr = self.xtr.as_matrix() self.xtr.rotate(45, (1, 0, 0)) self.xtr.translate((0, -1., -1.)) self.ytr = STTransform() self.ytr = self.ytr.as_matrix() self.ytr.rotate(-45, (0, 1, 0)) self.ytr.translate((-1, 0, -1.)) self.ztr = STTransform() self.ztr = self.ztr.as_matrix() self.ztr.rotate(45, (0, 1, 0)) self.ztr.rotate(90, (1, 0, 0)) self.ztr.translate((-1, -1, 0.)) self.xax.transform = ChainTransform(transform, self.xtr) self.yax.transform = ChainTransform(transform, self.ytr) self.zax.transform = ChainTransform(transform, self.ztr)
def _remove_layer(self, event): """When a layer is removed, remove its parent.""" layer = event.item vispy_layer = self.layer_to_visual[layer] vispy_layer.node.transforms = ChainTransform() vispy_layer.node.parent = None del self.layer_to_visual[layer]
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 550)) self.meshes = [] self.rotation = AffineTransform() # Generate some data to work with global mdata mdata = create_sphere(20, 40, 1.0) # Mesh with pre-indexed vertices, uniform color self.meshes.append(visuals.MeshVisual(meshdata=mdata, color='r')) ## Mesh with pre-indexed vertices, per-face color ## Because vertices are pre-indexed, we get a different color ## every time a vertex is visited, resulting in sharp color ## differences between edges. verts = mdata.get_vertices(indexed='faces') nf = verts.size // 9 fcolor = np.ones((nf, 3, 4), dtype=np.float32) fcolor[..., 0] = np.linspace(1, 0, nf)[:, np.newaxis] fcolor[..., 1] = np.random.normal(size=nf)[:, np.newaxis] fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis] mesh = visuals.MeshVisual(vertices=verts, face_colors=fcolor) self.meshes.append(mesh) ## Mesh with unindexed vertices, per-vertex color ## Because vertices are unindexed, we get the same color ## every time a vertex is visited, resulting in no color differences ## between edges. verts = mdata.get_vertices() faces = mdata.get_faces() nv = verts.size // 3 vcolor = np.ones((nv, 4), dtype=np.float32) vcolor[:, 0] = np.linspace(1, 0, nv) vcolor[:, 1] = np.random.normal(size=nv) vcolor[:, 2] = np.linspace(0, 1, nv) self.meshes.append(visuals.MeshVisual(verts, faces, vcolor)) self.meshes.append( visuals.MeshVisual(verts, faces, vcolor, shading='flat')) self.meshes.append( visuals.MeshVisual(verts, faces, vcolor, shading='smooth')) # Lay out meshes in a grid grid = (3, 3) s = 300. / max(grid) for i, mesh in enumerate(self.meshes): x = 800. * (i % grid[0]) / grid[0] + 400. / grid[0] - 2 y = 800. * (i // grid[1]) / grid[1] + 400. / grid[1] + 2 transform = ChainTransform([ STTransform(translate=(x, y), scale=(s, s, 1)), self.rotation ]) tr_sys = visuals.transforms.TransformSystem(self) tr_sys.visual_to_document = transform mesh.tr_sys = tr_sys self.show() self.timer = app.Timer(connect=self.rotate) self.timer.start(0.016)
def _remove(event): """When a layer is removed, remove its viewer.""" layers = event.source layer = event.item layer._order = 0 layer._node.transforms = ChainTransform() layer._node.parent = None
def niimg_to_transform(vol, hdr, as_bgd=True, vol_bgd=None, hdr_bgd=None): """Get transformations of nii.gz files for cross-sections. Parameters ---------- vol : array_like 3D volume data. hdr : array_like Array of transformation of shape (4, 4). as_bgd : bool | True Specify if the volume is a background image or have to be considered as an activation image. vol_bgd : array_like | None Volume data if `as_bgd` is True. hdr_bgd : array_like | None Transformation array if `as_bgd` is True. Returns ------- sg_tf : ChainTransform Transformation of sagittal view cr_tf : ChainTransform Transformation of coronal view ax_tf : ChainTransform Transformation of axial view """ # Get transformation variables sh_img, diag_img, tr_img = _niimg_var(vol, hdr) # Get the normalization transformation depending if the volume is an image # background or an activation image if as_bgd: # Background image sg_norm, cr_norm, ax_norm = _niimg_norm(sh_img - 1, diag_img, tr_img) else: # Activation image sh_bgd, diag_bgd, tr_bgd = _niimg_var(vol_bgd, hdr_bgd) sg_norm, cr_norm, ax_norm = _niimg_norm(sh_bgd - 1, diag_bgd, tr_bgd) # Get MNI and rotation transformations sg_mni, cr_mni, ax_mni = _niimg_mni(hdr) sg_rot, cr_rot, ax_rot = _niimg_rot() # Build the chain of transformation sg_tf = ChainTransform([sg_norm, sg_rot, sg_mni]) cr_tf = ChainTransform([cr_norm, cr_rot, cr_mni]) ax_tf = ChainTransform([ax_norm, ax_rot, ax_mni]) return sg_tf, cr_tf, ax_tf
def _on_display_change(self): parent = self.node.parent self.node.transforms = ChainTransform() self.node.parent = None if self.layer.dims.ndisplay == 2: self.node = Compound([Markers(), Markers(), Line()]) else: self.node = Compound([Markers(), Markers()]) self.node.parent = parent self._reset_base()
def _remove_layer(self, event): """When a layer is removed, remove its parent. Parameters ---------- event : qtpy.QtCore.QEvent Event from the Qt context. """ layer = event.item vispy_layer = self.layer_to_visual[layer] vispy_layer.node.transforms = ChainTransform() vispy_layer.node.parent = None del self.layer_to_visual[layer]
def _remove_layer(self, event): """When a layer is removed, remove its parent. Parameters ---------- event : napari.utils.event.Event The napari event that triggered this method. """ layer = event.value vispy_layer = self.layer_to_visual[layer] vispy_layer.node.transforms = ChainTransform() vispy_layer.node.parent = None del vispy_layer self._reorder_layers(None)
def _on_display_change(self): parent = self.node.parent self.node.transforms = ChainTransform() self.node.parent = None if self.layer.dims.ndisplay == 2: self.node = Compound([Markers(), Markers(), Line()]) else: self.node = Markers() self.node.parent = parent self.layer._update_dims() self.layer._set_view_slice() self.reset()
def _remove_layer(self, event): """When a layer is removed, remove its parent. Parameters ---------- event : napari.utils.event.Event The napari event that triggered this method. """ layer = event.item vispy_layer = self.layer_to_visual[layer] self.canvas.events.draw.disconnect(vispy_layer.on_draw) vispy_layer.node.transforms = ChainTransform() vispy_layer.node.parent = None del vispy_layer self._reorder_layers(None)
def __init__(self): app.Canvas.__init__(self, keys='interactive') n = 10000 pos = 0.25 * np.random.randn(n, 2).astype(np.float32) color = np.random.uniform(0, 1, (n, 3)).astype(np.float32) size = np.random.uniform(2, 12, (n, 1)).astype(np.float32) self.points = MarkerVisual(pos=pos, color=color, size=size) self.panzoom = STTransform(scale=(1, 0.2), translate=(0, 500)) w2 = (self.size[0]/2, self.size[1]/2) self.transform = ChainTransform([self.panzoom, STTransform(scale=w2, translate=w2), LogTransform(base=(0, 2, 0))]) self.tr_sys = TransformSystem(self) self.tr_sys.visual_to_document = self.transform
def __init__(self): app.Canvas.__init__(self, keys='interactive') ps = self.pixel_scale n = 10000 pos = 0.25 * np.random.randn(n, 2).astype(np.float32) color = np.random.uniform(0, 1, (n, 3)).astype(np.float32) size = np.random.uniform(2*ps, 12*ps, (n, 1)).astype(np.float32) self.points = MarkerVisual(pos=pos, color=color, size=size) self.panzoom = STTransform(scale=(1, 0.2), translate=(0, 500)) w2 = (self.size[0]/2, self.size[1]/2) self.transform = ChainTransform([self.panzoom, STTransform(scale=w2, translate=w2), LogTransform(base=(0, 2, 0))]) self.tr_sys = TransformSystem(self) self.tr_sys.visual_to_document = self.transform gloo.set_state(blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'))
def vpnormalize(obj, dist=1.): """Get the vispy transformation for normalizing objects. Parameters ---------- obj : tuple The ndarray of the object to rescale. Must be a (..., 2) or (..., 3). dist : float | 1. The final rescaling value. Returns ------- normalize : vispy.transformations VisPy transformation to normalize. """ # Prepare the transformation chain : t = ChainTransform() # Recenter the object : t.prepend(vprecenter(obj)) # Rescale : t.prepend(vprescale(obj, dist)) return t
def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 550)) self.meshes = [] self.rotation = MatrixTransform() # Generate some data to work with global mdata mdata = create_sphere(20, 40, 1.0) # Mesh with pre-indexed vertices, uniform color self.meshes.append(visuals.MeshVisual(meshdata=mdata, color='b')) # Mesh with pre-indexed vertices, per-face color # Because vertices are pre-indexed, we get a different color # every time a vertex is visited, resulting in sharp color # differences between edges. rng = np.random.RandomState(0) verts = mdata.get_vertices(indexed='faces') nf = verts.size // 9 fcolor = np.ones((nf, 3, 4), dtype=np.float32) fcolor[..., 0] = np.linspace(1, 0, nf)[:, np.newaxis] fcolor[..., 1] = rng.randn(nf, 1) fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis] mesh = visuals.MeshVisual(vertices=verts, face_colors=fcolor) self.meshes.append(mesh) # Mesh with unindexed vertices, per-vertex color # Because vertices are unindexed, we get the same color # every time a vertex is visited, resulting in no color differences # between edges. verts = mdata.get_vertices() faces = mdata.get_faces() nv = verts.size // 3 vcolor = np.ones((nv, 4), dtype=np.float32) vcolor[:, 0] = np.linspace(1, 0, nv) vcolor[:, 1] = rng.randn(nv) vcolor[:, 2] = np.linspace(0, 1, nv) self.meshes.append(visuals.MeshVisual(verts, faces, vcolor)) self.meshes.append( visuals.MeshVisual(verts, faces, vcolor, shading='flat')) self.meshes.append( visuals.MeshVisual(verts, faces, vcolor, shading='smooth')) # Mesh with color indexed into a colormap verts = mdata.get_vertices(None) faces = mdata.get_faces() values = rng.randn(len(verts)) mesh = visuals.MeshVisual(vertices=verts, faces=faces, vertex_values=values, shading='smooth') mesh.clim = [-1, 1] mesh.cmap = 'viridis' mesh.shininess = 0.01 self.meshes.append(mesh) # Lay out meshes in a grid grid = (3, 3) s = 300. / max(grid) for i, mesh in enumerate(self.meshes): x = 800. * (i % grid[0]) / grid[0] + 400. / grid[0] - 2 y = 800. * (i // grid[1]) / grid[1] + 400. / grid[1] + 2 transform = ChainTransform([ STTransform(translate=(x, y), scale=(s, s, s)), self.rotation ]) mesh.transform = transform mesh.transforms.scene_transform = STTransform(scale=(1, 1, 0.01)) self.show() self.timer = app.Timer(connect=self.rotate) self.timer.start(0.016)
def __init__(self): app.Canvas.__init__(self, keys='interactive') self.meshes = [] self.rotation = AffineTransform() # Generate some data to work with global mdata mdata = create_sphere(20, 40, 1.0) # Mesh with pre-indexed vertices, uniform color verts = mdata.get_vertices(indexed='faces') mesh = ModularMesh(pos=verts, color=(1, 0, 0, 1)) self.meshes.append(mesh) # Mesh with pre-indexed vertices, per-face color # Because vertices are pre-indexed, we get a different color # every time a vertex is visited, resulting in sharp color # differences between edges. nf = verts.size // 9 fcolor = np.ones((nf, 3, 4), dtype=np.float32) fcolor[..., 0] = np.linspace(1, 0, nf)[:, np.newaxis] fcolor[..., 1] = np.random.normal(size=nf)[:, np.newaxis] fcolor[..., 2] = np.linspace(0, 1, nf)[:, np.newaxis] mesh = ModularMesh(pos=verts, color=fcolor) self.meshes.append(mesh) # Mesh with unindexed vertices, per-vertex color # Because vertices are unindexed, we get the same color # every time a vertex is visited, resulting in no color differences # between edges. verts = mdata.get_vertices() faces = mdata.get_faces() nv = verts.size // 3 vcolor = np.ones((nv, 4), dtype=np.float32) vcolor[:, 0] = np.linspace(1, 0, nv) vcolor[:, 1] = np.random.normal(size=nv) vcolor[:, 2] = np.linspace(0, 1, nv) mesh = ModularMesh(pos=verts, faces=faces, color=vcolor) self.meshes.append(mesh) # Mesh colored by vertices + grid contours mesh = ModularMesh(pos=verts, faces=faces) mesh.color_components = [ VertexColorComponent(vcolor), GridContourComponent(spacing=(0.13, 0.13, 0.13)) ] self.meshes.append(mesh) # Phong shaded mesh mesh = ModularMesh(pos=verts, faces=faces) normal_comp = VertexNormalComponent(mdata) mesh.color_components = [ VertexColorComponent(vcolor), GridContourComponent(spacing=(0.1, 0.1, 0.1)), ShadingComponent(normal_comp, lights=[((-1, 1, -1), (1.0, 1.0, 1.0))], ambient=0.2) ] self.meshes.append(mesh) # Phong shaded mesh, flat faces mesh = ModularMesh(pos=mdata.get_vertices(indexed='faces')) normal_comp = VertexNormalComponent(mdata, smooth=False) mesh.color_components = [ VertexColorComponent(vcolor[mdata.get_faces()]), GridContourComponent(spacing=(0.1, 0.1, 0.1)), ShadingComponent(normal_comp, lights=[((-1, 1, -1), (1.0, 1.0, 1.0))], ambient=0.2) ] self.meshes.append(mesh) # Lay out meshes in a grid grid = (3, 3) s = 300. / max(grid) for i, mesh in enumerate(self.meshes): x = 800. * (i % grid[0]) / grid[0] + 400. / grid[0] - 2 y = 800. * (i // grid[1]) / grid[1] + 400. / grid[1] + 2 mesh.transform = ChainTransform([ STTransform(translate=(x, y), scale=(s, s, 1)), self.rotation ]) mesh.tr_sys = visuals.transforms.TransformSystem(self) mesh.tr_sys.visual_to_document = mesh.transform self.size = (800, 800) self.show() self.timer = app.Timer(connect=self.rotate) self.timer.start(0.016)