0

So I have the following code:

import numpy as np

array1 = np.array([[[[2, 2, 3], [0, 2, 0], [2, 0, 0]],
[[1, 2, 2], [2, 2, 0], [0, 2, 3]],
[[0, 4, 2], [2, 2, 2], [2, 2, 3]]],
[[[2, 3, 0], [3, 2, 0], [2, 0, 3]],
[[0, 2, 2], [2, 2, 0], [2, 2, 3]],
[[1, 0, 2], [2, 2, 2], [2, 2, 0]]],
[[[2, 0, 0], [0, 2, 0], [2, 0, 0]],
[[2, 2, 2], [0, 2, 0], [2, 2, 0]],
[[0, 2, 2], [2, 2, 2], [2, 2, 0]]]])

array2 = np.array([[[[2, 2, 3], [0, 2, 0], [2, 0, 0]],
[[1, 2, 2], [2, 2, 0], [0, 2, 3]],
[[0, 4, 2], [2, 2, 2], [2, 2, 3]]],
[[[2, 3, 0], [3, 2, 0], [2, 0, 3]],
[[0, 2, 2], [2, 10, 0], [2, 2, 3]],
[[1, 0, 2], [2, 2, 2], [2, 2, 0]]],
[[[2, 0, 0], [0, 2, 0], [2, 0, 0]],
[[2, 2, 2], [0, 2, 0], [2, 2, 0]],
[[0, 2, 2], [2, 2, 2], [2, 2, 0]]]])

def calc(x, y):
    result = y/x
    return result

final_result = []
for x, y in zip(array1, array2):
    final_result.append(calc(np.array(x), np.array(y)))

So all in all I have two lists that include some 3D arrays, and then I have defined a function. The last part is where I use each 3D array in the function, and I ultimately end up with a list (final_result) of some other 3D arrays where the function has been used on each entry from array1 and array2.

However, as you can see, array1 which ultimately gives the x values in the function does have 0 values in some of the entries. And yes, mathematically, this is no good. But in this case, I really just need the entries that does have a zero x-entry to be zero. So it doesn't need to run the function whenever that happens, but just skip it, and leave that entry as zero.

Can this be done?

Denver Dang
  • 2,433
  • 3
  • 38
  • 68
  • 1
    What about using: try and except case to avoid such errors ? – Shashank Mar 02 '17 at 18:43
  • 1
    "I have two lists that include some 3D arrays" - that's not what the code you've posted has. The code uses 2 4-dimensional arrays. Lists of arrays are usually a bad idea; you should only use them if you have a very strong, specific reason to do so. – user2357112 Mar 02 '17 at 18:45
  • Maybe I'm not using the right terms then :) What I actually have (not here) is an image converted into a 3D matrix. So what you might see as 4D is actually just slices of the images (CT scan). – Denver Dang Mar 02 '17 at 18:55

1 Answers1

3

This question has been answered here. Numpy has a specific way to catch such errors:

def calc( a, b ):
    """ ignore / 0, div0( [-1, 0, 1], 0 ) -> [0, 0, 0] """
    with np.errstate(divide='ignore', invalid='ignore'):
        c = np.true_divide( a, b )
        c[ ~ np.isfinite( c )] = 0  # -inf inf NaN
    return c
Community
  • 1
  • 1
nico
  • 2,022
  • 4
  • 23
  • 37
  • Beat me to it! I was also going to point out that you can set globally how numpy handles these errors, this documentation page is quite helpful: https://docs.scipy.org/doc/numpy/reference/generated/numpy.seterr.html – exp1orer Mar 02 '17 at 18:48
  • @exp1orer, can you give an example why it is bad to rely on the default numpy settings? – Marat Mar 02 '17 at 18:56
  • @Marat I'm not making any claim about whether the default settings are good or bad, it depends on the use. In general, I think that nico's suggestion of using a context manager to be explicit about where you expect these errors to occur, is probably best. – exp1orer Mar 03 '17 at 20:54
  • @exp1orer thank you. I think this comment adds some value to the answer – Marat Mar 04 '17 at 00:04