Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve move_to_x #661

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ehrapy/anndata/anndata_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,21 @@ def delete_from_obs(adata: AnnData, to_delete: list[str]) -> AnnData:
return adata


def move_to_x(adata: AnnData, to_x: list[str] | str) -> AnnData:
def move_to_x(
adata: AnnData,
to_x: list[str] | str,
copy_uns: bool = True,
copy_obsm: bool = True,
copy_varm: bool = True,
) -> AnnData:
"""Move features from obs to X inplace.

Args:
adata: The AnnData object
to_x: The columns to move to X
copy: Whether to return a copy or not
copy_uns: Whether to copy .uns from the original AnnData object to the new one
copy_obsm: Whether to copy .obsm from the original AnnData object to the new one
copy_varm: Whether to copy .varm from the original AnnData object to the new one

Returns:
A new AnnData object with moved columns from obs to X. This should not be used for datetime columns currently.
Expand Down Expand Up @@ -318,6 +326,12 @@ def move_to_x(adata: AnnData, to_x: list[str] | str) -> AnnData:
# AnnData's concat discards var if they dont match in their keys, so we need to create a new var
created_var = _create_new_var(adata, cols_not_in_x)
new_adata.var = pd.concat([adata.var, created_var], axis=0)
if copy_uns:
new_adata.uns = adata.uns
if copy_obsm:
new_adata.obsm = adata.obsm
if copy_varm:
new_adata.varm = adata.varm

logg.info(f"Added `{cols_not_in_x}` features to `X`.")
else:
Expand Down
21 changes: 21 additions & 0 deletions tests/anndata/test_anndata_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ def test_move_to_x(adata_move_obs_mix):
)


def test_move_to_x_copy_uns(adata_move_obs_mix):
move_to_obs(adata_move_obs_mix, ["name"], copy_obs=True)
adata_move_obs_mix.uns["test"] = "test"
new_adata = move_to_x(adata_move_obs_mix, ["name"], copy_uns=True)
assert "test" in new_adata.uns


def test_move_to_x_copy_obsm(adata_move_obs_mix):
move_to_obs(adata_move_obs_mix, ["name"], copy_obs=True)
adata_move_obs_mix.obsm["test"] = np.random.rand(adata_move_obs_mix.n_obs, 5)
new_adata = move_to_x(adata_move_obs_mix, ["name"], copy_obsm=True)
assert "test" in new_adata.obsm


def test_move_to_x_copy_varm(adata_move_obs_mix):
move_to_obs(adata_move_obs_mix, ["name"], copy_obs=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing copy_obs=True here means that "name" is kept as variable; so the move_to_x doesn't actually move it back; hence the failing example in my comment was a bit hidden :)

adata_move_obs_mix.varm["test"] = np.random.rand(adata_move_obs_mix.n_vars, 5)
new_adata = move_to_x(adata_move_obs_mix, ["name"], copy_varm=True)
assert "test" in new_adata.varm


def test_move_to_x_invalid_column_names(adata_move_obs_mix):
move_to_obs(adata_move_obs_mix, ["name"], copy_obs=True)
move_to_obs(adata_move_obs_mix, ["clinic_id"], copy_obs=False)
Expand Down
Loading