Skip to content

Numpy 1.14 style changes - #6959

Merged
bsipocz merged 8 commits into
astropy:masterfrom
mhvk:numpy-dev-style-changes
Dec 12, 2017
Merged

bsipocz merged 8 commits into
astropy:masterfrom
mhvk:numpy-dev-style-changes

Conversation

@mhvk

@mhvk mhvk commented Dec 9, 2017

Copy link
Copy Markdown
Contributor

Numpy 1.14 is being branched, so it seemed time to ensure we're passing all tests.

EDIT: commits are by module - feel free to just look at relevant commits! Check-list for review:

  • coordinates
  • io.fits
  • io.votable
  • nddata
  • stats
  • table
  • time
  • visualization

An annoyance is that bool arrays are now represented without dtype=bool (since that is obvious from the content), which meant that every time this was done in doctest, I had to add ..., which is rather ugly.

Furthermore, the repr of MaskedArray has become much more logical, but that means our doctests cannot work on both new and old - hence, I just skipped those (after adjusting to the new style).

@astropy-bot

astropy-bot Bot commented Dec 9, 2017

Copy link
Copy Markdown

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 labeled 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.

@bsipocz

bsipocz commented Dec 9, 2017

Copy link
Copy Markdown
Member

@mhvk - This being milestoned to 2.0.3 means that I can go ahead and backport #6090, right?

@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from e4cd13d to 4438383 Compare December 9, 2017 23:04
@mhvk

mhvk commented Dec 9, 2017

Copy link
Copy Markdown
Contributor Author

Ah, I had forgotten about that earlier discussion! I think if we can backport this without too much trouble, then, yes, let's do it (given that 2.0 is LTS).

@bsipocz

bsipocz commented Dec 9, 2017

Copy link
Copy Markdown
Member

