def _plot_subnetwork_graph(Theta, coords, network_ix): (x, y, z) = coords p = Theta.shape[0] network_ix = network_ix * np.ones((p,)) # 3D glass image of brain fig = mlab.figure(bgcolor=(1, 1, 1), size=(900, 769)) mlab.clf() fig.scene.disable_render = True vmin = np.min(Theta[np.abs(Theta) != 0]) vmax = np.max(np.abs(Theta)) tubes, nodes = plot_graph(-Theta, x, y, z, node_size=.6, edge_vmin=vmin, edge_vmax=vmax, node_colormap='spectral', node_color=(0.2, 0.2, 0.2), node_scalar=network_ix, tube_radius=.15) lut = tubes.module_manager.scalar_lut_manager.lut.table.to_array() lut = 255 * plt.cm.hot_r(np.linspace(0, 1, 256)) tubes.module_manager.scalar_lut_manager.lut.table = lut tubes.update_pipeline() #nodes.module_manager.scalar_lut_manager.lut.table = color nodes.update_pipeline() viz3d.plot_anat_3d(outline_color=(0, 0, 0), gyri_opacity=0.15) fig.scene.disable_render = False return fig
def plot_connectivity_graph(Theta=None, atlas_name="HarvardOxford", fig_name=None, partial=None, retain=.1, cluster_labels=None, plot_networks=False): """ plot the connectivity graph inside a glass brain """ if Theta is None: Theta = np.identity(96, dtype=np.float) Theta[10, 0] = .9 Theta[86, 3] = -.7 Theta_ = Theta.copy() p = Theta_.shape[0] # 3D positions of regions + labels regions = get_regions(atlas_name) X, Y, Z, roi_labels = zip( *[(int(region["x"]), int(region["y"]), int(region["z"]), region["label"]) for region in regions]) (x, y, z) = map(np.array, (X, Y, Z)) # 3D glass image of brain fig = mlab.figure(bgcolor=(1, 1, 1), size=(900, 769)) mlab.clf() fig.scene.disable_render = True # 2mm cortical map of Harvard_Oxford --> voxel to MNI coordinates (mm) if atlas_name in {"HarvardOxford", "HarvardOxfordExt"}: affine = np.identity(4, dtype=np.float) affine[0, 0] = -2. affine[1:3, 1:3] = 2. * affine[1:3, 1:3] affine[:3, 3] = np.array([90., -126., -72.]) affine /= 1.1 elif atlas_name == "Juelich": affine = np.identity(4, dtype=np.float) affine[0, 0] = -2. affine[1:3, 1:3] = 2. * affine[1:3, 1:3] affine[:3, 3] = np.array([90., -126., -72.]) affine /= 1.1 (x, y, z) = maps_3d.coord_transform(x, y, z, affine) partial_var = 1. / Theta_.flat[::p + 1] if partial: Theta_ = -Theta_.dot(np.diag(partial_var)) pctl = (1 - retain) * 100 thr = stats.scoreatpercentile( np.abs(Theta_[np.triu_indices(Theta_.shape[0], k=1)]), pctl) Theta_[np.abs(Theta_) < thr] = 0 #rois = mlab.points3d(x, y, z, partial_var, scale_factor=10, figure=fig) # construct edges as a list of tuples [(p1,p2),(p3,p4),...] which means # that an edge exists between vertices p1 and p2, p3 and p4, and so on #Theta_ = np.tril(Theta, -1) #edges_n1, edges_n2 = np.nonzero(Theta_) if cluster_labels is not None: n_clusters = np.unique(cluster_labels).size n_members = [cluster_labels[cluster_labels == c].size for c in np.arange(n_clusters - 1)] n_large_clusters = 5 large_cluster_ix = np.argsort( n_members, kind="mergesort")[-1:-n_large_clusters - 1:-1] n_networks = n_large_clusters + 1 network = [mlab2.find(large_cluster_ix == cluster_labels[ix])[0] if cluster_labels[ix] in large_cluster_ix else n_large_clusters for ix in np.arange(p)] else: network = None vmin = np.min(Theta_[np.abs(Theta_) != 0]) vmax = np.max(np.abs(Theta_)) tubes, nodes = plot_graph(-Theta_, x, y, z, node_size=.6, edge_vmin=vmin, edge_vmax=vmax, node_colormap='spectral', node_color=(0.2, 0.2, 0.2), node_scalar=network, tube_radius=.15) #tubes.parent.parent.parent.filter.vary_radius = \ # 'vary_radius_by_absolute_scalar' #tubes.module_manager.scalar_lut_manager.reverse_lut = True # Make points of the lut transparent lut = tubes.module_manager.scalar_lut_manager.lut.table.to_array() lut = 255 * plt.cm.hot_r(np.linspace(0, 1, 256)) tubes.module_manager.scalar_lut_manager.lut.table = lut tubes.update_pipeline() nodes.update_pipeline() viz3d.plot_anat_3d(outline_color=(0, 0, 0), gyri_opacity=0.15) fig.scene.disable_render = False if fig_name: mlab_save_views(fig_name, fig) return fig if plot_networks: ix_range = np.arange(p) for nw_ix in np.arange(n_networks - 1): nw_members = ix_range[cluster_labels == large_cluster_ix[nw_ix]] fig2 = _plot_subnetwork_graph( Theta[np.ix_(nw_members, nw_members)], (x[nw_members], y[nw_members], z[nw_members]), nw_ix) if fig_name: mlab_save_views(fig_name + "_nw%i" % nw_ix, fig2) return large_cluster_ix