-
Notifications
You must be signed in to change notification settings - Fork 675
feat: add n_obs_aggregated column to aggregated AnnData output #3824
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
dc7d517
ecb957d
230cedd
f0b4721
637520e
8eecad0
67ae802
863639e
1b7b162
aee5134
360e6b6
2780054
add648c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add n\_obs\_aggregated to sc.get.aggregate output to show the total number of observations aggregated per group. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,3 +544,38 @@ def test_factors(): | |
|
|
||
| res = sc.get.aggregate(adata, by=["a", "b", "c", "d"], func="sum") | ||
| np.testing.assert_equal(res.layers["sum"], adata.X) | ||
|
|
||
|
|
||
| def test_aggregate_adds_n_obs_aggregated_single_key(pbmc_adata): | ||
| result = sc.get.aggregate(pbmc_adata, by="louvain", func="mean") | ||
| # Check column exists | ||
| assert "n_obs_aggregated" in result.obs | ||
| # Counts should be positive | ||
| assert (result.obs["n_obs_aggregated"] > 0).all() | ||
|
||
| # Total counts should equal original n_obs | ||
| assert result.obs["n_obs_aggregated"].sum() == pbmc_adata.n_obs | ||
|
||
|
|
||
|
|
||
| def test_aggregate_adds_n_obs_aggregated_multiple_keys(pbmc_adata): | ||
| pbmc_adata.obs["percent_mito_binned"] = pd.cut( | ||
| pbmc_adata.obs["percent_mito"], bins=5 | ||
| ) | ||
| result = sc.get.aggregate( | ||
| pbmc_adata, by=["louvain", "percent_mito_binned"], func="mean" | ||
| ) | ||
|
||
| assert "n_obs_aggregated" in result.obs | ||
| # Still sums back to the total number of obs | ||
| assert result.obs["n_obs_aggregated"].sum() == pbmc_adata.n_obs | ||
|
||
|
|
||
|
|
||
| def test_aggregate_n_obs_aggregated_no_empty_groups(pbmc_adata): | ||
| # Force a categorical with unused categories | ||
| pbmc_adata.obs["fake_group"] = pd.Categorical( | ||
| ["A"] * (pbmc_adata.n_obs - 1) + ["B"], categories=["A", "B", "C"] | ||
| ) | ||
| result = sc.get.aggregate(pbmc_adata, by="fake_group", func="mean") | ||
| assert "n_obs_aggregated" in result.obs | ||
| # Only groups with data should appear | ||
| assert set(result.obs["fake_group"]) == {"A", "B"} | ||
|
||
| # Count check | ||
| assert result.obs["n_obs_aggregated"].sum() == pbmc_adata.n_obs | ||
|
||
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.
no?