Extracting signals from brain regions using the NiftiLabelsMasker#

This simple example shows how to extract signals from functional fMRI data and brain regions defined through an atlas. More precisely, this example shows how to use the NiftiLabelsMasker object to perform this operation in just a few lines of code.

Note

If you are using Nilearn with a version older than 0.9.0, then you should either upgrade your version or import maskers from the input_data module instead of the maskers module.

That is, you should manually replace in the following example all occurrences of:

from nilearn.maskers import NiftiMasker

with:

from nilearn.input_data import NiftiMasker

Retrieve the brain development functional dataset

We start by fetching the brain development functional dataset and we restrict the example to one subject only.

from nilearn import datasets
dataset = datasets.fetch_development_fmri(n_subjects=1)
func_filename = dataset.func[0]

# print basic information on the dataset
print('First functional nifti image (4D) is at: %s' % func_filename)
First functional nifti image (4D) is at: /home/yasmin/nilearn/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz

Load an atlas

We then load the Harvard-Oxford atlas to define the brain regions

atlas = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')

# The first label correspond to the background
print('The atlas contains {} non-overlapping regions'.format(
    len(atlas.labels) - 1))
The atlas contains 48 non-overlapping regions

Instantiate the mask and visualize atlas

from nilearn.maskers import NiftiLabelsMasker

# Instantiate the masker with label image and label values
masker = NiftiLabelsMasker(atlas.maps,
                           labels=atlas.labels,
                           standardize=True)

# Visualize the atlas
# Note that we need to call fit prior to generating the mask
masker.fit()

# At this point, no functional image has been provided to the masker.
# We can still generate a report which can be displayed in a Jupyter
# Notebook, opened in a browser using the .open_in_browser() method,
# or saved to a file using the .save_as_html(output_filepath) method.
report = masker.generate_report()
report
/home/yasmin/nilearn/nilearn/nilearn/maskers/nifti_labels_masker.py:343: UserWarning:

No image provided to fit in NiftiLabelsMasker. Plotting ROIs of label image on the MNI152Template for reporting.

NiftiLabelsMasker Class for masking of Niimg-like objects. NiftiLabelsMasker is useful when data from non-overlapping volumes should be extracted (contrarily to :class:`nilearn.maskers.NiftiMapsMasker`). Use case: Summarize brain signals from clusters that were obtained by prior K-means or Ward clustering.

image

No image provided to fit in NiftiLabelsMasker. Plotting ROIs of label image on the MNI152Template for reporting.

This reports shows the regions defined by the labels of the mask.

The masker has 48 different non-overlapping regions.

Regions summary
label valueregion namesize (in mm^3)relative size (in %)
1Frontal Pole12317611.75
2Insular Cortex187281.79
3Superior Frontal Gyrus406403.88
4Middle Frontal Gyrus425284.06
5Inferior Frontal Gyrus, pars triangularis88240.84
6Inferior Frontal Gyrus, pars opercularis110721.06
7Precentral Gyrus685846.54
8Temporal Pole376883.59
9Superior Temporal Gyrus, anterior division41680.4
10Superior Temporal Gyrus, posterior division146401.4
11Middle Temporal Gyrus, anterior division67840.65
12Middle Temporal Gyrus, posterior division202001.93
13Middle Temporal Gyrus, temporooccipital part160321.53
14Inferior Temporal Gyrus, anterior division51760.49
15Inferior Temporal Gyrus, posterior division155361.48
16Inferior Temporal Gyrus, temporooccipital part117601.12
17Postcentral Gyrus551605.26
18Superior Parietal Lobule232642.22
19Supramarginal Gyrus, anterior division139361.33
20Supramarginal Gyrus, posterior division180721.72
21Angular Gyrus192721.84
22Lateral Occipital Cortex, superior division782327.46
23Lateral Occipital Cortex, inferior division327123.12
24Intracalcarine Cortex112081.07
25Frontal Medial Cortex78080.74
26Juxtapositional Lobule Cortex (formerly Supplementary Motor Cortex)118721.13
27Subcallosal Cortex91360.87
28Paracingulate Gyrus235522.25
29Cingulate Gyrus, anterior division207361.98
30Cingulate Gyrus, posterior division192961.84
31Precuneous Cortex449844.29
32Cuneal Cortex98160.94
33Frontal Orbital Cortex251842.4
34Parahippocampal Gyrus, anterior division99840.95
35Parahippocampal Gyrus, posterior division56800.54
36Lingual Gyrus270482.58
37Temporal Fusiform Cortex, anterior division48800.47
38Temporal Fusiform Cortex, posterior division127521.22
39Temporal Occipital Fusiform Cortex117521.12
40Occipital Fusiform Gyrus144481.38
41Frontal Operculum Cortex54960.52
42Central Opercular Cortex150881.44
43Parietal Operculum Cortex89520.85
44Planum Polare59920.57
45Heschl's Gyrus (includes H1 and H2)48320.46
46Planum Temporale76160.73
47Supracalcarine Cortex20880.2
48Occipital Pole422084.03
Parameters
ParameterValue
background_label0
detrendFalse
dtypeNone
high_passNone
high_variance_confoundsFalse
labels['Background', 'Frontal Pole', 'Insular Cortex', 'Superior Frontal Gyrus', 'Middle Frontal Gyrus', 'Inferior Frontal Gyrus, pars triangularis', 'Inferior Frontal Gyrus, pars opercularis', 'Precentral Gyrus', 'Temporal Pole', 'Superior Temporal Gyrus, anterior division', 'Superior Temporal Gyrus, posterior division', 'Middle Temporal Gyrus, anterior division', 'Middle Temporal Gyrus, posterior division', 'Middle Temporal Gyrus, temporooccipital part', 'Inferior Temporal Gyrus, anterior division', 'Inferior Temporal Gyrus, posterior division', 'Inferior Temporal Gyrus, temporooccipital part', 'Postcentral Gyrus', 'Superior Parietal Lobule', 'Supramarginal Gyrus, anterior division', 'Supramarginal Gyrus, posterior division', 'Angular Gyrus', 'Lateral Occipital Cortex, superior division', 'Lateral Occipital Cortex, inferior division', 'Intracalcarine Cortex', 'Frontal Medial Cortex', 'Juxtapositional Lobule Cortex (formerly Supplementary Motor Cortex)', 'Subcallosal Cortex', 'Paracingulate Gyrus', 'Cingulate Gyrus, anterior division', 'Cingulate Gyrus, posterior division', 'Precuneous Cortex', 'Cuneal Cortex', 'Frontal Orbital Cortex', 'Parahippocampal Gyrus, anterior division', 'Parahippocampal Gyrus, posterior division', 'Lingual Gyrus', 'Temporal Fusiform Cortex, anterior division', 'Temporal Fusiform Cortex, posterior division', 'Temporal Occipital Fusiform Cortex', 'Occipital Fusiform Gyrus', 'Frontal Operculum Cortex', 'Central Opercular Cortex', 'Parietal Operculum Cortex', 'Planum Polare', "Heschl's Gyrus (includes H1 and H2)", 'Planum Temporale', 'Supracalcarine Cortex', 'Occipital Pole']
labels_img<class 'nibabel.nifti1.Nifti1Image'> data shape (91, 109, 91) affine: [[ 2. 0. 0. -90.] [ 0. 2. 0. -126.] [ 0. 0. 2. -72.] [ 0. 0. 0. 1.]] metadata: <class 'nibabel.nifti1.Nifti1Header'> object, endian='<' sizeof_hdr : 348 data_type : b'' db_name : b'' extents : 0 session_error : 0 regular : b'' dim_info : 0 dim : [ 3 91 109 91 1 1 1 1] intent_p1 : 0.0 intent_p2 : 0.0 intent_p3 : 0.0 intent_code : none datatype : uint8 bitpix : 8 slice_start : 0 pixdim : [1. 2. 2. 2. 1. 1. 1. 1.] vox_offset : 0.0 scl_slope : nan scl_inter : nan slice_end : 0 slice_code : unknown xyzt_units : 0 cal_max : 0.0 cal_min : 0.0 slice_duration : 0.0 toffset : 0.0 glmax : 0 glmin : 0 descrip : b'' aux_file : b'' qform_code : unknown sform_code : aligned quatern_b : 0.0 quatern_c : 0.0 quatern_d : 0.0 qoffset_x : -90.0 qoffset_y : -126.0 qoffset_z : -72.0 srow_x : [ 2. 0. 0. -90.] srow_y : [ 0. 2. 0. -126.] srow_z : [ 0. 0. 2. -72.] intent_name : b'' magic : b'n+1'
low_passNone
mask_imgNone
memoryMemory(location=None)
memory_level1
reportsTrue
resampling_targetdata
smoothing_fwhmNone
standardizeTrue
standardize_confoundsTrue
strategymean
t_rNone
verbose0