The backport was surprisingly easy, there were some minor conflict only (and I hope git didn't mess up the automated ones).

@mhvk mhvk mentioned this pull request Dec 10, 2017
@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from 4438383 to 601ecbc Compare December 10, 2017 00:41
@mhvk

mhvk commented Dec 10, 2017

Copy link
Copy Markdown
Contributor Author

This now should have everything alright except for the new problems introduced by #6859.

@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from 601ecbc to 0fd8b13 Compare December 10, 2017 00:59
Comment thread docs/table/operations.rst
15.0
17.0
15.7
>>> print(obs_by_name['mag_b'].groups.aggregate(np.mean)) # doctest: +SKIP

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.

I looked at this on my cell phone 30 minutes ago and these values looked horrible (like 15.000000000002) and there was a doctest FLOAT_COMPARE. Now it's just skipped. What is the actual now?

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.

They do look horrible on numpy 1,14 - I think this is due to a change in how scalars are represented - they don't necessarily go through arrayprint any more, but rather always are represented such that the value is uniquely defined. I'm going to raise a separate issue for that, but here just want to be sure tests will continue to pass when the 1.14rc hits pypi.

@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from 0fd8b13 to afdd9eb Compare December 10, 2017 01:45
<TD>268435456</TD>
<TD>922337203685477</TD>
<TD>8.9999990000000007</TD>
<TD>8.9990234375</TD>

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.

I changed the example such that it is 9 - 1/2**10 and thus exactly representable.

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.

I think "8.9999990000000007" was designed for a purpose (perhaps to test floating point precision), so I am not comfortable with this change. Is there no way to make test pass without changing the file?

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.

I think this is actually OK:

In [1]: 8.9999990000000007 == 8.999999
Out[1]: True

Previously, numpy just put out a large number of digits, so that one was sure that a number was well-represented, but it didn't not check it could be done with fewer; now it puts out just the right number (see above).

It also seems clear the "truth" was generated once, and is now tested against. The actual input file (regression.xml) does in fact had 8.999999 (and with numpy 1.14, that is what you get out; older versions are the ones that are problematic).

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.

Alright, you're the numpy expert here, so I'll just go with the flow.

>>> nd2 = NDDataSliceable(nd, mask=mask)
>>> nd2[1:3].mask
array([False, True], dtype=bool)
>>> nd2slc = nd2[1:3]

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.

Avoid showing a bool array since dtype=bool has disappeared.


def test_setting_uncertainty_wrong_shape_raises_error(ccd_data):
with pytest.raises(ValueError):
ccd_data.uncertainty = np.random.random(size=2 * ccd_data.shape)

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.

Oops, this made a 100,100,100,100 array (which is a bit much for my 2GB laptop)

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.

Yeah, I already fixed that downstream (astropy/ccdproc#579) but forgot that the test had been copied over...

'------ ------- ------- ---- ----{0}'
'target -- -- -- --{0}'
' V_mag 12.8667 1.72111 11.1 15.2{0}').format(os.linesep)
if NUMPY_LT_1_14:

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.

We really shouldn't be testing float output!

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.

We can maybe use another table, could you check the output with astropy/io/ascii/tests/t/simple_csv.csv ? (I don't have time to install numpy 1.14 right now, maybe later today or tomorrow):

❯ showtable astropy/io/ascii/tests/t/simple_csv.csv --stats
<Table length=2>
name mean std min max
---- ---- --- --- ---
   a  2.5 1.5   1   4
   b  3.5 1.5   2   5
   c  4.5 1.5   3   6

@taldcroft taldcroft Dec 10, 2017

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.

I think this is a case where we should be testing non-nice floating point values. The whole point of showtable is creating human-readable output that is informative and "pleasant". So using real-world values is important to catch formatting issues that our users will end up seeing.

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.

@taldcroft, @saimn - yes, although it may be that we have to define what is pleasant ourselves rather than rely on numpy doing it for us. But let's discuss in #6962, as it is somewhat orthogonal to the point of this PR, which is simply to get things to pass with numpy 1.14.x.

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.

Agreed about moving the discussion to #6962 , so the current change is fine by me.

Comment thread astropy/time/core.py
>>> delta, status = t.get_delta_ut1_utc(return_status=True)
>>> status == TIME_BEFORE_IERS_RANGE
array([ True, False], dtype=bool)
array([ True, False]...)

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.

Yes, ugly, but don't know what else to do...

"""

filename = tmpdir.join(self.filename).strpath
filename = str(tmpdir.join(self.filename))

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.

This allows one to call tests from ipython without having a tmpdir (no idea where to import it from)

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.

LGTM

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.

@mhvk I just noticed that there are 5 other instances in this file also using .strpath (lines 23, 28, 33, 42, and 54). Do those need to be changed also?

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.

A quick search reveals that there are a lot of tests across astropy that use tmpdir .strpath. Is a larger PR needed?

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.

Yes, you're right, we should probably change all. It may be easiest if I remove this commit from the PR, since it is really not numpy-dev related.

>>> jup # doctest: +FLOAT_CMP +REMOTE_DATA
<SkyCoord (GCRS: obstime=2014-09-22 23:22:00.000, obsgeoloc=( 3949481.689878457, -550931.9118838, 4961151.73733447) m, obsgeovel=( 40.1745933, 288.00078051, 0.) m / s): (ra, dec, distance) in (deg, deg, AU)
( 136.91116201, 17.02935408, 5.94386022)>
<SkyCoord (GCRS: obstime=2014-09-22 23:22:00.000, obsgeoloc=(3949481.68990863, -550931.91188162, 4961151.73733451) m, obsgeovel=(40.15954083, 287.47876693, -0.04597867) m / s): (ra, dec, distance) in (deg, deg, AU)

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.

Not completely sure why this didn't fail before: the new baryvel has z component of geovel != 0.

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.

yeah, that's weird... I'm seeing the same as you, and that has nothing to do with numpy... maybe it's some weird quirk of the FLOAT_CMP? Anyway, looks fine to me

Comment thread docs/nddata/ccddata.rst
fill_value = 1e+20)
<BLANKLINE>
>>> 2 * np.ones(3) * ccd_masked # one return value will be masked # doctest: +SKIP
masked_array(data=[2.0, 4.0, --],

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.

Don't know how to get doctest to pass both old and new format...

Comment thread docs/nddata/nddata.rst
>>> ndd4.mask
array([False, True, False], dtype=bool)

or even a masked Quantity::

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.

I don't think we should encourage this in any way! Also, it looks very different in numpy 1.14 (probably a buglet)

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.

Thanks for this feedback!

@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from 21e1597 to 92e3c3c Compare December 11, 2017 15:22
@mhvk
mhvk requested a review from adrn December 11, 2017 15:25

@pllim pllim left a comment

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.

I am not comfortable with changing actual file values for votable tests.

<TD>268435456</TD>
<TD>922337203685477</TD>
<TD>8.9999990000000007</TD>
<TD>8.9990234375</TD>

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.

I think "8.9999990000000007" was designed for a purpose (perhaps to test floating point precision), so I am not comfortable with this change. Is there no way to make test pass without changing the file?

@MSeifert04

Copy link
Copy Markdown
Contributor

I agree that io.fits looks good. I also basically approve the changes made in nddata, but I hope that either @crawfordsm or @mwcraig could also have a look.

"""

filename = tmpdir.join(self.filename).strpath
filename = str(tmpdir.join(self.filename))

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.

LGTM

<TD>268435456</TD>
<TD>922337203685477</TD>
<TD>8.9999990000000007</TD>
<TD>8.9990234375</TD>

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.

Alright, you're the numpy expert here, so I'll just go with the flow.

@eteq eteq left a comment

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.

LGTM as far as coordinates are concerned

>>> jup # doctest: +FLOAT_CMP +REMOTE_DATA
<SkyCoord (GCRS: obstime=2014-09-22 23:22:00.000, obsgeoloc=( 3949481.689878457, -550931.9118838, 4961151.73733447) m, obsgeovel=( 40.1745933, 288.00078051, 0.) m / s): (ra, dec, distance) in (deg, deg, AU)
( 136.91116201, 17.02935408, 5.94386022)>
<SkyCoord (GCRS: obstime=2014-09-22 23:22:00.000, obsgeoloc=(3949481.68990863, -550931.91188162, 4961151.73733451) m, obsgeovel=(40.15954083, 287.47876693, -0.04597867) m / s): (ra, dec, distance) in (deg, deg, AU)

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.

yeah, that's weird... I'm seeing the same as you, and that has nothing to do with numpy... maybe it's some weird quirk of the FLOAT_CMP? Anyway, looks fine to me

@mwcraig mwcraig left a comment

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.

The nddata pieces of this look goo to me. Thanks for taking this on!

Comment thread docs/nddata/nddata.rst
>>> ndd4.mask
array([False, True, False], dtype=bool)

or even a masked Quantity::

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.

Thanks for this feedback!

@taldcroft taldcroft left a comment

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.

👍

@bsipocz

bsipocz commented Dec 12, 2017

Copy link
Copy Markdown
Member

@mhvk - can you rebase? Also I think this got approval from each relevant submodule lead or deputy, so ready to go.

mhvk added 8 commits December 12, 2017 11:56
Use a double that is exactly representable: 9 - 1/2**10 instead
of 8.999999 which gets represented as 8.999999000007 (or so)
on numpy <1.14.
In the process, changed one test that took a lot of memory as it
created a random array of shape 100,100,100,100. Also removed
all mention of masked Quantities, as these really are not supported,
and should not be encouraged to be used in any way.
Note: cannot do MaskedArray sensibly, so just skipping those.
@mhvk
mhvk force-pushed the numpy-dev-style-changes branch from 92e3c3c to 8b935fb Compare December 12, 2017 16:56
@bsipocz

bsipocz commented Dec 12, 2017

Copy link
Copy Markdown
Member

passing and approved, so I'm merging this now!

@bsipocz
bsipocz merged commit 515fa9b into astropy:master Dec 12, 2017
@bsipocz

bsipocz commented Dec 12, 2017

Copy link
Copy Markdown
Member

Enormous thanks @mhvk for keeping us compatible with the latest and greatest and even the future numpy!

bsipocz added a commit that referenced this pull request Dec 12, 2017
@mhvk
mhvk deleted the numpy-dev-style-changes branch December 20, 2018 19:56
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.

9 participants