def plot_connectome(covs, tv=False): # g = np.zeros(shape=(160, 160)) g = covs dos_coords = datasets.fetch_coords_dosenbach_2010() dos_coords = dos_coords.rois dos_coords_table = [[x, y, z] for (x, y, z) in dos_coords ] # Reformat the atlas coordinates f = plt.figure(figsize=(2.3, 3.5)) # 2.2,2.3 if tv: plotting.plot_connectome(g, dos_coords_table, display_mode='z', output_file='figs/connectome_tv.pdf', edge_threshold="0.0005%", annotate=True, figure=f, node_size=18) else: plotting.plot_connectome(g, dos_coords_table, display_mode='z', output_file='figs/connectome.pdf', edge_threshold="0.0005%", annotate=True, figure=f, node_size=18)
def save_dists(path): # Get the MNI coordinates for the Dosenbach atlas # Here I load the dosenbach atlas directly from nilearn, but you will need to provide a list of the MNI coordinates as a 2d array (see reformatting below) dos_coords = datasets.fetch_coords_dosenbach_2010() print("networks", dos_coords.networks) labels = numpy.array( [str(x[2:x.rfind(' ') - 1]) for x in dos_coords.labels]) networks = numpy.array([x.decode("utf-8") for x in dos_coords.networks]) print("networks shape", dos_coords.networks.shape) dos_coords = dos_coords.rois dos_coords_table = [[x, y, z] for (x, y, z) in dos_coords ] # Reformat the atlas coordinates dists = numpy.zeros((160, 160)) for i in range(160): for j in range(160): dists[i, j] = dist(dos_coords_table[i], dos_coords_table[j]) # print("coords", dos_coords_table) numpy.savetxt(path + '/data/dists.csv', dists, delimiter=',') numpy.savetxt(path + '/data/labels.csv', labels, fmt="%s", delimiter=',') numpy.savetxt(path + '/data/networks.csv', networks, fmt="%s", delimiter=',')
title='Power correlation graph', edge_threshold='99.8%', node_size=20, colorbar=True) ############################################################################### # Note the 1. on the matrix diagonal: These are the signals variances, set to # 1. by the `spheres_masker`. Hence the covariance of the signal is a # correlation matrix ############################################################################### # Connectome extracted from Dosenbach's atlas # ------------------------------------------- # # We repeat the same steps for Dosenbach's atlas. dosenbach = datasets.fetch_coords_dosenbach_2010() coords = np.vstack(( dosenbach.rois['x'], dosenbach.rois['y'], dosenbach.rois['z'], )).T spheres_masker = input_data.NiftiSpheresMasker(seeds=coords, smoothing_fwhm=4, radius=4.5, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2.5)
# Tweak edge_threshold to keep only the strongest connections. plotting.plot_connectome( matrix, coords, title="Power correlation graph", edge_threshold="99.8%", node_size=20, colorbar=True ) ############################################################################### # Note the 1. on the matrix diagonal: These are the signals variances, set to # 1. by the `spheres_masker`. Hence the covariance of the signal is a # correlation matrix ############################################################################### # Connectome extracted from Dosenbach's atlas # ------------------------------------------- # # We repeat the same steps for Dosenbach's atlas. dosenbach = datasets.fetch_coords_dosenbach_2010() coords = np.vstack((dosenbach.rois["x"], dosenbach.rois["y"], dosenbach.rois["z"])).T spheres_masker = input_data.NiftiSpheresMasker( seeds=coords, smoothing_fwhm=4, radius=4.5, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2.5 ) timeseries = spheres_masker.fit_transform(fmri_filename, confounds=confounds_filename) covariance_estimator = GraphLassoCV() covariance_estimator.fit(timeseries) matrix = covariance_estimator.covariance_ plt.figure() plt.imshow(matrix, vmin=-1.0, vmax=1.0, cmap="RdBu_r", interpolation="nearest")
title='Node strength for the positive edges for Power atlas', node_cmap=cm.YlOrRd) # plot nodes' strength for negative edges plotting.plot_markers( node_strength_negative, coords, title='Node strength for the negative edges for Power atlas', node_cmap=cm.PuBu) ############################################################################### # Connectome extracted from Dosenbach's atlas # ------------------------------------------- # # We repeat the same steps for Dosenbach's atlas. dosenbach = datasets.fetch_coords_dosenbach_2010(legacy_format=False) coords = np.vstack(( dosenbach.rois['x'], dosenbach.rois['y'], dosenbach.rois['z'], )).T spheres_masker = NiftiSpheresMasker(seeds=coords, smoothing_fwhm=6, radius=4.5, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2)
def plot_connectome(path): # Each of these is a p X p matrix, where p = the number of brain regions # In this dataset, we're looking at 160 ROIs. Each matrix graphs the functional dependencies between pairs of ROIs (building a connectome) # for 2 groups - patients with ASD and control subjects folder = "data/full_1/nsimule_dist_2/" autism = numpy.loadtxt( path + folder + "autism_0.2_1.csv", delimiter=',' ) # File containing the output graph from running SIMULE - autism control = numpy.loadtxt( path + folder + "control_0.2_1.csv", delimiter=',' ) # File containing the output graph from running SIMULE - control autism = numpy.around(autism, decimals=3) != 0 control = numpy.around(control, decimals=3) != 0 print("shape", autism.shape) print("topleft", autism[:3, :3]) # find diff diff = autism - control print("diff shape", diff.shape) print("nonzeros autism", numpy.sum(diff != 0)) # Get the MNI coordinates for the Dosenbach atlas # Here I load the dosenbach atlas directly from nilearn, but you will need to provide a list of the MNI coordinates as a 2d array (see reformatting below) dos_coords = datasets.fetch_coords_dosenbach_2010() dos_coords = dos_coords.rois dos_coords_table = [[x, y, z] for (x, y, z) in dos_coords ] # Reformat the atlas coordinates # WARNING: The more coordinates, the slower this becomes. I haven't tried plotting with >160 coordinates, and it takes about a minute to plot. # You might need to consider filtering your coordinates down to fewer ROIs out = 'pdf' tit = {'fontsize': 14, 'horizontalalignment': 'center'} f = plt.figure(figsize=(2.3, 3.5)) # 2.2,2.3 plotting.plot_connectome( -1 * autism, dos_coords_table, display_mode='z', # output_file=path + "plots/connectomes/autism." + out, # title="$\mathrm{\Omega^{Autism}}$", annotate=False, figure=f, node_size=18) plt.title("$\mathrm{\hat{\Omega}^{Autism}}$", **tit) plt.savefig(path + "plots/connectomes/autism." + out) # print('out', plot_out) # plt.show() f = plt.figure(figsize=(2.3, 3.5)) # 2.2,2.3 plotting.plot_connectome( -1 * control, dos_coords_table, display_mode='z', # output_file=path + "plots/connectomes/autism." + out, # title="$\mathrm{\Omega^{Autism}}$", annotate=False, figure=f, node_size=18) plt.title("$\mathrm{\hat{\Omega}^{Control}}$", **tit) plt.savefig(path + "plots/connectomes/control." + out) f = plt.figure(figsize=(2.3, 3.5)) # 2.2,2.3 plotting.plot_connectome( -1 * diff, dos_coords_table, display_mode='z', # output_file=path + "plots/connectomes/autism." + out, # title="$\mathrm{\Omega^{Autism}}$", annotate=False, figure=f, node_size=18) plt.title("$\mathrm{\hat{\Omega}^{Autism}-\hat{\Omega}^{Control}}$", **tit) plt.savefig(path + "plots/connectomes/diff." + out)