Skip to content

fix: replace numpy/scipy with pure Python in experiment analytics#6830

Merged
gagantrivedi merged 4 commits into
mainfrom
fix/remove-numpy-scipy-deps
Mar 3, 2026
Merged

fix: replace numpy/scipy with pure Python in experiment analytics#6830
gagantrivedi merged 4 commits into
mainfrom
fix/remove-numpy-scipy-deps

Conversation

@gagantrivedi
Copy link
Copy Markdown
Member

  • I have read the Contributing Guide.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Removes numpy and scipy dependencies from the API, replacing them with pure Python (stdlib math and random) implementations. These libraries added significant per-worker RSS overhead but are only used by the demo experiment analytics endpoint.

  • Implement G-test (log-likelihood ratio) using the incomplete gamma function
  • Use random.betavariate for Bayesian Monte Carlo simulation (10k samples)
  • Remove numpy and scipy from pyproject.toml and regenerate poetry.lock
  • Convert tests from class-based to function-based to match project conventions
  • Remove stale test assertion

How did you test this code?

  • All 7 existing tests pass (pytest tests/unit/app_analytics/test_experiments.py)
  • Mypy passes in strict mode
  • Verified G-test and chi-squared survival function are numerically identical to scipy (errors at machine epsilon ~1e-17)
  • Verified Bayesian Monte Carlo results match scipy/numpy ground truth
  • Benchmarked end-to-end latency: ~16ms per call (acceptable for a demo endpoint)

Removes significant per-worker RSS overhead by replacing numpy and
scipy with stdlib math and random modules. The experiment analytics
endpoint is demo-only code, so the minor latency trade-off is well
worth the memory savings.

- Implement G-test via log-likelihood ratio + incomplete gamma function
- Use random.betavariate for Bayesian Monte Carlo (10k samples)
- Remove numpy and scipy from pyproject.toml and poetry.lock
- Convert tests from class-based to function-based
@gagantrivedi gagantrivedi requested a review from a team as a code owner March 3, 2026 04:22
@gagantrivedi gagantrivedi requested review from khvn26 and removed request for a team March 3, 2026 04:22
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Mar 3, 2026 5:01am
flagsmith-frontend-preview Ignored Ignored Preview Mar 3, 2026 5:01am
flagsmith-frontend-staging Ignored Ignored Preview Mar 3, 2026 5:01am

Request Review

@github-actions github-actions Bot added the api Issue related to the REST API label Mar 3, 2026
@github-actions github-actions Bot added the fix label Mar 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-6830 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-6830 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api-test:pr-6830 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api:pr-6830 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-6830 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6830 Finished ✅ Results

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.32%. Comparing base (380fecd) to head (f302c67).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6830      +/-   ##
==========================================
+ Coverage   98.30%   98.32%   +0.02%     
==========================================
  Files        1331     1331              
  Lines       49332    49394      +62     
==========================================
+ Hits        48494    48568      +74     
+ Misses        838      826      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

gagantrivedi and others added 2 commits March 3, 2026 10:31
Cover all new code paths through the public calculate_statistics API
and mark unreachable numerical guards with pragma: no cover.
@github-actions github-actions Bot added fix and removed fix labels Mar 3, 2026
Copy link
Copy Markdown
Contributor

@Zaimwa9 Zaimwa9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a lot of maths for fake graphs 😄

@gagantrivedi gagantrivedi merged commit 7617737 into main Mar 3, 2026
32 checks passed
@gagantrivedi gagantrivedi deleted the fix/remove-numpy-scipy-deps branch March 3, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants