Exemple #1
0
def full_brain_info(volume_img,
                    mesh='fsaverage5',
                    threshold=None,
                    cmap=cm.cold_hot,
                    black_bg=False,
                    symmetric_cmap=True,
                    vmax=None,
                    vmin=None,
                    vol_to_surf_kwargs={}):
    """
    Project 3D map on cortex; prepare info to plot both hemispheres.


    This computes the dictionary that gets inserted in the web page,
    which contains encoded meshes, colors, min and max values, and
    background color.

    """
    info = {}
    mesh = surface.surface._check_mesh(mesh)
    surface_maps = {
        h: surface.vol_to_surf(volume_img,
                               mesh['pial_{}'.format(h)],
                               inner_mesh=mesh.get('white_{}'.format(h), None),
                               **vol_to_surf_kwargs)
        for h in ['left', 'right']
    }
    colors = colorscale(cmap,
                        np.asarray(list(surface_maps.values())).ravel(),
                        threshold,
                        symmetric_cmap=symmetric_cmap,
                        vmax=vmax,
                        vmin=vmin)

    for hemi, surf_map in surface_maps.items():
        bg_map = surface.load_surf_data(mesh['sulc_{}'.format(hemi)])
        info['pial_{}'.format(hemi)] = mesh_to_plotly(
            mesh['pial_{}'.format(hemi)])
        info['inflated_{}'.format(hemi)] = mesh_to_plotly(
            mesh['infl_{}'.format(hemi)])

        info['vertexcolor_{}'.format(hemi)] = _get_vertexcolor(
            surf_map, colors['cmap'], colors['norm'], colors['abs_threshold'],
            bg_map)
    info["cmin"], info["cmax"] = float(colors['vmin']), float(colors['vmax'])
    info['black_bg'] = black_bg
    info['full_brain_mesh'] = True
    info['colorscale'] = colors['colors']
    return info
Exemple #2
0
def one_mesh_info(surf_map,
                  surf_mesh,
                  threshold=None,
                  cmap=cm.cold_hot,
                  black_bg=False,
                  bg_map=None,
                  symmetric_cmap=True,
                  vmax=None,
                  vmin=None):
    """Prepare info for plotting one surface map on a single mesh.

    This computes the dictionary that gets inserted in the web page,
    which contains the encoded mesh, colors, min and max values, and
    background color.

    """
    info = {}
    colors = colorscale(cmap,
                        surf_map,
                        threshold,
                        symmetric_cmap=symmetric_cmap,
                        vmax=vmax,
                        vmin=vmin)
    info['inflated_left'] = mesh_to_plotly(surf_mesh)
    info['vertexcolor_left'] = _get_vertexcolor(surf_map, colors['cmap'],
                                                colors['norm'],
                                                colors['abs_threshold'],
                                                bg_map)
    info["cmin"], info["cmax"] = float(colors['vmin']), float(colors['vmax'])
    info['black_bg'] = black_bg
    info['full_brain_mesh'] = False
    info['colorscale'] = colors['colors']
    return info
Exemple #3
0
def test_mesh_to_plotly(hemi):
    """Tests for function mesh_to_plotly."""
    fsaverage = fetch_surf_fsaverage()
    coord, triangles = load_surf_mesh(fsaverage[f'pial_{hemi}'])
    plotly = mesh_to_plotly(fsaverage[f'pial_{hemi}'])
    for i, key in enumerate(['_x', '_y', '_z']):
        assert np.allclose(decode(plotly[key], '<f4'), coord[:, i])
    for i, key in enumerate(['_i', '_j', '_k']):
        assert np.allclose(decode(plotly[key], '<i4'), triangles[:, i])
def test_mesh_to_plotly():
    fsaverage = fetch_surf_fsaverage()
    coord, triangles = surface.load_surf_mesh(fsaverage['pial_left'])
    plotly = js_plotting_utils.mesh_to_plotly(fsaverage['pial_left'])
    for i, key in enumerate(['_x', '_y', '_z']):
        assert np.allclose(js_plotting_utils.decode(plotly[key], '<f4'),
                           coord[:, i])
    for i, key in enumerate(['_i', '_j', '_k']):
        assert np.allclose(js_plotting_utils.decode(plotly[key], '<i4'),
                           triangles[:, i])
def test_mesh_to_plotly():
    fsaverage = fetch_surf_fsaverage()
    coord, triangles = surface.load_surf_mesh(fsaverage['pial_left'])
    plotly = js_plotting_utils.mesh_to_plotly(fsaverage['pial_left'])
    for i, key in enumerate(['_x', '_y', '_z']):
        assert np.allclose(
            js_plotting_utils.decode(plotly[key], '<f4'), coord[:, i])
    for i, key in enumerate(['_i', '_j', '_k']):
        assert np.allclose(
            js_plotting_utils.decode(plotly[key], '<i4'), triangles[:, i])
Exemple #6
0
def brain_plot():
    '''
    Make an HTML brain plot:
    Which nodes show the greatest change in PR Centrality for remembered vs. non-remembered items
    Plot the edges in the graph that CHANGE the most for this contrast, thereby contributing to change in centrality
    :return:
    '''

    patient = str(request.form.get('patient'))
    frequency = str(request.form.get('frequency'))

    adjacency_matrix, node_coords = get_adjacency(patient=patient,
                                                  frequency=frequency)

    connectome_info = plotting.html_connectome._get_connectome(
        adjacency_matrix, node_coords,
        threshold="99%", symmetric_cmap=False)

    # Todo: Figure out how to input all these params in the most sensible way

    connectome_info['line_width'] = app.linewidth
    connectome_info['colorbar'] = app.colorbar
    # connectome_info['cbar_height'] = app.colorbar_height
    # connectome_info['cbar_fontsize'] = app.colorbar_fontsize
    connectome_info['title'] = patient  # title
    # connectome_info['title_fontsize'] = None #title_fontsize

    # make connectome (plotly)
    plot_info = {"connectome": connectome_info}
    mesh = datasets.fetch_surf_fsaverage()
    for hemi in ['pial_left', 'pial_right']:
        plot_info[hemi] = mesh_to_plotly(mesh[hemi])

    # dump the plot to JSON
    graphJSON = json.dumps(plot_info)

    # Get the js libs
    js_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'js')
    with open(os.path.join(js_dir, 'surface-plot-utils.js')) as f:
        js_utils = f.read()

    # if I want to embed the js libraries:
    #    with open(os.path.join(js_dir, 'jquery.min.js')) as f:
    #        jquery = f.read()
    #    with open(os.path.join(js_dir, 'plotly-gl3d-latest.min.js')) as f:
    #        plotly = f.read()

    js_lib = """
        <script>
        {}
        </script>
        """.format(js_utils)

    html = get_html_template(
        'connectome_plot_template.html').safe_substitute(
        {'INSERT_CONNECTOME_JSON_HERE': graphJSON,
         'INSERT_JS_LIBRARIES_HERE': js_lib})

    # save out the html document to the html folder
    with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates',
                           '{}_connectome_plot.html'.format(patient)), "w") as file:
        file.write(html)

    return render_template('{}_connectome_plot.html'.format(patient))