asset module¶
Asset façade — a single object that owns an opened Zarr plus its metadata.
This module is the user-facing entry point introduced by the API redesign.
An Asset instance carries:
the URI of the alignment-channel Zarr,
optionally, the source Zarr URI used to discover that alignment channel,
the parsed
metadata.nd.json(ND/acquisition metadata),the parsed
processing.json,an opened
ome-zarrNode(lazily acquired, cached for the lifetime of the asset),per-asset configuration: overlay selector, S3 client, anonymous flag, cache directory, and template selection.
The methods on Asset (image, stub, apply_overlays) and
the lazy transforms property delegate to the existing free functions in
aind_zarr_utils.zarr, aind_zarr_utils.image, and
aind_zarr_utils.io.transforms, threading opened_zarr=self.opened_zarr
everywhere so that no S3 resource is read twice in a logical workflow.
Three eager constructors handle the common discovery patterns:
Asset.from_zarr()— pass any acquisition-Zarr URI; the alignment channel is auto-resolved from the asset root.Asset.from_root()— pass the asset root directly.Asset.from_neuroglancer()— pull a Zarr URI from theimage_sourcesof a Neuroglancer state and resolve from there.
The bare constructor Asset(alignment_zarr_uri, metadata, processing) is
the no-I/O path: pass already-loaded values explicitly. The Zarr is still
opened lazily on first access.
Notes
Asset is not thread-safe. ome-zarr’s Reader holds an
fsspec filesystem object that is fork-unsafe; do not pass an opened
Asset across processes.
- class aind_zarr_utils.asset.TransformPaths(point_paths, point_invert, image_paths, image_invert)[source]
Bases:
objectLocal filesystem paths for the pipeline’s ANTs transform chains.
Both the image (forward) and point (reverse) chain files are materialised together so that round-tripping points through CCF reuses the same downloaded files.
- point_paths
Paths to the transform files in the order ANTs expects when warping points (CCF → individual: image-direction inverse).
- image_paths
Paths to the transform files in the order ANTs expects when warping images (individual → CCF: forward direction).
- __init__(point_paths, point_invert, image_paths, image_invert)
- class aind_zarr_utils.asset.Asset(alignment_zarr_uri, metadata, processing, source_zarr_uri=None, overlay_selector=<factory>, s3_client=None, anonymous=True, cache_dir=None, template_used='SmartSPIM-template_2024-05-16_11-26-14', template_base=None)[source]
Bases:
objectA SmartSPIM acquisition with eager metadata and lazy Zarr/transform I/O.
- Parameters:
alignment_zarr_uri (
str) – URI of the alignment-channel Zarr. Use the classmethodsfrom_zarr()/from_root()/from_neuroglancer()to discover this from a different starting point.source_zarr_uri (
str|None) – Original acquisition Zarr URI used for discovery. Set byfrom_zarr()andfrom_neuroglancer();Noneforfrom_root()and explicit construction unless supplied.overlay_selector (
OverlaySelector) – Pipeline-overlay selector. Defaults to the cached singleton returned byget_selector(); the factory call avoids the import-time binding trap of using= get_selector()as a default-arg expression.s3_client (
S3Client|None) – S3 access knobs forwarded topipeline_transforms_local_paths().anonymous (
bool) – S3 access knobs forwarded topipeline_transforms_local_paths().cache_dir (
str|PathLike|None) – S3 access knobs forwarded topipeline_transforms_local_paths().template_used (
str) – Key naming the template→CCF transform set to use. Defaults to"SmartSPIM-template_2024-05-16_11-26-14".template_base (
str|PathLike|None) – Override base path for the template transforms (e.g. a local mirror). IfNone, the canonical S3 location is used.
- alignment_zarr_uri: str
- overlay_selector: OverlaySelector
- s3_client: S3Client | None
- anonymous: bool
- template_used: str
- classmethod from_zarr(zarr_uri, *, anonymous=True, s3_client=None, cache_dir=None, overlay_selector=None, template_used='SmartSPIM-template_2024-05-16_11-26-14', template_base=None)[source]
Discover the asset’s metadata + alignment Zarr from any acquisition Zarr URI.
Walks up two directory levels from
zarr_urito find the asset root, loadsmetadata.nd.jsonandprocessing.json, infers the alignment channel from the processing metadata, and pre-opens the alignment Zarr (eager). Thezarr_uriyou pass need not itself be the alignment channel.- Return type:
Asset
- classmethod from_root(asset_uri, *, anonymous=True, s3_client=None, cache_dir=None, overlay_selector=None, template_used='SmartSPIM-template_2024-05-16_11-26-14', template_base=None)[source]
Discover the asset’s metadata + alignment Zarr from the asset root URI.
- Return type:
Asset
- classmethod from_neuroglancer(ng_state, *, asset_uri=None, anonymous=True, s3_client=None, cache_dir=None, overlay_selector=None, template_used='SmartSPIM-template_2024-05-16_11-26-14', template_base=None)[source]
Discover the asset from a Neuroglancer state.
If
asset_uriis given, behaves likefrom_root(). Otherwise the firstimage_sourcesURL inng_stateis used as the starting point (passed tofrom_zarr()).- Return type:
Asset
- property opened_zarr: tuple[Node, dict]
The opened ome-zarr
(Node, metadata)tuple, opened on first access.
- property transforms: TransformPaths
The pipeline’s ANTs transform chains as local file paths.
First access downloads (or locates in the
aind_s3_cachecache) all required transform files; the result is cached for the lifetime of the asset.
- image(*, level=3, library='sitk', pipeline=False, origin=None, scale_unit='millimeter')[source]
Build a full image from the asset’s Zarr at the given pyramid level.
- Parameters:
level (
int) – Resolution level (0 = full resolution; higher = coarser). Default3.library (
Literal['sitk','ants']) – Backend. Default"sitk".pipeline (
bool) – IfTrue, apply pipeline overlay corrections in-place after constructing the image. DefaultFalse.origin (
Origin|None) – Origin specification. Defaults toOrigin.default(). Only valid whenpipeline=False; pipeline images use the corrected header’s origin.scale_unit (
str) – Output spacing unit. Default"millimeter".
- Returns:
The constructed image, with pipeline corrections applied if
pipeline=True.- Return type:
Image|ANTsImage
- stub(*, pipeline=False, origin=None, scale_unit='millimeter', level=0)[source]
Build a header-only SimpleITK stub for the asset’s Zarr.
- Return type:
- Returns:
stub (sitk.Image) – A 1×1×1 SimpleITK image whose spatial header matches what a full-resolution image would have, optionally with pipeline corrections applied.
size_ijk (tuple of three ints) – The level-0 native voxel dimensions of the acquisition, in SimpleITK index order.
Notes
Only SimpleITK is currently supported for stubs (mirrors the existing
zarr_to_sitk_stubAPI).originis only valid whenpipeline=False; pipeline stubs use the corrected header’s origin.
- apply_overlays(img, *, level=3)[source]
Apply pipeline overlay corrections in-place to
img.Type-dispatches on
imgto handle SimpleITK and ANTs. Seeaind_zarr_utils.image.apply_pipeline_overlays()for the per-level math; this method threads the asset’soverlay_selectorand cachedopened_zarrthrough.- Return type:
- transform(points, *, to)[source]
Project
pointsto coordinate spaceto.Walks the small transform graph defined in
aind_zarr_utils.points, applying one edge at a time. The asset’s cachedopened_zarrandtransformsare reused at every hop, so a multi-hop walk performs at most one Zarr open and one transform-chain download.- Parameters:
points (
Points) – Source points.points.spacedetermines the starting node in the graph.to (
Space) – Destination coordinate space.
- Returns:
New
Pointswith the samevalueskeys (and per-point descriptions, if any) projected toto.- Return type:
Points
- __init__(alignment_zarr_uri, metadata, processing, source_zarr_uri=None, overlay_selector=<factory>, s3_client=None, anonymous=True, cache_dir=None, template_used='SmartSPIM-template_2024-05-16_11-26-14', template_base=None)
Overview¶
Asset is the recommended user-facing entry point. It owns the
alignment-channel Zarr URI, parsed metadata, parsed processing metadata, the
opened Zarr cache, and transform-chain cache.
Common constructors:
from aind_zarr_utils import Asset
asset = Asset.from_zarr("s3://bucket/dataset/image.ome.zarr/0")
asset = Asset.from_root("s3://bucket/dataset")
asset = Asset.from_neuroglancer(ng_state)
No-I/O construction is available when metadata is already loaded:
asset = Asset(
alignment_zarr_uri="s3://bucket/dataset/alignment.ome.zarr/0",
metadata=metadata,
processing=processing,
)
Examples¶
Build images and stubs:
from aind_zarr_utils import Origin
sitk_img = asset.image(level=3)
ants_img = asset.image(level=3, library="ants")
stub, size_ijk = asset.stub(level=0)
pipeline_stub, native_size_ijk = asset.stub(pipeline=True)
anchored = asset.image(
level=3,
origin=Origin.at_corner("RAS", (0.0, 0.0, 0.0)),
)
Transform points:
from aind_zarr_utils import Points, Space
points = Points.from_neuroglancer(ng_state)
ccf = asset.transform(points, to=Space.CCF_MM)