Skip to content

Commit

Permalink
Merge pull request #2131 from single-cell-data/xan/early-error-datafr…
Browse files Browse the repository at this point in the history
…ame-on-ingest

[python] Add type checks to `obsm`, `obsp`, `varm` and `varp` early in ingestion
  • Loading branch information
XanthosXanthopoulos authored Feb 13, 2024
2 parents 798ad5c + ac10e71 commit ef6cb19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions apis/python/src/tiledbsoma/io/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from anndata._core import file_backing
from anndata._core.sparse_dataset import SparseDataset
from somacore.options import PlatformConfig
from typing_extensions import get_args

from .. import (
Collection,
Expand Down Expand Up @@ -421,6 +422,30 @@ def from_anndata(
"Second argument is not an AnnData object -- did you want from_h5ad?"
)

for key in anndata.obsm.keys():
if not isinstance(anndata.obsm[key], get_args(Matrix)):
raise TypeError(
f"obsm value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}"
)

for key in anndata.obsp.keys():
if not isinstance(anndata.obsp[key], get_args(Matrix)):
raise TypeError(
f"obsp value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}"
)

for key in anndata.varm.keys():
if not isinstance(anndata.varm[key], get_args(Matrix)):
raise TypeError(
f"varm value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}"
)

for key in anndata.varp.keys():
if not isinstance(anndata.varp[key], get_args(Matrix)):
raise TypeError(
f"varp value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}"
)

# For single ingest (no append):
#
# * obs, var, X, etc array indices map 1-1 to soma_joinid, soma_dim_0, soma_dim_1, etc.
Expand Down
10 changes: 10 additions & 0 deletions apis/python/tests/test_basic_anndata_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,3 +1134,13 @@ def test_index_names_io(tmp_path, obs_index_name, var_index_name):
assert bdata.var.index.name is None
else:
assert adata.var.index.name == bdata.var.index.name

def test_obsm_data_type(adata):
tempdir = tempfile.TemporaryDirectory()
soma_path = tempdir.name
bdata = anndata.AnnData(X=adata.X, obs=adata.obs, var=adata.var, obsm={"testing": adata.obs})

with pytest.raises(TypeError):
tiledbsoma.io.from_anndata(soma_path, bdata, measurement_name="RNA")

assert not any(Path(soma_path).iterdir())

0 comments on commit ef6cb19

Please sign in to comment.