Skip to content

Conversation

@mhvk
Copy link
Contributor

@mhvk mhvk commented May 17, 2017

fixes #6086

numpy 1.13 1.14 changes the formatting of float scalars, to be identical to what is used for arrays. This is a good thing in principle, but breaks a lot of doctests. Furthermore, array2string now has the style keyword deprecated.

This PR hopefully addresses both issues.

I'm putting the milestone at 2.0, since it may not be worth backporting... EDIT: especially since the real change is for numpy 1.14, not 1.13.

@mhvk
Copy link
Contributor Author

mhvk commented May 18, 2017

@bsipocz, @adrn - would you be able to have a look? It is mostly just keeping up with a change in numpy (one for the better, in my opinion) in which scalars now use the same formatting as values in an array.

@mhvk
Copy link
Contributor Author

mhvk commented May 18, 2017

As this seems to have solved the numpy-1.13rc problems as well, I'm reverting the allowed failure for rc.

fixes #6089

Copy link
Member

@adrn adrn left a comment

Choose a reason for hiding this comment

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

Left some comments, but mainly questions (sorry for asking so many!).

if NUMPY_LT_1_14:
assert repr(self.scalarfloatq) == "<Quantity 1.3 m>"
else:
assert repr(self.scalarfloatq) == "<Quantity 1.3 m>"
Copy link
Member

Choose a reason for hiding this comment

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

Huh. I know you're probably just making changes to make it consistent with what numpy does, but seems strange that an extra space is added here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The array style always has a place for the sign.

<Angle 3.5 rad>
>>> np.sin(a / 2) # doctest: +FLOAT_CMP
<Quantity 0.479425538604203>
>>> a == a
Copy link
Member

Choose a reason for hiding this comment

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

Why did this get removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't get doctest to think both ( True and (True were the same thing, and I thought this example was sufficiently trivial (and repeated below) that I could just remove it.

>>> g = Gaussian1D(amplitude=1, mean=0, stddev=1)
>>> g
<Gaussian1D(amplitude=1.0, mean=0.0, stddev=1.0)>
>>> g # doctest: +FLOAT_CMP
Copy link
Member

Choose a reason for hiding this comment

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

If FLOAT_CMP is needed here, what do the actual repr's end up looking like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For old numpy, 1.0, for new numpy 1.


>>> ndd = NDDataMaskBoolNumpy([1,2,3])
>>> ndd.mask = True
>>> ndd.mask = True, False, True
Copy link
Member

Choose a reason for hiding this comment

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

I know this is valid, but maybe we want to make it [True, False, True] to be more explicit? This might look strange to new users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, done

>>> import numpy as np
>>> np.exp((1j*0.125*u.cycle).to('', equivalencies=u.dimensionless_angles())) # doctest: +FLOAT_CMP
<Quantity (0.7071067811865476+0.7071067811865475j)>
>>> np.exp((1j*0.125*u.cycle).to('', equivalencies=u.dimensionless_angles())) # doctest: +SKIP
Copy link
Member

Choose a reason for hiding this comment

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

Why is this skipped now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New numpy removes the parentheses around the complex number; I didn't know how to get doctest to accept both as valid...

>>> u.Magnitude(15.)
<Magnitude 15.0 mag>
>>> u.Magnitude(10.*u.count/u.second)
>>> u.Magnitude(12.5) # doctest: +FLOAT_CMP
Copy link
Member

Choose a reason for hiding this comment

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

Are all these FLOAT_CMP necessary? The float format usually isn't messed up when no arithmetic is done, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this particular one is actually not necessary -- the real reason is to ensure that nnn.0 and nnn. are treated as being the same -- and here I changed to nnn.5 for that reason.

@bsipocz
Copy link
Member

bsipocz commented May 18, 2017

@mhvk - Looks good to me.

A bit more FLOAT_CMP were added to the docstring than I like, but I guess it's better to be on the safe side. I wish none of these doctest comments were shown in the python interpreter when asking for help, but that's a completely separate upstream issue.

@mhvk
Copy link
Contributor Author

mhvk commented May 18, 2017

@bsipocz - good point about the docstrings; I'll see if for those I can try a bit harder not to use FLOAT_CMP.

@mhvk mhvk force-pushed the numpy-style-changes branch from a56b7fe to 56372a4 Compare May 18, 2017 13:49
@mhvk
Copy link
Contributor Author

mhvk commented May 18, 2017

This is OK, but let's hold off a little as there is some discussion in numpy/numpy#8983 (comment) about whether the effects of the change were all that good (though I'd guess that would only lead to more/other edits...)

@bsipocz
Copy link
Member

bsipocz commented Jun 26, 2017

@mhvk - I guess there is still no clear decision upstream, so I'm moving the milestone of this to the next bugfix release.

@bsipocz bsipocz modified the milestones: v2.0.1, v2.0.0 Jun 26, 2017
@bsipocz bsipocz modified the milestones: v2.0.1, v2.0.2 Jul 28, 2017
@astropy-bot
Copy link

astropy-bot bot commented Jul 28, 2017

Hi there @mhvk 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labelled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃.

Everything looks good from my point of view! 👍

If there are any issues with this message, please report them here

@mhvk mhvk force-pushed the numpy-style-changes branch from af2c87f to 9b7f675 Compare October 15, 2017 15:45
@bsipocz
Copy link
Member

bsipocz commented Oct 16, 2017

@mhvk - Not backporting this would mean that the LTS version wont support 1.14. Do we really want that?

@mhvk
Copy link
Contributor Author

mhvk commented Oct 18, 2017

@bsipocz - I think this is mostly up to you - do you think this is easy enough to backport?

@mhvk
Copy link
Contributor Author

mhvk commented Oct 18, 2017

Note to whoever reviews: to make tests pass, I had to modify FLOAT_CMP to include eating up preceding spaces. It now is used so often that I wonder whether we should just enable it by default (like we already do with NORMALIZE_WHITESPACE. (This would be for a follow-up PR.)

@bsipocz
Copy link
Member

bsipocz commented Oct 18, 2017

@mhvk - if the rebase was relatively easy, then backporting shouldn't be too messed up either (I hope at least).

@bsipocz
Copy link
Member

bsipocz commented Oct 18, 2017

@mhvk - This looks uncontroversial to me, but if you think otherwise please assign someone to do the review.

@bsipocz
Copy link
Member

bsipocz commented Oct 18, 2017

Re: FLOAT_CMP - I think it's a good idea to have it on by default, the rst files and docstrings look rather messy with it. However I wonder whether we need to add another directive to switch it off, is there any place in the code that requires not using it?

@mhvk
Copy link
Contributor Author

mhvk commented Oct 18, 2017

Agreed this should be uncontroversial, so maybe you can give the required OK? And then perhaps we move the FLOAT_CMP to a new issue.

@bsipocz
Copy link
Member

bsipocz commented Oct 18, 2017

I agree that it's better to move the FLOAT_CMP to a separate issue/PR.

@bsipocz bsipocz merged commit 29a8a23 into astropy:master Oct 18, 2017
@bsipocz
Copy link
Member

bsipocz commented Oct 18, 2017

Thanks very much @mhvk!

@mhvk mhvk deleted the numpy-style-changes branch October 18, 2017 15:35
@mhvk
Copy link
Contributor Author

mhvk commented Oct 18, 2017

Finally things should work with numpy-dev again!

@bsipocz bsipocz mentioned this pull request Dec 9, 2017
8 tasks
@bsipocz bsipocz modified the milestones: v3.0.0, v2.0.3 Dec 9, 2017
bsipocz added a commit that referenced this pull request Dec 9, 2017
drdavella added a commit to drdavella/pytest-doctestplus that referenced this pull request Nov 14, 2018
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 7, 2020
ntessore pushed a commit to glass-dev/cosmology that referenced this pull request Dec 8, 2020
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.

Style issues with numpy dev

4 participants