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) grid with physical coordinates in millimetres.

  • structures

    (StructuresDict) –

    BrainGlobe structure dictionary (carries the treelib hierarchy tree).

  • mesh_to_physical

    ((4, 4) numpy.ndarray) –

    Homogeneous affine that maps mesh vertex coordinates (microns, atlas voxel space) to the DataArrays' physical space (millimetres).

  • rl_midline_um

    (float, default: 0.0 ) –

    Midpoint of the RL axis in microns (atlas voxel space). Used to clip mesh vertices to a single hemisphere.

Attributes:

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_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 the atlas physical space (millimetres).

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 (millimetres).

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 original atlas micron 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 the current physical space (millimetres).

  • 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_like

resample_like(
    reference: DataArray,
    transform: NDArray[float64],
    *,
    reference_interpolation: Literal[
        "linear", "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.

Parameters:

  • reference
    (DataArray) –

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

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

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

  • reference_interpolation
    ((linear, 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][].