Tools for working with atlas ontologies and custom hierarchy levels.
Project description
atlaslevels
atlaslevels is a Python package for working with atlas ontologies and curated hierarchy bundles across projects. It streamlines loading atlas metadata, defining and validating hierarchy levels as ordered parent-children sets, resolving region IDs, names and color codes, and selecting biologically meaningful region sets for different levels of analysis.
Docs
Installation
Install the package:
pip install atlaslevels
For local development in this repo:
pip install -e .
For local development with test/build tools:
pip install -e .[dev]
Allen Support
atlaslevels includes built-in support for use of the Allen CCFv3 ontology. The packaged Allen preset currently uses ccfv3-2022. This matters for validation and completeness semantics, because atlaslevels evaluates spatial coverage against the metadata that specifies which regions has voxel support in that specific atlas version.
Curated Allen CCFv3 hierarchy bundles include:
allen_gm: Hierarchy bundle with seven increasingly coarse levels (CustomLevel1_gm through CustomLevel7_gm) of grey matter regions.allen_wm: Hierarchy bundle with a single level (CustomLevel1_wm) highlighting fine white matter regions while collapsing grey matter and ventricular systems.
You can load the full ontology directly with:
from atlaslevels import load_preset_ontology
ontology = load_preset_ontology("allen_ccfv3")
You can also load the curated bundles:
from atlaslevels import load_preset_bundle
bundle = load_preset_bundle("allen_gm")
bundle.list_levels()
bundle.get_parent_names("CustomLevel1_gm")
Support for different ID systems
The package also includes a preset Allen-to-KimLab16bit ID map for workflows that need to convert between original Allen IDs and the 16bit labels used in some of the atlases published by the KimLab.
from atlaslevels import load_preset_id_map
id_map = load_preset_id_map("allen_ccfv3_allen_to_kimlab16bit")
kimlab_id = id_map.convert(526157192)
allen_id = id_map.invert().convert(20040)
Parent regions with voxels
A few regions in the Allen CCFv3 are both parent regions and have direct voxel support. This means that the parent region is not completely represented by the combination of all its children, which may matter for some workflows. atlaslevels can optionally include parent residual regions in child-expansion results.
List descendants under a selected parent in a level:
from atlaslevels import load_preset_bundle
bundle = load_preset_bundle("allen_gm")
children = bundle.get_child_names("CustomLevel2_gm", parent="Main olfactory bulb")
print(children)
Output:
[
"Main olfactory bulb, glomerular layer",
"Main olfactory bulb, granule layer",
"Main olfactory bulb, inner plexiform layer",
"Main olfactory bulb, mitral layer",
"Main olfactory bulb, outer plexiform layer",
]
Include the parent itself when it is a parent residual:
from atlaslevels import load_preset_bundle
bundle = load_preset_bundle("allen_gm")
children_plus_residual = bundle.get_child_names(
"CustomLevel2_gm",
parent="Main olfactory bulb",
include_parent_residual=True,
)
print(children_plus_residual)
Output:
[
"Main olfactory bulb, glomerular layer",
"Main olfactory bulb, granule layer",
"Main olfactory bulb, inner plexiform layer",
"Main olfactory bulb, mitral layer",
"Main olfactory bulb, outer plexiform layer",
"Main olfactory bulb",
]
Supported Workflows
The primary supported workflow for atlaslevels v0.3.0 is loading packaged presets such as:
load_preset_ontology("allen_ccfv3")load_preset_bundle("allen_gm")load_preset_bundle("allen_wm")
Custom bundle loading is also supported through the documented YAML schema.
Custom ontology loading is possible, but it currently expects canonicalized input rather than acting as a general-purpose ontology importer.
In particular:
AtlasOntology.from_json(...)expects the current nested JSON structure described by the code and examplesAtlasOntology.from_records(...)expects canonical node records plus matchingAtlasMetadata.node_metadataHierarchyBundle.from_yaml(...)andHierarchyBundle.from_dict(...)are the more mature custom-loading entry points
Future versions will simplify and improve documentation of the import of custom ontologies and hierarchy bundles.
Core Objects
The current package revolves around a small set of core objects:
AtlasMetadataAtlas-level provenance and configuration, loaded from YAML.AtlasNodeOne canonical ontology node with ID, parent, name, acronym, color, and direct-voxel flag.AtlasOntologyThe rooted ontology tree plus region lookup and structural queries.HierarchyLevelOne hierarchy level defined by an ordered list of parent region IDs.HierarchyBundleA family of hierarchy levels tied to one ontology, with query helpers for parent/child selection and mapping.ValidationIssue/ValidationResultStructured validation outputs.
Main Methods
The most useful methods at a glance are:
AtlasMetadata.from_yaml(path)AtlasOntology.from_json(path, metadata=...)HierarchyBundle.from_yaml(path, ontology=...)load_preset_ontology(name)load_preset_bundle(name)load_preset_id_map(name)validate_ontology(ontology)validate_bundle(bundle, ontology)
On AtlasOntology:
get_node(node_id)get_parent(node_id)get_children(node_id)get_ancestors(node_id)get_descendants(node_id)is_ancestor(ancestor_id, node_id)is_descendant(descendant_id, node_id)get_name(node_id)get_acronym(node_id)get_color(node_id)has_direct_voxel_support(node_id)is_parent_residual(node_id)resolve_name(name)resolve_acronym(acronym)
On HierarchyBundle:
list_levels()get_level(level_name)get_parent_ids(level_name)get_parent_names(level_name)get_parent_acronyms(level_name)map_region_to_level_parent(region, level_name)get_child_ids(level_name, parent, spatial_only=False, include_parent_residual=False)get_child_names(level_name, parent, spatial_only=False, include_parent_residual=False)get_child_acronyms(level_name, parent, spatial_only=False, include_parent_residual=False)
Example Usage
Load a built-in ontology preset:
from atlaslevels import load_preset_ontology
ontology = load_preset_ontology("allen_ccfv3")
print(ontology.get_name(315))
print(ontology.get_children(184))
print(ontology.is_ancestor(315, 68))
Load a built-in hierarchy bundle preset:
from atlaslevels import load_preset_bundle
bundle = load_preset_bundle("allen_gm")
print([level.level_name for level in bundle.list_levels()])
print(bundle.get_parent_names("CustomLevel4_gm"))
Map a region to its parent at a chosen level:
from atlaslevels import load_preset_bundle
bundle = load_preset_bundle("allen_gm")
parent_id = bundle.map_region_to_level_parent(68, "CustomLevel1_gm")
parent_name = bundle.ontology.get_name(parent_id) if parent_id is not None else None
print(parent_id, parent_name)
Load a custom ontology and bundle explicitly:
from atlaslevels import AtlasMetadata, AtlasOntology, HierarchyBundle, validate_bundle
metadata = AtlasMetadata.from_yaml("atlas_metadata.yaml")
ontology = AtlasOntology.from_json("ontology.json", metadata=metadata)
bundle = HierarchyBundle.from_yaml("my_bundle.yaml", ontology=ontology)
result = validate_bundle(bundle, ontology)
result.raise_if_errors()
Local Integration Tests
The default test suite uses the small in-repo fixtures plus the packaged full Allen preset.
Run the test suite with:
python -m pytest -q
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file atlaslevels-0.3.0.tar.gz.
File metadata
- Download URL: atlaslevels-0.3.0.tar.gz
- Upload date:
- Size: 68.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9103bf1ef5b66482f75628281c24dedfe5f19b5de34622440f95e24a24fb516a
|
|
| MD5 |
6cecf0439692f2dc8f8c763379e5be74
|
|
| BLAKE2b-256 |
c4eee4a9e2a531edc8ee931086643c719baac4fbcccc76b7305042599d92ddf6
|
File details
Details for the file atlaslevels-0.3.0-py3-none-any.whl.
File metadata
- Download URL: atlaslevels-0.3.0-py3-none-any.whl
- Upload date:
- Size: 70.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
272ba9b3fcc9990a077c980745116920e11705ad2433f826c15527450bdc9fec
|
|
| MD5 |
6a5a3b0fb14fef487c70902791f29d37
|
|
| BLAKE2b-256 |
f0593c458cd39c4bc371ecb8f0cacf311593cd3ecb2896e3b8cb790d78b7a07b
|