Commit 1784e31b authored by Alexander Froch's avatar Alexander Froch
Browse files

Merge branch 'master' into 112-flexible-validation-test-file-definition

parents f1c13835 66cb49e9
......@@ -13,6 +13,9 @@ darglint-ignore-regex = *
[darglint]
ignore=DAR203
docstring_style=numpy
strictness=full
log_level=INFO
[pylint.FORMAT]
max-line-length = 88
......
# flake8: noqa
# pylint: skip-file
from umami.helper_tools.histogram_tools import hist_ratio, hist_w_unc, step_divide
from umami.helper_tools.histogram_tools import hist_ratio, hist_w_unc, save_divide
......@@ -42,7 +42,7 @@ def hist_w_unc(a, bins, normed: bool = True):
return bin_edges, hist, unc, band
def step_divide(nominator, denominator, default=1.0):
def save_divide(nominator, denominator, default=1.0):
"""
Division using numpy divide function returning default value in cases where
denoinator is 0.
......@@ -60,11 +60,19 @@ def step_divide(nominator, denominator, default=1.0):
-------
ratio: array_like
"""
if isinstance(nominator, (int, float)) and isinstance(denominator, (int, float)):
output_shape = 1
else:
try:
output_shape = denominator.shape
except AttributeError:
output_shape = nominator.shape
ratio = np.divide(
nominator,
denominator,
out=np.ones(
nominator.shape,
output_shape,
dtype=float,
)
* default,
......@@ -111,14 +119,14 @@ def hist_ratio(nominator, denominator, nominator_unc, denominator_unc):
raise (
AssertionError("Denominator and denominator_unc don't have the same legth")
)
step_ratio = step_divide(nominator, denominator)
step_ratio = save_divide(nominator, denominator)
# Add an extra bin in the beginning to have the same binning as the input
# Otherwise, the ratio will not be exactly above each other (due to step)
step_ratio = np.append(np.array([step_ratio[0]]), step_ratio)
# Calculate rel uncertainties
nominator_rel_unc = step_divide(nominator_unc, nominator, default=0)
denominator_rel_unc = step_divide(denominator_unc, denominator, default=0)
nominator_rel_unc = save_divide(nominator_unc, nominator, default=0)
denominator_rel_unc = save_divide(denominator_unc, denominator, default=0)
# Calculate rel uncertainty
step_rel_unc = np.sqrt(nominator_rel_unc ** 2 + denominator_rel_unc ** 2)
......
......@@ -2,7 +2,7 @@ import unittest
import numpy as np
from umami.helper_tools import hist_ratio, hist_w_unc, step_divide
from umami.helper_tools import hist_ratio, hist_w_unc, save_divide
class hist_w_unc_TestCase(unittest.TestCase):
......@@ -51,17 +51,25 @@ class hist_w_unc_TestCase(unittest.TestCase):
np.testing.assert_almost_equal(band, self.band)
class step_divide_TestCase(unittest.TestCase):
class save_divide_TestCase(unittest.TestCase):
def test_zero_case(self):
steps = step_divide(np.zeros(2), np.zeros(2))
steps = save_divide(np.zeros(2), np.zeros(2))
np.testing.assert_equal(steps, np.ones(2))
def test_ones_case(self):
steps = step_divide(np.ones(2), np.ones(2))
steps = save_divide(np.ones(2), np.ones(2))
np.testing.assert_equal(steps, np.ones(2))
def test_half_case(self):
steps = step_divide(np.ones(2), 2 * np.ones(2))
steps = save_divide(np.ones(2), 2 * np.ones(2))
np.testing.assert_equal(steps, 0.5 * np.ones(2))
def test_denominator_float(self):
steps = save_divide(np.ones(2), 2)
np.testing.assert_equal(steps, 0.5 * np.ones(2))
def test_nominator_float(self):
steps = save_divide(1, np.ones(2) * 2)
np.testing.assert_equal(steps, 0.5 * np.ones(2))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment