Note
This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.
nilearn.maskers.MultiNiftiMasker¶
- class nilearn.maskers.MultiNiftiMasker(mask_img=None, runs=None, smoothing_fwhm=None, standardize=False, standardize_confounds=True, detrend=False, high_variance_confounds=False, low_pass=None, high_pass=None, t_r=None, target_affine=None, target_shape=None, mask_strategy='background', mask_args=None, dtype=None, memory=None, memory_level=0, n_jobs=1, verbose=0, reports=True, cmap='gray', clean_args=None)[source]¶
Applying a mask to extract time-series from multiple Niimg-like objects.
MultiNiftiMasker is useful when dealing with image sets from multiple subjects.
Use case: integrates well with decomposition by MultiPCA and CanICA (multi-subject models)
- Parameters:
- mask_imgNiimg-like object
See Input and output: neuroimaging data representation. Mask of the data. If not given, a mask is computed in the fit step. Optional parameters can be set using mask_args and mask_strategy to fine tune the mask extraction.
- runs
numpy.ndarray, optional Add a run level to the preprocessing. Each run will be detrended independently. Must be a 1D array of n_samples elements.
- smoothing_fwhm
floatorintor None, optional. If smoothing_fwhm is not None, it gives the full-width at half maximum in millimeters of the spatial smoothing to apply to the signal.
- standardizeany of: ‘zscore_sample’, ‘zscore’, ‘psc’, True, False or None; default=False
Strategy to standardize the signal:
'zscore_sample': The signal is z-scored. Timeseries are shifted to zero mean and scaled to unit variance. Uses sample std.'zscore': The signal is z-scored. Timeseries are shifted to zero mean and scaled to unit variance. Uses population std by calling defaultnumpy.stdwith N -ddof=0.Deprecated since Nilearn 0.10.1: This option will be removed in Nilearn version 0.14.0. Use
zscore_sampleinstead.'psc': Timeseries are shifted to zero mean value and scaled to percent signal change (as compared to original mean signal).True: The signal is z-scored (same as option zscore). Timeseries are shifted to zero mean and scaled to unit variance.Deprecated since Nilearn 0.13.0dev: In nilearn version 0.15.0,
Truewill be replaced by'zscore_sample'.False: Do not standardize the data.Deprecated since Nilearn 0.13.0dev: In nilearn version 0.15.0,
Falsewill be replaced byNone.
Deprecated since Nilearn 0.15.0dev: The default will be changed to
Nonein version 0.15.0.- standardize_confounds
bool, default=True If set to True, the confounds are z-scored: their mean is put to 0 and their variance to 1 in the time dimension.
- detrend
bool, optional Whether to detrend signals or not.
- high_variance_confounds
bool, default=False If True, high variance confounds are computed on provided image with
nilearn.image.high_variance_confoundsand default parameters and regressed out.- low_pass
floatorintor None, default=None Low cutoff frequency in Hertz. If specified, signals above this frequency will be filtered out. If None, no low-pass filtering will be performed.
- high_pass
floatorintor None, default=None High cutoff frequency in Hertz. If specified, signals below this frequency will be filtered out.
- t_r
floatorintor None, default=None Repetition time, in seconds (sampling period). Set to None if not provided.
- target_affine3x3 or a 4x4 array-like, or None, default=None
If specified, the image is resampled corresponding to this new affine.
Note
This parameter is passed to
nilearn.image.resample_img.- target_shape
tupleorlistor None, default=None If specified, the image will be resized to match this new shape. len(target_shape) must be equal to 3.
Note
If target_shape is specified, a target_affine of shape (4, 4) must also be given.
Note
This parameter is passed to
nilearn.image.resample_img.- mask_strategy{“background”, “epi”, “whole-brain-template”,”gm-template”, “wm-template”}, optional
The strategy used to compute the mask:
"background": Use this option if your images present a clear homogeneous background. Usesnilearn.masking.compute_background_maskunder the hood."epi": Use this option if your images are raw EPI images. Usesnilearn.masking.compute_epi_mask."whole-brain-template": This will extract the whole-brain part of your data by resampling the MNI152 brain mask for your data’s field of view. Usesnilearn.masking.compute_brain_maskwithmask_type="whole-brain".Note
This option is equivalent to the previous ‘template’ option which is now deprecated.
"gm-template": This will extract the gray matter part of your data by resampling the corresponding MNI152 template for your data’s field of view. Usesnilearn.masking.compute_brain_maskwithmask_type="gm".Added in Nilearn 0.8.1.
"wm-template": This will extract the white matter part of your data by resampling the corresponding MNI152 template for your data’s field of view. Usesnilearn.masking.compute_brain_maskwithmask_type="wm".Added in Nilearn 0.8.1.
Note
Depending on this value, the mask will be computed from
nilearn.masking.compute_multi_background_mask,nilearn.masking.compute_multi_epi_mask, ornilearn.masking.compute_multi_brain_mask.Default=’background’.
- mask_args
dict, optional If mask is None, these are additional parameters passed to
nilearn.masking.compute_background_mask, ornilearn.masking.compute_epi_maskto fine-tune mask computation. Please see the related documentation for details.- dtypedtype like, “auto” or None, default=None
Data type toward which the data should be converted. If “auto”, the data will be converted to int32 if dtype is discrete and float32 if it is continuous.
- memoryNone, instance of
joblib.Memory,str, orpathlib.Path Used to cache the masking process. By default, no caching is done. If a
stris given, it is the path to the caching directory.- memory_level
int, default=0 Rough estimator of the amount of memory used by caching. Higher value means more memory for caching. Zero means no caching.
- n_jobs
int, default=1 The number of CPUs to use to do the computation. -1 means ‘all CPUs’.
- verbose
int, default=0 Verbosity level (0 means no message).
- reports
bool, default=True If set to True, data is saved in order to produce a report.
- cmap
matplotlib.colors.Colormap, orstr, optional The colormap to use. Either a string which is a name of a matplotlib colormap, or a matplotlib colormap object. default=”gray” Only relevant for the report figures.
- clean_args
dictor None, default=None Keyword arguments to be passed to
cleancalled within the masker. Withinclean, kwargs prefixed with'butterworth__'will be passed to the Butterworth filter.
- Attributes:
- affine_4x4
numpy.ndarray Affine of the transformed image.
- clean_args_
dict Keyword arguments to be passed to
cleancalled within the masker. Withinclean, kwargs prefixed with'butterworth__'will be passed to the Butterworth filter.- mask_img_A 3D binary
nibabel.nifti1.Nifti1Image The mask of the data, or the one computed from
imgspassed to fit. If amask_imgis passed at masker construction, thenmask_img_is the resulting binarized version of it where each voxel isTrueif all values across samples (for example across timepoints) is finite value different from 0.- memory_joblib memory cache
- n_elements_
int The number of voxels in the mask.
Added in Nilearn 0.9.2.
- affine_4x4
See also
nilearn.image.resample_imgimage resampling
nilearn.masking.compute_epi_maskmask computation
nilearn.masking.apply_maskmask application on image
nilearn.signal.cleanconfounds removal and general filtering of signals
- __init__(mask_img=None, runs=None, smoothing_fwhm=None, standardize=False, standardize_confounds=True, detrend=False, high_variance_confounds=False, low_pass=None, high_pass=None, t_r=None, target_affine=None, target_shape=None, mask_strategy='background', mask_args=None, dtype=None, memory=None, memory_level=0, n_jobs=1, verbose=0, reports=True, cmap='gray', clean_args=None)[source]¶
- fit(imgs=None, y=None)[source]¶
Compute the mask corresponding to the data.
- Parameters:
- imgsNiimg-like objects,
listof Niimg-like objects or None, default=None See Input and output: neuroimaging data representation. Data on which the mask must be calculated. If this is a list, the affine is considered the same for all.
- yNone
This parameter is unused. It is solely included for scikit-learn compatibility.
- imgsNiimg-like objects,
- fit_transform(imgs, y=None, confounds=None, sample_mask=None)[source]¶
Fit to data, then transform it.
- Parameters:
- imgsImage object, or a
listof Image objects See Input and output: neuroimaging data representation. Data to be preprocessed
- yNone
This parameter is unused. It is solely included for scikit-learn compatibility.
- confounds
listof confounds, default=None List of confounds (arrays, dataframes, str or path of files loadable into an array). As confounds are passed to
nilearn.signal.clean, please see the related documentation for details about accepted types. Must be of same length than imgs.- sample_mask
listof sample_mask, default=None List of sample_mask (any type compatible with numpy-array indexing) to use for scrubbing outliers. Must be of same length as
imgs.shape = (total number of scans - number of scans removed)for explicit index (for example,sample_mask=np.asarray([1, 2, 4])), orshape = (number of scans)for binary mask (for example,sample_mask=np.asarray([False, True, True, False, True])). Masks the images along the last dimension to perform scrubbing: for example to remove volumes with high motion and/or non-steady-state volumes. This parameter is passed tonilearn.signal.clean.Added in Nilearn 0.8.0.
- imgsImage object, or a
- Returns:
- signals
listofnumpy.ndarrayornumpy.ndarray Signal for each voxel. Output shape for :
3D images: (number of elements,) array
4D images: (number of scans, number of elements) array
list of 3D images: list of (number of elements,) array
list of 4D images: list of (number of scans, number of elements) array
- signals
- get_feature_names_out(input_features=None)¶
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: [“class_name0”, “class_name1”, “class_name2”].
- Parameters:
- input_featuresarray-like of str or None, default=None
Only used to validate feature names with the names seen in fit.
- Returns:
- feature_names_outndarray of str objects
Transformed feature names.
- get_metadata_routing()¶
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)¶
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- inverse_transform(X)[source]¶
Transform the data matrix back to an image in brain space.
This step only performs spatial unmasking, without inverting any additional processing performed by
transform, such as temporal filtering or smoothing.- Parameters:
- signals1D/2D
numpy.ndarray Extracted signal. If a 1D array is provided, then the shape should be (number of elements,). If a 2D array is provided, then the shape should be (number of scans, number of elements).
- signals1D/2D
- Returns:
- img
nibabel.nifti1.Nifti1Image Transformed image in brain space. Output shape for :
1D array : 3D
nibabel.nifti1.Nifti1Imagewill be returned.2D array : 4D
nibabel.nifti1.Nifti1Imagewill be returned.
- img
- set_fit_request(*, imgs='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- imgsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
imgsparameter infit.
- Returns:
- selfobject
The updated object.
- set_output(*, transform=None)[source]¶
Set the output container when
"transform"is called.Warning
This has not been implemented yet.
- set_params(**params)¶
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_transform_request(*, confounds='$UNCHANGED$', imgs='$UNCHANGED$', sample_mask='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
transformmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- confoundsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
confoundsparameter intransform.- imgsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
imgsparameter intransform.- sample_maskstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_maskparameter intransform.
- Returns:
- selfobject
The updated object.
- transform(imgs, confounds=None, sample_mask=None)[source]¶
Apply mask, spatial and temporal preprocessing.
- Parameters:
- imgs :Image object, or a :obj:`list` of Image objects
See Input and output: neuroimaging data representation. Data to be preprocessed
- confounds
listof confounds, default=None List of confounds (arrays, dataframes, str or path of files loadable into an array). As confounds are passed to
nilearn.signal.clean, please see the related documentation for details about accepted types. Must be of same length than imgs.- sample_mask
listof sample_mask, default=None List of sample_mask (any type compatible with numpy-array indexing) to use for scrubbing outliers. Must be of same length as
imgs.shape = (total number of scans - number of scans removed)for explicit index (for example,sample_mask=np.asarray([1, 2, 4])), orshape = (number of scans)for binary mask (for example,sample_mask=np.asarray([False, True, True, False, True])). Masks the images along the last dimension to perform scrubbing: for example to remove volumes with high motion and/or non-steady-state volumes. This parameter is passed tonilearn.signal.clean.
- Returns:
- signals
listofnumpy.ndarrayornumpy.ndarray Signal for each voxel. Output shape for :
3D images: (number of elements,) array
4D images: (number of scans, number of elements) array
list of 3D images: list of (number of elements,) array
list of 4D images: list of (number of scans, number of elements) array
- signals
- transform_imgs(imgs_list, confounds=None, sample_mask=None, copy=True, n_jobs=1)[source]¶
Prepare multi subject data in parallel.
- Parameters:
- imgs
listof Niimg-like objects See Input and output: neuroimaging data representation. Images to process.
- confounds
listof confounds, default=None List of confounds (arrays, dataframes, str or path of files loadable into an array). As confounds are passed to
nilearn.signal.clean, please see the related documentation for details about accepted types. Must be of same length than imgs.- sample_mask
listof sample_mask, default=None List of sample_mask (any type compatible with numpy-array indexing) to use for scrubbing outliers. Must be of same length as
imgs.shape = (total number of scans - number of scans removed)for explicit index (for example,sample_mask=np.asarray([1, 2, 4])), orshape = (number of scans)for binary mask (for example,sample_mask=np.asarray([False, True, True, False, True])). Masks the images along the last dimension to perform scrubbing: for example to remove volumes with high motion and/or non-steady-state volumes. This parameter is passed tonilearn.signal.clean.Added in Nilearn 0.8.0.
- copy
bool, default=True If True, guarantees that output array has no memory in common with input array.
- n_jobs
int, default=1 The number of CPUs to use to do the computation. -1 means ‘all CPUs’.
- imgs
- Returns:
- signals
listofnumpy.ndarray Signal for each voxel. Output shape for :
list of 3D images: list of (number of elements,) array
list of 4D images: list of (number of scans, number of elements) array
- signals
- transform_single_imgs(imgs, confounds=None, sample_mask=None, copy=True)[source]¶
Apply mask, spatial and temporal preprocessing.
- Parameters:
- imgs3D/4D Niimg-like object
See Input and output: neuroimaging data representation. Images to process.
- confounds
numpy.ndarray,str,pathlib.Path,pandas.DataFrameorlistof confounds timeseries, default=None This parameter is passed to
nilearn.signal.clean. Please see the related documentation for details. shape: (number of scans, number of confounds)- sample_maskAny type compatible with numpy-array indexing, default=None
shape = (total number of scans - number of scans removed)for explicit index (for example,sample_mask=np.asarray([1, 2, 4])), orshape = (number of scans)for binary mask (for example,sample_mask=np.asarray([False, True, True, False, True])). Masks the images along the last dimension to perform scrubbing: for example to remove volumes with high motion and/or non-steady-state volumes. This parameter is passed tonilearn.signal.clean.- copy
bool, default=True Indicates whether a copy is returned or not.
- Returns:
- signals
numpy.ndarray,pandas.DataFrameor polars.DataFrame Signal for each element.
Changed in Nilearn 0.13.0dev: Added
set_outputsupport.The type of the output is determined by
set_output(): see the scikit-learn documentation.Output shape for :
For Numpy outputs:
3D images: (number of elements,)
4D images: (number of scans, number of elements) array
For DataFrame outputs:
3D or 4D images: (number of scans, number of elements) array
- signals
Examples using nilearn.maskers.MultiNiftiMasker¶
Encoding models for visual stimuli from Miyawaki et al. 2008
Reconstruction of visual stimuli from Miyawaki et al. 2008