def test_get_vertexcolor(): fsaverage = fetch_surf_fsaverage() mesh = surface.load_surf_mesh(fsaverage['pial_left']) surf_map = np.arange(len(mesh[0])) colors = html_surface.colorscale('jet', surf_map, 10) vertexcolors = html_surface._get_vertexcolor( surf_map, colors['cmap'], colors['norm'], colors['abs_threshold'], fsaverage['sulc_left']) assert len(vertexcolors) == len(mesh[0]) vertexcolors = html_surface._get_vertexcolor( surf_map, colors['cmap'], colors['norm'], colors['abs_threshold']) assert len(vertexcolors) == len(mesh[0])
def test_get_vertexcolor(): fsaverage = fetch_surf_fsaverage() mesh = surface.load_surf_mesh(fsaverage['pial_left']) surf_map = np.arange(len(mesh[0])) colors = html_surface.colorscale('jet', surf_map, 10) vertexcolors = html_surface._get_vertexcolor( surf_map, colors['cmap'], colors['norm'], colors['abs_threshold'], fsaverage['sulc_left']) assert len(vertexcolors) == len(mesh[0]) vertexcolors = html_surface._get_vertexcolor( surf_map, colors['cmap'], colors['norm'], colors['abs_threshold']) assert len(vertexcolors) == len(mesh[0])
def plot_surf(mesh, data, view="right", threshold="85%", output_file=None): coords, triangles = surface.load_surf_mesh(mesh) x, y, z = coords.T i, j, k = triangles.T colors = colorscale(cold_hot, data, threshold) vertexcolor = _get_vertexcolor( data, colors["cmap"], colors["norm"], colors["abs_threshold"], surface.load_surf_data(fsaverage["sulc_right"]), ) mesh_3d = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, vertexcolor=vertexcolor) fig = go.Figure(data=[mesh_3d]) fig.update_layout(scene_camera=CAMERAS[view], **LAYOUT) if output_file is not None: fig.write_image(output_file) return fig
def _plot_surf_plotly(coords, faces, surf_map=None, bg_map=None, hemi='left', view='lateral', cmap=None, symmetric_cmap=True, colorbar=False, threshold=None, vmin=None, vmax=None, cbar_vmin=None, cbar_vmax=None, cbar_tick_format=".1f", title=None, title_font_size=18, output_file=None): """Helper function for plot_surf. .. versionadded:: 0.8.2 This function handles surface plotting when the selected engine is plotly. .. note:: This function assumes that plotly and kaleido are installed. .. warning:: This function is new and experimental. Please report bugs that you may encounter. """ try: import plotly.graph_objects as go from nilearn.plotting.displays import PlotlySurfaceFigure except ImportError: msg = "Using engine='plotly' requires that ``plotly`` is installed." raise ImportError(msg) # noqa x, y, z = coords.T i, j, k = faces.T if cmap is None: cmap = cold_hot bg_data = None if bg_map is not None: bg_data = load_surf_data(bg_map) if bg_data.shape[0] != coords.shape[0]: raise ValueError('The bg_map does not have the same number ' 'of vertices as the mesh.') if surf_map is not None: _check_surf_map(surf_map, coords.shape[0]) colors = colorscale( cmap, surf_map, threshold, vmax=vmax, vmin=vmin, symmetric_cmap=symmetric_cmap ) vertexcolor = _get_vertexcolor( surf_map, colors["cmap"], colors["norm"], colors["abs_threshold"], bg_data ) else: if bg_data is None: bg_data = np.zeros(coords.shape[0]) colors = colorscale('Greys', bg_data, symmetric_cmap=False) vertexcolor = _get_vertexcolor( bg_data, colors["cmap"], colors["norm"], colors["abs_threshold"] ) mesh_3d = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, vertexcolor=vertexcolor) fig_data = [mesh_3d] if colorbar: dummy = _get_cbar_plotly( colors["colors"], float(colors["vmin"]), float(colors["vmax"]), cbar_tick_format ) fig_data.append(dummy) cameras_view = _set_view_plot_surf_plotly(hemi, view) fig = go.Figure(data=fig_data) fig.update_layout(scene_camera=CAMERAS[cameras_view], title=_configure_title_plotly(title, title_font_size), **LAYOUT) if output_file is not None: try: import kaleido # noqa: F401 except ImportError: msg = ("Saving figures to file with engine='plotly' requires " "that ``kaleido`` is installed.") raise ImportError(msg) # noqa plotly_figure = PlotlySurfaceFigure(figure=fig, output_file=output_file) if output_file is not None: plotly_figure.savefig() return plotly_figure