Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish restructuring the tests to follow the structure of the code #6125

Merged
merged 12 commits into from
Oct 1, 2022

Conversation

Armavica
Copy link
Member

What is this PR about?
This PR restructures the last test files to follow the structure of the code, as discussed in #5777.

I think that this closes #5777. One last unresolved question is whether we want to move tests out of pymc, like it is done in aesara for example.

Checklist

Major / Breaking Changes

  • ...

Bugfixes / New features

  • ...

Docs / Maintenance

  • Finish restructuring the tests to follow the structure of the code

@codecov
Copy link

codecov bot commented Sep 13, 2022

Codecov Report

Merging #6125 (a9eca4a) into main (244c37d) will increase coverage by 0.33%.
The diff coverage is 99.77%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6125      +/-   ##
==========================================
+ Coverage   93.04%   93.37%   +0.33%     
==========================================
  Files          91      100       +9     
  Lines       20813    21896    +1083     
==========================================
+ Hits        19365    20445    +1080     
- Misses       1448     1451       +3     
Impacted Files Coverage Δ
pymc/tests/smc/test_smc.py 100.00% <ø> (ø)
pymc/tests/variational/test_inference.py 99.02% <99.02%> (ø)
pymc/tests/step_methods/hmc/test_hmc.py 98.14% <100.00%> (ø)
pymc/tests/step_methods/hmc/test_nuts.py 100.00% <100.00%> (ø)
pymc/tests/step_methods/test_compound.py 100.00% <100.00%> (ø)
pymc/tests/step_methods/test_metropolis.py 100.00% <100.00%> (ø)
pymc/tests/step_methods/test_slicer.py 100.00% <100.00%> (ø)
pymc/tests/variational/test_approximations.py 100.00% <100.00%> (ø)
pymc/tests/variational/test_callbacks.py 100.00% <100.00%> (ø)
pymc/tests/variational/test_opvi.py 100.00% <100.00%> (ø)
... and 3 more

@Armavica
Copy link
Member Author

Armavica commented Sep 16, 2022

These metropolis tests are flaky even though they are supposed to be SeededTests. It is because _get_seeds_per_chain creates a numpy.random.default_rng(None) which is based on entropy, not on the global numpy random generator:

pymc/pymc/sampling.py

Lines 287 to 290 in 4ea8dde

if random_state is None or isinstance(random_state, int):
if chains == 1 and isinstance(random_state, int):
return (random_state,)
return _get_unique_seeds_per_chain(np.random.default_rng(random_state).integers)

My suggestion is to use a seed from the global numpy random generator in case number 4 described in this function's docstring: this makes these tests deterministic. Concretely I would add this couple of lines before L287 above:

if random_state is None:
    random_state = np.random.randint(2**30)

Would that be okay?

@ricardoV94
Copy link
Member

ricardoV94 commented Sep 16, 2022

No, we officially don't allow users to control seeding via global seeding anymore (even if we still use global seeding internally), because those have less quality.

The solution is to explicitly pass random_seed to the sampling routines in those tests.

But how often does it fail? I haven't seen it fail before for a long time now.

pymc/tests/helpers.py Outdated Show resolved Hide resolved
@Armavica
Copy link
Member Author

I see, that makes sense. I will change the name of the class and we will see if that repeats.

But doesn't this mean that SeededTest in its current form is not doing much?

@ricardoV94
Copy link
Member

I see, that makes sense. I will change the name of the class and we will see if that repeats.

But doesn't this mean that SeededTest in its current form is not doing much?

Yes it's not doing much anymore, other than providing a seed when requested

@@ -149,7 +149,7 @@ jobs:
- pymc/tests/test_variational_inference.py pymc/tests/test_initial_point.py
- pymc/tests/test_model.py pymc/tests/test_step.py
- pymc/tests/gp/test_cov.py pymc/tests/gp/test_gp.py pymc/tests/gp/test_mean.py pymc/tests/gp/test_util.py pymc/tests/ode/test_ode.py pymc/tests/ode/test_utils.py pymc/tests/test_smc.py pymc/tests/test_parallel_sampling.py
- pymc/tests/test_sampling.py pymc/tests/test_posteriors.py
- pymc/tests/test_sampling.py pymc/tests/step_methods/test_metropolis.py pymc/tests/step_methods/test_slicer.py pymc/tests/step_methods/hmc/test_nuts.py
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it enough to do -pymc/tests/step_methods/ ?

Copy link
Member Author

@Armavica Armavica Sep 16, 2022

Choose a reason for hiding this comment

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

Yes it is, but here I selected only the few files where tests from test_posteriors ended up, to not increase too much the runtime of the tests. The rest is already tested in other platforms. I am not sure how much of a difference that makes, though. If we test the whole of step_methods I will also check that the check_all_tests_are_covered.py script understands what happens.

@Armavica
Copy link
Member Author

Armavica commented Sep 16, 2022

These two tests (TestKmeansInducing::test_kmeans (this one is tested on both Windows and Linux on the github runners but fails only on Windows) and TestDEMetropolisZ::test_tuning_reset) have been consistently failing on this PR, even though they pass locally on my computer. I will check if I did something wrong.

@ricardoV94
Copy link
Member

@Armavica Do you want to try to rebase this from main and see if we can get it merged quickly? One of the flaky tests you mention was fixed recently

@ricardoV94
Copy link
Member

Otherwise it might be easier to do it one module at a time

@Armavica
Copy link
Member Author

The TestDEMetropolisZ::test_tuning_reset does seem to fail about 10-20% of the time on my laptop. Should I just add a seed?

@ricardoV94
Copy link
Member

The TestDEMetropolisZ::test_tuning_reset does seem to fail about 10-20% of the time on my laptop. Should I just add a seed?

Yes

@Armavica Armavica requested a review from ricardoV94 September 30, 2022 22:31
@ricardoV94 ricardoV94 merged commit f515e91 into pymc-devs:main Oct 1, 2022
@ricardoV94
Copy link
Member

Awesome work @Armavica, are we done?

@Armavica
Copy link
Member Author

Armavica commented Oct 2, 2022

Awesome work @Armavica, are we done?

Yes, that was the last batch!

@Armavica Armavica deleted the structure-tests branch October 7, 2024 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mirror codebase structure in tests
2 participants