This report was generated based on information provided at instantiation and fit time. Note that the masker can potentially perform resampling at transform time.



Fitting the mask and generating a report

masker.fit(func_filename)

# We can again generate a report, but this time, the provided functional
# image is displayed with the ROI of the atlas.
# The report also contains a summary table giving the region sizes in mm3
report = masker.generate_report()
report

NiftiLabelsMasker Class for masking of Niimg-like objects. NiftiLabelsMasker is useful when data from non-overlapping volumes should be extracted (contrarily to :class:`nilearn.maskers.NiftiMapsMasker`). Use case: Summarize brain signals from clusters that were obtained by prior K-means or Ward clustering.

image

This reports shows the regions defined by the labels of the mask.

The masker has 48 different non-overlapping regions.

Regions summary
label valueregion namesize (in mm^3)relative size (in %)
1Frontal Pole12317611.75
2Insular Cortex187281.79
3Superior Frontal Gyrus406403.88
4Middle Frontal Gyrus425284.06
5Inferior Frontal Gyrus, pars triangularis88240.84
6Inferior Frontal Gyrus, pars opercularis110721.06
7Precentral Gyrus685846.54
8Temporal Pole376883.59
9Superior Temporal Gyrus, anterior division41680.4
10Superior Temporal Gyrus, posterior division146401.4
11Middle Temporal Gyrus, anterior division67840.65
12Middle Temporal Gyrus, posterior division202001.93
13Middle Temporal Gyrus, temporooccipital part160321.53
14Inferior Temporal Gyrus, anterior division51760.49
15Inferior Temporal Gyrus, posterior division155361.48
16Inferior Temporal Gyrus, temporooccipital part117601.12
17Postcentral Gyrus551605.26
18Superior Parietal Lobule232642.22
19Supramarginal Gyrus, anterior division139361.33
20Supramarginal Gyrus, posterior division180721.72
21Angular Gyrus192721.84
22Lateral Occipital Cortex, superior division782327.46
23Lateral Occipital Cortex, inferior division327123.12
24Intracalcarine Cortex112081.07
25Frontal Medial Cortex78080.74
26Juxtapositional Lobule Cortex (formerly Supplementary Motor Cortex)118721.13
27Subcallosal Cortex91360.87
28Paracingulate Gyrus235522.25
29Cingulate Gyrus, anterior division207361.98
30Cingulate Gyrus, posterior division192961.84
31Precuneous Cortex449844.29
32Cuneal Cortex98160.94
33Frontal Orbital Cortex251842.4
34Parahippocampal Gyrus, anterior division99840.95
35Parahippocampal Gyrus, posterior division56800.54
36Lingual Gyrus270482.58
37Temporal Fusiform Cortex, anterior division48800.47
38Temporal Fusiform Cortex, posterior division127521.22
39Temporal Occipital Fusiform Cortex117521.12
40Occipital Fusiform Gyrus144481.38
41Frontal Operculum Cortex54960.52
42Central Opercular Cortex150881.44
43Parietal Operculum Cortex89520.85
44Planum Polare59920.57
45Heschl's Gyrus (includes H1 and H2)48320.46
46Planum Temporale76160.73
47Supracalcarine Cortex20880.2
48Occipital Pole422084.03
Parameters
ParameterValue
background_label0
detrendFalse
dtypeNone
high_passNone
high_variance_confoundsFalse
labels['Background', 'Frontal Pole', 'Insular Cortex', 'Superior Frontal Gyrus', 'Middle Frontal Gyrus', 'Inferior Frontal Gyrus, pars triangularis', 'Inferior Frontal Gyrus, pars opercularis', 'Precentral Gyrus', 'Temporal Pole', 'Superior Temporal Gyrus, anterior division', 'Superior Temporal Gyrus, posterior division', 'Middle Temporal Gyrus, anterior division', 'Middle Temporal Gyrus, posterior division', 'Middle Temporal Gyrus, temporooccipital part', 'Inferior Temporal Gyrus, anterior division', 'Inferior Temporal Gyrus, posterior division', 'Inferior Temporal Gyrus, temporooccipital part', 'Postcentral Gyrus', 'Superior Parietal Lobule', 'Supramarginal Gyrus, anterior division', 'Supramarginal Gyrus, posterior division', 'Angular Gyrus', 'Lateral Occipital Cortex, superior division', 'Lateral Occipital Cortex, inferior division', 'Intracalcarine Cortex', 'Frontal Medial Cortex', 'Juxtapositional Lobule Cortex (formerly Supplementary Motor Cortex)', 'Subcallosal Cortex', 'Paracingulate Gyrus', 'Cingulate Gyrus, anterior division', 'Cingulate Gyrus, posterior division', 'Precuneous Cortex', 'Cuneal Cortex', 'Frontal Orbital Cortex', 'Parahippocampal Gyrus, anterior division', 'Parahippocampal Gyrus, posterior division', 'Lingual Gyrus', 'Temporal Fusiform Cortex, anterior division', 'Temporal Fusiform Cortex, posterior division', 'Temporal Occipital Fusiform Cortex', 'Occipital Fusiform Gyrus', 'Frontal Operculum Cortex', 'Central Opercular Cortex', 'Parietal Operculum Cortex', 'Planum Polare', "Heschl's Gyrus (includes H1 and H2)", 'Planum Temporale', 'Supracalcarine Cortex', 'Occipital Pole']
labels_img<class 'nibabel.nifti1.Nifti1Image'> data shape (91, 109, 91) affine: [[ 2. 0. 0. -90.] [ 0. 2. 0. -126.] [ 0. 0. 2. -72.] [ 0. 0. 0. 1.]] metadata: <class 'nibabel.nifti1.Nifti1Header'> object, endian='<' sizeof_hdr : 348 data_type : b'' db_name : b'' extents : 0 session_error : 0 regular : b'' dim_info : 0 dim : [ 3 91 109 91 1 1 1 1] intent_p1 : 0.0 intent_p2 : 0.0 intent_p3 : 0.0 intent_code : none datatype : uint8 bitpix : 8 slice_start : 0 pixdim : [1. 2. 2. 2. 1. 1. 1. 1.] vox_offset : 0.0 scl_slope : nan scl_inter : nan slice_end : 0 slice_code : unknown xyzt_units : 0 cal_max : 0.0 cal_min : 0.0 slice_duration : 0.0 toffset : 0.0 glmax : 0 glmin : 0 descrip : b'' aux_file : b'' qform_code : unknown sform_code : aligned quatern_b : 0.0 quatern_c : 0.0 quatern_d : 0.0 qoffset_x : -90.0 qoffset_y : -126.0 qoffset_z : -72.0 srow_x : [ 2. 0. 0. -90.] srow_y : [ 0. 2. 0. -126.] srow_z : [ 0. 0. 2. -72.] intent_name : b'' magic : b'n+1'
low_passNone
mask_imgNone
memoryMemory(location=None)
memory_level1
reportsTrue
resampling_targetdata
smoothing_fwhmNone
standardizeTrue
standardize_confoundsTrue
strategymean
t_rNone
verbose0

This report was generated based on information provided at instantiation and fit time. Note that the masker can potentially perform resampling at transform time.



Process the data with the NiftiLablesMasker

In order to extract the signals, we need to call transform on the functional data

signals = masker.transform(func_filename)
# signals is a 2D matrix, (n_time_points x n_regions)
signals.shape
(168, 48)

Plot the signals

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15, 5))
ax = fig.add_subplot(111)
for label_idx in range(3):
    ax.plot(signals[:, label_idx],
            linewidth=2,
            label=atlas.labels[label_idx + 1])  # 0 is background
ax.legend(loc=2)
ax.set_title("Signals for first 3 regions")
plt.show()
Signals for first 3 regions

Total running time of the script: ( 0 minutes 13.876 seconds)

Estimated memory usage: 549 MB

Gallery generated by Sphinx-Gallery