Note

This page is a reference documentation. It only explains the function signature, and not how to use it. Please refer to the user guide for the big picture.

index_img

nilearn.image.index_img(imgs, index)[source]

Index into an image in the last dimension.

Common use cases include extracting an image out of img or creating a 4D (or 2D for surface) image whose data is a subset of img data.

Parameters:
imgs4D Niimg-like object or 2D SurfaceImage

See Input and output: neuroimaging data representation.

indexAny type compatible with numpy array indexing

Used for indexing the data array in the last dimension.

Returns:
Nifti1Image or SurfaceImage

Indexed image.

Examples

>>> # First we concatenate two MNI152 images to create a 4D-image.
>>> from nilearn import datasets
>>> from nilearn.image import concat_imgs, index_img
>>> joint_mni_image = concat_imgs(
...     [datasets.load_mni152_template(), datasets.load_mni152_template()]
... )
>>> print(joint_mni_image.shape)
(197, 233, 189, 2)
>>>
>>> # We can now select one slice from the last dimension of this 4D-image.
>>> single_mni_image = index_img(joint_mni_image, 1)
>>> print(single_mni_image.shape)
(197, 233, 189)
>>>
>>> # We can also select multiple frames using the `slice` constructor.
>>> five_mni_images = concat_imgs([datasets.load_mni152_template()] * 5)
>>> five_mni_images.shape
(197, 233, 189, 5)
>>> first_three_images = index_img(five_mni_images, slice(0, 3))
>>> first_three_images.shape
(197, 233, 189, 3)

Examples using nilearn.image.index_img

3D and 4D niimgs: handling and visualizing

3D and 4D niimgs: handling and visualizing

An introduction tutorial to fMRI decoding

An introduction tutorial to fMRI decoding

Visualizing a probabilistic atlas

Visualizing a probabilistic atlas

Comparing classifiers in decoding

Comparing classifiers in decoding

Cortical surface-based searchlight decoding

Cortical surface-based searchlight decoding

Decoding of a dataset after GLM fit for signal extraction

Decoding of a dataset after GLM fit for signal extraction

Decoding with ANOVA + SVM

Decoding with ANOVA + SVM

Decoding with fast ensembling of regularized models

Decoding with fast ensembling of regularized models

Effect of standardization on decoding performance

Effect of standardization on decoding performance

ROI-based decoding analysis

ROI-based decoding analysis

Searchlight analysis

Searchlight analysis

Tuning a parameter with cross-validation

Tuning a parameter with cross-validation

Understanding Decoder

Understanding Decoder

Clustering methods to learn a brain parcellation from fMRI

Clustering methods to learn a brain parcellation from fMRI

Regions extraction using dictionary learning and functional connectomes

Regions extraction using dictionary learning and functional connectomes

Regions Extraction of Default Mode Networks

Regions Extraction of Default Mode Networks

Simple example of NiftiMasker use

Simple example of NiftiMasker use

Understanding NiftiMasker and mask computation

Understanding NiftiMasker and mask computation

Advanced decoding using scikit-learn

Advanced decoding using scikit-learn

Independent Component Analysis (ICA) of fMRI timeseries

Independent Component Analysis (ICA) of fMRI timeseries

Massively univariate analysis of a visual task

Massively univariate analysis of a visual task

SearchLight with a custom correlation estimator

SearchLight with a custom correlation estimator