annbatch.Loader#
- class annbatch.Loader(*, batch_sampler=None, chunk_size=None, preload_nchunks=None, shuffle=None, return_index=False, batch_size=None, preload_to_gpu=False, drop_last=None, to=None, rng=None)#
A loader for on-disk data anndata stores.
This loader by default batches together slice requests (
chunk_sizeparameter) to the underlying stores to achieve higher performance. You can also usechunk_size==1for perfect random sampling (for relevant samplers), although this comes at a performance penalty for on-disk (and likely in-memory) data as well. Custom samplers are supported via thebatch_samplerargument. We thus recommend usingDatasetCollectionto preshuffle your data (or pre-shuffling in-memory). The loader is agnostic to the on-disk chunking/sharding, but it may be advisable to align with the in-memory chunk size for dense.When providing a custom sampler,
chunk_size,preload_nchunks,batch_size,shuffle,drop_last, andrngmust not be set (they are controlled by thebatch_samplerinstead). When providing these arguments and nobatch_sampler, they are used to construct aRandomSampler(ifshuffle=True) orSequentialSampler.- Parameters:
- batch_sampler
Sampler|None(default:None) If not provided, a default
SequentialSamplerorRandomSamplerwill be used with the same defaults below.- chunk_size
int|None(default:None) The obs size (i.e., axis 0) of contiguous array data to fetch. Mutually exclusive with
batch_sampler. Defaults to 512.- preload_nchunks
int|None(default:None) The number of chunks of contiguous array data to fetch. Mutually exclusive with
batch_sampler. Defaults to 32.- shuffle
bool|None(default:None) Whether or not to shuffle the data. Mutually exclusive with
batch_sampler. Defaults to False.- batch_size
int|None(default:None) Batch size to yield from the dataset. Mutually exclusive with
batch_sampler. Defaults to 1.- drop_last
bool|None(default:None) Set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. Leave as False when using in conjunction with a
torch.utils.data.DataLoader. Mutually exclusive withbatch_sampler. Defaults to False.- rng
Generator|None(default:None) Random number generator for shuffling. Mutually exclusive with
batch_sampler. Defaults tonp.random.default_rng()if not provided.- return_index
bool(default:False) Whether or not to yield the index on each iteration.
- preload_to_gpu
bool(default:False) Whether or not to use cupy for non-io array operations like vstack and indexing once the data is in memory internally (and batches yielded if
tois not set). This option entails greater GPU memory usage, but is faster at least for sparse operations.torch.vstack()does not support CSR sparse matrices, hence the current use ofcupyinternally (which also meanstorchis an optional dep). Furthermore, there is no way to allocate pinned memory for jax arrays. Setting this toFalseis advisable when using thetorch.utils.data.DataLoaderwrapper or potentially with dense data due to memory pressure. For top performance, this should be used in conjunction withto="torch"and thentorch.Tensor.to_dense()if you wish to densify (or similar for jax, although jax may be very slow for sparse->dense transform).cupy.cuda.MemoryPool.free_all_blocks()(i.e., the method of the pool ofcupy.get_default_memory_pool()) is called aggressively to keep memory usage low. If you are using your own memory pool or allocator, you may have to free blocks on your own.- to
Literal['torch','jax'] |None(default:None) The output library for which you would like your array output.
- batch_sampler
Examples
>>> from annbatch import Loader >>> ds = Loader( batch_size=4096, chunk_size=32, preload_nchunks=512, ).add_adata(my_anndata) >>> for batch in ds: # optionally convert to dense # batch = batch.to_dense() do_fit(batch)
Attributes table#
The sampler used to generate batches. |
|
The type of on-disk data used in this loader. |
|
The total number of observations in this instance i.e., the sum of the first axis of all added datasets. |
|
The total number of variables in this instance i.e., the second axis (which is the same) across all datasets. |
|
The var annotations for the variables in this loader. |
Methods table#
|
Append an adata to this dataset. |
|
Append adatas to this dataset. |
|
Append a dataset to this dataset. |
|
Append datasets to this dataset. |
|
Load from an existing |
Attributes#
- Loader.batch_sampler#
The sampler used to generate batches.
- Returns:
The sampler.
- Loader.dataset_type#
The type of on-disk data used in this loader.
- Returns:
The type used.
- Loader.n_obs#
The total number of observations in this instance i.e., the sum of the first axis of all added datasets.
- Returns:
The number of observations.
- Loader.n_var#
The total number of variables in this instance i.e., the second axis (which is the same) across all datasets.
- Returns:
The number of variables.
- Loader.var#
The var annotations for the variables in this loader.
- Returns:
The var DataFrame or None if no var annotations were provided.
Methods#
- Loader.add_adata(adata)#
Append an adata to this dataset.
- Parameters:
- adata
AnnData A
anndata.AnnDataobject, withzarr.Array,scipy.sparse.csr_matrix,scipy.sparse.csr_array,numpy.ndarray, oranndata.abc.CSRDatasetas the data matrix inX, andobscontaining annotations to yield in apandas.DataFrame.varmust match thevarof any previously added datasets. OnlyX,obs, andvarare kept for now: anyobsmandlayerselements are ignored and aFutureWarningis emitted (a future release will additionally load and yield them if present in the passed inadata).
- adata
- Return type:
Self
- Loader.add_adatas(adatas)#
Append adatas to this dataset.
- Parameters:
- adatas
list[AnnData] List of
anndata.AnnDataobjects, withzarr.Array,scipy.sparse.csr_matrix,scipy.sparse.csr_array,numpy.ndarray, oranndata.abc.CSRDatasetas the data matrix inX, andobscontaining annotations to yield in apandas.DataFrame. OnlyX,obs, andvarare kept for now: anyobsmandlayerselements are ignored and aFutureWarningis emitted (a future release will additionally load and yield them).
- adatas
- Return type:
Self
- Loader.add_dataset(dataset, obs=None, var=None)#
Append a dataset to this dataset.
- Parameters:
- dataset
CSRDataset|Array|csr_array|csr_matrix|ndarray A
zarr.Arrayoranndata.abc.CSRDatasetobject, generally fromanndata.AnnData.X.- obs
DataFrame|None(default:None) DataFrameobs, generally fromanndata.AnnData.obs.- var
DataFrame|None(default:None) DataFramevar, generally fromanndata.AnnData.var.varmust match thevarof any previously added datasets.
- dataset
- Return type:
Self
- Loader.add_datasets(datasets, obs=None, var=None)#
Append datasets to this dataset.
- Parameters:
- datasets
list[CSRDataset|Array|csr_array|csr_matrix|ndarray] List of
zarr.Arrayoranndata.abc.CSRDatasetobjects, generally fromanndata.AnnData.X. They must all be of the same type and match that of any already added datasets.- obs
list[DataFrame] |None(default:None) List of
DataFramefor annotating observations (i.e., samples), generally fromanndata.AnnData.obs.- var
list[DataFrame] |None(default:None) List of
DataFramefor annotating features, generally fromanndata.AnnData.var. All var DataFrames must be identical.
- datasets
- Return type:
Self
- Loader.use_collection(collection, *, load_adata=<function load_all_aligned>)#
Load from an existing
annbatch.DatasetCollection.This function can only be called once. If you want to manually add more data, use
Loader.add_adatas()or open an issue.- Parameters:
- collection
DatasetCollection The collection whose on-disk datasets should be used in this loader.
- load_adata
Callable[[Group],AnnData] (default:<function load_all_aligned at 0x743989a28720>) A custom load function - recall that whatever is found in
Xandobswill be yielded in batches. OnlyX,obs, andvarare yielded for now; a future release will additionally yield observation-alignedobsmandlayerselements.
- collection
- Return type:
Self