From b07c5fd2503e5f771fc9ae0507eb53d7de0f7130 Mon Sep 17 00:00:00 2001 From: bkmartinjr Date: Thu, 19 Jan 2023 20:08:32 +0000 Subject: [PATCH 1/2] add test for to_anndata column_names --- apis/python/tests/test_experiment_query.py | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/apis/python/tests/test_experiment_query.py b/apis/python/tests/test_experiment_query.py index 7847a453be..6544e30bb5 100644 --- a/apis/python/tests/test_experiment_query.py +++ b/apis/python/tests/test_experiment_query.py @@ -482,6 +482,113 @@ def test_X_as_series(): ) +@pytest.mark.xfail( + # see comment on test_experiment_query_all + sys.version_info >= (3, 10), + reason="typeguard bug #242", +) +@pytest.mark.parametrize( + "n_obs,n_vars,obsp_layer_names,varp_layer_names", [(101, 99, ["foo"], ["bar"])] +) +def test_experiment_query_column_names(soma_experiment): + """ + Verify that column_names is correctly handled in the various obs/var accessors. + + Returned columsn should be the union of the columns specifically requested via + column_names, and the columns implicitly requested via value_filter. + """ + + # default + with soma_experiment.axis_query("RNA") as query: + assert set(next(query.obs()).column_names) == {"soma_joinid", "label"} + assert set(next(query.var()).column_names) == {"soma_joinid", "label"} + ad = query.to_anndata("raw") + assert set(ad.obs.keys()) == {"soma_joinid", "label"} + assert set(ad.var.keys()) == {"soma_joinid", "label"} + + # column_names only + with soma_experiment.axis_query("RNA") as query: + assert set(next(query.obs(column_names=["soma_joinid"])).column_names) == { + "soma_joinid" + } + assert set(next(query.var(column_names=["soma_joinid"])).column_names) == { + "soma_joinid" + } + ad = query.to_anndata( + "raw", column_names={"obs": ["soma_joinid"], "var": ["soma_joinid"]} + ) + assert set(ad.obs.keys()) == {"soma_joinid"} + assert set(ad.var.keys()) == {"soma_joinid"} + + assert set(next(query.obs(column_names=["label"])).column_names) == {"label"} + assert set(next(query.var(column_names=["label"])).column_names) == {"label"} + ad = query.to_anndata("raw", column_names={"obs": ["label"], "var": ["label"]}) + assert set(ad.obs.keys()) == {"label"} + assert set(ad.var.keys()) == {"label"} + + assert set( + next(query.obs(column_names=["soma_joinid", "label"])).column_names + ) == {"soma_joinid", "label"} + assert set( + next(query.var(column_names=["soma_joinid", "label"])).column_names + ) == {"soma_joinid", "label"} + ad = query.to_anndata( + "raw", + column_names={ + "obs": ["soma_joinid", "label"], + "var": ["soma_joinid", "label"], + }, + ) + assert set(ad.obs.keys()) == {"soma_joinid", "label"} + assert set(ad.var.keys()) == {"soma_joinid", "label"} + + # column_names and value_filter + with soma_experiment.axis_query( + "RNA", + obs_query=soma.AxisQuery( + value_filter="label in [" + ",".join([f"'{i}'" for i in range(101)]) + "]" + ), + var_query=soma.AxisQuery( + value_filter="label in [" + ",".join([f"'{i}'" for i in range(99)]) + "]" + ), + ) as query: + assert set(next(query.obs(column_names=["soma_joinid"])).column_names) == { + "soma_joinid", + "label", + } + assert set(next(query.var(column_names=["soma_joinid"])).column_names) == { + "soma_joinid", + "label", + } + ad = query.to_anndata( + "raw", column_names={"obs": ["soma_joinid"], "var": ["soma_joinid"]} + ) + assert set(ad.obs.keys()) == {"soma_joinid", "label"} + assert set(ad.var.keys()) == {"soma_joinid", "label"} + + assert set(next(query.obs(column_names=["label"])).column_names) == {"label"} + assert set(next(query.var(column_names=["label"])).column_names) == {"label"} + ad = query.to_anndata("raw", column_names={"obs": ["label"], "var": ["label"]}) + assert set(ad.obs.keys()) == {"label"} + assert set(ad.var.keys()) == {"label"} + + assert set( + next(query.obs(column_names=["soma_joinid", "label"])).column_names + ) == {"soma_joinid", "label"} + assert set( + next(query.var(column_names=["soma_joinid", "label"])).column_names + ) == {"soma_joinid", "label"} + ad = query.to_anndata( + "raw", + column_names={ + "obs": ["soma_joinid", "label"], + "var": ["soma_joinid", "label"], + }, + ) + assert set(ad.obs.keys()) == {"soma_joinid", "label"} + assert set(ad.var.keys()) == {"soma_joinid", "label"} + + """ Fixture support & utility functions below. """ From e99e113065e524cb9141a1f3800be01b417ffd56 Mon Sep 17 00:00:00 2001 From: bkmartinjr Date: Thu, 19 Jan 2023 20:28:50 +0000 Subject: [PATCH 2/2] PR feedback fixes --- apis/python/tests/test_experiment_query.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apis/python/tests/test_experiment_query.py b/apis/python/tests/test_experiment_query.py index 6544e30bb5..7060b2c402 100644 --- a/apis/python/tests/test_experiment_query.py +++ b/apis/python/tests/test_experiment_query.py @@ -494,7 +494,7 @@ def test_experiment_query_column_names(soma_experiment): """ Verify that column_names is correctly handled in the various obs/var accessors. - Returned columsn should be the union of the columns specifically requested via + Returned columns should be the union of the columns specifically requested via column_names, and the columns implicitly requested via value_filter. """ @@ -546,10 +546,10 @@ def test_experiment_query_column_names(soma_experiment): with soma_experiment.axis_query( "RNA", obs_query=soma.AxisQuery( - value_filter="label in [" + ",".join([f"'{i}'" for i in range(101)]) + "]" + value_filter="label in [" + ",".join(f"'{i}'" for i in range(101)) + "]" ), var_query=soma.AxisQuery( - value_filter="label in [" + ",".join([f"'{i}'" for i in range(99)]) + "]" + value_filter="label in [" + ",".join(f"'{i}'" for i in range(99)) + "]" ), ) as query: assert set(next(query.obs(column_names=["soma_joinid"])).column_names) == {