Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Fix piecewise implicit return type conversion #24309

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from

Conversation

priyank-p0
Copy link

Closes #24155 and #19755.

Fix for the bug which resulted in the output of piecewise function to be same dtype as the input. Now, the output dtype matches with the dtype of funclist output instead of the input.


func_val = func(vals, *args, **kw)
y = y.astype(asarray(func_val).dtype)
y[cond] = func_val
Copy link
Contributor

Choose a reason for hiding this comment

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

A few thoughts here:

  1. We probably need some core devs to chime in on whether a behavior change is justified from NumPy's perspective. I don't see many of the regulars discussing in either of the issues at this stage. We'd be breaking from presumably long-term documented behavior, even if it is a bit odd.
  2. Even if folks were in favor of a change that is not backwards compatible, on the basis that it might be considered a "bug fix," we'd almost certainly need to add some tests for various scenarios.
  3. What happens when the functions in funclist output values with dtypes that can't safely be combined?
    • Should we even convert out to object in weird cases?
    • Should we allow out to have its dtype size reduced relative to x if all functions in funclist return smaller width types than current type of x? I can imagine this breaking some control flows in potentially surprising ways perhaps.
    • is there anything to stop one of the functions in funclist from returning different types in different cases? You can pretty much make these functions do whatever you want with the return type, right? So, wouldn't it be hard to rigidly define what happens in these cases? Oof.

Copy link
Author

@priyank-p0 priyank-p0 Aug 21, 2023

Choose a reason for hiding this comment

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

@tylerjereddy Thank you for reviewing the code. You're right the changes do alter the traditional behavior. One thing I could think of, to support backward compatibility, is to introduce a flag which could allow the user to specify if they want to match the output dtype with the function output dtype or match it with the input dtype. I believe this could potentially answer the second point that you made. And finally, I can modify the implementation to match the piecewise output dtype with highest precesion output of all the functions in the funclist, but you're right we would require additional tests to make sure that there isn't any unexpected behavior. But yeah definetly need some intput from the core developers on the issue since any changes would result in modifying traditional behavior.

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.

BUG: numpy.piecewise returns data of the same type as the input type, which is undesirable
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.