Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,10 @@ def write_sparse_compressed(
_REGISTRY.register_write(store_type, cls, spec)(func)


@_REGISTRY.register_write(H5Group, _CSRDataset, IOSpec("", "0.1.0"))
@_REGISTRY.register_write(H5Group, _CSCDataset, IOSpec("", "0.1.0"))
@_REGISTRY.register_write(ZarrGroup, _CSRDataset, IOSpec("", "0.1.0"))
@_REGISTRY.register_write(ZarrGroup, _CSCDataset, IOSpec("", "0.1.0"))
@_REGISTRY.register_write(H5Group, _CSRDataset, IOSpec("csr_matrix", "0.1.0"))
@_REGISTRY.register_write(H5Group, _CSCDataset, IOSpec("csc_matrix", "0.1.0"))
@_REGISTRY.register_write(ZarrGroup, _CSRDataset, IOSpec("csr_matrix", "0.1.0"))
@_REGISTRY.register_write(ZarrGroup, _CSCDataset, IOSpec("csc_matrix", "0.1.0"))
def write_sparse_dataset(
f: GroupStorageType,
k: str,
Expand All @@ -800,9 +800,6 @@ def write_sparse_dataset(
fmt=elem.format,
dataset_kwargs=dataset_kwargs,
)
# TODO: Cleaner way to do this
f[k].attrs["encoding-type"] = f"{elem.format}_matrix"
f[k].attrs["encoding-version"] = "0.1.0"


def write_cupy_dask(f, k, elem, _writer, dataset_kwargs=MappingProxyType({})):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_backed_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,22 @@ def test_anndata_sparse_compat(tmp_path: Path, diskfmt: Literal["h5ad", "zarr"])
assert_equal(adata.X, base)


def test_write(tmp_path: Path, diskfmt: Literal["h5ad", "zarr"]):
base = sparse.random(10, 10, format="csr")

f = (
open_write_group(tmp_path / f"parent_store.{diskfmt}", mode="a")
if diskfmt == "zarr"
else h5py.File(tmp_path / f"parent_store.{diskfmt}", "a")
)

ad.io.write_elem(f, "a_sparse_matrix", base)
adata = ad.AnnData(sparse_dataset(f["a_sparse_matrix"]))
ad.io.write_elem(f, "adata", adata)
adata_roundtripped = ad.io.read_elem(f["adata"])
assert_equal(adata_roundtripped.X, base)


def test_backed_sizeof(
ondisk_equivalent_adata: tuple[AnnData, AnnData, AnnData, AnnData],
):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_io_dispatched.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import h5py
import pytest
import scipy.sparse as sp
import zarr

import anndata as ad
Expand All @@ -16,6 +17,7 @@
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path
from typing import Literal


@pytest.mark.zarr_io
Expand Down Expand Up @@ -93,6 +95,24 @@ def test_read_dispatched_null_case(tmp_path: Path):
assert_equal(expected, actual)


@pytest.mark.zarr_io
@pytest.mark.parametrize("sparse_format", ["csr", "csc"])
def test_write_dispatched_csr_dataset(
tmp_path: Path, sparse_format: Literal["csr", "csc"]
):
ad.io.write_elem(
open_write_group(tmp_path / "arr.zarr"),
"/",
sp.random(10, 10, format=sparse_format),
)
X = ad.io.sparse_dataset(zarr.open(tmp_path / "arr.zarr"))

def zarr_writer(func, store, elem_name: str, elem, iospec, dataset_kwargs):
assert iospec.encoding_type == f"{sparse_format}_matrix"

write_dispatched(zarr.open(tmp_path / "check.zarr", mode="w"), "/X", X, zarr_writer)


@pytest.mark.zarr_io
def test_write_dispatched_chunks(tmp_path: Path):
from itertools import chain, repeat
Expand Down