Skip to main content

Lazy slicing and transpose operations for h5py and zarr

Project description

lazyslice

PyPI version codecov License Python

Lazy transposing and slicing of h5py datasets and zarr arrays

lazyslice allows you to compose multiple slice and transpose operations on h5py datasets and zarr arrays without reading data until explicitly requested. This is useful for working with large datasets where you want to defer I/O operations.

Installation

pip install lazyslice

For zarr support:

pip install lazyslice[zarr]

Quick Start

from lazyslice import DatasetView
import h5py

# Open an HDF5 file and create a lazy view
with h5py.File('data.h5', 'r') as f:
    view = DatasetView(f['dataset'])
    
    # Chain lazy operations - no data is read yet
    result = (view
        .lazy_slice[1:40:2, :, 0:50:5]
        .lazy_transpose([2, 0, 1])
        .lazy_slice[8, 5:10])
    
    # Data is only read when you access it
    data = result[:]  # or result.read()

Usage

h5py Datasets

from lazyslice import DatasetView
import h5py

with h5py.File('data.h5', 'r') as f:
    dsetview = DatasetView(f['dataset'])
    
    # Lazy slicing
    view1 = dsetview.lazy_slice[1:40:2, :, 0:50:5]
    
    # Lazy transpose
    view2 = view1.lazy_transpose([2, 0, 1])
    
    # Chain operations
    view3 = view2.lazy_slice[8, 5:10]
    
    # Read data
    data = view3[:]          # Using indexing
    data = view3.read()      # Using read() method

zarr Arrays

from lazyslice import DatasetView
import zarr

zarray = zarr.open('data.zarr')
zarrview = DatasetView(zarray)

# Same API as h5py
view = zarrview.lazy_slice[1:10:2, :, 5:10].lazy_transpose([0, 2, 1])
data = view.read()

Lazy Iteration

# Iterate over slices without loading all data at once
for slice_view in view.lazy_iter(axis=1):
    chunk = slice_view[:]
    process(chunk)

Transpose Shortcut

from lazyslice import lazy_transpose

# Quick transpose without creating a view first
transposed_view = lazy_transpose(dataset, axes=[2, 0, 1])
data = transposed_view[:]

API Reference

DatasetView

The main class for creating lazy views over datasets.

Properties:

  • shape - Shape of the view after all lazy operations
  • dataset - The underlying dataset
  • lazy_slice - Enable lazy slicing mode for the next indexing operation
  • T - Transpose (reverse axis order)

Methods:

  • read() - Read and return the data as a numpy array
  • lazy_transpose(axis_order=None) - Lazily transpose the array
  • lazy_iter(axis=0) - Lazily iterate over slices along an axis
  • read_direct(dest, source_sel=None, dest_sel=None) - Read directly into an existing array

lazy_transpose(dset, axes=None)

Convenience function to create a transposed view of a dataset.

Migration from lazy_ops

If you're upgrading from lazy_ops, the main changes are:

  1. Package renamed: lazy_opslazyslice
  2. Import path changed: from lazy_ops import DatasetViewfrom lazyslice import DatasetView
  3. New read() method (recommended over dsetread(), which is still available for backward compatibility)

Development

# Clone the repository
git clone https://github.com/catalystneuro/lazyslice.git
cd lazyslice

# Install in development mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=lazyslice

License

BSD-3-Clause

Project details


Download files

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

Source Distribution

lazyslice-0.3.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

lazyslice-0.3.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file lazyslice-0.3.0.tar.gz.

File metadata

  • Download URL: lazyslice-0.3.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for lazyslice-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9911cd6e9cede1a11bb5379e9357f0af31c2444e309223e32b2cb79ec573827b
MD5 121275239c0dc18323896474542cfad4
BLAKE2b-256 bdafdaa40fe57cc909fcc8768102e541dde9cb69c794feb91ff054dab34423a4

See more details on using hashes here.

File details

Details for the file lazyslice-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: lazyslice-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for lazyslice-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 485df89191d481f74e203ff6236101ecdd0d7c83ac86e116b52143f4214a01bc
MD5 9878eebe7152094790ba21bb9fd972a2
BLAKE2b-256 78218db5d3036715f109d7371af9361eb839f7208a03e4b8687a182be2dc399a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page