Skip to content

Sample nearly flat linear-linear Tabular densities - #4266

Open
rpinho wants to merge 1 commit into
openmc-dev:developfrom
rpinho:fix-tabular-near-flat-sampling
Open

rpinho wants to merge 1 commit into
openmc-dev:developfrom
rpinho:fix-tabular-near-flat-sampling

Conversation

@rpinho

@rpinho rpinho commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Who hits this: users who sample a tabulated spectrum with the default linear-linear interpolation (https://docs.openmc.org/en/stable/pythonapi/generated/openmc.stats.Tabular.html#openmc.stats.Tabular.sample).

For a nearly flat density, the inverse CDF subtracts nearly equal densities. Tabular([0, 1], [1, 1 + 2**-50]).sample(32, seed=17) raises an AssertionError, and so does the falling case. Even [0.3, 0.1 + 0.2] raises it.

Now it gets the distance from the area and the mean density. For other tables the samples change a little (up to 8e-14). It has a test for this.

The C++ code has the same problem. I didn't change it here.

AI Assistance

  • Harness: Codex CLI (found the bug, wrote the patch and tests); Claude Code (separate clean-room review from the bug report, opened this PR)
  • Model: GPT-6.2-Astra (Codex); Claude Opus 5.5 (Claude Code)
  • Reasoning effort: high (both)

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

I'm doing this with AI agents. A second agent on a different model checked the bug again from the report alone, without the first agent's code.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved sampling accuracy for distributions using linear-linear interpolation, including cases with equal, nearly equal, or zero endpoint values. Samples now more closely reflect the intended probability distribution across the full range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The linear-linear branch of Tabular._sample_unbiased now calculates sample offsets using interpolated squared endpoint densities. Parameterized tests check quantiles and unit weights across several endpoint-height pairs.

Changes: Linear-linear tabular sampling

Layer / File(s) Summary
Sampling calculation and validation
openmc/stats/univariate.py, tests/unit_tests/test_stats.py
The sampler replaces the quadratic inverse-CDF solution with an offset calculation based on interpolated squared endpoint densities. Tests check analytic CDF values against seeded uniform draws and verify unit weights.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: Bugs

Suggested reviewers: pshriwise, paulromano

Merge Risk: Minimal · up to 2549b

This change fixes assertion errors when sampling from nearly flat tabulated densities, and the new tests cover the reported cases. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Sample nearly flat linear-linear Tabular densities" matches the main change. The summary shows Tabular._sample_unbiased changing linear-linear sampling, and the PR description says the fix …
Description check Passed The description explains the problem, gives a reproducer, describes the new approach, and notes the small sample change (up to 8e-14) and the test. It also fills in the AI Assistance section. Three ga…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Sample nearly flat linear-linear Tabular densities" matches the main change. The summary shows Tabular._sample_unbiased changing linear-linear sampling, and the PR description says the fix addresses an AssertionError for nearly flat densities. The title is specific and short, and it names the affected interpolation mode and class.

Full details: Description check

Explanation

The description explains the problem, gives a reproducer, describes the new approach, and notes the small sample change (up to 8e-14) and the test. It also fills in the AI Assistance section. Three gaps remain: the "Fixes #" line is left as a template placeholder with no issue number, the documentation checkbox is unchecked with no note, and the description does not state the test command or result. These gaps are not critical, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d54a5f6e-3c3c-4c93-bad7-566433da33b5
Commits

Reviewing files that changed from the base of the PR and between 32b7c1d and 2549b3c.

Files selected for processing: 2

  • openmc/stats/univariate.py
  • tests/unit_tests/test_stats.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

This branch has not been deployed

No deployments
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