Skip to content

feat(backend): B19 paired t-test - #104

Open
az-said wants to merge 1 commit into
mainfrom
feat/backend-b19-welch-ttest
Open

feat(backend): B19 paired t-test#104
az-said wants to merge 1 commit into
mainfrom
feat/backend-b19-welch-ttest

Conversation

@az-said

@az-said az-said commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds POST /api/cohorts/compare running Welch's two-sample t-test on a chosen biochemical metric between two precomputed cohorts.
  • New backend/services/cohort_stats.py with welch_t_test(a, b) → {t, p, df} — thin wrapper around scipy.stats.ttest_ind(equal_var=False). Cleans None / NaN, requires n >= 2 per group, falls back to a manual Welch-Satterthwaite df calc on scipy < 1.11.
  • New backend/api/routes/cohorts.py registered in api/main.py. Reads precomputed JSON from backend/data/precomputed/<id>.json (M2 artifact). Validates: metric is a Literal{muH, hydrophobicity, charge}, a != b, alphanumeric+._- dataset ids only (path-traversal guard), missing precomputed file → 404 with a helpful "run M2" message, n < 2 → 422 (not 500).
  • Adds scipy >= 1.14, < 2.0 to backend/requirements.txt. ~50MB image bump, but unlocks every downstream stats endpoint (rank tests, distribution comparisons) the cohort and ranking workflows will want.

Test plan

  • `pytest backend/tests/test_cohort_stats.py -v` — 11 cases all green
    • 4 unit: scipy parity, NaN/None drop, n<2 rejection, identical-distribution sanity
    • 7 route: happy path, NULL skipping, missing dataset, unsupported metric, identical cohorts, path traversal, too-few-samples
  • Manual: build a stub precomputed file, then
    `curl -X POST http://localhost:8000/api/cohorts/compare -H "Content-Type: application/json" -d '{"a":"peleg_118","b":"uperin_frog","metric":"muH"}'`

Dependency

The route reads from backend/data/precomputed/<id>.json produced by M2. The route returns a clean 404 when the artifact is missing, so the merge order between B19 and M2 is flexible.

Out of scope (per dispatch)

  • No changes to backend/schemas/api_models.py response shapes.
  • No changes to process_upload_dataframe internals.

Closes B19 from `docs/internal/WAVE_2_8_DISPATCH_2026_06_18.md` §3.

Adds Welch's two-sample t-test (unequal variances) for cohort
comparison between precomputed datasets.

services/cohort_stats.py: thin wrapper around scipy.stats.ttest_ind
with the equal_var=False default. Cleans None/NaN before testing
and requires n >= 2 per group (Welch's is undefined otherwise).
Returns {t, p, df}. df falls back to a manual Welch-Satterthwaite
calculation when scipy < 1.11 (no result.df attribute).

api/routes/cohorts.py: POST /api/cohorts/compare with body
{a, b, metric}. Loads backend/data/precomputed/<id>.json (M2
artifact), extracts the metric column, runs the t-test, returns
{a, b, metric, n_a, n_b, t, p, df}.

Validation:
- metric is a Pydantic Literal of {muH, hydrophobicity, charge}
- a == b is a 422 (user mistake, not a server error)
- dataset id is alphanumeric + ._- only (path-traversal guard)
- missing precomputed file is a 404 with a helpful "run M2" message
- n < 2 per group is a 422, not a 500

requirements.txt: pin scipy >= 1.14, < 2.0. Adds ~50MB to the
image but unlocks every downstream stats endpoint (rank tests,
distribution comparisons) we'll want for the cohort and ranking
workflows.

Tests: 11 cases — 4 unit tests on welch_t_test (scipy parity,
NaN handling, n<2 rejection, identical-distribution sanity) and
7 route tests (happy path, NULL skipping, missing dataset,
unsupported metric, identical cohorts, path traversal,
too-few-samples).
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@saidaz24-meet, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 5 minutes and 23 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d47a59f2-2942-47ac-b024-7bf08ec30e3c

📥 Commits

Reviewing files that changed from the base of the PR and between 834d90a and 76a6386.

📒 Files selected for processing (5)
  • backend/api/main.py
  • backend/api/routes/cohorts.py
  • backend/requirements.txt
  • backend/services/cohort_stats.py
  • backend/tests/test_cohort_stats.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/backend-b19-welch-ttest

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 and usage tips.

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.

1 participant