From 6a1896aa8496fafa711a912d2bf58d08c6f144dd Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Mon, 13 May 2019 11:46:20 +0200 Subject: [PATCH] separate the calculation --- SecondaryValue/SecondaryValue.py | 51 +++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/SecondaryValue/SecondaryValue.py b/SecondaryValue/SecondaryValue.py index 82bc926..8170240 100644 --- a/SecondaryValue/SecondaryValue.py +++ b/SecondaryValue/SecondaryValue.py @@ -85,7 +85,7 @@ class SecondaryValue: return kwargs - def __call__(self, *args, **kwargs): + def _calculate(self, *args, **kwargs): """Calculates a value from the expression by substituting variables by the values of the given keyword arguments. If an argument is specified as a tuplpe of (value, error) the @@ -94,21 +94,9 @@ class SecondaryValue: :returns: value or [value, error] or [value, error], dependencies :rtype: numpy data type or np array of [value, errors, ...] or - a tuple the beforementioned as first element and a - dictionary with the calculated dependencies as a second value + a tuple the beforementioned as first element """ - kwargs, dep_values = self._calc_deps(**kwargs) - kwargs = self._inject_defaults(**kwargs) - - # check for missing symbols - if not self._symbols <= set(kwargs.keys()): - return RuntimeError('Missing symbols: ' + - (self._symbols - set(kwargs.keys())).__str__()) - - # filter out unneeded - kwargs = {var: val for var, val in kwargs.items() if var in self._symbols} - max_uncertainties = max([len(val) for _, val in kwargs.items() \ if isinstance(val, Iterable)] or [0]) @@ -133,11 +121,40 @@ class SecondaryValue: terms = np.array([np.sqrt(t.dot(t)) for t in terms], dtype=self._dtype) - if dep_values: - return np.insert(terms, 0, self._parsed.subs(values)), dep_values - return np.insert(terms, 0, self._parsed.subs(values)) + def __call__(self, *args, **kwargs): + """Calculates a value from the expression by substituting + variables by the values of the given keyword arguments. If an + argument is specified as a tuplpe of (value, error) the + gausssian error propagation will be computed. + + :returns: value or [value, error] or [value, error], dependencies + + :rtype: numpy data type or np array of [value, errors, ...] or + a tuple the beforementioned as first element and a + dictionary with the calculated dependencies as a second value + """ + + kwargs, dep_values = self._calc_deps(**kwargs) + kwargs = self._inject_defaults(**kwargs) + + # check for missing symbols + if not self._symbols <= set(kwargs.keys()): + return RuntimeError('Missing symbols: ' + + (self._symbols - set(kwargs.keys())).__str__()) + + # filter out unneeded + kwargs = {var: val for var, val in kwargs.items() \ + if var in self._symbols} + + terms = self._calculate(*args, **kwargs) + + if dep_values: + return terms, dep_values + + return terms + @lru_cache(maxsize=32) def _get_derivatives(self, *args):