Skip to content

test: move example reference values into the scripts themselves - #92

Open
endolith wants to merge 2 commits into
masterfrom
reference-values-in-examples
Open

test: move example reference values into the scripts themselves#92
endolith wants to merge 2 commits into
masterfrom
reference-values-in-examples

Conversation

@endolith

@endolith endolith commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Background
The example scripts are stochastic Monte Carlo scripts that verify the simulator against published results (Merrill 1984, Weber 1977). ests/test_examples.py (added in #87) ran each script and compared its computed able against hardcoded REFERENCE_VALUES/TOLERANCES in the test file.

Problem
The reference values were duplicated in the test file, far from the scripts they describe, even though the values already lived in the scripts (e.g. the merrill_table_1 dicts used for plotting, and the "Typical result" docstring tables). Issue #91 asks that the reference values live in the example scripts themselves.

Visible symptoms
Adding a new example script required editing both the script and the test file. The provenance of each reference (paper value vs. the script's own output) was only documented in test comments, not next to the data.

What this PR changes

  • Each example script now defines its own
    eference_table (the values its computed able is checked against, in the script's column order) and olerance (absolute comparison tolerance). Fixes actually the examples should double as tests. #91.
  • ests/test_examples.py no longer hardcodes any reference data; it runs each script (in a subprocess, per test: run example scripts in subprocesses so joblib workers can pickle #90) and verifies able against the script's own
    eference_table.
  • Reference provenance per issue merrill_1984_table_2 and table_4 don't reproduce the published Merrill (1984) tables #88:
    • Scripts that reproduce the published tables (merrill_1984_table_1/3, the Weber scripts) use the paper's values. For able_1/ able_3 this is the existing merrill_table_1 dict, renamed to
      eference_table.
    • Scripts that do not reproduce the papers (merrill_1984_table_2/4, the four figure scripts, off by up to ~7–9 pp) use the docstring "Typical result"/"Results with N elections" values, with a comment noting they are a regression guard until the discrepancy is fixed.
  • The four figure scripts were restructured to keep a table per sub-figure (keyed by fig label) instead of overwriting a single able each loop iteration, so both sub-figures are checked.
  • examples/README.md documents the convention.

Tests

  • pytest tests/test_examples.py -m slow: 10 passed (each script ~30–60 s).
  • Full suite: 215 passed, 10 deselected.
  • Blocking lint (
    uff check . --select=E9,F63,F7,F82) passes.
  • Programmatically confirmed all embedded references match the previous REFERENCE_VALUES/docstrings exactly.

Related work

Summary by CodeRabbit

  • Documentation

    • Added guidance explaining how example outputs are compared with documented reference values and tolerances.
  • Tests

    • Improved validation of example results, including separate figure outputs and clearer checks for missing, empty, or mismatched data.
    • Added reference results and comparison tolerances across Merrill and Weber examples.
    • Updated example output handling to support both grouped and flat result tables.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@endolith, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

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 reviews.

How do review limits work?

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

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, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9d0a2e47-0f60-403d-b491-e164950d9071

📥 Commits

Reviewing files that changed from the base of the PR and between 79afafb and 28cb15e.

📒 Files selected for processing (12)
  • examples/README.md
  • examples/merrill_1984_fig_2c_2d.py
  • examples/merrill_1984_fig_2c_2d_updated.py
  • examples/merrill_1984_fig_4a_4b.py
  • examples/merrill_1984_fig_4a_4b_updated.py
  • examples/merrill_1984_table_1_fig_1.py
  • examples/merrill_1984_table_2.py
  • examples/merrill_1984_table_3_fig_3.py
  • examples/merrill_1984_table_4.py
  • examples/weber_1977_effectiveness_table.py
  • examples/weber_1977_table_4.py
  • tests/test_examples.py
📝 Walkthrough

Walkthrough

Example scripts now expose their computed tables, reference values, and tolerances. tests/test_examples.py loads these values, normalizes flat or figure-keyed tables, and validates computed rows against embedded references.

Changes

Example regression validation

Layer / File(s) Summary
Example output and reference contracts
examples/merrill_1984_*.py, examples/weber_1977_*.py
Figure-based scripts now retain separate result tables. Example scripts define reference_table and tolerance values. Existing Merrill plotting references use the renamed variable.
Generic example test validation
tests/test_examples.py, examples/README.md
The test harness reads each script’s table, reference table, and tolerance. It supports flat and nested tables and checks missing methods, row lengths, empty results, and numerical differences.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • endolith/elsim#87: Both changes update tests/test_examples.py to validate example outputs against reference tables.
  • endolith/elsim#89: Both changes strengthen example table validation with row presence and length checks.
  • endolith/elsim#90: Both changes use subprocess-based example validation with embedded reference data and tolerances.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes moving example reference values into the scripts, which is the primary change.
Linked Issues check ✅ Passed The PR satisfies issue #91 by embedding reference values in scripts and verifying computed results through test_examples.py.
Out of Scope Changes check ✅ Passed The documentation, figure-table restructuring, provenance notes, and test updates support the linked issue objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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 reference-values-in-examples

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

@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.37%. Comparing base (79afafb) to head (28cb15e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #92   +/-   ##
=======================================
  Coverage   96.37%   96.37%           
=======================================
  Files          17       17           
  Lines         496      496           
=======================================
  Hits          478      478           
  Misses         18       18           
Flag Coverage Δ
no-numba 95.76% <ø> (ø)
numba 88.10% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@what-the-diff

what-the-diff Bot commented Aug 5, 2026

Copy link
Copy Markdown

PR Summary

  • Readme and Script Updates

    • The README.md document was updated to include information about scripts serving as tests, along with their associated reference tables and tolerances.
    • The scripts examples/merrill_1984_fig_2c_2d.py, examples/merrill_1984_fig_2c_2d_updated.py, examples/merrill_1984_fig_4a_4b.py, examples/merrill_1984_fig_4a_4b_updated.py, examples/merrill_1984_table_1_fig_1.py, examples/merrill_1984_table_2.py, examples/merrill_1984_table_3_fig_3.py, examples/merrill_1984_table_4.py, examples/weber_1977_effectiveness_table.py, and examples/weber_1977_table_4.py have all been updated. In these scripts, a variable called table has been initiated as a dictionary and modifications have been made to how data is stored, printed, and compared against a reference_table. A tolerance level has also been introduced for tests.
  • Documentation Update

    • The documentation of tests has been enhanced to help users better understand expected output and tolerance levels.
  • Static Reference Values and Tolerance Removed

    • The storage of reference values and tolerances has been revamped. The explicitly defined REFERENCE_VALUES and TOLERANCES dictionaries were removed, paving the way for a more dynamic handling of reference and tolerance levels within the example scripts themselves, making them more adaptable from script to script.
  • Function Output Modification

    • Changes were made to the _run function, shifting the output variable from table.pkl to result.pkl. Furthermore, the output now includes the table, reference_table, and tolerance.
  • Simplified Output Value Processing

    • New functions named _method_values, _reference_values, and _is_nested were introduced. They have made the processing of output values and their comparison with reference values more streamlined and efficient.
  • More Flexible Assertion Checks

    • The assertions in the test_example function were replaced with a more flexible verification process. This allows more dynamic comparisons between computed results and script-defined references, accommodating nested structures.

@endolith
endolith force-pushed the reference-values-in-examples branch from 6633600 to dfa2315 Compare August 6, 2026 03:52
Base automatically changed from fix/test-run-subprocess to master August 6, 2026 04:12
@endolith
endolith force-pushed the reference-values-in-examples branch from dfa2315 to 7b86c18 Compare August 6, 2026 04:15

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/test_examples.py (1)

45-46: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use numpydoc docstrings for the changed functions.

Add Parameters and Returns sections where applicable. Update test_example to state why it verifies the embedded-reference contract.

As per coding guidelines, “Use numpydoc-format docstrings” and “Every test function must have a docstring explaining the behavior it verifies and why.”

Also applies to: 61-67, 73-78, 89-96, 110-112

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_examples.py` around lines 45 - 46, Update the docstrings for _run
and the other changed functions, including test_example, to use numpydoc format
with applicable Parameters and Returns sections. Ensure every test function
explains both the behavior it verifies and why, with test_example explicitly
documenting the embedded-reference contract.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_examples.py`:
- Around line 95-122: Update _assert_close to assert that the computed method
keys exactly match the expected method keys, then retain the per-row validation.
In test_example, validate that table and reference_table have identical figure
keys before iterating nested results, while preserving the existing non-nested
path. Add regression coverage for an empty nested table and for a missing
computed figure.

---

Nitpick comments:
In `@tests/test_examples.py`:
- Around line 45-46: Update the docstrings for _run and the other changed
functions, including test_example, to use numpydoc format with applicable
Parameters and Returns sections. Ensure every test function explains both the
behavior it verifies and why, with test_example explicitly documenting the
embedded-reference contract.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c4d10ba-6189-4fbb-bb1c-0796d888b679

📥 Commits

Reviewing files that changed from the base of the PR and between 0ec02cd and 7b86c18.

📒 Files selected for processing (12)
  • examples/README.md
  • examples/merrill_1984_fig_2c_2d.py
  • examples/merrill_1984_fig_2c_2d_updated.py
  • examples/merrill_1984_fig_4a_4b.py
  • examples/merrill_1984_fig_4a_4b_updated.py
  • examples/merrill_1984_table_1_fig_1.py
  • examples/merrill_1984_table_2.py
  • examples/merrill_1984_table_3_fig_3.py
  • examples/merrill_1984_table_4.py
  • examples/weber_1977_effectiveness_table.py
  • examples/weber_1977_table_4.py
  • tests/test_examples.py

Comment thread tests/test_examples.py
endolith and others added 2 commits August 6, 2026 00:46
Each example script now defines its own ``reference_table`` (the values its
computed ``table`` is checked against, in the script's column order) and a
``tolerance`` (absolute comparison tolerance), so the script doubles as a
test.  test_examples.py no longer hardcodes REFERENCE_VALUES/TOLERANCES; it
just runs each script (in a subprocess) and verifies ``table`` against the
script's own ``reference_table``.  Fixes #91.

Reference provenance (see issue #88):

- merrill_1984_table_1/3 and the Weber scripts reproduce the published
  tables, so their references are the paper's values (for table_1 and table_3
  this is the existing ``merrill_table_1`` dict, renamed ``reference_table``).
- merrill_1984_table_2/4 and the four figure scripts do not reproduce the
  papers (up to ~7-9 pp off), so those references are the docstring "Typical
  result"/"Results with N elections" values, with a comment noting they are a
  regression guard until the discrepancy is fixed.

The figure scripts were restructured to keep a table per sub-figure (keyed by
fig label) instead of overwriting a single ``table`` each loop iteration, so
both sub-figures are checked.  The examples README documents the convention.

Co-authored-by: opencode <opencode@anomalyco.ai>
_assert_close now requires the computed methods to match the reference
exactly, catching extra rows that were previously ignored.  test_example
also checks that a figure script's computed figures match its reference
figures, so a missing figure can't pass silently.  Add unit tests for
both helpers and the empty-table/missing-row/missing-figure cases.

Co-authored-by: opencode <opencode@anomalyco.ai>
@endolith
endolith force-pushed the reference-values-in-examples branch from c3d366b to 28cb15e Compare August 6, 2026 04:46
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.

actually the examples should double as tests.

1 participant