Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import os
import pytest
# noinspection PyUnresolvedReferences
from zfit.core.testing import setup_function, teardown_function, tester
from zfit.util.exception import (LimitsIncompatibleError, LimitsNotSpecifiedError, SpaceIncompatibleError, )
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import zfit
from zfit.core.dimension import add_spaces, limits_consistent, limits_overlap, combine_spaces
obs = ['obs' + str(i) for i in range(4)]
space1 = zfit.Space(obs=obs)
space2 = zfit.Space(obs=reversed(obs))
space3 = zfit.Space(obs=obs)
space4 = zfit.Space(obs=obs[0:2])
lower1d_1 = ((1,),)
upper1d_1 = ((2,),)
lower1d_12 = ((3,),)
upper1d_12 = ((4,),)
lower1d_22 = ((5,),)
upper1d_22 = ((6,),)
combined_lim_1d_12_and_22 = (((3,), (5,)), ((4,), (6,)))
lower1d_2 = ((1,), (3,))
def _integrate(self, limits, norm_range):
raise SpecificFunctionNotImplementedError # fallback to the default sampling
@zfit.supports()
def _analytic_integrate(self, limits, norm_range):
raise SpecificFunctionNotImplementedError
@zfit.supports()
def _numeric_integrate(self, limits, norm_range):
raise SpecificFunctionNotImplementedError
mu1, mu2 = 0, 1.7
frac = 0.7
obs = zfit.Space('obs1', (mu1 - 0.5, mu2 + 1))
limits = zfit.Space('obs1', (mu1 - 0.3, mu2 + 0.1))
gauss1 = zfit.pdf.Gauss(obs=obs, mu=mu1, sigma=0.93)
gauss2 = zfit.pdf.Gauss(obs=obs, mu=mu2, sigma=1.2)
sumpdf = zfit.pdf.SumPDF([gauss1, gauss2], frac)
sumpdf_true = SimpleSampleSumPDF([gauss1, gauss2], frac)
integral = sumpdf.integrate(limits=limits, norm_range=False).numpy()
integral_true = sumpdf_true.integrate(limits=limits, norm_range=False).numpy()
assert integral_true == pytest.approx(integral, rel=0.03)
assert integral_true < 0.85
analytic_integral = sumpdf.analytic_integrate(limits=limits, norm_range=False).numpy()
assert integral_true == pytest.approx(analytic_integral, rel=0.03)
# Copyright (c) 2020 zfit
import zfit
# noinspection PyUnresolvedReferences
from zfit.core.testing import setup_function, teardown_function, tester
limits1 = (-4, 3)
limits2 = (-2, 5)
limits3 = (-1, 7)
obs1 = 'obs1'
obs2 = 'obs2'
space1 = zfit.Space(obs=obs1, limits=limits1)
space2 = zfit.Space(obs=obs1, limits=limits2)
space3 = zfit.Space(obs=obs1, limits=limits3)
space4 = zfit.Space(obs=obs2, limits=limits2)
space5 = space1.combine(space4)
def test_norm_range():
gauss1 = zfit.pdf.Gauss(1., 4., obs=space1)
gauss2 = zfit.pdf.Gauss(1., 4., obs=space1)
gauss3 = zfit.pdf.Gauss(1., 4., obs=space2)
sum1 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss2], fracs=0.4)
assert sum1.obs == (obs1,)
assert sum1.norm_range == space1
sum2 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss3], fracs=0.34)
assert sum2.norm_range.limits is None
def test_combine_spaces():
shift = 30
lower1, upper1 = [0, 1], [2, 3]
lower1b, upper1b = [0 + shift, 1 + shift], [2 + shift, 3 + shift]
lower2, upper2 = [-4, 1], [10, 3]
lower2b, upper2b = [-4 + shift, 1 + shift], [10 + shift, 3 + shift]
lower3, upper3 = [9, 1, 0], [11, 3, 2]
obs1 = ['x', 'y']
space1a = zfit.Space(obs1, limits=(lower1, upper1))
space1b = zfit.Space(obs1, limits=(lower1b, upper1b))
obs2 = ['z', 'y']
space2a = zfit.Space(obs2, limits=(lower2, upper2))
space2b = zfit.Space(obs2, limits=(lower2b, upper2b))
obs3 = ['a', 'y', 'x']
space3 = zfit.Space(obs3, limits=(lower3, upper3))
space3inc = zfit.Space(obs3, limits=(lower3, upper3[::-1]))
lower12 = [lower1[0], lower1[1], lower2[0]]
upper12 = [upper1[0], upper1[1], upper2[0]]
space12a = zfit.Space(('x', 'y', 'z'), limits=(lower12, upper12))
space12b = zfit.Space(('x', 'y', 'z'), limits=([low + shift for low in lower12], [up + shift for up in upper12]))
# space3 = zfit.Space(('x', 'y', 'z'), limits=([lower1[0], lower1[1], lower2[0]], [upper1[0], upper1[1], upper2[0]]))
obs2inv = space2a.with_obs(['y', 'z'])
space = space1a * space2a
assert space == space12a
assert space == space12a * obs2inv
assert space == space12a * obs2inv * space12a
setup_func_general()
def teardown_function():
Limit._experimental_allow_vectors = False
teardown_func_general()
ztf = z
from zfit.core.sample import accept_reject_sample
mu_true = 1.5
sigma_true = 1.2
low, high = -3.8, 2.9
obs1 = zfit.Space('obs1', (low, high))
class GaussNoAnalyticSampling(zfit.pdf.Gauss):
def _analytic_sample(self, n, limits: Space):
raise AnalyticSamplingNotImplementedError # HACK do make importance sampling work
class UniformNoAnalyticSampling(zfit.pdf.Uniform):
def _analytic_sample(self, n, limits: Space):
raise AnalyticSamplingNotImplementedError # HACK do make importance sampling work
def create_gauss1():
mu = zfit.Parameter("mu_sampling1", mu_true, mu_true - 2., mu_true + 7.)
# Copyright (c) 2019 zfit
import numpy as np
import zfit
from zfit import ztf
from zfit.core.histogram import midpoints_from_hist
from zfit.core.testing import setup_function, teardown_function, tester
import tensorflow as tf
data1 = np.random.normal(size=(1000, 3))
obs1 = zfit.Space("obs1", limits=(-100, 300))
obs2 = zfit.Space("obs2", limits=(-100, 300))
obs3 = zfit.Space("obs3", limits=(-100, 300))
obs = obs1 * obs2 * obs3
def test_histogramdd():
histdd_kwargs = {"sample": data1}
hist = zfit.hist.histogramdd(**histdd_kwargs)
bincount_np, edges_np = zfit.run(hist)
bincount_true, edges_true = np.histogramdd(**histdd_kwargs)
np.testing.assert_allclose(bincount_true, bincount_np)
np.testing.assert_allclose(edges_true, edges_np)
def test_histdata():
data = zfit.Data.from_numpy(obs=obs, array=data1)
shift = 30
lower1, upper1 = [0, 1], [2, 3]
lower1b, upper1b = [0 + shift, 1 + shift], [2 + shift, 3 + shift]
lower2, upper2 = [-4, 1], [10, 3]
lower2b, upper2b = [-4 + shift, 1 + shift], [10 + shift, 3 + shift]
lower3, upper3 = [9, 1, 0], [11, 3, 2]
obs1 = ['x', 'y']
space1a = zfit.Space(obs1, limits=(lower1, upper1))
space1b = zfit.Space(obs1, limits=(lower1b, upper1b))
obs2 = ['z', 'y']
space2a = zfit.Space(obs2, limits=(lower2, upper2))
space2b = zfit.Space(obs2, limits=(lower2b, upper2b))
obs3 = ['a', 'y', 'x']
space3 = zfit.Space(obs3, limits=(lower3, upper3))
space3inc = zfit.Space(obs3, limits=(lower3, upper3[::-1]))
lower12 = [lower1[0], lower1[1], lower2[0]]
upper12 = [upper1[0], upper1[1], upper2[0]]
space12a = zfit.Space(('x', 'y', 'z'), limits=(lower12, upper12))
space12b = zfit.Space(('x', 'y', 'z'), limits=([low + shift for low in lower12], [up + shift for up in upper12]))
# space3 = zfit.Space(('x', 'y', 'z'), limits=([lower1[0], lower1[1], lower2[0]], [upper1[0], upper1[1], upper2[0]]))
obs2inv = space2a.with_obs(['y', 'z'])
space = space1a * space2a
assert space == space12a
assert space == space12a * obs2inv
assert space == space12a * obs2inv * space12a
space = space3 * space1a
assert space == space3
for other_lower, other_upper in zip(lowers, uppers):
lower_same = np.allclose(lower, other_lower)
upper_same = np.allclose(upper, other_upper)
assert not lower_same ^ upper_same, "Bug, please report as issue. limits_overlap did not catch right."
if lower_same and upper_same:
break
else:
lowers.append(lower)
uppers.append(upper)
lowers = tuple(lowers)
uppers = tuple(uppers)
if len(lowers) == 0:
limits = None
else:
limits = lowers, uppers
new_space = zfit.Space(obs=spaces[0].obs, limits=limits)
return new_space
# Copyright (c) 2020 zfit
import numpy as np
import zfit
# create space
xobs = zfit.Space('xobs', (-4, 4))
yobs = zfit.Space('yobs', (-3, 5))
zobs = zfit.Space('z', (-2, 4))
obs = xobs * yobs * zobs
# parameters
mu1 = zfit.Parameter("mu1", 1., -4, 6)
mu23 = zfit.Parameter("mu_shared", 1., -4, 6)
sigma12 = zfit.Parameter("sigma_shared", 1., 0.1, 10)
sigma3 = zfit.Parameter("sigma3", 1., 0.1, 10)
# model building, pdf creation
gauss_x = zfit.pdf.Gauss(mu=mu1, sigma=sigma12, obs=xobs)
gauss_y = zfit.pdf.Gauss(mu=mu23, sigma=sigma12, obs=yobs)
gauss_z = zfit.pdf.Gauss(mu=mu23, sigma=sigma3, obs=zobs)
product_gauss = zfit.pdf.ProductPDF([gauss_x, gauss_y, gauss_z])
param1 = params['super_param']
param2 = params['param2']
param3 = params['param3']
lower, upper = limits.limit1d
lower = z.convert_to_tensor(lower) # the limits are now 1-D, for axis 1
upper = z.convert_to_tensor(upper)
# calculate the integral here, dummy integral
integral = data_0 ** 2 * param1 * param2 * param3 + z.reduce_sum([lower, upper])
return integral
# define the space over which it is defined. Here, we use the axes
lower_axis1 = ((-5,),)
upper_axis1 = ((zfit.Space.ANY_UPPER,),)
integral_axis1_limits = zfit.Space(axes=(1,),
limits=(lower_axis1, upper_axis1))
CustomPDF2D.register_analytic_integral(func=integral_axis1,
limits=integral_axis1_limits)
if __name__ == '__main__':
obs = zfit.Space('obs1', (-10, 10)) * zfit.Space('obs2', (-3, 5))
pdf = CustomPDF2D(1, 2, 3, obs=obs)
sample = pdf.sample(n=1000)