8000 BUG: fix calling `np.nanvar` and `np.nanstd` with `Quantity` inputs + an `out` argument by neutrinoceros · Pull Request #17354 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fix calling np.nanvar and np.nanstd with Quantity inputs + an out argument#17354

Merged
mhvk merged 2 commits intoastropy:mainfrom
neutrinoceros:units/bug/17353/nanvar_out_regression
Nov 10, 2024
Merged

BUG: fix calling np.nanvar and np.nanstd with Quantity inputs + an out argument#17354
mhvk merged 2 commits intoastropy:mainfrom
neutrinoceros:units/bug/17353/nanvar_out_regression

Conversation

@neutrinoceros
Copy link
Contributor

Description

Fixes #17353

Given this was a regression in astropy 6.1.5, I would be tempted to propose a hotfix 6.1.6 release, so I'm requesting a review from the release team.

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

@neutrinoceros neutrinoceros added this to the v6.1.5 milestone Nov 8, 2024
@neutrinoceros neutrinoceros requested a review from a team November 8, 2024 10:09
@github-actions github-actions bot added the units label Nov 8, 2024
@github-actions
Copy link
Contributor
github-actions bot commented Nov 8, 2024

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?
  • 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
Contributor
github-actions bot commented Nov 8, 2024

👋 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?

@neutrinoceros neutrinoceros force-pushed the units/bug/17353/nanvar_out_regression branch from b5fc1b0 to 6cac14d Compare November 8, 2024 10:10
@neutrinoceros neutrinoceros force-pushed the units/bug/17353/nanvar_out_regression branch from 6cac14d to e553767 Compare November 8, 2024 10:14
Copy link
Contributor
@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Darn, silly to miss the out argument. See inline comments about just ignoring the unit of out, since it can be overwritten.

return (a.view(np.ndarray),) + args, kwargs, unit, None


def _nanop_helper(a, *, out, _out_unit_from_a):
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this is not how ufuncs treat out, so will need a bit of adjustment. Basically, if out is given, the unit should just be overwritten (just like the data are), so out_unit is just the unit of a, or dimensionless if a is not a Quantity (which is possible; numpy functions dispatch on both input and output arrays).

Copy link
Contributor Author
@neutrinoceros neutrinoceros Nov 9, 2024

Choose a reason for hiding this comment

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

numpy functions dispatch on both input and output arrays

this is actually why I needed to factor in out's units: if not, calling np.nanvar(q, out=o) (where q and o are Quantitys) would first be processed nanvar's helper (which strips out q's unit) and then through nanstd's helper (because o is still a quantity), but then o is the only place where the output unit is still available.

This seems critical so I want to find an agreement on this point before I move on to your other suggestions.

# TODO: for an ndarray output, we could in principle
# try converting all Quantity to dimensionless.
raise NotImplementedError
out_unit = out.unit
Copy link
Contributor

Choose a reason for hiding this comment

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

So I think this check goes to the detailed code, just using getattr(a, "unit", u.one)

if out_unit is None:
out_unit = _out_unit_from_a(a)
else:
assert out_unit == _out_unit_from_a(a)
Copy link
Contributor

Choose a reason for hiding this comment

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

As noted, out's unit can be overwritten and a can be a plain ndarray.

return out_kwarg, out_unit


@function_helper
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure nanstd actually needs a helper...

@neutrinoceros neutrinoceros marked this pull request as ready for review November 9, 2024 09:33
@mhvk mhvk force-pushed the units/bug/17353/nanvar_out_regression branch from e553767 to e276f5f Compare November 9, 2024 14:10
@mhvk
Copy link
Contributor
mhvk commented Nov 9, 2024

@neutrinoceros - I had to try myself to see what the problem was and ended up fixing it locally - and felt it was easiest to just force-push your branch, sorry! The tests now also include one for array input with quantity output.

Copy link
Contributor
@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Approving, though since I changed @neutrinoceros's code, best to get him to approve too.

# Also check array input, Quantity output.
o2 = np.nanstd(self.q.value, out=out)
assert o2 is out
assert o2.unit == u.dimensionless_unscaled
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 makes me wonder, shouldn't we also check the unit in o/out in the first half of the test ? (same question for test_nanvar_out)

Copy link
Contributor

Choose a reason for hiding this comment

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

It really is a bit of overkill, since just checking equality checks the unit too (and I chose it to be initialized with the wrong one one purpose). Here, though, I felt that with an array input it made perhaps more sense.

But obviously feel free to add the unit check!

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 chose it to be initialized with the wrong one one purpose

this wasn't obvious to me just from reading the test and I can easily imagine it looking like an oversight or a mistake to some future reader, so I expanded the tests with parametrization and added a comment to make this intention very explicit.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I think this covers the bases! Let's get it in.

@neutrinoceros neutrinoceros modified the milestones: v6.1.5, v6.1.6 Nov 10, 2024
@neutrinoceros neutrinoceros force-pushed the units/bug/17353/nanvar_out_regression branch from e276f5f to e96dc43 Compare November 10, 2024 16:51
@neutrinoceros
Copy link
Contributor Author

I revised the test again, so I feel like @mhvk should give it one (last ?) look before we merge.

@mhvk mhvk merged commit c5aad52 into astropy:main Nov 10, 2024
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Nov 10, 2024
…d` with `Quantity` inputs + an `out` argument
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Nov 10, 2024
…d` with `Quantity` inputs + an `out` argument
neutrinoceros added a commit that referenced this pull request Nov 11, 2024
…354-on-v7.0.x

Backport PR #17354 on branch v7.0.x (BUG: fix calling `np.nanvar` and `np.nanstd` with `Quantity` inputs + an `out` argument)
neutrinoceros added a commit that referenced this pull request Nov 11, 2024
…354-on-v6.1.x

Backport PR #17354 on branch v6.1.x (BUG: fix calling `np.nanvar` and `np.nanstd` with `Quantity` inputs + an `out` argument)
@neutrinoceros neutrinoceros deleted the units/bug/17353/nanvar_out_regression branch November 11, 2024 08:34
@larrybradley larrybradley mentioned this pull request Nov 14, 2024
1 task
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.

numpy.nanvar() now raises an error if out is specified in version 6.1.5

2 participants

0