How to use the zfit.z.function function in zfit

To help you get started, we’ve selected a few zfit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zfit / zfit / tests / test_limit.py View on Github external
assert limit2.has_rect_limits ^ bool(limit_fn)
        assert limit2.has_limits
        assert limit2.limits_are_set
        assert not limit2.limits_are_false
        inside21 = limit2.inside(2)
        inside22 = limit2.inside(2, guarantee_limits=True)
        outside2 = limit2.inside(4)
        if graph:
            equal = limit.equal(limit2, allow_graph=allow_graph)
        else:
            equal = limit == limit2

        return inside, inside2, outside, equal, inside21, inside22, outside2

    if graph:
        test = z.function(test)
    inside, inside2, outside, equal, inside21, inside22, outside2 = test(limit_fn=limit_fn)
    assert not (equal ^ bool(limit_fn))  # if a limit_fn is specified, this has precedency over the rect
    assert inside
    assert inside2
    assert not outside
    assert inside21
    assert inside22
    assert bool(outside2) ^ bool(limit_fn)  # if limit_fn, this is outside
github zfit / zfit / tests / test_coordinates.py View on Github external
with pytest.raises(ValueError):
            coords.reorder_x(x, x_axes=x_axes)
        with pytest.raises(ValueError):
            coords.reorder_x(x, func_axes=func_axes)

        coords = testclass(axes=axes)
        with pytest.raises(ValueError):
            coords.reorder_x(x, x_obs=x_obs)
        with pytest.raises(ValueError):
            coords.reorder_x(x, func_obs=func_obs)

        return x_all

    if graph:
        test = z.function(test)
    all_x = test()
    true_x = tf.constant([1, 2, 3, 4], dtype=tf.float64)
    for x in all_x:
        assert np.allclose(x, true_x)
github zfit / zfit / tests / test_parameter.py View on Github external
    @z.function
    def tf_call():
        repr_value = repr(param1)
        repr_value2 = repr(param2)
        assert str(val) not in repr_value
        assert str(val2) not in repr_value2
        assert 'graph-node' in repr_value
        assert 'graph-node' in repr_value2
github zfit / zfit / tests / test_math.py View on Github external
param2 = zfit.Parameter('param2', 5.)
    param3 = zfit.Parameter('param3', 2.)

    def func1():
        return param1 * param2 ** 2 + param3 ** param1

    def create_derivatives(func1, params):
        num_hessian_diag = numerical_hessian(func1, params=params, hessian='diag')
        num_hessian = numerical_hessian(func1, params=params)
        tf_hessian_diag = autodiff_hessian(func1, params=params, hessian='diag')
        tf_hessian = autodiff_hessian(func1, params=params)
        return num_hessian, num_hessian_diag, tf_hessian, tf_hessian_diag

    params = [param1, param2, param3]
    if graph:
        create_derivatives = z.function(create_derivatives)
    num_hessian, num_hessian_diag, tf_hessian, tf_hessian_diag = create_derivatives(func1, params)
    np.testing.assert_allclose(num_hessian, tf_hessian, rtol=1e-4, atol=1e-10)
    tf_hessian_diag_from_hessian = [tf_hessian[i, i] for i in range(len(params))]
    np.testing.assert_allclose(tf_hessian_diag_from_hessian, tf_hessian_diag, rtol=1e-4, atol=1e-10)
    np.testing.assert_allclose(num_hessian_diag, tf_hessian_diag, rtol=1e-4, atol=1e-10)
github zfit / zfit / tests / test_sampling.py View on Github external
        @z.function
        def __call__(self, n_to_produce, limits, dtype):
            importance_sampling_called[0] = True
            n_to_produce = tf.cast(n_to_produce, dtype=tf.int32)
            gaussian_sample = gauss_sampler.sample(n=n_to_produce, limits=limits)
            weights = gauss_sampler.pdf(gaussian_sample)
            weights_max = None
            thresholds = tf.random.uniform(shape=(n_to_produce,), dtype=dtype)
            return gaussian_sample, thresholds, weights, weights_max, n_to_produce
github zfit / zfit / zfit / models / polynomials.py View on Github external
@z.function(wraps='zfit_tensor')
def chebyshev_recurrence(p1, p2, _, x):
    """Recurrence relation for Chebyshev polynomials.

    T_{n+1}(x) = 2 x T_{n}(x) - T_{n-1}(x)

    """
    return 2 * tf.multiply(x, p1) - p2
github zfit / zfit / zfit / models / physics.py View on Github external
@z.function(wraps='zfit_tensor')
def crystalball_func(x, mu, sigma, alpha, n):
    t = (x - mu) / sigma * tf.sign(alpha)
    abs_alpha = tf.abs(alpha)
    a = tf.pow((n / abs_alpha), n) * tf.exp(-0.5 * tf.square(alpha))
    b = (n / abs_alpha) - abs_alpha
    cond = tf.less(t, -abs_alpha)
    func = z.safe_where(cond,
                        lambda t: _powerlaw(b - t, a, -n),
                        lambda t: tf.exp(-0.5 * tf.square(t)),
                        values=t, value_safer=lambda t: tf.ones_like(t) * (b - 2))
    func = tf.maximum(func, tf.zeros_like(func))
    return func
github zfit / zfit / zfit / models / polynomials.py View on Github external
@z.function(wraps='zfit_tensor')
def hermite_recurrence(p1, p2, n, x):
    """Recurrence relation for Hermite polynomials (physics).

    :math:`H_{n+1}(x) = 2x H_{n}(x) - 2n H_{n-1}(x)`

    """
    return 2 * (tf.multiply(x, p1) - n * p2)
github zfit / zfit / zfit / core / space.py View on Github external
@z.function(wraps='tensor', experimental_relax_shapes=True)
def calculate_rect_area(rect_limits):
    lower, upper = rect_limits
    diff = upper - lower
    area = z.unstable.reduce_prod(diff, axis=-1)
    return area