From 760a322329f30f975d705faa0b85688d9e61cf65 Mon Sep 17 00:00:00 2001 From: colganwi Date: Mon, 2 Dec 2024 14:24:33 -0500 Subject: [PATCH] h5ad without X --- CHANGELOG.md | 10 ++++++++++ pyproject.toml | 2 +- src/treedata/_core/read.py | 3 ++- tests/test_readwrite.py | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2847c25..07d53ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ and this project adheres to [Semantic Versioning][]. ### Fixed +## [0.1.2] - 2024-12-02 + +### Added + +### Changed + +### Fixed + +- Fixed `KeyError: "Unable to synchronously open object (object 'X' doesn't exist)"'` when reading h5ad without X field (#40) + ## [0.1.1] - 2024-11-25 ### Added diff --git a/pyproject.toml b/pyproject.toml index bd19974..da585fe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ requires = ["hatchling"] [project] name = "treedata" -version = "0.1.1" +version = "0.1.2" description = "anndata with trees" readme = "README.md" requires-python = ">=3.10" diff --git a/src/treedata/_core/read.py b/src/treedata/_core/read.py index 3bd9466..13ef36a 100755 --- a/src/treedata/_core/read.py +++ b/src/treedata/_core/read.py @@ -76,7 +76,8 @@ def _read_tdata(f, filename, backed) -> dict: backed = "r" # Read X if not backed if not backed: - d["X"] = _read_elem(f["X"]) + if "X" in f: + d["X"] = _read_elem(f["X"]) else: d.update({"filename": filename, "filemode": backed}) # Read standard elements diff --git a/tests/test_readwrite.py b/tests/test_readwrite.py index 1b5bf2c..3ce3c1c 100755 --- a/tests/test_readwrite.py +++ b/tests/test_readwrite.py @@ -102,6 +102,14 @@ def test_read_anndata(X, tmp_path): assert tdata.obst_keys() == [] +def test_read_no_X(X, tmp_path): + tdata = td.TreeData(obs=pd.DataFrame(index=["0", "1", "2"])) + file_path = tmp_path / "test.h5ad" + tdata.write_h5ad(file_path) + tdata2 = td.read_h5ad(file_path) + assert tdata2.X is None + + def test_h5ad_backing(tdata, tree, tmp_path): tdata_copy = tdata.copy() assert not tdata.isbacked