Repository navigation
feat: write support for keys that aren’t file names - #2662
Conversation
eb84892 to
6a98023
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2662 +/- ##
==========================================
- Coverage 88.00% 86.55% -1.46%
==========================================
Files 51 51
Lines 8454 8603 +149
==========================================
+ Hits 7440 7446 +6
- Misses 1014 1157 +143
|
6a98023 to
2d67fe1
Compare
ilan-gold
left a comment
There was a problem hiding this comment.
Discussion is a bit mangled now, but scverse/zarr-convention-dataframe#1 is quite a strong suggestion that seems simpler to implement. Maybe let's discuss there and then I will implement there (and you here) whatever mechanism we settle on (your implementation here of other data frame things does not have to match mine otherwise)
622fd5a to
20b6199
Compare
0428361 to
ed0ed9c
Compare
Documentation build overview
159 files changed ·
|
4aee430 to
4c77cfe
Compare
4c77cfe to
8fd7652
Compare
8fd7652 to
b0850ab
Compare
Co-authored-by: Ilan Gold <[email protected]>
There was a problem hiding this comment.
Plus here is a Claude adverserial report
-
1.
write_usewith a column named""overwrites the obs indexAn unnamed index is stored as
"", but_resolve_usecompares againstidx.name(which reads back asNone), sonames.index("")returns the index's position and the column data replaces the index.Test
def test_write_use_empty_col_keeps_index(tmp_path, fmt): p = tmp_path / f"t.{fmt}" adata = ad.AnnData(np.zeros((2, 3)), obs=pd.DataFrame({"a": [1, 2]}, index=["c1", "c2"])) write(adata, p, fmt, compat="0.14") adata.obs[""] = [7, 8] write(adata, p, fmt, use=A.obs[""], compat="0.14") pd.testing.assert_frame_equal(read(p, fmt).obs, adata.obs) # index is now ['7', '8']
-
2.
ad.io.write_eleminto a 0.2.0 dict / 0.3.0 dataframe group silently loses data or corrupts the groupwrite_elem(f["uns"], "new", …)writes a child no reader sees."item_0"overwrites another key's value."keys"/"columns"replaces the names array, so the whole group reads back garbled with no error (read_namesdoesstr(n)on whatever it finds). This should raise and point towrite_use, andread_namesshould reject anything that isn't a string array.Test
@pytest.mark.parametrize("key", ["new", "item_0", "keys"]) def test_write_elem_into_indexed_dict_raises(tmp_path, key): p = tmp_path / "t.h5ad" ad.AnnData(np.zeros((2, 3)), uns={"a": 1}).write_h5ad(p, compat="0.14") with h5py.File(p, "r+") as f, pytest.raises(ValueError): ad.io.write_elem(f["uns"], key, 2) @pytest.mark.parametrize("key", ["new", "column_0", "columns"]) def test_write_elem_into_indexed_df_raises(tmp_path, key): p = tmp_path / "t.h5ad" obs = pd.DataFrame({"a": [1, 2]}, index=["c1", "c2"]) ad.AnnData(np.zeros((2, 3)), obs=obs).write_h5ad(p, compat="0.14") with h5py.File(p, "r+") as f, pytest.raises(ValueError): ad.io.write_elem(f["obs"], key, np.array([3, 4]))
-
3.
read_lazycan't read a file the PR newly allowstest_column_named_indexmakes_indexa normal column name, butread_lazyon that file raisesNotImplementedError, because the unnamed index is renamed to_indexand collides with the column.Test
def test_read_lazy_col_named_index(tmp_path, fmt): pytest.importorskip("xarray") p = tmp_path / f"t.{fmt}" obs = pd.DataFrame({"_index": [1, 2]}, index=["c1", "c2"]) write(ad.AnnData(np.zeros((2, 3)), obs=obs), p, fmt, compat="0.14") lazy = ad.experimental.read_lazy(p) pd.testing.assert_frame_equal(lazy.obs.to_memory(), obs)
-
4. "Any string works" isn't true for h5ad (not sure if this one is important)
h5py variable-length strings reject embedded NUL characters (
ValueError: VLEN strings do not support embedded NULLs). zarr is fine. Either reword the docs or handle it. TheNULin theindexed_keysdocstring is also ambiguous (the Windows device name or the character?).Test
def test_nul_in_key(tmp_path, fmt): # fails for h5ad only p = tmp_path / f"t.{fmt}" write(ad.AnnData(np.zeros((2, 3)), uns={"a\x00b": 1}), p, fmt, compat="0.14") assert list(read(p, fmt).uns) == ["a\x00b"]
-
5. Docs: path access to children no longer works
With
compat="0.14", children of mappings areitem_N, sog["layers/dense"](the recipe in theread_elem_lazydocstring) raisesKeyError, andread_dispatchedcallbacks see/obsm/item_0instead of/obsm/X_pca. Neither the release note norwrite-compat.mdmentions this. A note there would help, ideally with a public way to get a child by key (e.g. exposingmapping_items).Test helpers
import h5py import numpy as np import pandas as pd import pytest import anndata as ad from anndata.acc import A @pytest.fixture(params=["h5ad", "zarr"]) def fmt(request): return request.param def write(adata, path, fmt, **kw): getattr(adata, f"write_{fmt}")(path, **kw) def read(path, fmt): return getattr(ad, f"read_{fmt}")(path)
|
1 & 3: yup, fixing! 2: idk how defensive we should be. 4: I know. Intentional omission, because that’s a HDF5 implementation detail that doesn’t have anything to do with the spec. I thought there was no way to handle it, is there? (also regarding the 5: same as your inline comment, yes! |
ilan-gold
left a comment
There was a problem hiding this comment.
Should scverse/zarr-convention-dataframe#4 be part of this as well?
Co-authored-by: Ilan Gold <[email protected]>
This and the two PRs below it are I need in 0.14 (for scanpy 2.0 preview), except for
.unsaccessors.See also zarr-developers/zarr-specs#128
/and\inobsmkeys,obscolumns,unskeys, … #2450, fixes read_zarr fails with obs columns that differ only by case #2576, fixes Give better errors when writing illegal column names #321