Skip to content

confusius.atlas

atlas

Brain atlas integration via BrainGlobe.

Modules:

  • atlas

    Atlas class for brain atlas integration via BrainGlobe.

Classes:

  • Atlas

    Brain atlas wrapper backed by BrainGlobe, exposing DataArrays.

Atlas

Brain atlas wrapper backed by BrainGlobe, exposing DataArrays.

Use from_brainglobe to construct an instance.

Parameters:

  • dataset

    (Dataset) –

    Dataset with variables reference, annotation, and hemispheres on a common (z, y, x) physical grid. Atlases constructed with from_brainglobe currently use millimeters, but Atlas itself only requires that units stay internally consistent.

  • structures

    (StructuresDict) –

    BrainGlobe structure dictionary (carries the treelib hierarchy tree).

  • mesh_vertex_transform

    ((4, 4) numpy.ndarray or xarray.DataArray) –

    Pull transform that maps the atlas' current physical coordinates back to the base atlas physical coordinates.

  • rl_midline

    (float, default: 0.0 ) –

    Midpoint of the RL axis in the base atlas physical units. Used to clip mesh vertices to a single hemisphere before applying any mesh vertex transform.

Attributes:

  • reference (DataArray) –

    Reference template DataArray.

  • annotation (DataArray) –

    Region annotations DataArray with integer labels on the same physical grid.

  • hemispheres (DataArray) –

    Hemisphere map DataArray (1 = left, 2 = right) on the same physical grid.

  • lookup (DataFrame) –

    DataFrame with columns acronym, name, rgb_triplet indexed by structure index.

  • cmap (ListedColormap) –

    ListedColormap derived from annotation.attrs["rgb_lookup"].

  • norm (BoundaryNorm) –

    BoundaryNorm derived from annotation.attrs["rgb_lookup"].

Methods:

  • ancestors

    Return the ancestor nodes of region, from root down (exclusive).

  • from_brainglobe

    Construct an Atlas from a BrainGlobe atlas name or instance.

  • get_masks

    Return integer region masks stacked along a masks dimension.

  • get_mesh

    Return vertex coordinates and face indices for a region's mesh.

  • resample

    Resample the atlas onto an explicit output grid.

  • resample_like

    Resample the atlas onto the grid of reference.

  • search

    Search structures by name or acronym.

  • show_tree

    Print the structure hierarchy tree.

annotation property

annotation: DataArray

Region annotations DataArray.

attrs["rgb_lookup"] carries a {id: [r, g, b]} dict used for colormap construction.

Returns:

  • DataArray

    The region annotation DataArray with integer labels.

cmap property

ListedColormap derived from annotation.attrs["rgb_lookup"].

Returns:

hemispheres property

hemispheres: DataArray

Hemisphere map DataArray (1 = left, 2 = right).

Returns:

  • DataArray

    The hemisphere map DataArray.

lookup property

lookup: DataFrame

DataFrame with columns acronym, name, rgb_triplet.

The DataFrame is indexed by structure index.

Returns:

  • DataFrame

    The structure lookup DataFrame, built from the BrainGlobe atlas's StructuresDict. Cached on first access.

norm property

BoundaryNorm derived from annotation.attrs["rgb_lookup"].

Returns:

reference property

reference: DataArray

Reference template DataArray.

Returns:

  • DataArray

    The reference template DataArray.

ancestors

ancestors(region: int | str) -> list[Node]

Return the ancestor nodes of region, from root down (exclusive).

Parameters:

  • region
    (int or str) –

    Structure index or acronym.

Returns:

  • list[Node]

    Ancestor nodes ordered from root toward region, not including region itself.

from_brainglobe classmethod

from_brainglobe(
    atlas: str | BrainGlobeAtlas, **kwargs: object
) -> Atlas

Construct an Atlas from a BrainGlobe atlas name or instance.

Parameters:

  • atlas
    (str or BrainGlobeAtlas) –

    Either a BrainGlobe atlas name string (e.g. "allen_mouse_25um") or an already-loaded BrainGlobeAtlas instance.

  • **kwargs
    (object, default: {} ) –

    Additional keyword arguments forwarded to BrainGlobeAtlas when atlas is a string. Common options include brainglobe_dir (override the atlas cache directory) and check_latest (disable the latest-version check). Ignored when atlas is already a BrainGlobeAtlas instance.

Returns:

  • Atlas

    Atlas with DataArrays in atlas physical space. BrainGlobe metadata and meshes are currently converted from microns to millimeters.

Examples:

>>> atlas = Atlas.from_brainglobe("allen_mouse_25um")
>>> atlas = Atlas.from_brainglobe("allen_mouse_25um", check_latest=False)
>>> atlas = Atlas.from_brainglobe(bg_atlas_instance)

get_masks

get_masks(
    regions: int | str | Sequence[int | str],
    sides: Literal["left", "right", "both"]
    | Sequence[Literal["left", "right", "both"]] = "both",
) -> DataArray

Return integer region masks stacked along a masks dimension.

Each layer along mask has values in {0, region_id}; voxels belonging to the requested region (including all descendants in the hierarchy) carry the region's index, all others are zero.

Parameters:

  • regions
    (int or str or sequence of int or str) –

    One or more regions, each given as a structure index or acronym.

  • sides
    ((left, right, both), default: "left" ) –

    Hemisphere filter. Pass a scalar to apply the same side to all regions, or a sequence of the same length as regions for per-region control.

Returns:

  • DataArray

    Integer DataArray with dims ["mask", "z", "y", "x"]. The mask coordinate holds the region acronym for each layer.

Raises:

  • KeyError

    If any requested region acronym or index is not found in the atlas.

  • ValueError

    If sides is a sequence whose length does not match regions, or if any element of sides is not "left", "right", or "both".

Examples:

>>> atlas.get_masks("VISp")
>>> atlas.get_masks("VISp", sides="left")
>>> atlas.get_masks(["VISp", "AUDp", "MOp"])
>>> atlas.get_masks(["VISp", "AUDp"], sides=["left", "both"])

get_mesh

get_mesh(
    region: int | str,
    side: Literal["left", "right", "both"] = "both",
) -> tuple[NDArray[float64], NDArray[int32]]

Return vertex coordinates and face indices for a region's mesh.

Reads the OBJ file bundled with the BrainGlobe atlas, optionally clips to one hemisphere, then transforms vertices from micron space to the DataArrays' current physical space.

Parameters:

  • region
    (int or str) –

    Structure index or acronym.

  • side
    ((left, right, both), default: "left" ) –

    Hemisphere to include. "both" keeps the full mesh. "left" and "right" clip in the base atlas physical space along the RL axis at the volume midline. Only triangles whose three vertices all fall on the requested side are retained; the cut face is not closed.

    Note

    Generalising axis detection from the orientation attribute for non-asr atlases is not yet implemented.

Returns:

  • vertices ( (ndarray, shape(N, 3)) ) –

    Vertex coordinates in millimeters.

  • faces ( (ndarray, shape(M, 3)) ) –

    Zero-indexed triangle face indices (int32).

Raises:

  • KeyError

    If the requested region is not found in the atlas.

  • ValueError

    If the atlas does not have mesh files.

resample

resample(
    transform: MeshVertexTransform,
    *,
    shape: Sequence[int],
    spacing: Sequence[float],
    origin: Sequence[float],
    dims: Sequence[str],
    reference_interpolation: Literal[
        "linear", "nearest", "bspline"
    ] = "linear",
    sitk_threads: int = -1,
) -> Atlas

Resample the atlas onto an explicit output grid.

Mirrors confusius.registration.resample_volume for atlas objects by constructing a temporary reference grid and delegating to resample_like.

Parameters:

  • transform
    ((N+1, N+1) numpy.ndarray or xarray.DataArray) –

    Pull transform mapping output physical coordinates to the current atlas physical coordinates.

  • shape
    (sequence of int) –

    Number of voxels along each output axis, in dims order.

  • spacing
    (sequence of float) –

    Voxel spacing along each output axis, in dims order.

  • origin
    (sequence of float) –

    Physical origin along each output axis, in dims order.

  • dims
    (sequence of str) –

    Dimension names of the output atlas grid.

  • reference_interpolation
    ((linear, nearest, bspline), default: "linear" ) –

    Interpolation used for the reference volume.

  • sitk_threads
    (int, default: -1 ) –

    Number of SimpleITK threads.

Returns:

  • Atlas

    Resampled atlas on the requested grid.

resample_like

resample_like(
    reference: DataArray,
    transform: NDArray[float64] | DataArray,
    *,
    reference_interpolation: Literal[
        "linear", "nearest", "bspline"
    ] = "linear",
    sitk_threads: int = -1,
) -> Atlas

Resample the atlas onto the grid of reference.

Mirrors confusius.registration.resample_like. Returns a new Atlas whose DataArrays live on reference's grid.

  • reference: resampled with reference_interpolation.
  • annotation and hemispheres: resampled with nearest-neighbour to preserve integer labels.
  • Meshes returned by get_mesh will also be in the new physical space.

moving, reference, and any DataArray transform must use the same physical coordinate units when such metadata is defined.

Parameters:

  • reference
    (DataArray) –

    Target grid. Must be 2D or 3D and must not have a time dimension.

  • transform
    ((N+1, N+1) numpy.ndarray or xarray.DataArray) –

    Pull/inverse transform returned by register_volume, mapping reference physical coordinates to atlas physical coordinates.

    • Affine (numpy.ndarray): homogeneous matrix.
    • B-spline (xarray.DataArray): control-point DataArray.
    • Displacement field (xarray.DataArray): dense field with attrs["type"] == "displacement_field_transform".
  • reference_interpolation
    ((linear, nearest, bspline), default: "linear" ) –

    Interpolation used for the reference variable.

  • sitk_threads
    (int, default: -1 ) –

    Number of SimpleITK threads. Negative values use max(1, cpu_count + 1 + sitk_threads).

Returns:

  • Atlas

    New Atlas with DataArrays on reference's grid.

Examples:

>>> _, affine = atlas.reference.fusi.register.to_volume(
...     fusi_mean, metric="mattes_mi", transform="affine"
... )
>>> atlas_fusi = atlas.resample_like(fusi_mean, affine)

search

search(
    pattern: str,
    field: Literal["all", "acronym", "name"] = "all",
) -> DataFrame

Search structures by name or acronym.

Parameters:

  • pattern
    (str) –

    Substring or regex pattern.

  • field
    ((all, acronym, name), default: "all" ) –

    Which column to search.

    • "all": case-insensitive substring match on both acronym and name.
    • "acronym" / "name": full regex match on that column only.

Returns:

Examples:

>>> atlas.search("visual cortex")
>>> atlas.search("VISp", field="acronym")

show_tree

show_tree(**kwargs) -> None

Print the structure hierarchy tree.

Parameters:

  • **kwargs

    Additional keyword arguments forwarded to [treelib.Tree.show][].