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.MultiNiftiLabelsMasker¶
- class nilearn.maskers.MultiNiftiLabelsMasker(labels_img=None, labels=None, lut=None, background_label=0, mask_img=None, smoothing_fwhm=None, standardize=False, standardize_confounds=True, high_variance_confounds=False, detrend=False, low_pass=None, high_pass=None, t_r=None, dtype=None, resampling_target='data', memory=None, memory_level=1, verbose=0, strategy='mean', keep_masked_labels=True, reports=True, n_jobs=1, clean_args=None, **kwargs)[source]¶
Class for extracting data from multiple Niimg-like objects using labels of non-overlapping brain regions.
MultiNiftiLabelsMasker is useful when data from non-overlapping volumes and from different subjects should be extracted (contrary to
nilearn.maskers.NiftiLabelsMasker
).For more details on the definitions of labels in Nilearn, see the Extraction of signals from regions: NiftiLabelsMasker, NiftiMapsMasker section.
- Parameters:
- labels_imgNiimg-like object or None, default=None
See Input and output: neuroimaging data representation. Region definitions, as one image of labels.
- labels
list
ofstr
, optional Full labels corresponding to the labels image. This is used to improve reporting quality if provided.
Warning
The labels must be consistent with the label values provided through labels_img.
- lut
pandas.DataFrame
orstr
orpathlib.Path
to a TSV file or None, default=None Mutually exclusive with
labels
. Act as a look up table (lut) with at least columns ‘index’ and ‘name’. Formatted according to ‘dseg.tsv’ format from BIDS.- background_label
int
orfloat
, default=0 Label used in labels_img to represent background.
Warning
This value must be consistent with label values and image provided.
- mask_imgNiimg-like object, optional
See Input and output: neuroimaging data representation. Mask to apply to regions before extracting signals.
- smoothing_fwhm
float
orint
or 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.
- standardize{‘zscore_sample’, ‘zscore’, ‘psc’, True, False}, 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.std
with N -ddof=0
.'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.False
: Do not standardize the data.
- 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.
- high_variance_confounds
bool
, default=False If True, high variance confounds are computed on provided image with
nilearn.image.high_variance_confounds
and default parameters and regressed out.- detrend
bool
, optional Whether to detrend signals or not.
- low_pass
float
orint
or 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
float
orint
or None, default=None High cutoff frequency in Hertz. If specified, signals below this frequency will be filtered out.
- t_r
float
orint
or None, default=None Repetition time, in seconds (sampling period). Set to None if not provided.
- 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.
- resampling_target{“data”, “labels”, None}, default=”data”
Gives which image gives the final shape/size:
“data” means the atlas is resampled to the shape of the data if needed
“labels” means en mask_img and images provided to fit() are resampled to the shape and affine of maps_img
None means no resampling: if shapes and affines do not match, a ValueError is raised
- memoryNone, instance of
joblib.Memory
,str
, orpathlib.Path
Used to cache the masking process. By default, no caching is done. If a
str
is given, it is the path to the caching directory.- memory_level
int
, default=1 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).
- strategy
str
, default=”mean” The name of a valid function to reduce the region with. Must be one of: sum, mean, median, minimum, maximum, variance, standard_deviation.
- keep_masked_labels
bool
, default=True When a mask is supplied through the “mask_img” parameter, some atlas regions may lie entirely outside of the brain mask, resulting in empty time series for those regions. If True, the masked atlas with these empty labels will be retained in the output, resulting in corresponding time series containing zeros only. If False, the empty labels will be removed from the output, ensuring no empty time series are present.
Deprecated since version 0.10.2: The ‘True’ option for
keep_masked_labels
is deprecated. The default value will change to ‘False’ in 0.13, and thekeep_masked_labels
parameter will be removed in 0.15.- reports
bool
, default=True If set to True, data is saved in order to produce a report.
- clean_args
dict
or None, default=None Keyword arguments to be passed to
clean
called within the masker. Withinclean
, kwargs prefixed with'butterworth__'
will be passed to the Butterworth filter.- kwargsdict
Keyword arguments to be passed to functions called within the masker. Kwargs prefixed with ‘clean__’ will be passed to
clean
. Withinclean
, kwargs prefixed with ‘butterworth__’ will be passed to the Butterworth filter (i.e., clean__butterworth__).Deprecated since version 0.11.2dev.
Use
clean_args
instead!It is recommended to pass parameters to use for data cleaning via
dict
to theclean_args
parameter.Passing parameters via “kwargs” is mutually exclusive with passing cleaning parameters via
clean_args
.
- Attributes:
- mask_img_A 3D binary
nibabel.nifti1.Nifti1Image
or None. The mask of the data. If no
mask_img
was passed at masker construction, thenmask_img_
isNone
, otherwise is the resulting binarized version ofmask_img
where each voxel isTrue
if all values across samples (for example across timepoints) is finite value different from 0.- labels_img_
nibabel.nifti1.Nifti1Image
The labels image.
- mask_img_A 3D binary
- __init__(labels_img=None, labels=None, lut=None, background_label=0, mask_img=None, smoothing_fwhm=None, standardize=False, standardize_confounds=True, high_variance_confounds=False, detrend=False, low_pass=None, high_pass=None, t_r=None, dtype=None, resampling_target='data', memory=None, memory_level=1, verbose=0, strategy='mean', keep_masked_labels=True, reports=True, n_jobs=1, clean_args=None, **kwargs)[source]¶
- fit(imgs=None, y=None)[source]¶
Prepare signal extraction from regions.
- Parameters:
- imgs
list
of Niimg-like objects or None, default=None See Input and output: neuroimaging data representation. Image data passed to the reporter.
- yNone
This parameter is unused. It is solely included for scikit-learn compatibility.
- imgs
- fit_transform(imgs, y=None, confounds=None, sample_mask=None)[source]¶
Fit to data, then transform it.
- Parameters:
- imgsNiimg-like object, or a
list
of Niimg-like 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
list
of 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
list
of 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 version 0.8.0.
- imgsNiimg-like object, or a
- Returns:
- signals
list
ofnumpy.ndarray
ornumpy.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_metadata_routing()¶
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating 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(signals)[source]¶
Compute voxel signals from region signals.
Any mask given at initialization is taken into account.
Changed in version 0.9.2: This method now supports 1D arrays, which will produce 3D images.
- 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.Nifti1Image
will be returned.2D array : 4D
nibabel.nifti1.Nifti1Image
will be returned.
- img
- property labels_¶
Return list of labels of the regions.
- property n_elements_¶
Return number of regions.
This is equal to the number of unique values in the fitted label image, minus the background value.
Added in version 0.9.2.
- property region_ids_¶
Return dictionary containing the region ids corresponding
to each column in the array
returned by transform.
The region id corresponding to
region_signal[:,i]
isregion_ids_[i]
.region_ids_['background']
is the background label.Added in version 0.10.3.
- property region_names_¶
Return a dictionary containing the region names corresponding
to each column in the array returned by transform.
The region names correspond to the labels provided in labels in input. The region name corresponding to
region_signal[:,i]
isregion_names_[i]
.Added in version 0.10.3.
- set_fit_request(*, imgs='$UNCHANGED$')¶
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- imgsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
imgs
parameter infit
.
- Returns:
- selfobject
The updated object.
- set_inverse_transform_request(*, signals='$UNCHANGED$')¶
Request metadata passed to the
inverse_transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toinverse_transform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toinverse_transform
.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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- signalsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
signals
parameter ininverse_transform
.
- Returns:
- selfobject
The updated object.
- 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$')¶
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- confoundsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
confounds
parameter intransform
.- imgsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
imgs
parameter intransform
.- sample_maskstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_mask
parameter intransform
.
- Returns:
- selfobject
The updated object.
- transform(imgs, confounds=None, sample_mask=None)[source]¶
Apply mask, spatial and temporal preprocessing.
- Parameters:
- imgsNiimg-like object, or a
list
of Niimg-like objects See Input and output: neuroimaging data representation. Data to be preprocessed
- confounds
list
of 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
list
of 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
.
- imgsNiimg-like object, or a
- Returns:
- signals
list
ofnumpy.ndarray
ornumpy.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, n_jobs=1, sample_mask=None)[source]¶
Extract signals from a list of 4D niimgs.
- Parameters:
- imgs
list
of Niimg-like objects See Input and output: neuroimaging data representation. Images to process.
- confounds
list
of 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.- n_jobs
int
, default=1 The number of CPUs to use to do the computation. -1 means ‘all CPUs’.
- sample_mask
list
of 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
.
- imgs
- Returns:
- signals
list
ofnumpy.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)[source]¶
Extract signals from a single 4D niimg.
- Parameters:
- imgs3D/4D Niimg-like object
See Input and output: neuroimaging data representation. Images to process.
- confounds
numpy.ndarray
,str
,pathlib.Path
,pandas.DataFrame
orlist
of 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
.Added in version 0.8.0.
- Returns:
- signals
numpy.ndarray
Signal for each voxel. Output shape for :
3D images: (number of elements,) array
4D images: (number of scans, number of elements) array
- signals
Examples using nilearn.maskers.MultiNiftiLabelsMasker
¶

Comparing connectomes on different reference atlases