Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distribution fixes to work with EarthLocation #15304

Merged
merged 11 commits into from Sep 18, 2023

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Sep 11, 2023

This pull request fixes Distribution so that it can work with EarthLocation. It does not yet check all the methods, but initialization via both geocentric and geodetic coordinates. Builds on #15296 (which in turn builds on #15295). so review by commit may be easiest.

Supercedes #14653 (and contains the commits from that; hence preferably no squash merge)

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions
Copy link

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions
Copy link

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

Copy link
Member

@eteq eteq left a comment

Choose a reason for hiding this comment

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

Thanks @mhvk, I think this is looking really good, definitely ready to go beyond "draft".

I have a few inline suggestions, but nothing major. I will admit I was not able to follow all of the numpy machinery, but I am satisfied enough that the tests are thorough and behave as expected.

I have some misgivings about the relatively dramatic-but-subtle API changes but I think they are already well-justified enough that's the benefits outweigh the costs.

astropy/uncertainty/core.py Show resolved Hide resolved

if NUMPY_LT_2_0:
from numpy.core.multiarray import normalize_axis_index
from numpy.lib.function_base import _parse_gufunc_signature
Copy link
Member

Choose a reason for hiding this comment

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

I just looked and _parse_gufunc_signature hasn't changed in 7 years, so this is probably not an issue, but... should we be worried it might change as a private function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should, and really the "true" gufunc parsing in the ufunc machinery should be exposed, which I hope to do one day, but for now I think it is fine to rely on our testing. Note that this is used in Masking too, where tests are a little more rigorous.

# TODO: this covers the case where a user puts in a list or so,
# but for those one could just explicitly do something like
# _generated_subclasses[list] = NdarrayDistribution
return NdarrayDistribution
Copy link
Member

Choose a reason for hiding this comment

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

I think this makes sense to assume, but probably should document in the Disribution main docstring that it will default to NdarrayDistribution if there's not a better choice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good point, done, now mention that NdarrayDistribution is the general outcome for non-ndarray input.

Comment on lines 46 to 51

The distribution, with sampling along the *trailing* axis. If 1D, the sole
dimension is used as the sampling axis (i.e., it is a scalar distribution).
If an |ndarray| or subclass, the data will not be copied unless it is not
possible to take a view (generally, only when the strides of the last axis
are negative).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The distribution, with sampling along the *trailing* axis. If 1D, the sole
dimension is used as the sampling axis (i.e., it is a scalar distribution).
If an |ndarray| or subclass, the data will not be copied unless it is not
possible to take a view (generally, only when the strides of the last axis
are negative).
The distribution, with sampling along the *trailing* axis. If 1D, the sole
dimension is used as the sampling axis (i.e., it is a scalar distribution).
If an |ndarray| or subclass, the data will not be copied unless it is not
possible to take a view (generally, only when the strides of the last axis
are negative).

Not sure if this is strictly necessary (it was at one point but maybe it's changed?), but most of the other docstrings follow the indented-under-parameters convention

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, no, that is me trying to get line links in other, forgetting to remove a blank line, and then some pre-commit changing the indent. Thanks for noticing! Now corrected.

# Sanity check. This should never happen!
raise RuntimeError(
f"found {base_cls=}, which does not have _DistributionRepr at "
"__mro__[1]. Please raise an issue."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"__mro__[1]. Please raise an issue."
"__mro__[1]. Please raise an issue at "
"https://github.com/astropy/astropy/issues/new/choose."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I should not have been that lazy... Now corrected.

@mhvk mhvk added the API change PRs and issues that change an existing API, possibly requiring a deprecation period label Sep 16, 2023
@mhvk mhvk marked this pull request as ready for review September 16, 2023 14:03
@mhvk
Copy link
Contributor Author

mhvk commented Sep 16, 2023

@eteq - Thanks so much for the quick review. As noted inline, I made the changes and set this to auto-merge since there is other stuff hanging on it, and I really want to clean my mess of branches up a bit! Plus, then I can rebase my old #14423 on this and also get Representation working!

@pllim
Copy link
Member

pllim commented Sep 16, 2023

@mhvk , you need to rebase to pick up matplotlib pin for RTD. Auto-merge is currently blocked.

mhvk and others added 11 commits September 17, 2023 10:59
As part of this, define _get_distribution_cls in analogy
to what is done in Masked._get_masked_cls. This is used
both in the initializer and in .view().
Arguably, this is more logical, since we want the array
to look like a regular array as much as possible.  It is
also required to get structured dtypes to work with Quantity.
This involves a second indirection in the actual dtype
of the array, which now has a "samples" entry that itself
consists of a n_samples "sample" entries. This is required
to ensure we can get specific fields out of a distribution
with a structured dtype.
With the new sample indirection, it becomes possible, by
using stride trickery to ensure that, e.g., viewing a complex
number as two floats will give the correct new shape.
…ution.

Also update tests a bit to ensure correct exceptions are raised.
This involved ensuring __array_ufunc__ could deal with gufuncs
properly, as well as making an initial __array_function__ which
so far only supports np.empty_like.
This adds new information to the previous .feature one,
and renames both it and the .api one to the correct PR number.
@mhvk
Copy link
Contributor Author

mhvk commented Sep 17, 2023

@pllim - thanks! Rebased with the matplotlib 3.8 fixes in, so now RTD does not fail anymore, but we got a new one (also unrelated, grrrr), cp39-manylinux wheel building is failing -- see #15333. Sorry to be the bearer of this further bad news!

# TODO: this part could be removed, with the try/except around the
# assignment to struc["x"], ..., below. But this is a small API change
# in that it will no longer possible to initialize with a unit-full x,
# and unit-less y, z. Arguably, though, that would just solve a bug.
Copy link
Member

Choose a reason for hiding this comment

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

I agree it's a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it annoyed, so I in fact have a follow-up PR ready to fix it! I decided not to apply that here since I felt it was good here to keep the changes to EarthLocation as minimal as possible.

Now if only this didn't get messed up by the numpy 1.26 release, which broken our python 3.9 wheels build...

@@ -602,6 +602,7 @@ def _validate_angles(self, angles=None):
# Ensure ndim>=1 so that comparison is done using the angle dtype.
# Otherwise, e.g., np.array(np.pi/2, 'f4') > np.pi/2 will yield True.
# (This feels like a bug -- see https://github.com/numpy/numpy/issues/23247)
# TODO: address this again when/if numpy 2.0 exists - see above issue
Copy link
Member

Choose a reason for hiding this comment

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

Is there a follow up issue for this TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is from @eteq's commit, so I don't take responsibility, but I think we're OK: our current code works fine, we can adjust whenever someone sees the TODO; a sort-of implicit issue, I guess!

Copy link
Member

Choose a reason for hiding this comment

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

This is a lot of text for a change log. You sure some of this doesn't belong in user doc?

Copy link
Member

Choose a reason for hiding this comment

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

What about user doc? Seems like an undocumented new feature?

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 current documentation does not go even close to the detailed level here... In a way, all this is an implementation detail (which is why I felt I could change it in the first place), so this API change notice is really addressed to people who actually use Distribution in gory detail (if any exist).

Note that I do hope to update the documentation once I can use Distribution in SkyCoord (not too far off, I'm hoping).

@pllim pllim disabled auto-merge September 18, 2023 13:23
@pllim pllim merged commit 763ee0c into astropy:main Sep 18, 2023
22 of 25 checks passed
@mhvk mhvk deleted the distribution-earthlocation branch September 18, 2023 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period coordinates Enhancement uncertainty
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants