Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# grab center coordinates for atlas labels
atlas = kwargs.pop('atlas', data.atlas)
coords = plotting.find_parcellation_cut_coords(labels_img=atlas)
# assign node colors
cmap = kwargs.pop('cmap', 'jet')
node_cmap = plt.get_cmap('bone')
node_norm = mpl.colors.Normalize(vmin=-0.8, vmax=1.2)
node_colors = np.ravel([_[-1] for i,_ in enumerate(coords)])
node_colors = [_ / np.max(node_colors) for _ in node_colors]
node_colors = node_cmap(node_norm(node_colors))
# plot connectome matrix
fig = plt.figure(figsize=(12,5))
ax = plt.subplot2grid((1, 2), (0, 1), rowspan=1, colspan=1)
display = plotting.plot_matrix(
connectivity,
vmin=-.5, vmax=.5, colorbar=True, cmap=cmap,
axes=ax, #title='{} Matrix'.format(metric.title()),
)
# plot connectome with 99.7% edge strength in the connectivity
ax = plt.subplot2grid((1, 2), (0, 0), rowspan=1, colspan=1)
display = plotting.plot_connectome(
connectivity, coords,
edge_threshold="99.9%", display_mode='z',
node_color=node_colors, node_size=20, edge_kwargs=dict(lw=4),
edge_vmin=-.8, edge_vmax=.8, edge_cmap=cmap,
colorbar=False, black_bg=not True, alpha=0.5,
annotate=False,
axes=ax,
)
def plot_matrices(cov, prec, title):
"""Plot covariance and precision matrices, for a given processing. """
prec = prec.copy() # avoid side effects
# Put zeros on the diagonal, for graph clarity.
size = prec.shape[0]
prec[list(range(size)), list(range(size))] = 0
span = max(abs(prec.min()), abs(prec.max()))
# Display covariance matrix
plt.figure()
plt.imshow(cov, interpolation="nearest",
vmin=-1, vmax=1, cmap=plotting.cm.bwr)
plt.colorbar()
plt.title("%s / covariance" % title)
# Display precision matrix
plt.figure()
plt.imshow(prec, interpolation="nearest",
vmin=-span, vmax=span,
cmap=plotting.cm.bwr)
plt.colorbar()
plt.title("%s / precision" % title)
deformed = nighres.registration.apply_coordinate_mappings(
image=dataset1['t1map'],
mapping1=syn_results['mapping'],
save_data=True, file_name="sub001_sess1_t1map",
output_dir=out_dir, overwrite=False)
inverse = nighres.registration.apply_coordinate_mappings(
image=dataset2['t1w'],
mapping1=syn_results['inverse'],
save_data=True, file_name="sub002_sess1_t1w",
output_dir=out_dir, overwrite=False)
############################################################################
# Now we look at the coregistered images from applying the deformation
if not skip_plots:
plotting.plot_img(deformed['result'],
annotate=False, draw_cross=False)
plotting.plot_img(inverse['result'],
annotate=False, draw_cross=False)
############################################################################
#############################################################################
# If the example is not run in a jupyter notebook, render the plots:
if not skip_plots:
plotting.show()
###############################################################################
# Visualizing brain parcellations
# -------------------------------
# import plotting module and use `plot_roi` function, since the maps are in 3D
from nilearn import plotting
# The coordinates of all plots are selected automatically by itself
# We manually change the colormap of our choice
plotting.plot_roi(networks_64, cmap=plotting.cm.bwr,
title='64 regions of brain clusters')
plotting.plot_roi(networks_197, cmap=plotting.cm.bwr,
title='197 regions of brain clusters')
plotting.plot_roi(networks_444, cmap=plotting.cm.bwr_r,
title='444 regions of brain clusters')
plotting.show()
hemi='left', view='medial', colorbar=True,
title='Plotting without background')
###############################################################################
# Many different options are available for plotting, for example thresholding,
# or using custom colormaps
plotting.plot_surf_stat_map(fsaverage['pial_left'], stat_map=stat_map,
hemi='left', view='medial', colorbar=True,
bg_map=fsaverage['sulc_left'], bg_on_data=True,
cmap='Spectral', threshold=.5,
title='Threshold and colormap')
###############################################################################
# The plots can be saved to file, in which case the display is closed after
# creating the figure
plotting.plot_surf_stat_map(fsaverage['infl_left'], stat_map=stat_map,
hemi='left', bg_map=fsaverage['sulc_left'],
bg_on_data=True, threshold=.6, colorbar=True,
output_file='plot_surf_stat_map.png')
plotting.show()
# Display Destrieux parcellation on inflated fsaverage5 surface
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='lateral',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
###############################################################################
# Display Destrieux parcellation with different views: posterior
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='posterior',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
###############################################################################
# Display Destrieux parcellation with different views: ventral
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='ventral',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
plotting.show()
##############################################################################
# 3D visualization in a web browser
# ---------------------------------
# An alternative to :func:`nilearn.plotting.plot_surf_roi` is to use
# :func:`nilearn.plotting.view_surf` for more interactive
# visualizations in a web browser. See :ref:`interactive-surface-plotting` for
# more details.
view = plotting.view_surf(fsaverage.infl_left, parcellation,
cmap='gist_ncar', symmetric_cmap=False)
# extract time series from all subjects and concatenate them
mm = data.X[index, :].copy()
time_series = [np.vstack(mm)]
# calculate correlation matrices across indexed frames in data
connectome_measure = ConnectivityMeasure(kind=metric)
connectome_measure.fit_transform(time_series)
connectivity = connectome_measure.mean_
np.fill_diagonal(connectivity, 0)
#connectivity[np.abs(connectivity) < 0.2] = 0.0
# grab center coordinates for atlas labels
atlas = kwargs.pop('atlas', data.atlas)
coords = plotting.find_parcellation_cut_coords(labels_img=atlas)
# assign node colors
cmap = kwargs.pop('cmap', 'jet')
node_cmap = plt.get_cmap('bone')
node_norm = mpl.colors.Normalize(vmin=-0.8, vmax=1.2)
node_colors = np.ravel([_[-1] for i,_ in enumerate(coords)])
node_colors = [_ / np.max(node_colors) for _ in node_colors]
node_colors = node_cmap(node_norm(node_colors))
# plot connectome matrix
fig = plt.figure(figsize=(12,5))
ax = plt.subplot2grid((1, 2), (0, 1), rowspan=1, colspan=1)
display = plotting.plot_matrix(
connectivity,
vmin=-.5, vmax=.5, colorbar=True, cmap=cmap,
axes=ax, #title='{} Matrix'.format(metric.title()),
data.func[0])
first_epi_file = data.func[0]
# First the compute the mean image, from the 4D series of image
mean_func = image.mean_img(first_epi_file)
# Then we smooth, with a varying amount of smoothing, from none to 20mm
# by increments of 5mm
for smoothing in range(0, 25, 5):
smoothed_img = image.smooth_img(mean_func, smoothing)
plotting.plot_epi(smoothed_img,
title="Smoothing %imm" % smoothing)
plotting.show()
#
# Grab parcellations of brain image stored in attribute `labels_img_`
kmeans_labels_img = kmeans.labels_img_
plotting.plot_roi(kmeans_labels_img, mean_func_img,
title="KMeans parcellation",
display_mode='xz')
# kmeans_labels_img is a Nifti1Image object, it can be saved to file with
# the following code:
kmeans_labels_img.to_filename('kmeans_parcellation.nii.gz')
##################################################################
# Finally show them
plotting.show()