Lazy slicing and transpose operations for h5py and zarr
Project description
lazyslice
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 operationsdataset- The underlying datasetlazy_slice- Enable lazy slicing mode for the next indexing operationT- Transpose (reverse axis order)
Methods:
read()- Read and return the data as a numpy arraylazy_transpose(axis_order=None)- Lazily transpose the arraylazy_iter(axis=0)- Lazily iterate over slices along an axisread_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:
- Package renamed:
lazy_ops→lazyslice - Import path changed:
from lazy_ops import DatasetView→from lazyslice import DatasetView - New
read()method (recommended overdsetread(), 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9911cd6e9cede1a11bb5379e9357f0af31c2444e309223e32b2cb79ec573827b
|
|
| MD5 |
121275239c0dc18323896474542cfad4
|
|
| BLAKE2b-256 |
bdafdaa40fe57cc909fcc8768102e541dde9cb69c794feb91ff054dab34423a4
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
485df89191d481f74e203ff6236101ecdd0d7c83ac86e116b52143f4214a01bc
|
|
| MD5 |
9878eebe7152094790ba21bb9fd972a2
|
|
| BLAKE2b-256 |
78218db5d3036715f109d7371af9361eb839f7208a03e4b8687a182be2dc399a
|