Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into serialization_2020
Browse files Browse the repository at this point in the history
Deleted obsolete kwargs parameter in ComplexParameter.from_polar().
  • Loading branch information
SebastianJL committed Apr 22, 2020
2 parents 0987c98 + 10c29b3 commit 144a962
Show file tree
Hide file tree
Showing 35 changed files with 810 additions and 245 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Major Features and Improvements
`DefaultToyStrategy` in `zfit.mnimize`.
- Exceptions are now publicly available in `zfit.exception`
- Added nice printout for `FitResult` and `FitResult.params`.
- `get_params` is now more meaningful, returning by default all independent parameters of the pdf, including yields.
Arguments (`floating`, `is_yield`) allow for more fine-grained control.

Breaking changes
------------------
Expand All @@ -62,7 +64,17 @@ Breaking changes
To extract limits from multiple limits, `MultiSpace` and `Space` are both iterables, returning
the containing spaces respectively itself (for the `Space` case).
- SumPDF changed in the behavior. Read above in the Major Features and Improvement.
- Integrals of extended PDFs are not extended anymore.
- Integrals of extended PDFs are not extended anymore, but `ext_integrate` now returns the
integral multiplied by the yield.

Depreceations
-------------
- `ComposedParameter` takes now `params` instead of `dependents` as argument, it acts now as
the arguments to the `value_fn`. To stay future compatible, create e.g. `def value_fn(p1, pa2)`
and using `params = ['param1, param2]`, `value_fn` will then be called as `value_fn(param1, parma2)`.
`value_fn` without arguments will probably break in the future.
- `FitResult.error` has been renamed to `errors` to better reflect that multiple errors, the lower and
upper are returned.


Bug fixes and small changes
Expand All @@ -89,6 +101,7 @@ Requirement changes
Thanks
------
- Johannes Lade for code review and discussions.
- Hans Dembinski for useful inputs to the uncertainties.

0.4.3 (11.3.2020)
=================
Expand Down
16 changes: 16 additions & 0 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ As an example, with the :py:class:`~zfit.minimize.Minuit` one can calculate the

.. code-block:: pycon
>>> param_errors = result.errors()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{{}}}'.format(var.name, errors['upper'], errors['lower']))
mu: ^{+0.00998104141841555}_{-0.009981515893414316}
sigma: ^{+0.007099472590970696}_{-0.0070162654764939734}
Once we've performed the fit and obtained the corresponding uncertainties, it is now important to examine the fit results.
The object
>>> param_errors = result.errors()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{{}}}'.format(var.name, errors['upper'], errors['lower']))
mu: ^{+0.00998104141841555}_{-0.009981515893414316}
sigma: ^{+0.007099472590970696}_{-0.0070162654764939734}

Once we've performed the fit and obtained the corresponding uncertainties, it is now important to examine the fit results.
The object
>>> param_errors = result.error()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{{}}}'.format(var.name, errors['upper'], errors['lower']))
Expand Down
16 changes: 16 additions & 0 deletions docs/intro/minimize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ The :py:func:`~zfit.minimizers.fitresult.FitResult.error` method can be used to

.. code-block:: pycon
>>> param_errors = result.errors()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{-{}}}'.format(var.name, errors['upper'], errors['lower']))
mu: ^{+0.00998104141841555}_{--0.009981515893414316}
sigma: ^{+0.007099472590970696}_{--0.0070162654764939734}
The
>>> param_errors = result.errors()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{-{}}}'.format(var.name, errors['upper'], errors['lower']))
mu: ^{+0.00998104141841555}_{--0.009981515893414316}
sigma: ^{+0.007099472590970696}_{--0.0070162654764939734}


The
>>> param_errors = result.error()
>>> for var, errors in param_errors.items():
... print('{}: ^{{+{}}}_{{-{}}}'.format(var.name, errors['upper'], errors['lower']))
Expand Down
69 changes: 64 additions & 5 deletions docs/intro/parameter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,76 @@ parameter evaluated with the new value at run-time:
Dependent Parameter
-------------------

A parameter can be composed of several other parameters. We can use any :py:class:`~tf.Tensor` for that
and the dependency will be detected automatically. They can be used equivalently to :py:class:`~zfit.Parameter`.
A parameter can be composed of several other parameters. They can be used equivalently to :py:class:`~zfit.Parameter`.

