Skip to main content

lazystack

This package provides a familiar interface for lazily reading scientific image stacks, particularly stacks of x-ray images. It's cross-platform, lightweight, and easy to use. Stacks opened via lazystack are indexable (including NumPy-style fancy indexing), iterable, sliceable, and only load the underlying data into memory when absolutely necessary.

Supported formats

  • HDF (via h5py) — datasets must be (num_images, height, width) grayscale images.
  • TIFF and other TIFF-family files (via tifffile) — grayscale stacks only; colour/multichannel images and hyperstacks are not supported.
  • Hamamatsu DCIMG (.dcimg).
  • Hamamatsu HIS (.his).

TIFF-family files that store the whole stack as a single 3D image (rather than a sequence of 2D images) require the optional zarr package (pip install zarr).

Installation

$ pip install git+https://github.com/domjurkschat/lazystack

For developers, you can clone the repository and run

$ pip install -e .

or

$ uv sync

Usage

Lazy stacks can be created by passing a path or a list/array of paths to the lazystack function. The underlying data is only materialised via integer indexing or upon being cast to a NumPy array, e.g., via np.asarray() or .asarray(). For example:

from lazystack import lazystack

with lazystack("path/to/somehis.his") as images:
    # Access familiar NDArray attributes, e.g., shape, dtype, size.
    shape = images.shape
    # Slicing the lazystack produces a view (no images in memory).
    substack = images[0:50]
    # Slicing a view or lazystack also produces a view.
    subsubstack = substack[0:10]
    # Spatial slicing also produces a view.
    subsubsubstack = subsubstack[:, 100:, 50:-50]
    # Integer indexing materialises an image from the view.
    image = subsubsubstack[5]
    # `np.asarray()` materialises the whole view.
    subsubsubstack = np.asarray(subsubsubstack)

A lazystack exposes NumPy-like attributes: shape, dtype, ndim, size (total elements), and itemsize (bytes per element). Disk usage is available via image_nbytes (bytes per frame) and nbytes (bytes for the whole stack). A one-line summary is available via str(stack) or stack.info.

It's best to open lazystacks within a context manager, but you can also open and close them manually, e.g.:

images = lazystack("path/to/somehis.his")
# Do some stuff.
# ...
# Don't forget to close!
images.close()

If you're opening HDF files, the desired dataset path (within the HDF file) must also be specified, e.g.:

images = lazystack("path/to/some/hdf5.h5", "path/to/some/dset")

Directories of files can be opened by supplying a list or array of filenames, e.g.:

filenames = sorted(input_path.glob("*.tif"))
with lazystack(filenames) as images:
    # Do some stuff.

Currently, this only supports TIFF files.

lazystack also provides iter_chunks for prefetching and yielding successive chunks along any axis of a lazystack (or regular 3D NumPy array), e.g.:

from lazystack import lazystack, iter_chunks

# How many chunks will be prefetched.
num_prefetch = 1
# Memory allowance for each chunk (in GB).
chunk_size_gb = 1.0
# Axis to chunk over (must be 0, 1, or 2).
axis = 0
# Step along the chunk axis (must be at least 1).
step = 1

with lazystack("path/to/somedcimg.dcimg") as images:
    # `start_idx` and `stop_idx` specify the index bounds of each chunk, useful 
    #   for output.
    for chunk, start_idx, stop_idx in iter_chunks(
        images, 
        chunk_size_gb=chunk_size_gb,
        axis=axis,
        step=step,
        num_prefetch=num_prefetch
    ):
        # Do some stuff.

Contributing

Contributions are very welcome! Don't hesitate to reach out if you have any questions and feel free to open an issue if you have any feedback or encounter any bugs.

Tests can be run via:

$ uv run pytest

To-do list:

  • Improve test suite.
  • Nested spatial indexing.
  • Stacks of stacks.
  • Other file formats.
  • Colour/multichannel and hyperstack support.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazystack-0.5.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazystack-0.5.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file lazystack-0.5.0.tar.gz.

File metadata

  • Download URL: lazystack-0.5.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lazystack-0.5.0.tar.gz
Algorithm Hash digest
SHA256 bb5918648a9fb23985b0b5e9bf042f4c0dff15a341622b5bf37c2c51cfacfa58
MD5 1c903c7694baa4e4668b0a1c6d761744
BLAKE2b-256 8356ba2c8aa899fbbebf814ccd70c77e6ff7e28d80dcabfdfa7eb81a2c21a22a

See more details on using hashes here.

File details

Details for the file lazystack-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: lazystack-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lazystack-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e432b623d5ea142b7942239c17ab52c9ba7c9cce7a25266734d43abe01455d51
MD5 f044ea0d99f25d2a35bdcd30a375920f
BLAKE2b-256 34eea6bc7889a2bcfc6f735c888964c76c27ce1399aa99655d4124055d0894dd

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

This release

0.5.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page