31

I have two numpy arrays and I am trying to divide one with the other and at the same time, I want to make sure that the entries where the divisor is 0, should just be replaced with 0.

So, I do something like:

log_norm_images = np.where(b_0 > 0, np.divide(diff_images, b_0), 0)

This gives me a run time warning of:

RuntimeWarning: invalid value encountered in true_divide

Now, I wanted to see what was going on and I did the following:

xx = np.isfinite(diff_images)
print (xx[xx == False])

xx = np.isfinite(b_0)
print (xx[xx == False])

However, both of these return empty arrays meaning that all the values in the arrays are finite. So, I am not sure where the invalid value is coming from. I am assuming checking b_0 > 0 in the np.where function takes care of the divide by 0.

The shape of the two arrays are (96, 96, 55, 64) and (96, 96, 55, 1)

Luca
  • 10,458
  • 24
  • 107
  • 234

5 Answers5

20

You may have a NAN, INF, or NINF floating around somewhere. Try this:

np.isfinite(diff_images).all()
np.isfinite(b_0).all()

If one or both of those returns False, that's likely the cause of the runtime error.

rchang
  • 5,150
  • 1
  • 15
  • 25
  • 3
    Thanks. They both return true though :/ – Luca Jan 08 '15 at 15:19
  • 2
    It seems like you can safely ignore the warning. I tried doing this with two random arrays (and an arbitrary element in one of the arrays set to 0) - I get the runtime warning the first time I run `np.where`, but if I repeat the exact same expression a second time I don't get the warning. – rchang Jan 08 '15 at 15:40
  • 12
    I decided to ignore this after verifying that the output indeed looks like it should. So, using "with np.errstate(invalid='ignore', divide='ignore'):" – Luca Jan 08 '15 at 22:58
  • 1
    @Luca I think you should write this as an answer. – Nagabhushan S N Nov 27 '20 at 20:14
11

The reason you get the runtime warning when running this:

log_norm_images = np.where(b_0 > 0, np.divide(diff_images, b_0), 0)

is that the inner expression

np.divide(diff_images, b_0)

gets evaluated first, and is run on all elements of diff_images and b_0 (even though you end up ignoring the elements that involve division-by-zero). In other words, the warning happens before the code that ignores those elements. That is why it's a warning and not an error: there are legitimate cases like this one where the division-by-zero is not a problem because it's being handled in a later operation.

drammock
  • 2,373
  • 29
  • 40
3

Another useful Numpy command is nan_to_num(diff_images) By default it replaces in a Numpy array; NaN to zero, -INF to -(large number) and +INF to +(large number)

You can change the defaults, see https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html

arame3333
  • 9,887
  • 26
  • 122
  • 205
0

As @drammock pointed out, the cause of the warning is that some of the values in b_0 is 0 and the runtime warning is generated before the np.where is evaluated. While @Luca's suggestion of running np.errstate(invalid='ignore', divide='ignore'):" before the np.where will prevent the warning in this case, there may be other legitimate cases where this warning could be generated. For instance, corresponding elements of b_0 and diff_images are set to np.inf, which would return np.nan.

So to prevent warnings for known cases (i.e. b_0 = 0) and allow for warnings of unknown cases, evaluate the np.where first then evaluate the arithmetic:

#First, create log_norm_images
log_norm_images = np.zeros(b_0.shape)
#Now get the valid indexes
valid = where(b_0 > 0)
#Lastly, evaluate the division problem at the valid indexes
log_norm_images[valid] = np.divide(diff_images[valid], b_0[valid])
DavidH
  • 415
  • 4
  • 21
-2
num = np.array([1,2,3,4,5])
den = np.array([1,1,0,1,1])
res = np.array([None]*5)
ix  = (den!=0)
res[ix] = np.divide( num[ix], den[ix] )
print(res)

[1.0 2.0 None 4.0 5.0]

Farid Khafizov
  • 1,062
  • 12
  • 8