Giving credit

Table Of Contents

Previous topic

7.5.8. nilearn.image.load_img

Next topic

7.5.10. nilearn.image.mean_img

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.

7.5.9. nilearn.image.math_img

nilearn.image.math_img(formula, **imgs)

Interpret a numpy based string formula using niimg in named parameters.

New in version 0.2.3.

Parameters:

formula: str

The mathematical formula to apply to image internal data. It can use numpy imported as ‘np’.

imgs: images (Nifti1Image or file names)

Keyword arguments corresponding to the variables in the formula as Nifti images. All input images should have the same geometry (shape, affine).

Returns:

return_img: Nifti1Image

Result of the formula as a Nifti image. Note that the dimension of the result image can be smaller than the input image. The affine is the same as the input image.

See also

nilearn.image.mean_img
To simply compute the mean of multiple images

Notes

This function is the Python equivalent of ImCal in SPM or fslmaths in FSL.

Examples

Let’s load an image using nilearn datasets module:

>>> from nilearn import datasets
>>> anatomical_image = datasets.load_mni152_template()

Now we can use any numpy function on this image:

>>> from nilearn.image import math_img
>>> log_img = math_img("np.log(img)", img=anatomical_image)

We can also apply mathematical operations on several images:

>>> result_img = math_img("img1 + img2",
...                       img1=anatomical_image, img2=log_img)

7.5.9.1. Examples using nilearn.image.math_img