Skip to content

Add composition_test(sample_col=) and say what the default p-values assume - #150

Merged
shanikawm merged 3 commits into
mainfrom
fix/composition-sample-col-144
Oct 7, 2026
Merged

shanikawm merged 3 commits into
mainfrom
fix/composition-sample-col-144

Conversation

@shanikawm

Copy link
Copy Markdown
Contributor

Fixes #144.

The Fisher test per group counts every cell as an independent observation, so with thousands of cells nearly every difference is significant, including one that is only donor-to-donor variation. In the issue's null simulation (12 donors, 6 against 6, each with its own cell-type proportions, no condition effect) the default reports a significant cell type in 35 of 40 runs, which I reproduced exactly. The unit that replicates is the donor.

What changed

  • sample_col=, as the issue's second suggestion. It names the sample each cell came from. Each group's proportion in each sample is compared between the two levels with a two-sided Mann-Whitney test, BH-adjusted across groups as before. It is opt-in, and the default path is unchanged.
  • The docstring now says what the default p-values assume (the issue's first suggestion), and who should use sample_col. The spatial skill's example uses it and says why, and the api-map signature has it.
  • A warning for a design too small to reach significance, using the exact bound: the smallest two-sided p a Mann-Whitney test can give is 2 / C(n1 + n2, n1), which is 0.1 for three samples against three, and 0.057 for three against four. Past 0.05 nothing can come out significant, however large the difference. I checked the formula against scipy for five designs.

Judgement calls, in case you want any of them different

  • Mann-Whitney, not a model. It is what the issue's own contrast used, needs only scipy, and makes no distributional assumption about proportions. A beta-binomial or a mixed model would also handle covariates such as age and site, which the IBD data confounds with condition, but that is a larger feature.
  • In sample mode prop_<level> is the mean of the per-sample proportions, odds_ratio and chisq_p are not reported (they are cell-level quantities), and df.attrs carries method, sample_col and n_samples. df.attrs['method'] is also set on the default path ("fisher_exact"); nothing else there changes.
  • A sample in both levels is an error, naming the offenders, since the comparison would not be between samples. A cell with no sample id is left out up front, so the result does not depend on the pandas version (astype(str) would make it a sample called "nan" on pandas 2 and drop it on pandas 3), and a level none of whose cells has a sample is a clear ValueError rather than scipy's.

One bug the tests found on the way

scipy returns NaN, not an error, when every value is identical (a group with the same proportion in every sample). _bh then turned every adjusted p-value into NaN. That case is now p = 1, and there is a test for it.

Verification

  • The issue's simulation, 40 runs: default 35, sample_col="donor" 1 (the nominal 5% is 2), no spurious warnings. It is not just conservative: a cell type made three times as common in one condition is found in 10 of 10 runs.
  • 14 tests in tests/test_composition_samples.py: the null simulation (with a precondition that it can show the problem), the table checked against scipy.stats.mannwhitneyu computed independently, the errors, the warning at 6, 7, 8 and 12 donors, the NaN case, and cells with no sample.
  • Nine mutants, each failing its own test: sample mode falling back to cells (the original bug), scipy's NaN left in, no nesting check, the warning never or always firing, the group means swapped, a one-sided test, cells with no sample not filtered, and no empty-level guard.
  • Full suite locally on the merged base: 1816 passed, 30 skipped, including the strict docs build. ruff unchanged on composition.py (2), mypy at its 4 baseline errors.

No release is needed for this change on its own. The CHANGELOG entry is under [Unreleased] → Added.

🤖 Generated with Claude Code

shanikawm and others added 3 commits October 7, 2026 21:52
…ssume (#144)

The Fisher test per group counts every cell as an independent observation, so with
thousands of cells nearly every difference is significant, including one that is
only donor-to-donor variation. In the issue's null simulation (12 donors, 6
against 6, each with its own cell-type proportions, no condition effect) the
default reported a significant cell type in 35 of 40 runs. The unit that replicates
is the donor.

sample_col names the sample each cell came from. Each group's proportion in each
sample is compared between the two levels with a two-sided Mann-Whitney test, BH
adjusted across groups as before. In the same simulation that is 1 significant run
in 40, and a cell type three times as common in one condition is still found in 10
of 10. prop_<level> becomes the mean of the per-sample proportions; odds_ratio and
chisq_p are cell-level and are not reported. A sample in both levels is an error,
and a design too small to reach p < 0.05, such as three against three, warns using
the exact bound 2 / C(n1 + n2, n1). scipy returns NaN when every value is
identical, which would have turned every adjusted p-value into NaN, so that is p = 1.

The default path is unchanged apart from the docstring and attrs['method']. The
shared tail (BH, labels, direction, order) is one helper both paths use. Seven
mutants of the new mode each fail their own test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… when a level has none (#144)

A missing sample id went through astype(str), which makes it a sample called "nan"
on pandas 2 and drops it on pandas 3, and either way its cell was still counted in
n_<level>. Such cells are now dropped before anything is computed, so the result
does not depend on the pandas version, and the group set and the cell counts follow
the cells actually used.

A level none of whose cells has a sample made scipy raise "x and y must be of nonzero
size". That is now a ValueError naming the level. Both are mutation-tested.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@shanikawm
shanikawm merged commit c0d6af4 into main Oct 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

composition_test treats cells as replicates: 35 of 40 null simulations report a significant cell type

1 participant