.. code:: pycon
>>> mu2 = zfit.Parameter("mu_two", 7)
>>> dependent_func = lambda m, m2: m * 5 + m2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, params=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_dependencies() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_dependencies() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_cache_deps() # returns ordered-set(mu, mu2)
A special case of the above is :py
>>> dependent_func = lambda: mu * 5 + mu2 # or any kind of computation
>>> dep_param = zfit.ComposedParameter("dependent_param", dependent_func, dependents=[mu, mu2])
>>> dependents = dep_param.get_dependents() # returns ordered-set(mu, mu2)
>>> dependents = dep_param.get_params() # returns ordered-set(mu, mu2)
A special case of the above is :py:class:`~zfit.ComplexParameter`: it takes a complex :py:class:`tf.Tensor` as input and provides a few special methods (like :py:func:`~zfit.ComplexParameter.real`, :py:func:`~zfit.ComplexParameterconj` etc.) to easier deal with them.
Additionally, the :py:func:`~zfit.ComplexParameter.from_cartesian` and :py:func:`~zfit.ComplexParameter.from_polar` methods can be used to initialize polar parameters from floats, avoiding the need of creating complex :py:class:`tf.Tensor` objects.
A special case of the above is :py:class:`~zfit.ComplexParameter`: it takes a complex :py:class:`tf.Tensor` as input and
provides a few special methods (like :py:func:`~zfit.ComplexParameter.real`, :py:func:`~zfit.ComplexParameterconj` etc.)
to easier deal with them.
Additionally, the :py:func:`~zfit.ComplexParameter.from_cartesian` and :py:func:`~zfit.ComplexParameter.from_polar`
methods can be used to initialize polar parameters from floats, avoiding the need of creating complex
:py:class:`tf.Tensor` objects.
4 changes: 2 additions & 2 deletions examples/multidim_preprocess_fit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 zfit
# Copyright (c) 2020 zfit

import numpy as np
import zfit
Expand Down Expand Up @@ -43,5 +43,5 @@
print(result.params)

# do the error calculations, here with minos
param_errors = result.error()
param_errors = result.errors()
print(param_errors)
2 changes: 1 addition & 1 deletion examples/plot_sig_bkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
result = minimizer.minimize(nll)

# do the error calculations, here with minos
param_errors = result.error()
param_errors = result.errors()

plt.figure()
plt.title("After fitting")
Expand Down
2 changes: 1 addition & 1 deletion examples/signal_bkg_mass_extended_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
print(result.params)
# do the error calculations, here with minos
param_errors = result.hesse(method='hesse_np')
param_errors = result.error()
param_errors = result.errors()
2 changes: 1 addition & 1 deletion examples/signal_bkg_mass_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@

# do the error calculations, here with minos
param_errors = result.hesse()
param_errors = result.error()
param_errors = result.errors()
print(result.params)
5 changes: 2 additions & 3 deletions examples/simple_fit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 zfit
# Copyright (c) 2020 zfit

import numpy as np
import zfit
Expand All @@ -25,5 +25,4 @@
result = minimizer.minimize(nll)

# do the error calculations, here with minos
param_errors = result.error()

param_errors = result.errors()
11 changes: 7 additions & 4 deletions tests/test_basePDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,13 @@ def test_normalization(pdf_factory):
assert probs == pytest.approx(1., rel=0.05)
assert log_probs == pytest.approx(tf.math.log(probs_small).numpy(), rel=0.05)
dist = dist.create_extended(z.constant(test_yield))
probs_extended = dist.pdf(samples)
result_extended = probs_extended.numpy()
result_extended = np.average(result_extended) * (high - low)
assert result_extended == pytest.approx(1, rel=0.05)
probs = dist.pdf(samples)
probs_extended = dist.ext_pdf(samples)
result = probs.numpy()
result = np.average(result) * (high - low)
result_ext = np.average(probs_extended) * (high - low)
assert result == pytest.approx(1, rel=0.05)
assert result_ext == pytest.approx(test_yield, rel=0.05)


@pytest.mark.parametrize('gauss_factory', [create_gauss1, create_test_gauss1])
Expand Down
10 changes: 5 additions & 5 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from zfit import z
# noinspection PyUnresolvedReferences
from zfit.core.testing import setup_function, teardown_function, tester
from zfit.util.cache import Cachable, invalidate_graph, clear_graph_cache
from zfit.util.cache import GraphCachable, invalidate_graph, clear_graph_cache
from zfit.z.zextension import FunctionWrapperRegistry


class Example1(Cachable):
class Example1(GraphCachable):

def value(self):
value = self._cache.get("value")
Expand All @@ -24,11 +24,11 @@ def change_param(self, new_param):
return None


class MotherExample1(Cachable):
class MotherExample1(GraphCachable):

def __init__(self, test1, test2):
super().__init__()
self.add_cache_dependents([test1, test2])
self.add_cache_deps([test1, test2], )
self.test1 = test1
self.test2 = test2

Expand Down Expand Up @@ -65,7 +65,7 @@ def test_mother_cache():
CONST = 40


class GraphCreator1(Cachable):
class GraphCreator1(GraphCachable):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_gaussian_constraint_matrix():
assert constr_np == pytest.approx(trueval)
#assert constr_np == pytest.approx(3.989638)

assert constr.get_dependents() == set(params)
assert constr.get_cache_deps() == set(params)


def test_gaussian_constraint():
Expand All @@ -88,7 +88,7 @@ def test_gaussian_constraint():
constr = GaussianConstraint(params=params, observation=observed, uncertainty=sigma)
constr_np = constr.value().numpy()
assert constr_np == pytest.approx(true_val)
assert constr.get_dependents() == set(params)
assert constr.get_cache_deps() == set(params)

param_vals[0] = 2
params[0].set_value(param_vals[0])
Expand Down Expand Up @@ -185,4 +185,4 @@ def func():
constr_np = constr.value().numpy()
assert constr_np == pytest.approx(2.02)

assert constr.get_dependents() == set(params)
assert constr.get_cache_deps() == set(params)
Loading

0 comments on commit 144a962

Please sign in to comment.