How to use the zfit.run 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 marinang / lauztat / tests / test_config.py View on Github external
loss = UnbinnedNLL(model=model, data=data, fit_range=[obs])
        return loss

    config = Config(model, data_, lossbuilder, MinuitMinimizer())

    bf = config.bestfit

    assert bf.params[mean]["value"] == pytest.approx(1.2, abs=0.01)
    assert bf.params[sigma]["value"] == pytest.approx(0.1, abs=0.01)

    # test sampling

    sampler = config.sampler(n=len(data))
    toys = config.sample(sampler, 2)
    next(toys)
    toy1 = zfit.run(sampler)

    assert np.mean(toy1) == pytest.approx(1.2, abs=0.01)
    assert np.std(toy1) == pytest.approx(0.1, abs=0.01)

    next(toys)
    toy2 = zfit.run(sampler)
    assert np.mean(toy2) == pytest.approx(1.2, abs=0.01)
    assert np.std(toy2) == pytest.approx(0.1, abs=0.01)

    assert np.mean(toy1) == pytest.approx(np.mean(toy2), abs=0.01)
    assert np.std(toy1) == pytest.approx(np.std(toy2), abs=0.01)

    with pytest.raises(StopIteration):
        next(toys)
github zfit / zfit / tests / test_pdfs.py View on Github external
def normalization_testing(pdf, limits=None):
    limits = (low, high) if limits is None else limits
    space = Space(obs=obs1, limits=limits)
    with pdf.set_norm_range(space):
        samples = tf.cast(np.random.uniform(low=space.lower, high=space.upper, size=(40000, pdf.n_obs)),
                          dtype=tf.float64)
        samples = zfit.Data.from_tensor(obs=space, tensor=samples)
        probs = pdf.pdf(samples)
        result = probs.numpy()
        result = zfit.run(np.average(result) * space.rect_area())
        assert pytest.approx(result, rel=0.03) == 1
github zfit / zfit / tests / test_integrate.py View on Github external
def test_mc_integration(chunksize, limits):
    # simpel example
    zfit.run.chunking.active = True
    zfit.run.chunking.max_n_points = chunksize
    num_integral = zintegrate.mc_integrate(func=func1_5deps,
                                           limits=Space(limits=limits_simple_5deps,
                                                        axes=tuple(range(5))),
                                           n_axes=5)
    if isinstance(limits, list):
        spaces = [Space(limits=limit, axes=tuple(range(1))) for limit in limits]
        space2 = spaces[0] + spaces[1]
    else:
        space2 = Space(limits=limits2, axes=tuple(range(1)))
    num_integral2 = zintegrate.mc_integrate(func=func2_1deps,
                                            limits=space2,
                                            n_axes=1)
    num_integral3 = zintegrate.mc_integrate(func=func3_2deps,
                                            limits=Space(limits=limits3,
                                                         axes=(0, 1)),
                                            n_axes=2)
github zfit / zfit / tests / test_pdf_kde.py View on Github external
integral_silverman = kde_silverman.integrate(limits=limits, norm_range=False)
    #
    # import matplotlib.pyplot as plt
    # plt.plot(data, kde_adaptive.pdf(data), 'x')
    #
    # data_plot = np.linspace(np.min(data) - 1, np.max(data) + 1, 1000)
    # plt.hist(data, bins=20, density=1)
    # plt.plot(data_plot, kde_adaptive.pdf(data_plot), 'x')
    # plt.plot(data_plot, kde_silverman.pdf(data_plot), 'x')
    # plt.plot(data_plot, kde.pdf(data_plot), 'x')
    # plt.show()

    rel_tol = 0.04
    assert zfit.run(integral_trunc) == pytest.approx(1., rel=rel_tol)
    assert zfit.run(integral) == pytest.approx(expected_integral, rel=rel_tol)
    assert zfit.run(integral_adaptive) == pytest.approx(expected_integral, rel=rel_tol)
    assert zfit.run(integral_silverman) == pytest.approx(expected_integral, rel=rel_tol)

    sample = kde_adaptive.sample(1000)
    sample2 = kde_adaptive_trunc.sample(1000)
    assert sample.nevents == 1000
    assert sample2.nevents == 1000
github zfit / zfit / tests / test_pdf_cb.py View on Github external
def test_cb_integral():
    obs = zfit.Space('x', limits=bounds)

    mu_ = zfit.Parameter('mu_cb5', mu)
    sigma_ = zfit.Parameter('sigma_cb5', sigma)
    alphal_ = zfit.Parameter('alphal_cb5', alphal)
    nl_ = zfit.Parameter('nl_cb5', nl)

    cbl = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=alphal_, n=nl_)
    int_limits = (-1, 3)
    integral_numeric = cbl.numeric_integrate(limits=int_limits, norm_range=False)

    integral = cbl.analytic_integrate(limits=int_limits, norm_range=False)
    integral_numeric = zfit.run(integral_numeric)
    integral = zfit.run(integral)

    assert pytest.approx(integral_numeric, integral, 1e-5)
github zfit / zfit / tests / test_histogram.py View on Github external
def test_histdata():
    data = zfit.Data.from_numpy(obs=obs, array=data1)

    histdd_kwargs = {"bins": 4}
    histdata = data.create_hist(name="histdata", bin_kwargs=histdd_kwargs)

    bincount_true, edges_true = np.histogramdd(sample=data1, **histdd_kwargs)
    bincount_np, edges_np = zfit.run(histdata.hist())
    np.testing.assert_allclose(bincount_true, bincount_np)
    np.testing.assert_allclose(edges_true, edges_np)
github zfit / zfit / tests / test_execution.py View on Github external
def test_cpu_management(n_cpu, taken, left):
    zfit.run.set_n_cpu(n_cpu=n_cpu)
    _cpu = copy.deepcopy(zfit.run._cpu)
    assert zfit.run.n_cpu == n_cpu
    with zfit.run.aquire_cpu(max_cpu=taken) as cpus:
        assert zfit.run.n_cpu == left
        assert isinstance(cpus, list)
        assert len(cpus) == n_cpu - left
    assert zfit.run.n_cpu == n_cpu
    assert _cpu == zfit.run._cpu
github zfit / zfit / tests / test_space_0.py View on Github external
def test_equality(space1, space2):
    """
    Args:
        space1:
        space2:
    """
    assert space1.axes == space2.axes
    assert space1.obs == space2.obs
    np.testing.assert_allclose(space1.rect_limits, space2.rect_limits)
    # TODO: reactivate below
    assert zfit.run(space1.rect_area()) == pytest.approx(zfit.run(space2.rect_area()), rel=1e-8)