def show_segmentation(voxel_segmentation, size=2.0, width=500, height=500): ipyvolume.figure(width=width, height=height) ipyvolume.view(0, 90) def get_color(label, info): if label == "stem": color = (128, 128, 128) elif label == "unknown": color = (255, 255, 255) elif 'pm_leaf_number' in info: color_map = order_color_map() color = color_map[info['pm_leaf_number']] color = tuple([int(255 * x) for x in color]) else: if label == "growing_leaf": color = (255, 0, 0) else: color = (0, 255, 0) return "rgb" + str(color) for vo in voxel_segmentation.voxel_organs: voxels_position = numpy.array( list(map(tuple, list(vo.voxels_position())))) plot_voxel(voxels_position, size=size * 1, color=get_color(vo.label, vo.info)) if ((vo.label == "mature_leaf" or vo.label == "growing_leaf") and len(vo.voxel_segments) > 0 and "pm_position_tip" in vo.info): plot_voxel(numpy.array([vo.info['pm_position_tip']]), size=size * 2, color="red", marker="sphere") plot_voxel(numpy.array([vo.info['pm_position_base']]), size=size * 2, color="blue", marker="sphere") voxels_position = numpy.array( list(voxel_segmentation.get_voxels_position())) x_min = voxels_position[:, 0].min() x_max = voxels_position[:, 0].max() y_min = voxels_position[:, 1].min() y_max = voxels_position[:, 1].max() z_min = voxels_position[:, 2].min() z_max = voxels_position[:, 2].max() xyz_max = max(x_max - x_min, y_max - y_min, z_max - z_min) ipyvolume.xlim(x_min, x_min + xyz_max) ipyvolume.ylim(y_min, y_min + xyz_max) ipyvolume.zlim(z_min, z_min + xyz_max) ipyvolume.show()
def show_mesh(vertices, faces, color='green', width=500, height=500, colors=None): ipyvolume.figure(width=width, height=height) ipyvolume.view(0, 90) ipyvolume.plot_trisurf(vertices[:, 0], vertices[:, 1], vertices[:, 2], triangles=faces, color=color) x_min = vertices[:, 0].min() x_max = vertices[:, 0].max() y_min = vertices[:, 1].min() y_max = vertices[:, 1].max() z_min = vertices[:, 2].min() z_max = vertices[:, 2].max() xyz_max = max(x_max - x_min, y_max - y_min, z_max - z_min) ipyvolume.xlim(x_min, x_min + xyz_max) ipyvolume.ylim(y_min, y_min + xyz_max) ipyvolume.zlim(z_min, z_min + xyz_max) ipyvolume.show()
def draw_tensor(tensor_img): ipv.figure() # ipv.volshow(tensor_img[...,0], level=[0.36, 0.55,1], opacity=[0.11,0.13, 0.13], level_width=0.05, data_min=0, data_max=1 ,lighting=True) ipv.volshow(tensor_img[...,0], level=[0.36, 0.17,0.36], opacity=[0.05,0.13, 0.10], level_width=0.05, data_min=0, data_max=1 ,lighting=True) ipv.view(-30, 45) ipv.show()
def show_syntehtic_plant(vertices, faces, meta_data=None, size=0.5, color='green', width=500, height=500): ipyvolume.figure(width=width, height=height) ipyvolume.view(0, 90) ipyvolume.plot_trisurf(vertices[:, 0], vertices[:, 1], vertices[:, 2], triangles=faces, color=color) voxels_position = vertices if meta_data is not None: ranks = meta_data['leaf_order'] polylines = { n: list(map(numpy.array, list(zip(*meta_data['leaf_polylines'][i])))) for i, n in enumerate(ranks) } voxels = set() for leaf_order in polylines: x, y, z, r = polylines[leaf_order] polyline = numpy.array(list(zip(x, y, z))) * 10 - \ numpy.array([0, 0, 750]) plot_voxel(polyline, size=size, color="red") voxels = voxels.union(set(map(tuple, list(polyline)))) voxels = voxels.union(set(map(tuple, list(voxels_position)))) voxels_position = numpy.array(list(voxels), dtype=numpy.int) x_min = voxels_position[:, 0].min() x_max = voxels_position[:, 0].max() y_min = voxels_position[:, 1].min() y_max = voxels_position[:, 1].max() z_min = voxels_position[:, 2].min() z_max = voxels_position[:, 2].max() xyz_max = max(x_max - x_min, y_max - y_min, z_max - z_min) ipyvolume.xlim(x_min, x_min + xyz_max) ipyvolume.ylim(y_min, y_min + xyz_max) ipyvolume.zlim(z_min, z_min + xyz_max) ipyvolume.show()
def show_voxel_grid(voxel_grid, color='green', size=2, width=500, height=500): ipyvolume.figure(width=width, height=height, controls=True, lighting=True) plot_voxel(voxel_grid.voxels_position, size=size, color=color) x_min = voxel_grid.voxels_position[:, 0].min() x_max = voxel_grid.voxels_position[:, 0].max() y_min = voxel_grid.voxels_position[:, 1].min() y_max = voxel_grid.voxels_position[:, 1].max() z_min = voxel_grid.voxels_position[:, 2].min() z_max = voxel_grid.voxels_position[:, 2].max() xyz_max = max(x_max - x_min, y_max - y_min, z_max - z_min) ipyvolume.xlim(x_min, x_min + xyz_max) ipyvolume.ylim(y_min, y_min + xyz_max) ipyvolume.zlim(z_min, z_min + xyz_max) ipyvolume.view(0, 90) ipyvolume.show()
def show_skeleton(voxel_skeleton, size=2, with_voxel=True, voxels_color='green', polyline_color='red', width=500, height=500): ipyvolume.figure(width=width, height=height) ipyvolume.view(0, 90) if with_voxel: voxels_position = voxel_skeleton.voxels_position() plot_voxel(voxels_position, size=size / 2, color=voxels_color) voxels_position = voxel_skeleton.voxels_position_polyline() plot_voxel(voxels_position, size=size, color=polyline_color) for vs in voxel_skeleton.segments: for color, index in [("blue", 0), ("red", -1)]: plot_voxel(numpy.array([vs.polyline[index]]), size=size * 2, marker="sphere", color=color) x_min = voxels_position[:, 0].min() x_max = voxels_position[:, 0].max() y_min = voxels_position[:, 1].min() y_max = voxels_position[:, 1].max() z_min = voxels_position[:, 2].min() z_max = voxels_position[:, 2].max() xyz_max = max(x_max - x_min, y_max - y_min, z_max - z_min) ipyvolume.xlim(x_min, x_min + xyz_max) ipyvolume.ylim(y_min, y_min + xyz_max) ipyvolume.zlim(z_min, z_min + xyz_max) ipyvolume.show()
def test_view(): fig = ipv.figure() az0, el0, r0 = ipv.view() ipv.view(azimuth=az0 + 42) az, el, r = ipv.view() assert az == az0 + 42 assert el == el0 assert r == r0 ipv.view(elevation=el0 + 42) az, el, r = ipv.view() assert az == az0 + 42 assert el == el0 + 42 assert r == r0 ipv.view(distance=r0 + 42) az, el, r = ipv.view() assert az == az0 + 42 assert el == el0 + 42 assert r == r0 + 42 ipv.view(42, 42, 42) az, el, r = ipv.view() assert az == 42 assert el == 42 assert r == 42
def test_view(): ipv.figure() az0, el0, r0 = ipv.view() ipv.view(azimuth=az0 + 42) az, el, r = ipv.view() assert az == pytest.approx(az0 + 42) assert el == el0 assert r == r0 ipv.view(elevation=el0 + 42) az, el, r = ipv.view() assert az == pytest.approx(az0 + 42) assert el == pytest.approx(el0 + 42) assert r == r0 ipv.view(distance=r0 + 42) az, el, r = ipv.view() assert az == pytest.approx(az0 + 42) assert el == pytest.approx(el0 + 42) assert r == pytest.approx(r0 + 42) ipv.view(42, 42, 42) az, el, r = ipv.view() assert az == pytest.approx(42) assert el == pytest.approx(42) assert r == pytest.approx(42)
def __init__(self, subject_id, hemi, surf, title=None, cortex='classic', alpha=1.0, size=800, background='black', foreground=None, figure=None, subjects_dir=None, views=['lateral'], offset=True, show_toolbar=False, offscreen=False, interaction=None, units='mm'): # surf = surface if cortex != 'classic': raise ValueError('Options for parameter "cortex" ' + 'is not yet supported.') if figure is not None: raise ValueError('figure parameter is not supported yet.') if interaction is not None: raise ValueError('"interaction" parameter is not supported.') self._foreground = foreground self._hemi = hemi self._units = units self._title = title self._subject_id = subject_id self._views = views # for now only one color bar can be added # since it is the same for all figures self._colorbar_added = False # array of data used by TimeViewer self._data = {} # load geometry for one or both hemispheres as necessary offset = None if (not offset or hemi != 'both') else 0.0 if hemi in ('both', 'split'): self._hemis = ('lh', 'rh') elif hemi in ('lh', 'rh'): self._hemis = (hemi, ) else: raise ValueError('hemi has to be either "lh", "rh", "split", ' + 'or "both"') if isinstance(size, int): fig_w = size fig_h = size else: fig_w, fig_h = size self.geo = {} self._figures = [[] for v in views] self._hemi_meshes = {} self._overlays = {} for h in self._hemis: # Initialize a Surface object as the geometry geo = Surface(subject_id, h, surf, subjects_dir, offset, units=self._units) # Load in the geometry and curvature geo.load_geometry() geo.load_curvature() self.geo[h] = geo for ri, v in enumerate(views): fig = ipv.figure(width=fig_w, height=fig_h, lighting=True) fig.animation = 0 self._figures[ri].append(fig) ipv.style.box_off() ipv.style.axes_off() ipv.style.background_color(background) ipv.view(views_dict[v].azim, views_dict[v].elev) for ci, h in enumerate(self._hemis): if ci == 1 and hemi == 'split': # create a separate figure for right hemisphere fig = ipv.figure(width=fig_w, height=fig_h, lighting=True) fig.animation = 0 self._figures[ri].append(fig) ipv.style.box_off() ipv.style.axes_off() ipv.style.background_color(background) ipv.view(views_dict[v].azim, views_dict[v].elev) hemi_mesh = self._plot_hemi_mesh(self.geo[h].coords, self.geo[h].faces, self.geo[h].grey_curv) self._hemi_meshes[h + '_' + v] = hemi_mesh ipv.squarelim() self._add_title()
def plot_brain_mesh(rh_vertices=None, lh_vertices=None, rh_faces=None, lh_faces=None, rh_color='grey', lh_color='grey', fig_size=(500, 500), azimuth=90, elevation=90): """Function for plotting triangular format Freesurfer surface of the brain. Parameters ---------- rh_vertices : numpy.array, optional Array of right hemisphere vertex (x, y, z) coordinates, of size number_of_vertices x 3. Default is None. lh_vertices : numpy.array, optional Array of left hemisphere vertex (x, y, z) coordinates, of size number_of_vertices x 3. Default is None. rh_faces : numpy.array, optional Array defining right hemisphere mesh triangles, of size number_of_faces x 3. Default is None. lh_faces : numpy.array, optional Array defining mesh triangles, of size number_of_faces x 3. Default is None. rh_color : str | numpy.array, optional Color for each point/vertex/symbol of the right hemisphere, can be string format, examples for red:’red’, ‘#f00’, ‘#ff0000’ or ‘rgb(1,0,0), or rgb array of shape (N, 3). Default value is 'grey'. lh_color : str | numpy.array, optional Color for each point/vertex/symbol of the left hemisphere, can be string format, examples for red:’red’, ‘#f00’, ‘#ff0000’ or ‘rgb(1,0,0), or rgb array of shape (N, 3). Default value is 'grey'. fig_size : (int, int), optional Width and height of the figure. Default is (500, 500). azimuth : int, optional Angle of rotation about the z-axis (pointing up) in degrees. Default is 90. elevation : int, optional Vertical rotation where 90 means ‘up’, -90 means ‘down’, in degrees. Default is 90. Returns ------- fig : ipyvolume.Figure Ipyvolume object presenting the figure. rh_mesh : ipyvolume.Mesh Ipyvolume object presenting the built mesh for right hemisphere. lh_mesh : ipyvolume.Mesh Ipyvolume object presenting the built mesh for right hemisphere. """ rh_mesh = None lh_mesh = None fig = ipv.figure(width=fig_size[0], height=fig_size[1], lighting=True) if (rh_vertices is not None) and (rh_faces is not None): rh_mesh = plot_hemisphere_mesh(rh_vertices, rh_faces, rh_color) if (lh_vertices is not None) and (lh_faces is not None): lh_mesh = plot_hemisphere_mesh(lh_vertices, lh_faces, lh_color) style.use('minimal') ipv.view(azimuth, elevation) ipv.squarelim() ipv.show() return fig, rh_mesh, lh_mesh