Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

mask

mask

Utilities for creating and manipulating brain masks.

Methods:

NameDescription
collapse_maskCollapse separate masks into one integer-labeled mask.
create_sphereGenerate spheres in brain-mask space.
expand_maskExpand an integer-labeled mask into separate binary masks.
roi_to_brainPopulate an expanded binary ROI mask with a vector or matrix of per-ROI values.
roi_to_brain_from_atlasPaint per-parcel values onto voxel space using a labeled atlas.

Methods

collapse_mask

collapse_mask(mask, auto_label = True, custom_mask = None)

Collapse separate masks into one integer-labeled mask.

Overlapping areas are ignored.

Parameters:

NameTypeDescriptionDefault
masknibabel or BrainData instance holding 2+ separate masks (stacked along the first axis).required
auto_labelIf True (default), label the collapsed regions with sequential integers (1, 2, 3, …) in mask order. If False, keep each mask’s own values as its label.True
custom_masknibabel instance or string to file path; optional.None

Returns:

NameTypeDescription
outBrainData instance of a mask with different integers indicating different masks.

create_sphere

create_sphere(coordinates, radius = 5, mask = None)

Generate spheres in brain-mask space.

Parameters:

NameTypeDescriptionDefault
coordinatesa vector of sphere centers of the form [px, py, pz] or [[px1, py1, pz1], ..., [pxn, pyn, pzn]]required
radiusradius of the sphere(s). A scalar creates one sphere per center; a vector creates multiple spheres if len(radius) > 15
maskNifti1Image (or path to a mask file) defining the brain space. Defaults to the package brain-space mask when None.None

Returns:

NameTypeDescription
Nifti1ImageA binary image with the requested spheres in mask space.

expand_mask

expand_mask(mask, custom_mask = None)

Expand an integer-labeled mask into separate binary masks.

Parameters:

NameTypeDescriptionDefault
masknibabel or BrainData instancerequired
custom_masknibabel instance or string to file path; optionalNone

Returns:

NameTypeDescription
outBrainData instance of multiple binary masks

roi_to_brain

roi_to_brain(data, mask_x)

Populate an expanded binary ROI mask with a vector or matrix of per-ROI values.

Accepts lists, numpy arrays, polars DataFrame/Series, or pandas DataFrame/Series. Internally coerces to a numpy array and operates on it — 1-D input produces a single BrainData image; 2-D input (ROIs by observations) produces a stack of BrainData images, one per observation.

Parameters:

NameTypeDescriptionDefault
dataROI values. 1-D length must equal len(mask_x); 2-D shape must be (n_rois, n_obs) or (n_obs, n_rois).required
mask_xAn expanded binary mask (BrainData) with one row per ROI.required

Returns:

NameTypeDescription
BrainDataA BrainData instance with each ROI populated by the
provided value(s).

roi_to_brain_from_atlas

roi_to_brain_from_atlas(values, atlas, source_mask, *, roi_labels = None, fill: float = np.nan)

Paint per-parcel values onto voxel space using a labeled atlas.

Sibling of roi_to_brain, but accepts a labeled atlas (one integer label per voxel — the form carried by SpatialScale), not an expanded mask with one binary row per ROI. Voxels whose atlas label is not in roi_labels (or whose label is 0) receive fill.

Parameters:

NameTypeDescriptionDefault
valuesPer-parcel scalars, either 1-D (n_parcels,) for a single image or 2-D (n_images, n_parcels) for a stack of images. The trailing (parcel) axis must match len(roi_labels) (or the number of unique non-zero atlas labels when roi_labels is None).required
atlasLabeled image — BrainData, Nifti1Image, or path-like. Resampled to source_mask (nearest-neighbor) if shapes/affines differ.required
source_maskNifti1Image (or path) defining the output voxel grid. The returned BrainData is masked to this image.required
roi_labelsInteger atlas IDs in the same order as values. If None, defaults to np.unique of the atlas with 0 stripped (sorted ascending).None
fillfloatValue for voxels not in any provided ROI. Default np.nan.nan

Returns:

NameTypeDescription
BrainDataMasked to source_mask, with each in-atlas voxel set to its
parcel’s scalar from values. Holds a single image when values is
1-D, or n_images images when values is 2-D (n_images, n_parcels).

Examples:

>>> from nltools.mask import roi_to_brain_from_atlas
>>> brain_map = roi_to_brain_from_atlas(
...     values=accuracies,
...     atlas=atlas_img,
...     source_mask=brain_mask,
...     roi_labels=[1, 2, 3],
... )