Skip to content

Set up Hypothesis and add property-based tests for Time precision#10373

Merged
mhvk merged 4 commits into
astropy:masterfrom
Zac-HD:property-tests
May 25, 2020
Merged

Set up Hypothesis and add property-based tests for Time precision#10373
mhvk merged 4 commits into
astropy:masterfrom
Zac-HD:property-tests

Conversation

@Zac-HD

@Zac-HD Zac-HD commented May 19, 2020

Copy link
Copy Markdown
Contributor

This pull request follows issue #9017. The major contribution is by @aarchiba (not me!), who wrote the excellent tests for Time precision in #9532 and found bugs #9533 and #9579.

I have rebased on master and clarified a few Hypothesis idioms, added a section to the testing guide, and simplified the install and CI configuration (as pytest-astropy now depends on Hypothesis 🎉).

Reviewers please note that I am not an astrophysicist - I'm a Hypothesis core developer and want to support trustworthy and open scientific computing, but you'll have to bring the domain expertise 😁

EDIT: Close #9532

yield


def assert_almost_equal(a, b, *, rtol=None, atol=None, label=''):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like it gotta move upstream to testing utils or something.

Disclaimer: I did not follow original discussions in the other PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HypothesisWorks/hypothesis#2393 summarises Hypothesis' position concisely: target()ing the loss of precision enables a guided search mode, and reporting the highest score makes serious problems obvious even after shrinking to a minimal example.

This specific function leverages that upstream functionality, and also integrates with Astropy's time and units functionality tightly enough that I'm not sure I could make it generic without introducing precision errors. I'm happy to move it elsewhere in Astropy if you have a suggestion, though personally I'd be inclined to leave it as-is until an equivalent helper is needed elsewhere ("YAGNI").

@pllim

pllim commented May 19, 2020

Copy link
Copy Markdown
Member

Thanks for reviving this, @Zac-HD !

p.s. Looks like tests are failing with HypothesisDeprecationWarning.

@Zac-HD

Zac-HD commented May 20, 2020

Copy link
Copy Markdown
Contributor Author

p.s. Looks like tests are failing with HypothesisDeprecationWarning.

Ah, bad interaction with function-scoped fixtures. Just adding (scope='module') is enough to fix that, and makes things marginally more efficient to boot.

Edit: no, wait - we're injecting a context manager, why not just use that directly? One moment...

Edit 2: oh, because it's an expensive operation. Back to module-scoped fixture, plus a comment explaining this time...

@Zac-HD Zac-HD force-pushed the property-tests branch 2 times, most recently from e14767e to e7a7776 Compare May 20, 2020 01:49
@pllim

pllim commented May 20, 2020

Copy link
Copy Markdown
Member

Oh, looks like a real failure now. Maybe @taldcroft or @mhvk can make sense of it.

Falsifying example: test_conversion_never_loses_precision(
    iers_b=None, scale1='tai', scale2='tcb', jds=(-31738.500000000346, 0.0),
)

@Zac-HD Zac-HD force-pushed the property-tests branch 2 times, most recently from 30f1f84 to f5d1d8f Compare May 20, 2020 02:48
@Zac-HD

Zac-HD commented May 20, 2020

Copy link
Copy Markdown
Contributor Author

OK, I've marked that one test as xfail and everything else is working.

My preference would be to merge this PR as-is if it's otherwise acceptable, and have someone with more astro background than me investigate it in a follow-up issue - 'has an xfail test' seems to me like an improvement on the no-property-tests status quo 😜

@pllim

pllim commented May 20, 2020

Copy link
Copy Markdown
Member

Seems a little heavy-handed to xfail the whole test when only one of the inputs failed, but then again this has dragged on long enough, so I can see pros/cons on both sides. 🤔

Please be patient with us while we wait for the subpackage maintainers to review this thoroughly. Thanks again!

@Zac-HD

Zac-HD commented May 20, 2020

Copy link
Copy Markdown
Contributor Author

Seems a little heavy-handed to xfail the whole test when only one of the inputs failed

That's the thing - many inputs failed, but Hypothesis reports a single minimal reproducing example. It makes debugging much easier, though it can also make errors look less serious than they really are.

@mhvk

mhvk commented May 20, 2020

Copy link
Copy Markdown
Contributor

@Zac-HD - the failure is almost certainly due to erfa, in an issue that is being addressed (for overly long discussion, see #6970). I think it makes sense to xfail it for now, so we actually notice if things improve in the future. That said, should there be a test with double the tolerance now that does not fail, so we know we do not regress?

@Zac-HD

This comment has been minimized.

Comment thread astropy/time/tests/test_precision.py Outdated
@Zac-HD Zac-HD force-pushed the property-tests branch 2 times, most recently from 2c62cf1 to 160fdec Compare May 21, 2020 00:08
@pep8speaks

pep8speaks commented May 21, 2020

Copy link
Copy Markdown

Hello @Zac-HD 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style.

There are no PEP8 style issues with this pull request - thanks! 🎉

Comment last updated at 2020-05-25 01:21:54 UTC

@Zac-HD Zac-HD force-pushed the property-tests branch 3 times, most recently from dad8c9e to 240d919 Compare May 22, 2020 06:03
@Zac-HD

Zac-HD commented May 22, 2020

Copy link
Copy Markdown
Contributor Author

OK, I've done all the investigation I can and the tests are all passing now 😁 @mhvk, I think we're ready to merge!

Happy to help out with the Hypothesis parts going forward, but further work really needs more Astropy expertise than I have at the moment.

@mhvk mhvk force-pushed the property-tests branch 2 times, most recently from ca37a06 to 215fc11 Compare May 24, 2020 21:31
@mhvk

mhvk commented May 24, 2020

Copy link
Copy Markdown
Contributor

OK, I tried to directly fix the failing test for numpy 1.16 instead. If that fails, one can go back to the xfail (but now on the right function 😄)

And add a bit more clarification for the two places where this
can happen.
@mhvk mhvk force-pushed the property-tests branch from 215fc11 to 3751d50 Compare May 25, 2020 01:21
@mhvk

mhvk commented May 25, 2020

Copy link
Copy Markdown
Contributor

And of course I did it wrong at least twice, but I think I now found that the failing test was just a case already found before, of a UTC JD close to 0, and had nothing to do with the numpy version (that is the risk of hypothesis, I guess, that the different travis runs do not necessarily try the same tests - good in many ways, but tricky here).

@Zac-HD

Zac-HD commented May 25, 2020

Copy link
Copy Markdown
Contributor Author

Thanks @mhvk! This is definitely getting into territory where the limit is my knowledge of Time code, not Hypothesis 🙂

@mhvk

mhvk commented May 25, 2020

Copy link
Copy Markdown
Contributor

OK, tests now pass, so I'll merge. Thanks so much @Zac-HD and @aarchiba for getting this going!!!

@mhvk mhvk merged commit 825cadd into astropy:master May 25, 2020
@Zac-HD Zac-HD deleted the property-tests branch May 25, 2020 13:54
@Zac-HD

Zac-HD commented May 25, 2020

Copy link
Copy Markdown
Contributor Author

You're welcome!

I've updated the list of suggested tests in #9017 - the next set should be faster now that we've sorted out all the dependency and config issues, and upgraded Hypothesis a bit 😁

@astrofrog astrofrog modified the milestones: v4.0.3, v4.0.2 May 27, 2020
astrofrog pushed a commit that referenced this pull request May 27, 2020
Set up Hypothesis and add property-based tests for `Time` precision
astrofrog pushed a commit that referenced this pull request May 27, 2020
Set up Hypothesis and add property-based tests for `Time` precision
@mhvk

mhvk commented Jun 2, 2020

Copy link
Copy Markdown
Contributor

@bsipocz - I think I did the milestone wrong here. This is really a new type of testing that I think might be better done in 4.2. Is it too late to change it? If you have already included it in 4.1 or 4.0.2, then probably not quite worth reverting either.

@pllim

pllim commented Jun 3, 2020

Copy link
Copy Markdown
Member

Re: milestone -- cc @astrofrog and @eteq

@astrofrog

astrofrog commented Jun 5, 2020

Copy link
Copy Markdown
Member

I think this is already in 4.1rc1, no harm in including it and certainly not much point removing it I think. It's also already backported to v4.0.x.

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.

5 participants