Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize Nones #999

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/release-notes/999.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{data}`None` values can now be serialized to `.h5ad` and `.zarr`,
preserving e.g. {attr}`~anndata.AnnData.uns` structure through saving and loading {user}`flying-sheep`
24 changes: 24 additions & 0 deletions src/anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,30 @@ def write_raw(
_writer.write_elem(g, "varm", dict(raw.varm), dataset_kwargs=dataset_kwargs)


########
# Null #
########


@_REGISTRY.register_read(H5Array, IOSpec("null", "0.1.0"))
@_REGISTRY.register_read(ZarrArray, IOSpec("null", "0.1.0"))
def read_null(_elem, _reader) -> None:
return None


@_REGISTRY.register_write(H5Group, type(None), IOSpec("null", "0.1.0"))
def write_null_h5py(f, k, _v, _writer, dataset_kwargs=MappingProxyType({})):
f.create_dataset(k, data=h5py.Empty("f"), **dataset_kwargs)


@_REGISTRY.register_write(ZarrGroup, type(None), IOSpec("null", "0.1.0"))
def write_null_zarr(f, k, _v, _writer, dataset_kwargs=MappingProxyType({})):
import zarr

# zarr has no first-class null dataset
f.create_dataset(k, data=zarr.empty(()), **dataset_kwargs)


############
# Mappings #
############
Expand Down
3 changes: 0 additions & 3 deletions src/anndata/_io/specs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ def write_elem(

dest_type = type(store)

if elem is None:
return lambda *_, **__: None

# Normalize k to absolute path
if not PurePosixPath(k).is_absolute():
k = str(PurePosixPath(store.name) / k)
Expand Down
1 change: 1 addition & 0 deletions tests/test_io_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def create_sparse_store(
@pytest.mark.parametrize(
("value", "encoding_type"),
[
pytest.param(None, "null", id="none"),
pytest.param("hello world", "string", id="py_str"),
pytest.param(np.str_("hello world"), "string", id="np_str"),
pytest.param(np.array([1, 2, 3]), "array", id="np_arr_int"),
Expand Down
25 changes: 25 additions & 0 deletions tests/test_readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,31 @@ def test_adata_in_uns(tmp_path, diskfmt, roundtrip):
assert_equal(orig, curr)


@pytest.mark.parametrize(
"uns_val",
[
pytest.param(dict(base=None), id="dict_val"),
pytest.param(
pd.DataFrame(dict(col_0=["string", None])),
id="df",
marks=pytest.mark.xfail(
reason="Nullable string arrays not yet implemented"
),
),
],
)
def test_none_dict_value_in_uns(diskfmt, tmp_path, roundtrip, uns_val):
pth = tmp_path / f"adata_dtype.{diskfmt}"

orig = ad.AnnData(np.ones((3, 4)), uns=dict(val=uns_val))
curr = roundtrip(orig, pth)

if isinstance(orig.uns["val"], pd.DataFrame):
pd.testing.assert_frame_equal(curr.uns["val"], orig.uns["val"])
else:
assert curr.uns["val"] == orig.uns["val"]


def test_io_dtype(tmp_path, diskfmt, dtype, roundtrip):
pth = tmp_path / f"adata_dtype.{diskfmt}"

Expand Down
Loading