-
Notifications
You must be signed in to change notification settings - Fork 175
Add adapt_vars_like to align .var between AnnData objects (issue #1697) #1986
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
Open
amalia-k510
wants to merge
19
commits into
scverse:main
Choose a base branch
from
amalia-k510:gene_panel_selection_1697
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
06bb519
gene panel selection feature
amalia-k510 24da345
comments fix
amalia-k510 74b3ebd
import error fix
amalia-k510 aa2295f
import error fix and init script update to make new fxn accessible
amalia-k510 16264bf
doc string fix and api.md added
amalia-k510 46b728f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c09cf63
typo fix
amalia-k510 d1910d4
Merge branch 'gene_panel_selection_1697' of https://github.com/amalia…
amalia-k510 0dd7878
Switch to reindex
amalia-k510 5d6279e
tests and manual fix for the missing genes case
amalia-k510 a14af7f
test fix and comments
amalia-k510 a30c365
reindexer fix
amalia-k510 fa48833
Update __init__.py
amalia-k510 11fdb50
import error and spelling error
amalia-k510 715fd0f
AxisStorable implementaiton
amalia-k510 63a2933
adding new_varp and new_obsp for consistency
amalia-k510 8c30646
target change to None and test for it
amalia-k510 ac4c067
testing all aspects
amalia-k510 3a65f62
Merge branch 'main' into gene_panel_selection_1697
ilan-gold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,5 @@ def __getattr__(attr_name: str) -> Any: | |
"settings", | ||
"types", | ||
"typing", | ||
"utils", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,14 @@ | |
|
||
from itertools import repeat | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
from scipy import sparse | ||
|
||
import anndata as ad | ||
from anndata.tests.helpers import gen_typed_df | ||
from anndata.utils import make_index_unique | ||
from anndata.utils import adapt_vars_like, make_index_unique | ||
|
||
|
||
def test_make_index_unique() -> None: | ||
|
@@ -57,3 +58,127 @@ def test_adata_unique_indices(): | |
|
||
pd.testing.assert_index_equal(v.obsm["df"].index, v.obs_names) | ||
pd.testing.assert_index_equal(v.varm["df"].index, v.var_names) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("source", "target", "expected_X"), | ||
[ | ||
pytest.param( | ||
ad.AnnData(X=np.ones((1, 3)), var=pd.DataFrame(index=["a", "b", "c"])), | ||
ad.AnnData( | ||
X=np.array([[1, 2, 3]]), var=pd.DataFrame(index=["a", "b", "c"]) | ||
), | ||
np.array([[1, 2, 3]]), | ||
id="exact_match", | ||
), | ||
pytest.param( | ||
ad.AnnData(X=np.ones((1, 3)), var=pd.DataFrame(index=["a", "b", "c"])), | ||
ad.AnnData( | ||
X=np.array([[3, 2, 1]]), var=pd.DataFrame(index=["c", "b", "a"]) | ||
), | ||
np.array([[1, 2, 3]]), | ||
id="different_order", | ||
), | ||
], | ||
) | ||
def test_adapt_vars(source, target, expected_X): | ||
output = adapt_vars_like(source, target) | ||
np.testing.assert_array_equal(output.X, expected_X) | ||
assert list(output.var_names) == list(source.var_names) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("source", "target", "fill_value", "expected_X"), | ||
[ | ||
pytest.param( | ||
ad.AnnData(X=np.ones((1, 2)), var=pd.DataFrame(index=["g1", "g2"])), | ||
ad.AnnData(X=np.array([[7, 8]]), var=pd.DataFrame(index=["g3", "g4"])), | ||
0.5, | ||
np.array([[0.5, 0.5]]), | ||
id="no_shared_genes", | ||
), | ||
pytest.param( | ||
ad.AnnData(X=np.ones((1, 3)), var=pd.DataFrame(index=["g1", "g2", "g3"])), | ||
ad.AnnData(X=np.array([[1, 3]]), var=pd.DataFrame(index=["g1", "g3"])), | ||
-1, | ||
np.array([[1, -1, 3]]), | ||
id="missing_genes", | ||
), | ||
], | ||
) | ||
def test_adapt_vars_with_fill_value(source, target, fill_value, expected_X): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's merge this test and |
||
output = adapt_vars_like(source, target, fill_value=fill_value) | ||
np.testing.assert_array_equal(output.X, expected_X) | ||
assert list(output.var_names) == list(source.var_names) | ||
|
||
|
||
def test_adapt_vars_target_X_none(): | ||
source = ad.AnnData( | ||
X=np.ones((2, 2)), | ||
var=pd.DataFrame(index=["g1", "g2"]), | ||
) | ||
target = ad.AnnData( | ||
X=None, | ||
var=pd.DataFrame(index=["g2", "g3"]), | ||
obs=pd.DataFrame(index=["cell1", "cell2"]), | ||
) | ||
output = adapt_vars_like(source, target, fill_value=-1) | ||
assert output.X is None | ||
assert list(output.var_names) == list(source.var_names) | ||
|
||
|
||
def test_adapt_vars_all_objects(): | ||
source = ad.AnnData( | ||
X=np.ones((2, 3)), | ||
var=gen_typed_df(3, index=pd.Index(["a", "b", "c"])), | ||
) | ||
|
||
target = ad.AnnData( | ||
X=np.array([[1, 3], [2, 4]]), | ||
var=gen_typed_df(2, index=pd.Index(["a", "c"])), | ||
obs=pd.DataFrame(index=["cell1", "cell2"]), | ||
varm={"varm_key": np.array([[10, 11], [30, 31]])}, | ||
varp={"varp_key": np.array([[1, 2], [3, 4]])}, | ||
obsp={"obsp_key": np.array([[5, 6], [7, 8]])}, | ||
layers={"layer1": np.array([[1000, 3000], [1001, 3001]])}, | ||
) | ||
|
||
output = adapt_vars_like(source, target, fill_value=-1) | ||
|
||
expected_X = np.array( | ||
[ | ||
[1, -1, 3], | ||
[2, -1, 4], | ||
] | ||
) | ||
np.testing.assert_array_equal(output.X, expected_X) | ||
assert list(output.var_names) == ["a", "b", "c"] | ||
|
||
expected_layer = np.array( | ||
[ | ||
[1000, -1, 3000], | ||
[1001, -1, 3001], | ||
] | ||
) | ||
np.testing.assert_array_equal(output.layers["layer1"], expected_layer) | ||
|
||
expected_varm = np.array( | ||
[ | ||
[10, 11], | ||
[-1, -1], | ||
[30, 31], | ||
] | ||
) | ||
np.testing.assert_array_equal(output.varm["varm_key"], expected_varm) | ||
|
||
expected_varp = np.array( | ||
[ | ||
[1, -1, 2], | ||
[-1, -1, -1], | ||
[3, -1, 4], | ||
] | ||
) | ||
np.testing.assert_array_equal(output.varp["varp_key"], expected_varp) | ||
|
||
expected_obsp = np.array([[5, 6], [7, 8]]) | ||
np.testing.assert_array_equal(output.obsp["obsp_key"], expected_obsp) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.