Metadata-Version: 2.5
Name: spatialbatch
Version: 0.0.1rc1
Summary: A minibatch loader for SpatialData stores
Project-URL: Documentation, https://spatialbatch.readthedocs.io/
Project-URL: Homepage, https://github.com/rushin682/spatialbatch
Project-URL: Source, https://github.com/rushin682/spatialbatch
Author: Rushin Gindra
Maintainer-email: Rushin Gindra <rushin.gindra682@gmail.com>
License: MIT License
        
        Copyright (c) 2026, Rushin Gindra
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: annbatch[zarrs]
Requires-Dist: anndata>=0.13
Requires-Dist: dask>=2025.9
Requires-Dist: geopandas
Requires-Dist: numba
Requires-Dist: numpy
Requires-Dist: pandas>=2.2.2
Requires-Dist: scipy>1.15
Requires-Dist: session-info2
Requires-Dist: spatialdata>=0.6.1
Requires-Dist: tqdm
Requires-Dist: xarray
Requires-Dist: zarr>=3.1.4
Provides-Extra: torch
Requires-Dist: torch>=2; extra == 'torch'
Description-Content-Type: text/markdown

# spatialbatch

[![Tests][badge-tests]][tests]
[![Documentation][badge-docs]][documentation]

[badge-tests]: https://img.shields.io/github/actions/workflow/status/rushin682/spatialbatch/test.yaml?branch=main
[badge-docs]: https://app.readthedocs.org/projects/spatialbatch/badge/

A minibatch loader for on-disk [SpatialData][spatialdata] stores: tiles of every image and labels element, centered on each cell,
together with the cell's expression and annotations, streamed in large preloaded chunks.

`spatialbatch` is built on, and has [annbatch][] as a dependency. It follows the same design: a `Sampler` emits requests for chunks of
consecutive cells, one request is read from disk at once (concurrently for every modality, through async `zarr` and
optionally [zarrs-python][]), shuffled in memory and split into batches. No torch `DataLoader` is needed.
It reimplements `spatialstream.dataloaders.XeniumDataset`, which it reproduces exactly (see `tests/test_parity.py`).

```python
import spatialdata as sd
import zarr
from spatialbatch import Loader

zarr.config.set({"codec_pipeline.path": "zarrs.ZarrsCodecPipeline"})  # optional, see docs/zarr-configuration.md

loader = Loader(
    images=["morphology_focus"],
    labels=["cell_labels"],
    tile_dim_in_units=224,
    rasterize=True,
    rasterize_kwargs={"target_unit_to_pixels": 1.0},
    return_annotations=["cell_labels"],
    batch_size=32,
    shuffle=True,
    to="torch",
).add_sdata(sd.read_zarr("xenium.zarr"))

for batch in loader:
    batch["images"]["morphology_focus"]  # (32, 1, 224, 224)
    batch["labels"]["cell_labels"]  # (32, 224, 224)
    batch["X"]  # (32, n_genes) sparse CSR expression
```

For the best throughput, write the stores once into a spatially sorted, sharded `SpatialDatasetCollection`:

```python
from spatialbatch import Loader, SpatialDatasetCollection

collection = SpatialDatasetCollection("collection.zarr")
collection.add_sdatas(["a.zarr", "b.zarr"], images=["morphology_focus"], labels=["cell_labels"], tile_dim_in_units=224)
loader = Loader(images=["morphology_focus"], batch_size=32, shuffle=True, tile_dim_in_units=224).use_collection(
    collection
)
```

## Getting started

Please refer to the [documentation][],
in particular, the [API documentation][].

## Installation

You need to have Python 3.12 or newer installed on your system.
If you don't have Python installed, we recommend installing [uv][].

We recommend managing dependencies in project-specific virtual environments to avoid dependency conflicts.
This is most convenient using package managers such as [uv][].
Choose from the options below to install spatialbatch:

<!--
1. Add the latest release of `spatialbatch` from [PyPI][] to your `uv` project:

   ```bash
   uv add spatialbatch
   ```

1. Install the latest release into a [standard virtual environment][venv]:

   ```bash
   (after activating your venv)
   pip install spatialbatch
   ```

-->

1. Install the latest development version:

   ```bash
   pip install git+https://github.com/rushin682/spatialbatch.git  # (or `uv add`)
   ```

## Release notes

See the [changelog][].

## Contact

For questions and help requests, you can reach out in the [scverse discourse][].
If you found a bug, please use the [issue tracker][].

## Citation

> t.b.a

[spatialdata]: https://spatialdata.scverse.org
[annbatch]: https://github.com/scverse/annbatch
[zarrs-python]: https://zarrs-python.readthedocs.io
[uv]: https://github.com/astral-sh/uv
[scverse discourse]: https://discourse.scverse.org/
[issue tracker]: https://github.com/rushin682/spatialbatch/issues
[tests]: https://github.com/rushin682/spatialbatch/actions/workflows/test.yaml
[documentation]: https://spatialbatch.readthedocs.io
[changelog]: https://spatialbatch.readthedocs.io/page/changelog.html
[api documentation]: https://spatialbatch.readthedocs.io/page/api.html
[pypi]: https://pypi.org/project/spatialbatch
[venv]: https://docs.python.org/3/tutorial/venv.html
