pymc icon indicating copy to clipboard operation
pymc copied to clipboard

Finish restructuring the tests to follow the structure of the code

Open Armavica opened this issue 3 years ago • 6 comments

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

Armavica avatar Sep 13 '22 21:09 Armavica

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

codecov[bot] avatar Sep 13 '22 21:09 codecov[bot]

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: https://github.com/pymc-devs/pymc/blob/4ea8ddee7b55ccaadeec2549bb8c65617e032453/pymc/sampling.py#L287-L290

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?

Armavica avatar Sep 16 '22 02:09 Armavica

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.

ricardoV94 avatar Sep 16 '22 05:09 ricardoV94

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?

Armavica avatar Sep 16 '22 13:09 Armavica

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

ricardoV94 avatar Sep 16 '22 13:09 ricardoV94

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.

Armavica avatar Sep 16 '22 14:09 Armavica

@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 avatar Sep 30 '22 13:09 ricardoV94

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

ricardoV94 avatar Sep 30 '22 13:09 ricardoV94

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

Armavica avatar Sep 30 '22 19:09 Armavica

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

ricardoV94 avatar Sep 30 '22 19:09 ricardoV94

Awesome work @Armavica, are we done?

ricardoV94 avatar Oct 01 '22 07:10 ricardoV94

Awesome work @Armavica, are we done?

Yes, that was the last batch!

Armavica avatar Oct 02 '22 00:10 Armavica