-
Notifications
You must be signed in to change notification settings - Fork 774
fix: correct binary logistic marker score direction #4348
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
dnncha
wants to merge
3
commits into
scverse:main
Choose a base branch
from
dnncha:fix/binary-logreg-marker-direction
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.
+69
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Correct the direction of binary logistic-regression marker scores in `tl.rank_genes_groups` when the reported group is the classifier’s first class {smaller}`D O’Toole` |
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,3 +62,66 @@ def test_rank_genes_groups_with_unsorted_groups(): | |
| bdata.uns["rank_genes_groups"]["scores"]["Three"] | ||
| ).to_numpy() | ||
| np.testing.assert_equal(array_ad, array_bd) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("categories", [["A", "unused", "B"], ["B", "unused", "A"]]) | ||
| @pytest.mark.parametrize("target", ["A", "B"]) | ||
| @pytest.mark.parametrize("representation", ["dense", "sparse", "raw", "layer"]) | ||
|
Member
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. please remove the |
||
| def test_binary_logreg_scores_point_toward_requested_group( | ||
| categories, target, representation | ||
| ): | ||
| from anndata import AnnData | ||
|
Member
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. already imported in the module |
||
| from scipy.sparse import csr_matrix # noqa: TID251 | ||
|
|
||
| x = np.vstack([ | ||
| np.tile([10.0, 0.0, 1.0], (30, 1)), | ||
| np.tile([0.0, 10.0, 1.0], (30, 1)), | ||
| ]) | ||
| adata = AnnData( | ||
| x, | ||
| obs=pd.DataFrame( | ||
| {"group": pd.Categorical(["A"] * 30 + ["B"] * 30, categories=categories)}, | ||
| index=[f"cell_{i}" for i in range(60)], | ||
| ), | ||
| var=pd.DataFrame(index=["A_marker", "B_marker", "shared"]), | ||
| ) | ||
| kwargs = {"use_raw": False} | ||
| if representation == "sparse": | ||
| adata.X = csr_matrix(adata.X) | ||
| elif representation == "raw": | ||
| adata.raw = adata.copy() | ||
| adata.X = np.zeros_like(x) | ||
| kwargs = {"use_raw": True} | ||
| elif representation == "layer": | ||
| adata.layers["expression"] = adata.X.copy() | ||
| adata.X = np.zeros_like(x) | ||
| kwargs["layer"] = "expression" | ||
| reference = "B" if target == "A" else "A" | ||
| sc.tl.rank_genes_groups( | ||
| adata, "group", groups=[target], reference=reference, method="logreg", **kwargs | ||
| ) | ||
| result = adata.uns["rank_genes_groups"] | ||
| names = result["names"][target] | ||
| scores = dict(zip(names, result["scores"][target], strict=True)) | ||
| assert names[0] == f"{target}_marker" | ||
| assert scores[f"{target}_marker"] > 0 | ||
| assert scores[f"{reference}_marker"] < 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("categories", [["A", "B"], ["B", "A"]]) | ||
| def test_binary_logreg_default_group_direction(categories): | ||
| from anndata import AnnData | ||
|
|
||
| adata = AnnData( | ||
| np.vstack([np.tile([10.0, 0.0], (20, 1)), np.tile([0.0, 10.0], (20, 1))]), | ||
| obs=pd.DataFrame( | ||
| {"group": pd.Categorical(["A"] * 20 + ["B"] * 20, categories=categories)}, | ||
| index=[f"cell_{i}" for i in range(40)], | ||
| ), | ||
| var=pd.DataFrame(index=["A_marker", "B_marker"]), | ||
| ) | ||
| sc.tl.rank_genes_groups(adata, "group", method="logreg", use_raw=False) | ||
| result = adata.uns["rank_genes_groups"] | ||
| target = categories[0] | ||
| assert result["names"][target][0] == f"{target}_marker" | ||
| assert result["scores"][target][0] > 0 | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.