From 75c3ebe3effc39a0fbc78ae24d415293576fceed Mon Sep 17 00:00:00 2001 From: hiro98 Date: Sun, 20 Oct 2019 21:50:02 +0200 Subject: [PATCH] fix the calculation of vectorized dependencies without error --- SecondaryValue/SecondaryValue.py | 20 ++++++++++++++------ SecondaryValue/test_secondaryvalue.py | 7 ++++++- setup.py | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/SecondaryValue/SecondaryValue.py b/SecondaryValue/SecondaryValue.py index 2a662cd..2bb3c8f 100644 --- a/SecondaryValue/SecondaryValue.py +++ b/SecondaryValue/SecondaryValue.py @@ -67,10 +67,13 @@ class SecondaryValue: continue # we always calculate the depndencies - tmp = sec_val(retdeps=True, **kwargs) + tmp = sec_val(retdeps=True, ret_explicit_errors=True, **kwargs) + result, deps = tmp + value = result[0] + errors = result[1:] if len(result) > 1 else False - kwargs[name] = tmp[0] - calc_deps[name] = tmp + kwargs[name] = [value] + list(errors) if errors else (value,) + calc_deps[name] = (kwargs[name], deps) return kwargs, calc_deps @@ -135,7 +138,6 @@ class SecondaryValue: if isinstance(val, Iterable) and len(val) > i} \ for i in range(1, max_uncertainties)] - values = {var: (val[0] if isinstance(val, Iterable) else val) \ for var, val in kwargs.items()} @@ -204,7 +206,8 @@ class SecondaryValue: return terms - def __call__(self, *args, retdeps=False, **kwargs): + def __call__(self, *args, retdeps=False, + ret_explicit_errors=False, **kwargs): """Calculates a value from the expression by substituting The values and errors can be iterable, but must compatible @@ -213,6 +216,9 @@ class SecondaryValue: :param retdeps: wether to return a dictionary with the calculated dependencies + :param ret_explicit_errors: wether to return the errors even + if they are None, used internally + :returns: value or [value, error] or [value, error, ...], dependencies @@ -231,7 +237,9 @@ class SecondaryValue: vector_values) if not errors: - return (central_value, dep_values) if retdeps else central_value + retval = (central_value,) if ret_explicit_errors \ + else central_value + return (retval, dep_values) if retdeps else retval # calculate errors result = self._calculate_errors(errors, vector_values, scalar_values) diff --git a/SecondaryValue/test_secondaryvalue.py b/SecondaryValue/test_secondaryvalue.py index 70c2a47..655bed8 100644 --- a/SecondaryValue/test_secondaryvalue.py +++ b/SecondaryValue/test_secondaryvalue.py @@ -44,7 +44,12 @@ class TestDependecies(): result, deps = self.y(a=1, b=2, retdeps=True) assert result == 3 - assert deps == {'x': (2, {})} + assert deps == {'x': ((2,), {})} + + def test_vectorized_dependencies(self): + result = self.y(a=[[1,2]], b=2) + print(result) + assert numpy.isclose(result, [3,4]).all() def test_dep_calc_err(self): # no retdep diff --git a/setup.py b/setup.py index acf27a0..b394a85 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def readme(): return f.read() setup(name='SecondaryValue', - version='0.1.9', + version='0.2.0', description='A helper to calculate the gaussian error propagation.', long_description=readme(), long_description_content_type='text/markdown',