Skip to content

Parse and do float comparison of float output in doctests#2087

Merged
embray merged 10 commits into
astropy:masterfrom
embray:issue-2087
Jun 25, 2014
Merged

Parse and do float comparison of float output in doctests#2087
embray merged 10 commits into
astropy:masterfrom
embray:issue-2087

Conversation

@embray

@embray embray commented Feb 12, 2014

Copy link
Copy Markdown
Member

This is a suggestion I've made in a few places but I don't think I ever really made a ticket for it, so here it is: Many issues with doctests arise when slight changes in numerical code between Astropy versions, Python versions, or worst of all platforms result in slight differences in floating point results, causing the doctests to fail, since they work entirely on string comparison. This is even worse because on different platforms the default string formatting for different floating point values can differ.

Most of this time we address this using ellipses (... ) to ignore values above a certain decimal place, a feature supported by doctest. But this does not even work for all cases--for example I recently had a case where the doctest expected something like 1.000000000000001e-23, but my system was rounding this to 1e-23. I suppose this could be written 1...e-23 in the doctest, but that's pretty unclear. (ETA: I also found that on a different Python version the same test returned something like 9.999999999999e-24 which is pretty much impossible to deal with using the ellipses approach)

What if instead the doctest runner could actually parse floating point strings into actual float values and do a direct numerical comparison? Optionally, the comparison could even be made with a specific tolerance.

This could be cumbersome on tests that don't expect floating point values in the output, so I would suggest this be doing with a new doctest directive, perhaps # doctest: +FLOAT. This would use a regular expression to parse out all floating point values found in expected and actual test output, in order, so that they can be matched up one for one and compared. This flexible approach would allow it to work with floating point values that appear anywhere in the output, such as in the repr for a Quantity.

@embray

embray commented Feb 12, 2014

Copy link
Copy Markdown
Member Author

If this can be made to work then we could even remove a lot of the ellipses from the existing doctests.

@astrofrog

Copy link
Copy Markdown
Member

👍 to the idea!

@embray

embray commented Feb 12, 2014

Copy link
Copy Markdown
Member Author

I have a simple prototype of this already working, but I don't want to go too much further with it until we have something like #2050 in place, since it'll be a pain to test otherwise. I'm still not sure how to square my current implementation with the ELLIPSIS flag either. Currently if a test is using my FLOAT_COMPARISON flag it forces ELLIPSIS to be disabled. I might need to roll my own parsing of ellipses into the solution to make it complete.

@embray

embray commented Feb 12, 2014

Copy link
Copy Markdown
Member Author

Attached is aforementioned prototype.

Comment thread astropy/tests/pytest_plugins.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like self.normalize_floats can return None, so maybe we need to handle that case here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oops, that's a bug. It should probably just return want and got unmodified.

@mdboom

mdboom commented Feb 13, 2014

Copy link
Copy Markdown
Contributor

This is really cool. Gives me some motivation to work on #2050...

@mhvk

mhvk commented Feb 13, 2014

Copy link
Copy Markdown
Contributor

Would be wonderful!

@twmr

twmr commented Mar 2, 2014

Copy link
Copy Markdown

This is the pendant in sympy sympy/sympy#2051

@astrofrog

Copy link
Copy Markdown
Member

@embray - it looks like the Travis failure is related?

@astrofrog astrofrog added this to the v0.4.0 milestone Mar 16, 2014
@embray

embray commented Mar 17, 2014

Copy link
Copy Markdown
Member Author

Thanks @Thisch, I'll have to look into it. Maybe I can just "borrow" your work on this from SymPy :)

@embray embray self-assigned this Mar 17, 2014
@twmr

twmr commented Mar 17, 2014

Copy link
Copy Markdown

@embray go ahead!

@cdeil

cdeil commented Mar 18, 2014

Copy link
Copy Markdown
Member

I've recently run into issues with floats in doctests in an affiliated package.
Would be really nice to have a good solution to handle those ...

@astrofrog

Copy link
Copy Markdown
Member

@embray - shall we re-schedule to 1.0?

@embray

embray commented May 8, 2014

Copy link
Copy Markdown
Member Author

This would be really nice to have, for affiliated packages as well. I'll try to see if I can get this working tomorrow using the code from sympy.

@eteq

eteq commented May 27, 2014

Copy link
Copy Markdown
Member

@embray @astrofrog - perhaps we can re-schedule this to 0.4.1 instead of 1.0? If I understand right it's actually a doc fix, so it's permissible in a bugfix version?

@astrofrog

Copy link
Copy Markdown
Member

👍

@embray

embray commented May 27, 2014

Copy link
Copy Markdown
Member Author

We'll see. It's been high on my todo list. Let me see if I can get to it.

@embray embray modified the milestones: v0.4.1, v0.4.0 Jun 6, 2014
@mhvk mhvk mentioned this pull request Jun 18, 2014
@embray

embray commented Jun 20, 2014

Copy link
Copy Markdown
Member Author

Rebase and, as you can see, added the code from SymPy to do the float comparison. The original test I wrote for my own code passed with the SymPy code too, which is a good sign. I'm going to start looking at what doctests I can get passing with this enhancement.

@embray

embray commented Jun 24, 2014

Copy link
Copy Markdown
Member Author

I updated several of the existing doctests to make use of the FLOAT_CMP feature. I didn't do all of them, as there are just too many. But I chose a couple sets of docs that would exercise the feature well (cosmology, and some of the coordinates docs).

If anyone has time to kill they can try rewriting more of the doctests to use this feature, but otherwise it's maybe something to update as we go along.

One potential downside to this is that it means the doctests will be more sensitive to small changes in numeric results. Those changes could be significant and/or unintended in some cases, but unintentional in other cases. I think it's still better to err on the side of caution and, if some algorithmic changes result in slight differences then the author can check whether the difference is important and update the tests accordingly.

embray added a commit to embray/astropy that referenced this pull request Jun 24, 2014
@embray

embray commented Jun 24, 2014

Copy link
Copy Markdown
Member Author

Woot--build passed. Any objections to merging this for 0.4?

One last thing I might do is add some documentation for this feature.

embray added a commit to embray/astropy that referenced this pull request Jun 24, 2014
@astrofrog

Copy link
Copy Markdown
Member

Great! No objections from me :)

@eteq

eteq commented Jun 25, 2014

Copy link
Copy Markdown
Member

@embray - the last few coordinates PRs have added a lot of places in the coordinates documentation. where ... is used, and this might be more appropriate. So you might want to rebase and use this in those spots.

But of course it also works fine as-is right now, so either way works for me. And I'm 👍 on merging this regardless!

@embray

embray commented Jun 25, 2014

Copy link
Copy Markdown
Member Author

@eteq I already fixed a bunch of these in coordinates, but maybe I'll rebase and do a few more. Using this in more tests has helped iron out a few small corner cases so far.

@embray embray modified the milestones: v0.4.0, v0.4.1 Jun 25, 2014
@mhvk

mhvk commented Jun 25, 2014

Copy link
Copy Markdown
Contributor

Wonderful, all in favour of merging! Also note that there is one more equivalency doctest that came in recently (temperature_energy) and which has ellipses for the doctest.

embray added 10 commits June 25, 2014 12:44
…do about ellipses. Also note the warning that this could hide subtle differences in floating point representation that *could* be a bug, but also points out that there probably ought to be separate unit tests for issues like that.
Added code from Sympy's output checker for handling floating point
values in output.  This is similar to my previous approach but takes it
further, resolving most of the shortcomings of my original approach.  I
merged our existing OutputChecker subclass with the code from SymPy into
a new AstropyOutputChecker.

As roughly 100 lines of code were borrowed from the SymPy project I
included a copy of their license.
…eature, renamed FLOAT_COMPARISON to just FLOAT_CMP for the terseness.
…racters that may indicate the 'beginning' of a float. I think this is a bit arbitrary in fragile, as evidenced by the fact that I had to add a left-parens to the list, as it can certainly precede a float. Might change this later, but it's okay for now I guess.
@embray

embray commented Jun 25, 2014

Copy link
Copy Markdown
Member Author

Rebased. Updated several more of the coordinates test cases, and added documentation.

After the next build passes I'll merge this for 0.4. I won't be updating any more existing doctests in this PR or it'll never get done :) But once this is in people are free to contribute to that process.

embray added a commit that referenced this pull request Jun 25, 2014
Parse and do float comparison of float output in doctests
@embray embray merged commit 070742e into astropy:master Jun 25, 2014
@embray embray deleted the issue-2087 branch June 25, 2014 17:55
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 7, 2020
Parse and do float comparison of float output in doctests
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.

7 participants