From 80ce638013f168346c8ecc6d9faa3c9509ae8e01 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 3 Nov 2016 04:04:22 -0700 Subject: [PATCH] update 30 --- lib/util.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/util.py b/lib/util.py index c123b6f..ea6ef56 100644 --- a/lib/util.py +++ b/lib/util.py @@ -58,3 +58,13 @@ def monoize(s): s = np.sum(s, 1) s /= channels return s + +def div0(a, b): + """division, whereby division by zero equals zero""" + # http://stackoverflow.com/a/35696047 + a = np.asanyarray(a) + b = np.asanyarray(b) + with np.errstate(divide='ignore', invalid='ignore'): + c = np.true_divide(a, b) + c[~np.isfinite(c)] = 0 # -inf inf NaN + return c