From cb05d55b27ab37a176708da8661cded42d65fcdc Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Fri, 9 Aug 2024 16:43:52 -0400 Subject: [PATCH] don't mutate users' `uns` DataFrames in `from_anndata` --- apis/python/src/tiledbsoma/io/ingest.py | 3 ++- apis/python/tests/test_basic_anndata_io.py | 14 +------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/apis/python/src/tiledbsoma/io/ingest.py b/apis/python/src/tiledbsoma/io/ingest.py index 58520e2dc9..600bad859b 100644 --- a/apis/python/src/tiledbsoma/io/ingest.py +++ b/apis/python/src/tiledbsoma/io/ingest.py @@ -2567,7 +2567,8 @@ def _ingest_uns_node( num_rows = value.shape[0] with _write_dataframe( _util.uri_joinpath(coll.uri, key), - value, + # _write_dataframe modifies passed DataFrame in-place (adding a "soma_joinid" index) + value.copy(), None, axis_mapping=AxisIDMapping.identity(num_rows), **ingest_platform_ctx, diff --git a/apis/python/tests/test_basic_anndata_io.py b/apis/python/tests/test_basic_anndata_io.py index 76b8ada114..5199f73ec6 100644 --- a/apis/python/tests/test_basic_anndata_io.py +++ b/apis/python/tests/test_basic_anndata_io.py @@ -1024,19 +1024,7 @@ def test_uns_io(tmp_path, outgest_uns_keys): soma_uri = tmp_path.as_posix() tiledbsoma.io.from_anndata(soma_uri, adata, measurement_name="RNA") - - # NOTE: `from_anndata` mutates user-provided DataFrames in `uns`, demoting their index to a column named "index", - # and installing a `soma_joinid` index. Here we patch the "expected" adata to reflect this, before comparing to - # the post-`froM_anndata` `adata`. - # TODO: fix `from_anndata` to not modify DataFrames in user-provided `uns`. - expected_adata = deepcopy(adata0) - for k in ["pd_df_indexed", "pd_df_nonindexed"]: - df = expected_adata.uns[k] - expected_adata.uns[k] = df.reset_index().set_index( - pd.Index(list(range(len(df))), name=SOMA_JOINID) - ) - - assert_adata_equal(expected_adata, adata) + assert_adata_equal(adata0, adata) with tiledbsoma.Experiment.open(soma_uri) as exp: adata2 = tiledbsoma.io.to_anndata(