本文整理汇总了Python中nilearn.plotting.plot_connectome函数的典型用法代码示例。如果您正苦于以下问题:Python plot_connectome函数的具体用法?Python plot_connectome怎么用?Python plot_connectome使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了plot_connectome函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_connectivity_glassbrain
def plot_connectivity_glassbrain(subject_id, group, pc, roi_coords,
suffix, session='func1',
preprocessing_folder='pipeline_1',
save=True, msdl=False):
"""Plots connectome of pc
"""
title = '-'.join([suffix, group, subject_id, session])
output_folder = os.path.join(set_figure_base_dir('connectivity'), suffix)
if not os.path.isdir(output_folder):
os.makedirs(output_folder)
output_file = os.path.join(output_folder,
'_'.join([suffix, 'connectome', group,
session,
preprocessing_folder, subject_id]))
if msdl:
title += '_msdl'
output_file += '_msdl'
plt.figure(figsize=(10, 20), dpi=90)
if save:
plot_connectome(pc, roi_coords, edge_threshold='90%', title=title,
output_file=output_file)
else:
plot_connectome(pc, roi_coords, edge_threshold='90%', title=title)
示例2: plot_cluster_freqs_on_brain
def plot_cluster_freqs_on_brain(self, cluster_name, xyz, colormap='viridis', vmin=None, vmax=None, do_3d=False):
"""
Plot the frequencies of single electrode cluster on either a 2d or interactive 2d brain.
Parameters
----------
cluster_name: str
Name of column in self.res['clusters']
xyz: np.ndarray
3 x n array of electrode locations. Should be in MNI space.
colormap: str
matplotlib colormap name
vmin: float
lower limit of colormap values. If not given, lowest value in frequency column will be used
vmax: float
upper limit of colormap values. If not given, highest value in frequency column will be used
do_3d:
Whether to plot an interactive 3d brain, or a 2d brain
Returns
-------
If 2d, returns the matplotlib figure. If 3d, returns the html used to render the brain.
"""
# get frequecies for this cluster
freqs = self.res['clusters'][cluster_name].values
# get color for each frequency. Start with all black
colors = np.stack([[0., 0., 0., 0.]] * len(freqs))
# fill in colors of electrodes with defined frequencies
cm = plt.get_cmap(colormap)
cNorm = clrs.Normalize(vmin=np.nanmin(freqs) if vmin is None else vmin,
vmax=np.nanmax(freqs) if vmax is None else vmax)
colors[~np.isnan(freqs)] = cmx.ScalarMappable(norm=cNorm, cmap=cm).to_rgba(freqs[~np.isnan(freqs)])
# if plotting 2d, use the nilearn glass brain
if not do_3d:
fig, ax = plt.subplots()
ni_plot.plot_connectome(np.eye(xyz.shape[0]), xyz,
node_kwargs={'alpha': 0.7, 'edgecolors': None},
node_size=60, node_color=colors, display_mode='lzr',
axes=ax)
divider = make_axes_locatable(ax)
cax = divider.append_axes('bottom', size='4%', pad=0)
cb1 = mpl.colorbar.ColorbarBase(cax, cmap=colormap,
norm=cNorm,
orientation='horizontal')
cb1.set_label('Frequency', fontsize=20)
cb1.ax.tick_params(labelsize=14)
fig.set_size_inches(15, 10)
return fig
# if plotting 3d use the nilearn 3d brain. Unfortunately this doesn't add a colorbar.
else:
# you need to return the html. If in jupyter, it will automatically render
return ni_plot.view_markers(xyz, colors=colors, marker_size=6)
示例3: run_mini_pipeline
def run_mini_pipeline():
atlas = datasets.fetch_atlas_msdl()
atlas_img = atlas['maps']
labels = pd.read_csv(atlas['labels'])['name']
masker = NiftiMapsMasker(maps_img=atlas_img, standardize=True,
memory='/tmp/nilearn', verbose=0)
data = datasets.fetch_adhd(number_subjects)
figures_folder = '../figures/'
count=0
for func_file, confound_file in zip(data.func, data.confounds):
# fit the data to the atlas mask, regress out confounds
time_series = masker.fit_transform(func_file, confounds=confound_file)
correlation = np.corrcoef(time_series.T)
#plotting starts here
plt.figure(figsize=(10, 10))
plt.imshow(correlation, interpolation="nearest")
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)
corr_file = figures_folder+'subject_number_' + str(count) + '_correlation.pdf'
plt.savefig(corr_file)
atlas_region_coords = [plotting.find_xyz_cut_coords(img) for img in image.iter_img(atlas_img)]
threshold = 0.6
plotting.plot_connectome(correlation, atlas_region_coords, edge_threshold=threshold)
connectome_file = figures_folder+'subject_number_' + str(count) + '_connectome.pdf'
plt.savefig(connectome_file)
#graph setup
#binarize correlation matrix
correlation[correlation<threshold] = 0
correlation[correlation != 0] = 1
graph = nx.from_numpy_matrix(correlation)
partition=louvain.best_partition(graph)
values = [partition.get(node) for node in graph.nodes()]
plt.figure()
nx.draw_spring(graph, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=True)
graph_file = figures_folder+'subject_number_' + str(count) + '_community.pdf'
plt.savefig(graph_file)
count += 1
plt.close('all')
示例4:
###############################################################################
# Plot matrix and graph
# ---------------------
#
# We use `matplotlib` plotting functions to visualize our correlation matrix
# and display the graph of connections with `nilearn.plotting.plot_connectome`.
import matplotlib.pyplot as plt
from nilearn import plotting
plt.imshow(matrix, vmin=-1.0, vmax=1.0, cmap="RdBu_r", interpolation="nearest")
plt.colorbar()
plt.title("Power correlation matrix")
# 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
示例5: print
# All individual coefficients are stacked in a unique 2D matrix.
print('Correlations of ADHD patients are stacked in an array of shape {0}'
.format(correlation_matrices.shape))
###############################################################################
# as well as the average correlation across all fitted subjects.
mean_correlation_matrix = correlation_measure.mean_
print('Mean correlation has shape {0}.'.format(mean_correlation_matrix.shape))
###############################################################################
# We display the connectomes of the first 3 ADHD subjects and the mean
# correlation matrix over all ADHD patients.
from nilearn import plotting
plot_matrices(correlation_matrices[:4], 'correlation')
plotting.plot_connectome(mean_correlation_matrix, msdl_coords,
title='mean correlation over 13 ADHD subjects')
###############################################################################
# Look at blocks structure, reflecting functional networks.
###############################################################################
# Examine partial correlations
# ----------------------------
# We can also study **direct connections**, revealed by partial correlation
# coefficients. We just change the `ConnectivityMeasure` kind
partial_correlation_measure = ConnectivityMeasure(kind='partial correlation')
###############################################################################
# and repeat the previous operation.
partial_correlation_matrices = partial_correlation_measure.fit_transform(
adhd_subjects)
示例6:
# The covariance can be found at estimator.covariance_
plt.imshow(estimator.covariance_, interpolation="nearest",
vmax=1, vmin=-1, cmap=plt.cm.RdBu_r)
# And display the labels
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)
plt.title('Covariance')
##############################################################################
# And now display the corresponding graph
# ----------------------------------------
from nilearn import plotting
coords = atlas.region_coords
plotting.plot_connectome(estimator.covariance_, coords,
title='Covariance')
##############################################################################
# Display the sparse inverse covariance
# --------------------------------------
# we negate it to get partial correlations
plt.figure(figsize=(10, 10))
plt.imshow(-estimator.precision_, interpolation="nearest",
vmax=1, vmin=-1, cmap=plt.cm.RdBu_r)
# And display the labels
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)
plt.title('Sparse inverse covariance')
##############################################################################
示例7: NiftiMapsMasker
masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True,
memory='nilearn_cache', verbose=5)
data = datasets.fetch_adhd(n_subjects=1)
time_series = masker.fit_transform(data.func[0],
confounds=data.confounds)
correlation_matrix = np.corrcoef(time_series.T)
# Display the correlation matrix
from matplotlib import pyplot as plt
plt.figure(figsize=(10, 10))
plt.imshow(correlation_matrix, interpolation="nearest")
# And display the labels
x_ticks = plt.xticks(range(len(names)), names, rotation=90)
y_ticks = plt.yticks(range(len(names)), names)
# And now display the corresponding graph
from nilearn import plotting
coords = np.vstack((labels['x'], labels['y'], labels['z']))
# We threshold to keep only the 20% of edges with the highest value
# because the graph is very dense
plotting.plot_connectome(correlation_matrix, coords.T,
edge_threshold="80%")
plt.show()
示例8: range
__author__ = '2d Lt Kyle Palko'
import numpy as np
from nilearn import plotting as plt
# import Pitt 0050013, a young autistic subject
ts = np.genfromtxt('/media/kap/8e22f6f8-c4df-4d97-a388-0adcae3ec1fb/Python/Thesis/C200/ABIDE_pcp/cpac/filt_noglobal/'
'Pitt_0050013_rois_cc200.1D', skip_header=1)
cor = np.corrcoef(ts.T)
# create matrix
x = np.zeros((200, 200))
for i in range(0, np.size(x, axis=0)):
x[i][i] = 1
a = np.genfromtxt('cc200_roi.csv', delimiter=',')
row = np.array([c[0] for c in a])
col = np.array([c[1] for c in a])
del a
for i in range(0, 10):
r = row[i]
c = col[i]
x[r, c] = cor[r, c]
x[c, r] = cor[c, r]
node_coords = np.genfromtxt('cc200_lab_coord.csv', delimiter=',')
plt.plot_connectome(x, node_coords, output_file='corrtest_1.png', node_size=1)
示例9: zip
if kind == 'tangent':
mean_connectivity_matrix[kind] = conn_measure.mean_
else:
mean_connectivity_matrix[kind] = \
individual_connectivity_matrices[kind].mean(axis=0)
######################################################################
# Plot the mean connectome with hemispheric saggital cuts
import numpy as np
from nilearn import plotting
labels = atlas.labels
region_coords = atlas.region_coords
for kind in kinds:
plotting.plot_connectome(mean_connectivity_matrix[kind],
region_coords, edge_threshold='98%',
title=kind, display_mode='lzry')
######################################################################
# Use the connectivity coefficients to classify ADHD vs controls
from sklearn.svm import LinearSVC
from sklearn.cross_validation import StratifiedKFold, cross_val_score
classes = ['{0}{1}'.format(site, adhd) for site, adhd in zip(sites, adhds)]
print('Classification accuracy:')
mean_scores = []
cv = StratifiedKFold(classes, n_folds=3)
for kind in kinds:
svc = LinearSVC()
# Transform the connectivity matrices to 1D arrays
coonectivity_coefs = connectome.sym_to_vec(
示例10: ConnectivityMeasure
plt.tight_layout()
##########################################################################
# Compute partial correlation matrix
# -----------------------------------
# Using object :class:`nilearn.connectome.ConnectivityMeasure`: Its
# default covariance estimator is Ledoit-Wolf, allowing to obtain accurate
# partial correlations.
from nilearn.connectome import ConnectivityMeasure
connectivity_measure = ConnectivityMeasure(kind='partial correlation')
partial_correlation_matrix = connectivity_measure.fit_transform(
[time_series])[0]
##########################################################################
# Display connectome
# -------------------
from nilearn import plotting
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Default Mode Network Connectivity")
##########################################################################
# Display connectome with hemispheric projections.
# Notice (0, -52, 18) is included in both hemispheres since x == 0.
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Connectivity projected on hemispheres",
display_mode='lyrz')
plotting.show()
示例11: zip
for time_serie, label in zip(time_series.T, labels):
plt.plot(time_serie, label=label)
plt.title("Default Mode Network Time Series")
plt.xlabel("Scan number")
plt.ylabel("Normalized signal")
plt.legend()
plt.tight_layout()
##########################################################################
# Compute precision matrices
from sklearn.covariance import LedoitWolf
cve = LedoitWolf()
cve.fit(time_series)
##########################################################################
# Display connectome
from nilearn import plotting
plotting.plot_connectome(cve.precision_, dmn_coords, title="Default Mode Network Connectivity")
# Display connectome with hemispheric projections.
# Notice (0, -52, 18) is included in both hemispheres since x == 0.
title = "Connectivity projected on hemispheres"
plotting.plot_connectome(cve.precision_, dmn_coords, title=title, display_mode="lyrz")
plotting.show()
示例12: ConnectivityMeasure
from nilearn.connectome import ConnectivityMeasure
connectivity_measure = ConnectivityMeasure(kind='partial correlation')
partial_correlation_matrix = connectivity_measure.fit_transform(
[time_series])[0]
##########################################################################
# Display connectome
# -------------------
#
# We display the graph of connections with `:func: nilearn.plotting.plot_connectome`.
from nilearn import plotting
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Default Mode Network Connectivity")
##########################################################################
# Display connectome with hemispheric projections.
# Notice (0, -52, 18) is included in both hemispheres since x == 0.
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Connectivity projected on hemispheres",
display_mode='lyrz')
plotting.show()
##############################################################################
# 3D visualization in a web browser
# ---------------------------------
示例13: load_msdl_names_and_coords
roi_names, roi_coords = load_msdl_names_and_coords()
stat_av = read_test('pc', 'av', 'avg')
stat_v = read_test('pc', 'v', 'avg')
stat2 = read_test2('pc', ['av', 'v'], 'avg')
i, j = np.unravel_index(stat2.argmax(), stat2.shape)
print 'av 1sample pval :', stat_av[i, j]
print 'v 1sample pval :', stat_v[i, j]
print roi_names[i], roi_names[j]
print i, j
m = np.eye(2)
m[1,0] = stat2[i, j]
m[0,1] = m[1,0]
plot_connectome(m, [roi_coords[i], roi_coords[j]])
conn = []
behav = []
for i in range(len(dataset.subjects)):
c = load_dynacomp_fc(dataset.subjects[i], session='func1', metric='pc', msdl=True)
conn.append(c[i, j])
b = behav_data[i]['postRT'] - behav_data[i]['preRT']
behav.append(b)
sns.jointplot(np.array(conn), np.array(behav), kind='kde')
sns.axlabel('Connectivity', 'Behavior')
示例14: GraphLassoCV
###############################################################################
# Extract and plot correlation matrix
# calculate connectivity and plot Power-264 correlation matrix
connectivity = connectome.ConnectivityMeasure(kind='correlation')
corr_matrix = connectivity.fit_transform([timeseries])[0]
np.fill_diagonal(corr_matrix, 0)
plt.imshow(corr_matrix, vmin=-1., vmax=1., cmap='RdBu_r')
plt.colorbar()
plt.title('Power 264 Connectivity')
# Plot the connectome
plotting.plot_connectome(corr_matrix,
power_coords,
edge_threshold='99.8%',
node_size=20)
###############################################################################
# Extract and plot covariance and sparse covariance
# Compute the sparse inverse covariance
from sklearn.covariance import GraphLassoCV
estimator = GraphLassoCV()
estimator.fit(timeseries)
# Display the covariance
plt.figure(figsize=(5, 5))
plt.imshow(estimator.covariance_, interpolation="nearest",
示例15: print
###############################################################################
# as well as the average correlation across all fitted subjects.
mean_correlation_matrix = correlation_measure.mean_
print('Mean correlation has shape {0}.'.format(mean_correlation_matrix.shape))
###############################################################################
# We display the connectome matrices of the first 4 children
from nilearn import plotting
plot_matrices(correlation_matrices[:4], 'correlation')
###############################################################################
# The blocks structure that reflect functional networks are visible.
###############################################################################
# Now we display as a connectome the mean correlation matrix over all children.
plotting.plot_connectome(mean_correlation_matrix, msdl_coords,
title='mean correlation over all children')
###############################################################################
# Studying partial correlations
# -----------------------------
# We can also study **direct connections**, revealed by partial correlation
# coefficients. We just change the `ConnectivityMeasure` kind
partial_correlation_measure = ConnectivityMeasure(kind='partial correlation')
###############################################################################
# and repeat the previous operation.
partial_correlation_matrices = partial_correlation_measure.fit_transform(
children)
###############################################################################
# Most of direct connections are weaker than full connections,