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_with_zfit():
import zfit
from zfit.core.loss import UnbinnedNLL
from zfit.minimizers.minimizer_minuit import MinuitMinimizer
data = np.random.normal(1.2, 0.1, 10000)
obs = zfit.Space('x', limits=(0.1, 2.0))
mean = zfit.Parameter("mcalc", 1.2, 0.1, 2.)
sigma = zfit.Parameter("scalc", 0.1, 0.02, 0.2)
model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma)
data_ = zfit.data.Data.from_numpy(obs=obs, array=data)
def lossbuilder(model, data, weights=None):
loss = UnbinnedNLL(model=model, data=data, fit_range=[obs])
return loss
minimizer = MinuitMinimizer()
config = Config(model, data_, lossbuilder, minimizer)
calc = Calculator(config)
assert calc.minimizer == minimizer
calc.obsbestfit
assert calc.obsbestfit == config.bestfit
def test_fail_on_nan_strategy():
sigma = zfit.Parameter("sigma", 2.)
obs = zfit.Space("obs1", limits=(-4, 5))
gauss = zfit.pdf.Gauss(1., sigma, obs=obs)
sampler = gauss.create_sampler(3000)
sampler.set_data_range(obs)
nll = zfit.loss.UnbinnedNLL(model=gauss, data=sampler)
minimizer = zfit.minimize.Minuit(strategy=ToyStrategyFail())
sampler.resample()
sigma.set_value(2.1)
fitresult1 = minimizer.minimize(nll)
assert fitresult1.converged
sampler.resample()
sigma.set_value(math.inf)
fitresult2 = minimizer.minimize(nll)
assert not fitresult2.converged
assert fitresult2.edm == -999
assert fitresult2.fmin == -999
def test_truncated_gauss():
high = 2.
low = -0.5
truncated_gauss = zfit.pdf.TruncatedGauss(mu=1, sigma=2, low=low, high=high, obs=limits1)
gauss = zfit.pdf.Gauss(mu=1., sigma=2, obs=limits1)
probs_truncated = truncated_gauss.pdf(test_values)
probs_gauss = gauss.pdf(test_values)
probs_truncated_np, probs_gauss_np = [probs_truncated.numpy(), probs_gauss.numpy()]
bool_index_inside = np.logical_and(low < test_values, test_values < high)
inside_probs_truncated = probs_truncated_np[bool_index_inside]
outside_probs_truncated = probs_truncated_np[np.logical_not(bool_index_inside)]
inside_probs_gauss = probs_gauss_np[bool_index_inside]
assert inside_probs_gauss == pytest.approx(inside_probs_truncated, rel=1e-3)
assert all(outside_probs_truncated == 0)
def create_pdf(params):
param1, param2, param3, param4 = params
gauss1 = zfit.pdf.Gauss(param1, param2, obs=obs)
gauss2 = zfit.pdf.Gauss(param2, param2, obs=obs)
gauss3 = zfit.pdf.Gauss(param2, param4, obs=obs)
gauss4 = zfit.pdf.Gauss(param3, param1, obs=obs)
sum_pdf = zfit.pdf.SumPDF([gauss1, gauss2, gauss3, gauss4], fracs=[0.1, 0.3, 0.4])
return sum_pdf
def test_extract_extended_pdfs():
gauss1 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
gauss2 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
gauss3 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
gauss4 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
gauss5 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
gauss6 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
yield1 = zfit.Parameter('yield123' + str(np.random.random()), 200.)
# sum1 = 0.3 * gauss1 + gauss2
gauss3_ext = gauss3.create_extended(45)
gauss4_ext = gauss4.create_extended(100)
sum2_ext_daughters = gauss3_ext + gauss4_ext
sum3 = zfit.pdf.SumPDF((gauss5, gauss6), 0.4)
sum3_ext = sum3.create_extended(yield1)
sum_all = zfit.pdf.SumPDF(pdfs=[sum2_ext_daughters, sum3_ext])
sum_all.set_norm_range((-5, 5))
extracted_pdfs = extract_extended_pdfs(pdfs=sum_all)
assert frozenset(extracted_pdfs) == {gauss3_ext, gauss4_ext, sum3_ext}
def create_gauss3(nameadd=""):
mu3 = Parameter("mu3" + nameadd, mu_true, mu_true - 2., mu_true + 7.)
sigma3 = Parameter("sigma3" + nameadd, sigma_true, sigma_true - 10., sigma_true + 5.)
gauss3 = zfit.pdf.Gauss(mu=mu3, sigma=sigma3, obs=obs1)
return gauss3
def create_pdf(params):
param1, param2, param3, param4 = params
gauss1 = zfit.pdf.Gauss(param1, param2, obs=obs)
gauss2 = zfit.pdf.Gauss(param2, param2, obs=obs)
gauss3 = zfit.pdf.Gauss(param2, param4, obs=obs)
gauss4 = zfit.pdf.Gauss(param3, param1, obs=obs)
sum_pdf = zfit.pdf.SumPDF([gauss1, gauss2, gauss3, gauss4], fracs=[0.1, 0.3, 0.4])
return sum_pdf