Skip to content

Commit

Permalink
uns i/o improvements
Browse files Browse the repository at this point in the history
- `_extract_uns`: use `_read_dataframe` (restore index, rm `soma_joinid` on outgest)
- don't mutate users' `uns` DataFrames in `from_anndata`
  • Loading branch information
ryan-williams committed Aug 13, 2024
2 parents 0f9c9ab + cb05d55 commit 58fd462
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
3 changes: 2 additions & 1 deletion apis/python/src/tiledbsoma/io/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 2 additions & 14 deletions apis/python/tests/test_basic_anndata_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import tiledbsoma
import tiledbsoma.io
from tiledbsoma import SOMA_JOINID, Experiment, _constants, _factory
from tiledbsoma import Experiment, _constants, _factory
from tiledbsoma._soma_object import SOMAObject
from tiledbsoma.io._common import _TILEDBSOMA_TYPE
import tiledb
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 58fd462

Please sign in to comment.