FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: Fixed ``where`` keyword for ``np.mean`` & ``np.var`` methods by mtsokol · Pull Request #18560 · numpy/numpy · GitHub

/ numpy Public

BUG: Fixed where keyword for np.mean & np.var methods - #18560

Merged
seberg merged 4 commits into
numpy:mainfrom
mtsokol:np-mean-where-keyword-bug
Mar 11, 2021
Merged

seberg merged 4 commits into
numpy:mainfrom
mtsokol:np-mean-where-keyword-bug

Conversation

mtsokol commented Mar 6, 2021

Copy link
Copy Markdown
Member

Addresses #18552

Hi @seberg!

I've managed to fix where keyword issue by refactoring those if statements.

So the rcount is a scalar when where is not provided as we reduce equal number of elements for given axis (or axes). So the first and statement prevents from illegal comparison of array to 0 value that caused original error. If the where clause is provided and rcount is an array (as we might want to reduce different number of elements along axis due to masking) then we check second and statement if any of those reduce groups are zero. I also maintained this micro-optimization to avoid calling _any(rcount == 0) when where is True and not zero.

Is this correct?

Also I had to change umr_any to _any as I think there is another bug there (or umr_any shouldn't be used there). np.any without axis parameter evaluates to scalar value (that can be used in if statement). But umr_any does not behave that way - it only reduces one dimension. Here's code for reproducing:

import numpy as np
from numpy.core import umath as um
a = np.ones((3,4))

# used in numpy/core/_methods.py
umr_any = um.logical_or.reduce

# np.any works as expected
if np.any(a == 0):
    pass

# but umr_any can't be used in if statement as here in `_mean`
if umr_any(a == 0):
    pass

Here's a colab reproduction: https://colab.research.google.com/drive/1UJi6E50zzaLu2nZQWMEFHPLZnq2v6pzT?usp=sharing

Thank you for any help!

Comment thread numpy/core/_methods.py Outdated

rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
if rcount == 0 if where is True else umr_any(rcount == 0):
if (where is True and rcount == 0) or (where is not True and _any(rcount == 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

So, after puzzling longer... The error I made is the missing axis=None here. It seems that unlike our top-level aliases, ufunc.reduce uses axis=0 by default. And I frankly would not have known that without stumbling over it even otherwise...

It was silly to delete it, but just as bad that none of these have tests that fail here. Could you use the axis=None solution (could pass by position if you like), and add a simple test?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@seberg Sure! I've just pushed changes reverting to umr_any and adding test case

Comment thread numpy/core/_methods.py Outdated

rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
if rcount == 0 if where is True else umr_any(rcount == 0):
if (where is True and rcount == 0) or \

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't get this logic - surely _count_reduce_items can return an nd array even if where is True, and this will still break?

mtsokol Mar 9, 2021
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think it's impossible for _count_reduce_items to return ndarray with where=True. In such case it returns a scalar items = nt.intp(1) with accumulated items items *= .... Have you got an example where it returns an array when where=True?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah you're right, it doesn't do what I think it does.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks for adding a test. Is it just me? I understand that the ternary operator was a bit confusing trying to understand whats wrong, but I can read it much easier than the duplicated where is True check, personally?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, it was confusing for me 😅. Should I revert it to original if ... if .. else:?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe make a temporary variable with a nice name if you like to avoid the two if on a single line?

mtsokol Mar 9, 2021
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

On the second thought I revered it to original notation as it's all clear to me now, sorry for this confusion. All pushed.

seberg added the 09 - Backport-Candidate PRs tagged should be backported label Mar 9, 2021

mtsokol commented Mar 10, 2021

Copy link
Copy Markdown
Member Author

I think it's ready but I see that one of the CI stages failed that previously was passing (previously whole CI was green). Also I see that it doesn't regard code itself. Should I do something about it?

charris changed the title BUG: Fixed where keyword for np.mean & np.var methods BUG: Fixed where keyword for np.mean & np.var methods Mar 10, 2021

mtsokol commented Mar 11, 2021

Copy link
Copy Markdown
Member Author

Thanks! I did a force push just to retrigger CI after PR rename - it's all green now.

seberg commented Mar 11, 2021

Copy link
Copy Markdown
Member

Sorry, looks good to me, and happy to merge. If you are still up for it, a test for both std and var would be nice, if not, also fine.

mtsokol commented Mar 11, 2021
edited
Loading

Copy link
Copy Markdown
Member Author

Sorry, looks good to me, and happy to merge. If you are still up for it, a test for both std and var would be nice, if not, also fine.

Sure! Give me 20 minutes. Can I just copy-paste that part with correct _res to std and var?

seberg commented Mar 11, 2021

Copy link
Copy Markdown
Member

Thanks @mtsokol lets put this in! :).

mtsokol commented Mar 11, 2021

Copy link
Copy Markdown
Member Author

Great!
If there's a next not-so-hard issue to take please let me know! If there isn't right now I will check issues page somewhere in the future.

charris pushed a commit to charris/numpy that referenced this pull request Mar 14, 2021
…mpygh-18560)

* Fixed  keyword bug

* Added test case

* Reverted to original notation

* Added tests for var and std

Closes numpygh-18552
charris removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 14, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL