Skip to content

[Math] Make quantile1st/quantile3rd total for n<3; fix SummaryStatistics on small inputs (#9659) - #9664

Merged
timosachsenberg merged 1 commit into
developfrom
fix/quantile-small-n-9659
Jun 25, 2026
Merged

timosachsenberg merged 1 commit into
developfrom
fix/quantile-small-n-9659

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #9659. Math::quantile1st, Math::quantile3rd, and therefore Math::SummaryStatistics threw Exception::InvalidRange for input ranges of size 1 or 2 (they take the median of a half of the range, which is empty for n ≤ 2). For n ≥ 3 they already worked. This is a general-purpose statistics utility, so any caller handed a 1–2 element range was a latent crash — it surfaced as a ProteomicsLFQ acceptor run with exactly 2 donor features aborting via PIP-ECHO (#9647).

Changes (src/openms/include/OpenMS/MATH/StatisticFunctions.h)

  • quantile1st: for size < 3 return the minimum (the lower half is empty).
  • quantile3rd: for size < 3 return the maximum (the upper half is empty).
  • SummaryStatistics: variance divides by (n-1) and is undefined for a single value. Now that the quantile helpers no longer throw for n==1, guard it to report 0.0 (as in the empty case) instead of a silent NaN.
  • Doc comments updated (and the stale @exception ... NULL corrected to empty).

Why min / max (not Type-7 interpolation)

The fix preserves the existing algorithm's behavior. I cross-checked the convention against NumPy (all 9 Hyndman–Fan types), Python's statistics.quantiles, and the median-of-halves variants:

  • n = 1: every method that is defined returns the single value.
  • n = 2: every median-of-halves method (Moore-McCabe, Tukey hinges) and 7 of the 9 Hyndman–Fan types return Q1 = min, Q3 = max; only pure type-7 linear interpolation differs, and these helpers are not type-7.

Crucially, OpenMS's own algorithm already returns min/max at n = 3 and n = 4, so extending n ≤ 2 to min/max is continuous with existing behavior rather than a new convention. Routing through the newer interpolation-based quantile(begin, end, q) helper was rejected because it produces different values for n ≥ 3 (e.g. n=10 → 7.25/14.5 vs. the pinned 6.5/15.5) and would change the existing test and downstream reference outputs.

The n ≥ 3 code path is byte-identical, so pinned quantile values and reference outputs are unaffected.

Tests (StatisticFunctions_test.cpp)

  • quantile1st / quantile3rd at n = 1, 2 (sorted and unsorted), 3, 4, and empty-still-throws.
  • New SummaryStatistics section at n = 1, 2, 3, and empty (this class had no prior test coverage).

Verified: StatisticFunctions_test, FileInfo_test, PosteriorErrorProbabilityModel_test (direct helper callers), and TargetedExperiment_test (SummaryStatistics consumer) all pass against a freshly rebuilt library.

Note (out of scope)

The even-n -1/+1 offsets make these helpers a slightly non-standard median-of-halves variant (n=4 → min/max where Moore-McCabe/Tukey give 1.5/3.5). That quirk is load-bearing for the existing pinned test values, so it is intentionally left unchanged here.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved quantile calculations for small datasets so first and third quartiles now return sensible results for 1–2 values.
    • Prevented invalid variance results for single-value summaries by returning 0.0 when there is not enough data.
    • Empty inputs continue to be handled safely with the expected error behavior.
  • Tests

    • Expanded coverage for quartile and summary statistics behavior across empty, small, and unsorted inputs.

…ics on small inputs (#9659)

Math::quantile1st/quantile3rd took the median of an empty half for input
ranges of size 1 or 2, throwing Exception::InvalidRange. Math::SummaryStatistics
calls both unconditionally and inherited the crash (e.g. a ProteomicsLFQ
acceptor run with exactly 2 donor features aborted via PIP-ECHO, PR #9647).

- quantile1st: return the minimum for size < 3 (the lower half is empty);
  quantile3rd: return the maximum. This matches the median-of-halves
  convention and is continuous with the method's own size-3 and size-4
  results, which already yield min/max.
- SummaryStatistics: variance divides by (n-1) and is undefined for a single
  value; report 0.0 (as in the empty case) instead of a silent NaN now that
  the quantile calls no longer throw for n==1.
- Add StatisticFunctions_test coverage for quantile1st/quantile3rd at
  n = 1..4 and empty, plus a new SummaryStatistics section (previously
  untested).

The n>=3 code path is unchanged, so the pinned quantile values and downstream
reference outputs are unaffected.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 92c38347-077e-4920-ab8c-bac9d88073b2

📥 Commits

Reviewing files that changed from the base of the PR and between 34ddcb1 and 5f51f81.

📒 Files selected for processing (2)
  • src/openms/include/OpenMS/MATH/StatisticFunctions.h
  • src/tests/class_tests/openms/source/StatisticFunctions_test.cpp

📝 Walkthrough

Walkthrough

quantile1st and quantile3rd now handle 1–2 element ranges directly, SummaryStatistics returns 0.0 variance for counts of 0 or 1, and tests cover the updated small-range behavior.

Changes

Statistic edge-case fixes

Layer / File(s) Summary
Small-range quantiles and variance
src/openms/include/OpenMS/MATH/StatisticFunctions.h, src/tests/class_tests/openms/source/StatisticFunctions_test.cpp
quantile1st and quantile3rd return the first or last element for ranges of size 1–2, SummaryStatistics sets variance to 0.0 when count <= 1, and the class test covers the updated quantile and summary-statistics cases.

Poem

A rabbit nibbled at the math,
And smoothed the tiny-range path.
One hop for min, one hop for max,
No variance gremlins left to track.
Thump, thump — the stats now stay on track.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: making quantile1st/quantile3rd total for small inputs and updating SummaryStatistics.
Linked Issues check ✅ Passed The changes address #9659 by handling 1-2 element ranges in both quantile helpers, preventing SummaryStatistics crashes, and adding the requested tests.
Out of Scope Changes check ✅ Passed No unrelated changes stand out; the variance guard and documentation updates are directly tied to the small-input statistics fix.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/quantile-small-n-9659

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@timosachsenberg
timosachsenberg requested a review from cbielow June 25, 2026 20:37
@timosachsenberg
timosachsenberg merged commit efcfa6a into develop Jun 25, 2026
44 checks passed
@timosachsenberg
timosachsenberg deleted the fix/quantile-small-n-9659 branch June 25, 2026 20:37
timosachsenberg added a commit that referenced this pull request Jun 26, 2026
Documents PRs that landed after the last CHANGELOG update (#9656):
- #9647 PIP-ECHO match-between-runs algorithm in ProteomicsLFQ
- #9666 ProteomicsLFQ mzTab crash on non-string search settings
- #9658 ProteinQuantifier wrong per-file split on merged ID runs (fixes #5518)
- #9664 Math::quantile1st/3rd crash on 1–2 element inputs (fixes #9659)


Claude-Session: https://claude.ai/code/session_01DYJ6LYHiJADSGjCkfBBJiN

Co-authored-by: Claude <[email protected]>
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.

Math::SummaryStatistics / quantile1st / quantile3rd throw InvalidRange on inputs of size 1 or 2

1 participant