Skip to content

tests: Seed Tests, Log Random Seeds, Scripts, and Small Fixes - #138

Open
asher-m wants to merge 3 commits into
masterfrom
dev/tests/report-seeds-and-small-fixes
Open

asher-m wants to merge 3 commits into
masterfrom
dev/tests/report-seeds-and-small-fixes

Conversation

@asher-m

@asher-m asher-m commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

This PR:

  1. Adds tests/conftest.py:
    • For each test, accepts a seed from an environment variable UNCERTAINSCI_TEST_SEED if set, otherwise generates a new seed.
    • If a test fails, logs the random seed.
    • See notes on this implementation below.
  2. Adds environment creation and code check scripts.
    • scripts/conda-create-test-envs.sh creates Python environments according to versions listed in the project's pyproject.toml.
    • scripts/conda-run-checks.sh runs the tests, then lints and checks the code for style, analogous to the test.yml, lint.yml, and style.yml workflows.
    • Both scripts can be prompted for their usage with <script>.sh -h.
  3. Fixes a divide-by-zero bug in tests/test_ttr.py (resolves Divide by zero in test_ttr.py #136).

There are a plethora of intermittent (seed-dependent) test failures that motivated this PR, including the tolerance failure shown below and a rare optimize.bisect sign error (f(a) and f(b) must have different signs) in JacobiPolynomials.idistinv. This PR does not fix these bugs. Nonetheless, it adds the infrastructure to reproduce and classify them.

Notes on Seeding Tests

conftest.py uses a new, unique seed for each test unless UNCERTAINSCI_TEST_SEED is set, in which case it uses this seed for all tests asked for in the pytest call. For instance:

  • pytest tests/ would run all tests, each with a new, unique seed. For any test that fails, conftest.py would print the seed used when the test failed.
  • UNCERTAINSCI_TEST_SEED=1234 pytest tests/ would run all tests with a seed of 1234.
    • This is generally not desired behavior: this does not reproduce the command listed in the previous bullet, because each test received its own seed when executed with that command. UNCERTAINSCI_TEST_SEED should be set only when running a specific failing test.
  • UNCERTAINSCI_TEST_SEED=3999024806 pytest 'tests/test_jacobipolys.py::IDistTestCase::test_fidist_jacobi' would run test_fidist_jacobi with a seed of 3999024806 (see examples below; this is indeed a failing seed).

A few further notes on seeding tests:

Example

Running

UNCERTAINSCI_TEST_SEED=3999024806 pytest 'tests/test_jacobipolys.py::IDistTestCase::test_fidist_jacobi'

yields

(full failure output)
====================================================================== FAILURES ======================================================================
__________________________________________________________ IDistTestCase.test_fidist_jacobi __________________________________________________________

self = <test_jacobipolys.IDistTestCase testMethod=test_fidist_jacobi>

    def test_fidist_jacobi(self):
        """Tests the fast induced sampling routine for Jacobi polynomials."""
        alpha = np.random.random() * 11 - 1.
        beta = np.random.random() * 11 - 1.
    
        nmax = 4
        M = 10
        u = np.random.random(M)
    
        J = families.JacobiPolynomials(alpha=alpha, beta=beta)
    
        delta = 1e-1  # TODO: This is an unacceptably high tolerance.
    
        for n in range(nmax)[::-1]:
            x0 = J.idistinv(u, n)
            x1 = J.fidistinv(u, n)
    
            ind = np.nonzero(np.abs(x0 - x1) > delta)[0]
            if ind.size > 0:
                errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, u={3}'.format(
                    alpha, beta, n, np.array2string(u[ind])
                )
            else:
                errstr = ''
    
>           self.assertAlmostEqual(np.linalg.norm(x0 - x1, ord=np.inf), 0., delta=delta, msg=errstr)
E           AssertionError: np.float64(0.16770332704624347) != 0.0 within 0.1 delta (np.float64(0.16770332704624347) difference) : Failed for alpha=-0.026, beta=5.029, n=0, u=[0.00014939]

tests/test_jacobipolys.py:114: AssertionError
---------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------
Precomputing data for Jacobi parameters (alpha,beta) = (-0.025535, 5.028945)...Done
-------------------------------------------------------------------- random seed ---------------------------------------------------------------------
Reproduce with: UNCERTAINSCI_TEST_SEED=3999024806 pytest 'tests/test_jacobipolys.py::IDistTestCase::test_fidist_jacobi'
============================================================== short test summary info ===============================================================
FAILED tests/test_jacobipolys.py::IDistTestCase::test_fidist_jacobi - AssertionError: np.float64(0.16770332704624347) != 0.0 within 0.1 delta (np.float64(0.16770332704624347) difference) : Failed for alpha=-0.026, b...
================================================================= 1 failed in 9.57s ==================================================================

AI Use

This follows a similar pattern of AI use as #134: I proposed an initial plan and went through several iterations with Claude before arriving at a final design. I then had Claude implement these changes and verified the work.

* this is a bug in the logic of the tests, now resolved
* resolves #136
* wraps existing tests to report seeds alongside test-specific failure message
* wrapper reports under any failure, regardless of if the specific failure is located in the test source or library code
…hecks

* both scripts search for conda on path
* environment script creates environments according to the testing matrix design in .github/workflows/test.yml
  * if this workflow changes in the future, this script will additionally need to be modified
  * it should be straightforward to maintain this script in tandem with the testing workflow e.g. with an agent
* check script runs the tests, linting, and style checks as in the test.yml, lint.yml, and style.yml workflows
  * expects conda envs to have default names as created by conda-create-test-envs.sh script
@asher-m
asher-m requested a review from akilnarayan December 4, 2026 07:20
@asher-m asher-m self-assigned this Dec 4, 2026
@asher-m

asher-m commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

@akilnarayan, ironically the test failure was the result of a tolerance failure. Ubuntu 3.12, 13, and 14 passed, so this should still be good to go for 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.

Divide by zero in test_ttr.py

1 participant