-
Notifications
You must be signed in to change notification settings - Fork 132
Adding equivalent to numpy.nan_to_num functionality #540
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
Conversation
First PR from yesterday's code sprint. Main reasons for marking as in progress included:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening the PR. Looks great!
I left some small technical suggestions. We will also need some tests
x = switch(bitwise_and(isinf(x), pos), maxf, x) | ||
x = switch(bitwise_and(isinf(x), ~pos), minf, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's compare directly with posinf and neginf, if we don't have a isposinf
and isneginf
it's a good time to add it.
x = switch(bitwise_and(isinf(x), pos), maxf, x) | |
x = switch(bitwise_and(isinf(x), ~pos), minf, x) | |
x = switch(bitwise_and(isinf(x), pos), maxf, x) | |
x = switch(bitwise_and(isinf(x), ~pos), minf, x) |
# Get max and min values representable by x.dtype | ||
maxf = np.finfo(x.real.dtype).max | ||
minf = np.finfo(x.real.dtype).min |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's define it only when posinf and neginf are None
@@ -2937,7 +2937,58 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None | |||
return out | |||
|
|||
|
|||
def nan_to_num(x, nan=0.0, posinf=None, neginf=None): | |||
""" | |||
Replace NaN values with the `nan` keyword, +INF with the `posinf` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just copy Numpy docstring. No point in reinventing it
""" | ||
|
||
# Replace NaN's with nan keyword | ||
x = switch(isnan(x), nan, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of overriding x
, let's write all the checks isnan
`isposinf\
isneginf` on the original array.
|
Hi @aspeers , are you still working on this? |
Adding equivalent to numpy.nan_to_num functionality
(https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html)
addressing issue 479 (#479).
Motivation for these changes
Requested in issue #479
Implementation details
Added nan_to_num in pytensor/tensor/math.py
Checklist
Major / Breaking Changes
New features
Bugfixes
Documentation
Example:
Maintenance