Backends
Lazy image data backends and the registry used to select them. See Lazy loading and backends for an overview.
Protocols
ImageDataBackend
Bases: Protocol
Protocol for lazy image data access.
Implementations wrap different storage formats (in-memory tensors, NIfTI files via nibabel, NIfTI-Zarr via dask) behind a uniform, lazy I/O interface. This is an I/O adapter layer, not a lazy computation framework: it speeds up metadata reads and region slicing, but does not defer arithmetic or transforms.
Contract
shapeis always 4D(C, I, J, K), even for 3D NIfTI (channel dimension 1).affineis afloat64torch.Tensorof shape(4, 4).dtypereports the on-disk (or in-memory)numpydtype.__getitem__accepts the same indexing as [Image.__getitem__][torchio.Image.__getitem__], always returns a 4Dtorch.Tensorin(C, I, J, K)layout, and never drops axes (integer indices keep a size-1 dimension).to_tensormaterializes the full volume as atorch.Tensorpreserving the on-disk dtype where PyTorch supports it.
Source code in src/torchio/data/backends.py
shape
property
Shape as (C, I, J, K).
affine
property
\(4 \times 4\) affine matrix as a float64 tensor.
dtype
property
Data type of the image on disk.
LazyReader
Bases: Protocol
A custom reader that can build a lazy backend.
Readers passed to Image are normally simple callables returning
(tensor, affine), which always load the whole volume. A reader that also
implements create_backend opts in to lazy access: Image.shape,
affine, dtype, and slicing then go through the returned backend without
materializing the full tensor.
Source code in src/torchio/data/backends.py
Registration
BackendRequest
dataclass
Description of an image source used to resolve a lazy backend.
A request decouples backend selection from the Image class: resolvers
and custom backends receive a BackendRequest instead of an Image.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path | None
|
Resolved filesystem (or fsspec) path to the image, if any. |
remote_zarr_uri |
str | None
|
Remote NIfTI-Zarr URI, if the source is remote. |
zarr_store |
Any
|
An open Zarr store object, if the source is a store. |
affine |
TypeAffineMatrix | None
|
Optional \(4 \times 4\) affine override to apply to the backend. |
reader_kwargs |
Mapping[str, Any]
|
Extra keyword arguments forwarded to the loader. |
reader |
Any
|
The reader configured on the |
Source code in src/torchio/data/backends.py
register_backend(name, matcher, factory, *, prepend=True)
Register a lazy image data backend.
Registered backends are consulted by
resolve_backend in order. This is
the extension point for supporting new formats without editing the Image
class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier for the backend, used to unregister it later. Registering a new backend with an existing name replaces it. |
required |
matcher
|
BackendMatcher
|
Predicate returning |
required |
factory
|
BackendFactory
|
Callable that builds the backend from the request. |
required |
prepend
|
bool
|
If |
True
|
Source code in src/torchio/data/backends.py
unregister_backend(name)
Remove a previously registered backend by name (no-op if absent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name passed to
|
required |
Source code in src/torchio/data/backends.py
resolve_backend(request)
Resolve a lazy backend for request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BackendRequest
|
The source description. |
required |
Returns:
| Type | Description |
|---|---|
ImageDataBackend | None
|
The first matching backend, or |
ImageDataBackend | None
|
handle the request (for example a non-NIfTI file path, where the |
ImageDataBackend | None
|
caller falls back to a full read). |
Source code in src/torchio/data/backends.py
Built-in backends
NibabelBackend
Backend wrapping a nibabel image for lazy NIfTI access.
Data is accessed through nibabel's dataobj proxy, which supports
memory-mapped reads. Shape and affine are read from the header
without loading data.
This backend also works with NIfTI-Zarr files loaded via niizarr,
since zarr2nii returns a standard nibabel.Nifti1Image whose
dataobj is a dask array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nii
|
SpatialImage
|
A nibabel image (typically from |
required |
affine
|
TypeAffineMatrix | None
|
Optional \(4 \times 4\) affine that overrides the affine stored
in the NIfTI header. Used when the user passes an explicit affine
to |
None
|
Source code in src/torchio/data/backends.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
to_tensor()
Materialize the full image preserving the on-disk dtype.
Source code in src/torchio/data/backends.py
ZarrBackend
Backend wrapping a NIfTI-Zarr file for chunked lazy access.
NIfTI-Zarr files are loaded via niizarr.zarr2nii(), which returns
a nibabel image with a dask array as its dataobj. This backend
delegates to NibabelBackend for the actual data access.
Requires the nifti-zarr package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | object
|
Path to a |
required |
affine
|
TypeAffineMatrix | None
|
Optional \(4 \times 4\) affine that overrides the affine stored in the NIfTI-Zarr metadata. |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to |
{}
|
Source code in src/torchio/data/backends.py
TensorBackend
Backend wrapping an in-memory PyTorch tensor.
Used for images created from tensors or NumPy arrays (NumPy arrays are converted to tensors first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
4D tensor with shape (C, I, J, K). |
required |
affine
|
TypeAffineMatrix | None
|
\(4 \times 4\) affine tensor. Identity if not given. |
None
|