pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Adding equivalent to numpy.nan_to_num functionality

Open aspeers opened this issue 1 year ago • 2 comments

Adding equivalent to numpy.nan_to_num functionality (https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html) addressing issue 479 (https://github.com/pymc-devs/pytensor/issues/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

  • added nan_to_num

Bugfixes

  • ...

Documentation

Example:

import pytensor
from pytensor import tensor as pt

import numpy as np

# Replace NaN
print("Replace NaN")
x = pt.dvector("x")
y = pt.log(x)

f = pytensor.function([x], y)

t = np.random.normal(size=(10,))
print("x")
print(t)
print("f = log(x)")
print(f (t))

# Test: NaN default replacement
print("\nTest: NaN default replacement")
y1 = pt.math.nan_to_num(y)
f = pytensor.function([x], y1)
print(f (t))

# Test: NaN custom replacement
print("\nTest: NaN custom replacement")
y2 = pt.math.nan_to_num(y, nan=1)
f = pytensor.function([x], y2)
print(f (t))

# Replace +INF/-INF
print("\n\nReplace +INF/-INF")
a = pt.dvector("a")
b = pt.dvector("b")
c = pt.math.true_div(a,b)

t_a = np.random.normal(size=(10,))
t_b = np.zeros((10,))

f = pytensor.function([a, b], c)
print("a")
print(t_a)
print("b")
print(t_b)
print("f = a/b")
print(f(t_a, t_b))

# Test: +INF/-INF default replacement
c1 = pt.math.nan_to_num(c)
f = pytensor.function([a, b], c1)
print("\nTest: +INF/-INF default replacement")
print(f(t_a, t_b))

# Test: +INF/-INF custom replacement
c2 = pt.math.nan_to_num(c, posinf=5, neginf=-5)
f = pytensor.function([a, b], c2)
print("\nTest: +INF/-INF custom replacement")
print(f(t_a, t_b))

Maintenance

  • ...

aspeers avatar Dec 08 '23 21:12 aspeers

First PR from yesterday's code sprint. Main reasons for marking as in progress included:

  • Had some difficulty running pytest locally. Resulted in multiple failures (most involving missing files/config parameters) which I figured the community could help by providing the relevant pytest command to run.
  • Could use advice on adding unit tests for the addition.
  • Was suggested to use ifelse() by @jessegrabowski instead of traditional if statements. ifelse is currently not imported/used in the math.py file and adding it to the imports list was causing a circular reference. Any thoughts would be welcomed!

aspeers avatar Dec 08 '23 22:12 aspeers

ifelse is not appropriate because it requires a scalar condition, and here we want to work with arbitrary tensors

ricardoV94 avatar Dec 09 '23 12:12 ricardoV94

Hi @aspeers , are you still working on this?

Dhruvanshu-Joshi avatar May 30 '24 04:05 Dhruvanshu-Joshi