Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nilearn/nilearn into ev_d…
Browse files Browse the repository at this point in the history
…ecomposition

* 'master' of https://github.com/nilearn/nilearn:
  Fix Flake8 errors overlooked when merging PR nilearn#2028 (Plot connectome strength) (nilearn#2174)
  ENH: add connectome strength plot (nilearn#2028)
  Fix cache mixin tests (nilearn#2161)
  Do not fail if metadata cannot be updated for an image (nilearn#2167)
  [MRG] Nans in view connectome (nilearn#2166)
  • Loading branch information
kchawla-pi committed Oct 14, 2019
2 parents 259783f + bc6a019 commit 251778b
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 80 deletions.
1 change: 1 addition & 0 deletions doc/modules/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ uses.
plot_stat_map
plot_glass_brain
plot_connectome
plot_connectome_strength
plot_prob_atlas
plot_surf
plot_surf_roi
Expand Down
13 changes: 13 additions & 0 deletions doc/plotting/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ different heuristics to find cutting coordinates.
:target: ../auto_examples/03_connectivity/plot_sphere_based_connectome.html
:scale: 50

.. |plot_strength| image:: ../auto_examples/03_connectivity/images/sphx_glr_plot_sphere_based_connectome_004.png
:target: ../auto_examples/03_connectivity/plot_sphere_based_connectome.html
:scale: 50

.. |plot_anat| image:: ../auto_examples/01_plotting/images/sphx_glr_plot_demo_plotting_003.png
:target: ../auto_examples/01_plotting/plot_demo_plotting.html
:scale: 50
Expand Down Expand Up @@ -102,6 +106,15 @@ different heuristics to find cutting coordinates.
are demonstrated in
**Example:** :ref:`sphx_glr_auto_examples_03_connectivity_plot_atlas_comparison.py`

|plot_strength| :func:`plot_connectome_strength`
|hack|
Plotting a connectome strength

Functions for automatic extraction of coords based on
brain parcellations useful for :func:`plot_connectome`
are demonstrated in
**Example:** :ref:`sphx_glr_auto_examples_03_connectivity_plot_atlas_comparison.py`

|plot_prob_atlas| :func:`plot_prob_atlas`
|hack|
Plotting 4D probabilistic atlas maps
Expand Down
7 changes: 6 additions & 1 deletion doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ NEW
Yields very fast & accurate models, without creation of giant
clusters.
:class:`nilearn.regions.ReNA`

- Plot connectome strength
Use :func:`nilearn.plotting.plot_connectome_strength` to plot the strength of a
connectome on a glass brain. Strength is absolute sum of the edges at a node.
- Optimization to image resampling
:func:`nilearn.image.resample_img` has been optimized to pad rather than
resample images in the special case when there is only a translation
Expand Down Expand Up @@ -68,6 +70,9 @@ Changes
colormap. These arguments were already accepted in `kwargs` but not documented
before.

- :func:`nilearn.plotting.view_connectome` now converts NaNs in the adjacency
matrix to 0.

Fixes
-----

Expand Down
81 changes: 61 additions & 20 deletions examples/03_connectivity/plot_sphere_based_connectome.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
covariance** and **partial_correlation**, to recover the functional brain
**networks structure**.
We'll start by extracting signals from Default Mode Network regions and computing a
connectome from them.
We'll start by extracting signals from Default Mode Network regions and
computing a connectome from them.
"""

Expand All @@ -37,7 +37,7 @@
# connectivity dataset.

from nilearn import datasets
dataset = datasets.fetch_development_fmri(n_subjects=1)
dataset = datasets.fetch_development_fmri(n_subjects=20)

# print basic information on the dataset
print('First subject functional nifti image (4D) is at: %s' %
Expand All @@ -49,21 +49,21 @@
# ------------------------------------
dmn_coords = [(0, -52, 18), (-46, -68, 32), (46, -68, 32), (1, 50, -5)]
labels = [
'Posterior Cingulate Cortex',
'Left Temporoparietal junction',
'Right Temporoparietal junction',
'Medial prefrontal cortex',
]
'Posterior Cingulate Cortex',
'Left Temporoparietal junction',
'Right Temporoparietal junction',
'Medial prefrontal cortex',
]

##########################################################################
# Extracts signal from sphere around DMN seeds
# ----------------------------------------------
#
# We can compute the mean signal within **spheres** of a fixed radius
# We can compute the mean signal within **spheres** of a fixed radius
# around a sequence of (x, y, z) coordinates with the object
# :class:`nilearn.input_data.NiftiSpheresMasker`.
# The resulting signal is then prepared by the masker object: Detrended,
# band-pass filtered and **standardized to 1 variance**.
# The resulting signal is then prepared by the masker object: Detrended,
# band-pass filtered and **standardized to 1 variance**.

from nilearn import input_data

Expand All @@ -73,7 +73,7 @@
low_pass=0.1, high_pass=0.01, t_r=2,
memory='nilearn_cache', memory_level=1, verbose=2)

# Additionally, we pass confound information so ensure our extracted
# Additionally, we pass confound information to ensure our extracted
# signal is cleaned from confounds.

func_filename = dataset.func[0]
Expand Down Expand Up @@ -114,7 +114,8 @@
# Display connectome
# -------------------
#
# We display the graph of connections with `:func: nilearn.plotting.plot_connectome`.
# We display the graph of connections with
# `:func: nilearn.plotting.plot_connectome`.

from nilearn import plotting

Expand Down Expand Up @@ -172,7 +173,8 @@
#
#
# You can retrieve the coordinates for any atlas, including atlases
# not included in nilearn, using :func:`nilearn.plotting.find_parcellation_cut_coords`.
# not included in nilearn, using
# :func:`nilearn.plotting.find_parcellation_cut_coords`.


###############################################################################
Expand Down Expand Up @@ -231,8 +233,8 @@


###############################################################################
# Plot matrix and graph
# ---------------------
# Plot matrix, graph, and strength
# --------------------------------
#
# We use `:func: nilearn.plotting.plot_matrix` to visualize our correlation matrix
# and display the graph of connections with `nilearn.plotting.plot_connectome`.
Expand All @@ -247,13 +249,41 @@


###############################################################################
# .. note::
#
# 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
# .. note::
#
# 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.


###############################################################################
# Sometimes, the information in the correlation matrix is overwhelming and
# aggregating edge strength from the graph would help. Use the function
# `nilearn.plotting.plot_connectome_strength` to visualize this information.

plotting.plot_connectome_strength(
matrix, coords, title='Connectome strength for Power atlas'
)

###############################################################################
# From the correlation matrix, we observe that there is a positive and negative
# structure. We could make two different plots by plotting these strengths
# separately.

from matplotlib.pyplot import cm

# plot the positive part of of the matrix
plotting.plot_connectome_strength(
np.clip(matrix, 0, matrix.max()), coords, cmap=cm.YlOrRd,
title='Strength of the positive edges of the Power correlation matrix'
)

# plot the negative part of of the matrix
plotting.plot_connectome_strength(
np.clip(matrix, matrix.min(), 0), coords, cmap=cm.PuBu,
title='Strength of the negative edges of the Power correlation matrix'
)

###############################################################################
# Connectome extracted from Dosenbach's atlas
# -------------------------------------------
Expand Down Expand Up @@ -283,6 +313,17 @@

plotting.plot_connectome(matrix, coords, title='Dosenbach correlation graph',
edge_threshold="99.7%", node_size=20, colorbar=True)
plotting.plot_connectome_strength(
matrix, coords, title='Connectome strength for Power atlas'
)
plotting.plot_connectome_strength(
np.clip(matrix, 0, matrix.max()), coords, cmap=cm.YlOrRd,
title='Strength of the positive edges of the Power correlation matrix'
)
plotting.plot_connectome_strength(
np.clip(matrix, matrix.min(), 0), coords, cmap=cm.PuBu,
title='Strength of the negative edges of the Power correlation matrix'
)


###############################################################################
Expand Down
24 changes: 15 additions & 9 deletions nilearn/datasets/neurovault.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,19 +1651,25 @@ def _update_image(image_info, download_params):
"""
if not download_params['write_ok']:
return image_info
collection = _fetch_collection_for_image(
image_info, download_params)
image_info, collection = _download_image_terms(
image_info, collection, download_params)
metadata_file_path = os.path.join(
os.path.dirname(image_info['absolute_path']),
'image_{0}_metadata.json'.format(image_info['id']))
_write_metadata(image_info, metadata_file_path)
try:
collection = _fetch_collection_for_image(
image_info, download_params)
image_info, collection = _download_image_terms(
image_info, collection, download_params)
metadata_file_path = os.path.join(
os.path.dirname(image_info['absolute_path']),
'image_{0}_metadata.json'.format(image_info['id']))
_write_metadata(image_info, metadata_file_path)
except OSError:
warnings.warn(
"could not update metadata for image {}, "
"most likely because you do not have write "
"permissions to its metadata file".format(image_info["id"]))
return image_info


def _update(image_info, collection, download_params):
"""Update local metadata for an image and its collection."""
"Update local metadata for an image and its collection."""
image_info = _update_image(image_info, download_params)
return image_info, collection

Expand Down
4 changes: 2 additions & 2 deletions nilearn/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _set_mpl_backend():
from . import cm
from .img_plotting import plot_img, plot_anat, plot_epi, \
plot_roi, plot_stat_map, plot_glass_brain, plot_connectome, \
plot_prob_atlas, show
plot_connectome_strength, plot_prob_atlas, show
from .find_cuts import find_xyz_cut_coords, find_cut_slices, \
find_parcellation_cut_coords, find_probabilistic_atlas_cut_coords
from .matrix_plotting import plot_matrix
Expand All @@ -48,7 +48,7 @@ def _set_mpl_backend():

__all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',
'plot_roi', 'plot_stat_map', 'plot_glass_brain',
'plot_connectome', 'plot_prob_atlas',
'plot_connectome_strength', 'plot_connectome', 'plot_prob_atlas',
'find_xyz_cut_coords', 'find_cut_slices',
'show', 'plot_matrix', 'view_surf', 'view_img_on_surf',
'view_img', 'view_connectome', 'view_markers',
Expand Down
2 changes: 1 addition & 1 deletion nilearn/plotting/html_connectome.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _get_connectome(adjacency_matrix, coords, threshold=None,
marker_size=None, cmap=cm.cold_hot, symmetric_cmap=True):
connectome = {}
coords = np.asarray(coords, dtype='<f4')
adjacency_matrix = adjacency_matrix.copy()
adjacency_matrix = np.nan_to_num(adjacency_matrix, copy=True)
colors = colorscale(
cmap, adjacency_matrix.ravel(), threshold=threshold,
symmetric_cmap=symmetric_cmap)
Expand Down
Loading

0 comments on commit 251778b

Please sign in to comment.