|
12 | 12 | # - think about supporting the COO format |
13 | 13 | from __future__ import annotations |
14 | 14 |
|
| 15 | +import asyncio |
15 | 16 | import warnings |
16 | 17 | from abc import ABC |
17 | 18 | from collections.abc import Iterable |
@@ -661,6 +662,31 @@ def to_memory(self) -> CSMatrix | CSArray: |
661 | 662 | mtx.indptr = self._indptr |
662 | 663 | return mtx |
663 | 664 |
|
| 665 | + async def to_memory_async(self) -> CSMatrix | CSArray: |
| 666 | + format_class = get_memory_class( |
| 667 | + self.format, use_sparray_in_io=settings.use_sparse_array_on_read |
| 668 | + ) |
| 669 | + mtx = format_class(self.shape, dtype=self.dtype) |
| 670 | + mtx.indptr = self._indptr |
| 671 | + if isinstance(self._data, ZarrArray): |
| 672 | + await asyncio.gather( |
| 673 | + *( |
| 674 | + self.set_memory_async_from_zarr(mtx, attr) |
| 675 | + for attr in ["data", "indices"] |
| 676 | + ) |
| 677 | + ) |
| 678 | + else: |
| 679 | + mtx.data = self._data[...] |
| 680 | + mtx.indices = self._indices[...] |
| 681 | + return mtx |
| 682 | + |
| 683 | + async def set_memory_async_from_zarr( |
| 684 | + self, mtx: CSMatrix | CSArray, attr: Literal["indptr", "data", "indices"] |
| 685 | + ) -> None: |
| 686 | + setattr( |
| 687 | + mtx, attr, await getattr(self, f"_{attr}")._async_array.getitem(()) |
| 688 | + ) # TODO: better way to asyncify |
| 689 | + |
664 | 690 |
|
665 | 691 | class _CSRDataset(BaseCompressedSparseDataset, abc.CSRDataset): |
666 | 692 | """Internal concrete version of :class:`anndata.abc.CSRDataset`.""" |
|
0 commit comments