Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_param_func():
param1 = Parameter('param1', 1.)
param2 = Parameter('param2', 2.)
param3 = Parameter('param3', 3., floating=False)
param4 = Parameter('param4', 4.)
a = z.math.log(3. * param1) * tf.square(param2) - param3
func = SimpleFunc(func=lambda self, x: a * x, obs=obs1)
new_func = param4 * func
new_func_equivalent = func * param4
result1 = new_func.func(x=rnd_test_values).numpy()
result1_equivalent = new_func_equivalent.func(x=rnd_test_values).numpy()
result2 = func.func(x=rnd_test_values) * param4
np.testing.assert_array_equal(result1, result2)
np.testing.assert_array_equal(result1_equivalent, result2)
def test_set_values():
init_values = [1, 2, 3]
second_values = [5, 6, 7]
params = [zfit.Parameter(f'param_{i}', val) for i, val in enumerate(init_values)]
with zfit.param.set_values(params, second_values):
for param, val in zip(params, second_values):
assert param.value().numpy() == val
for param, val in zip(params, init_values):
assert param.value().numpy() == val
zfit.param.set_values(params, second_values)
for param, val in zip(params, second_values):
assert param.value().numpy() == val
zfit.param.set_values(params, init_values)
for param, val in zip(params, init_values):
assert param.value().numpy() == val
def create_gauss1():
mu = zfit.Parameter("mu_sampling1", mu_true, mu_true - 2., mu_true + 7.)
sigma = zfit.Parameter("sigma_sampling1", sigma_true, sigma_true - 10., sigma_true + 5.)
gauss_params1 = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs1, name="gauss_params1_sampling1")
return gauss_params1, mu, sigma
def test_get_params():
obs = zfit.Space('obs', (-4, 5))
mu_nofloat = zfit.Parameter('mu_nofloat', 1, floating=False)
mu2 = zfit.Parameter('mu2', 1)
sigma2 = zfit.Parameter('sigma2', 2)
sigma_comp = zfit.ComposedParameter('sigma_comp', lambda s: s * 0.7, params=sigma2)
yield1 = zfit.Parameter('yield1', 10)
yield2 = zfit.Parameter('yield2', 200)
yield2_comp = zfit.ComposedParameter('yield2_comp', lambda y: y * 0.9, params=yield2)
gauss = zfit.pdf.Gauss(mu_nofloat, sigma_comp, obs)
gauss2 = zfit.pdf.Gauss(mu2, sigma2, obs)
gauss_ext = gauss.create_extended(yield1)
gauss2_ext = gauss2.create_extended(yield2_comp)
frac = zfit.Parameter("frac", 0.4)
sum1 = zfit.pdf.SumPDF([gauss, gauss2], fracs=frac)
sum1_ext = zfit.pdf.SumPDF([gauss_ext, gauss2_ext])
assert set(gauss.get_params()) == {sigma2}
with pytest.raises(ValueError):
set(gauss.get_params(floating=True, is_yield=False, extract_independent=False))
def test_add():
param1 = zfit.Parameter("param1", 1.)
param2 = zfit.Parameter("param2", 2.)
param3 = zfit.Parameter("param3", 2.)
pdfs = [0] * 4
pdfs[0] = Gauss(param1, 4, obs=obs1)
pdfs[1] = Gauss(param2, 5, obs=obs1)
pdfs[2] = Gauss(3, 6, obs=obs1)
pdfs[3] = Gauss(4, 7, obs=obs1)
datas = [0] * 4
datas[0] = z.constant(1.)
datas[1] = z.constant(2.)
datas[2] = z.constant(3.)
datas[3] = z.constant(4.)
ranges = [0] * 4
ranges[0] = (1, 4)
def test_gaussian_constraint_orderbug2(): # as raised in #162, failed before fixing
param1 = zfit.Parameter("param1", 1500)
param5 = zfit.Parameter("param2", 0.5)
param2 = zfit.Parameter("param3", 1.0)
param3 = zfit.Parameter("param4", 1.0)
param4 = zfit.Parameter("param5", 1.0)
constraint = {"params": [param1, param2, param3, param4, param5],
"observation": [1500, 1.0, 1.0, 1.0, 0.5],
"uncertainty": [0.05 * 1500, 0.001, 0.01, 0.1, 0.05 * 0.5]}
constr1 = GaussianConstraint(**constraint)
# param_vals = [1500, 1.0, 1.0, 1.0, 0.5]
constraint['x'] = [m.numpy() for m in constraint['params']]
true_val = true_gauss_constr_value(x=constraint['x'], mu=constraint['observation'],
sigma=constraint['uncertainty'])
def test_breitwigner1():
test_values = tf.random.uniform(minval=-2, maxval=4, shape=(100,))
mean = zfit.Parameter('mean', mean1_true, -10, 10)
width = zfit.Parameter('width', width1_true)
bw1 = zfit.pdf.Cauchy(m=mean, gamma=width, obs=obs1)
probs1 = bw1.pdf(x=test_values)
# TODO: add scipy dist?
probs1 = probs1.numpy()
assert all(probs1) > 0
# np.testing.assert_allclose(probs1, probs1_tfp, rtol=1e-2)
sample1 = bw1.sample(100)
assert len(sample1.numpy()) == 100
n_bins = 50
# create space
obs = zfit.Space("x", limits=(-10, 10))
# parameters
mu = zfit.Parameter("mu", 1., -4, 6)
sigma = zfit.Parameter("sigma", 1., 0.1, 10)
lambd = zfit.Parameter("lambda", -0.06, -1, -0.01)
# model building, pdf creation
gauss = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs)
exponential = zfit.pdf.Exponential(lambd, obs=obs)
n_bkg = zfit.Parameter('n_bkg', 20000)
n_sig = zfit.Parameter('n_sig', 1000)
gauss_extended = gauss.create_extended(n_sig)
exp_extended = exponential.create_extended(n_bkg)
model = zfit.pdf.SumPDF([gauss_extended, exp_extended])
# data
# n_sample = 10000
data = model.create_sampler(n=21200)
data.resample()
# set the values to a start value for the fit
mu.set_value(0.5)
sigma.set_value(1.2)
lambd.set_value(-0.05)
# create NLL
# /Inv_KSppi_DD_Stripping17b_MCTruth_Trigger_NN.root"
path = "/home/jonas/Documents/physics/software/zfit_project/tmp/Inv_KSppi_DD_Stripping17b_MCTruth_Trigger_NN.root"
treepath = "DecayTree"
branches = ['M_ppiKS']
data = zfit.data.Data.from_root(path=path, treepath=treepath, branches=branches)
mu = zfit.Parameter("mu", 5619, 5500, 5650)
# mu = zfit.Parameter("mu", 5619)
mu2 = zfit.Parameter("mu2", 5619, 5500, 5650)
# mu2 = zfit.Parameter("mu2", 5619)
sigma = zfit.Parameter("sigma", 60, 0, 300)
# sigma = zfit.Parameter("sigma", 60)
sigma2 = zfit.Parameter("sigma2", 60, 0, 300)
# sigma2 = zfit.Parameter("sigma2", 60)
a0 = zfit.Parameter("a0", 1., 0., 1000.)
# a0 = zfit.Parameter("a0", 1.)
a1 = zfit.Parameter("a1", -1., -10., 0.)
# a1 = zfit.Parameter("a1", -1.)
n0 = zfit.Parameter("n0", 1, 0, 10.)
# n0 = zfit.Parameter("n0", 1)
n1 = zfit.Parameter("n1", 1., 0000001., 10.)
# n1 = zfit.Parameter("n1", 1)
frac = zfit.Parameter("frac", 0.5, 0., 1.)
# gauss = zfit.pdf.Gauss(obs=Bmass, mu=mu, sigma=sigma)
CB1 = zfit.pdf.CrystalBallPDF(obs=Bmass, mu=mu, sigma=sigma, alpha=a0, n=n0)
CB2 = zfit.pdf.CrystalBallPDF(obs=Bmass, mu=mu, sigma=sigma, alpha=a1, n=n1)
# CB1 = zfit.pdf.Gauss(obs=Bmass, mu=mu, sigma=sigma)
# CB2 = zfit.pdf.Gauss(obs=Bmass, mu=mu2, sigma=sigma2)