nltools.mask¶
Building masks and moving values between masks and brain images.
Utilities for creating and manipulating brain masks.
Functions:
| Name | Description |
|---|---|
create_sphere |
Generate binary spheres in the space of a brain mask. |
expand_mask |
Expand an integer-labeled mask into separate binary masks. |
collapse_mask |
Collapse separate masks into one integer-labeled mask. |
roi_to_brain |
Populate an expanded binary ROI mask with a vector or matrix of per-ROI values. |
roi_to_brain_from_atlas |
Paint per-parcel values onto voxel space using a labeled atlas. |
Functions:¶
create_sphere ¶
Generate binary spheres in the space of a brain mask.
Spheres are drawn with nilearn.maskers.NiftiSpheresMasker, so centers are
world (MNI) millimeter coordinates and the radius is in millimeters — the same
convention as nilearn's SearchLight and NiftiSpheresMasker. The result is
resolution-independent: the same request covers the same physical volume on a
1 mm, 2 mm, or 3 mm grid, up to voxel quantization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
list
|
Sphere center |
required |
radius
|
int | float | list
|
Radius of the sphere(s) in millimeters. A scalar applies to every center; a list gives one radius per center. |
5
|
mask
|
Nifti1Image | str
|
Image (or path) defining the brain space. Defaults to the package brain-space mask. |
None
|
Returns:
| Type | Description |
|---|---|
Nifti1Image
|
A binary image with the requested spheres in mask space. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
expand_mask ¶
Expand an integer-labeled mask into separate binary masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Nifti1Image | BrainData
|
Integer-labeled mask. |
required |
custom_mask
|
Nifti1Image | str
|
Brain mask (or path) used
when converting a nibabel |
None
|
Returns:
| Type | Description |
|---|---|
BrainData
|
One binary mask per unique non-zero label. |
collapse_mask ¶
Collapse separate masks into one integer-labeled mask.
Overlapping areas are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Nifti1Image | BrainData
|
Two or more separate masks stacked along the first axis. |
required |
auto_label
|
bool
|
If 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_mask
|
Nifti1Image | str
|
Brain mask (or path) used
when converting a nibabel |
None
|
Returns:
| Type | Description |
|---|---|
BrainData
|
A single mask whose integer values identify the source masks. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
roi_to_brain ¶
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:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list | ndarray | DataFrame | Series | DataFrame | Series
|
ROI values. 1-D length must equal |
required |
mask_x
|
BrainData
|
An expanded binary mask with one row per ROI. |
required |
Returns:
| Type | Description |
|---|---|
BrainData
|
A BrainData instance with each ROI populated by the provided value(s). |
roi_to_brain_from_atlas ¶
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), 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:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Per-parcel scalars, either 1-D |
required |
atlas
|
BrainData | Nifti1Image | str | Path
|
Labeled image.
Resampled to |
required |
source_mask
|
Nifti1Image | str | Path
|
Image (or path) defining the
output voxel grid. The returned |
required |
roi_labels
|
array - like
|
Integer atlas IDs in the same order as
|
None
|
fill
|
float
|
Value for voxels not in any provided ROI. Default |
nan
|
Returns:
| Type | Description |
|---|---|
BrainData
|
Masked to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples: