Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']
)
pars = pdf.config.suggested_init()
data = source['bindata']['data'] + pdf.config.auxdata
tensorlib, _ = backend
assert tensorlib.tolist(pdf.pdf(pars, data)) == pytest.approx(
[0.002417118312751542], 2.5e-05
)
assert tensorlib.tolist(pdf.expected_data(pars)) == pytest.approx(
[60.0, 51.020408630], 1e-08
)
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'],
source['bindata']['bkg'],
source['bindata']['bkgerr'],
batch_size=2,
)
pars = [pdf.config.suggested_init()] * 2
data = source['bindata']['data'] + pdf.config.auxdata
tensorlib, _ = backend
assert tensorlib.tolist(pdf.pdf(pars, data)) == pytest.approx(
[0.002417118312751542] * 2, 2.5e-05
)
assert tensorlib.tolist(pdf.expected_data(pars))
assert tensorlib.tolist(pdf.expected_data(pars)[0]) == pytest.approx(
[60.0, 51.020408630], 1e-08
def model_setup(backend):
np.random.seed(0)
n_bins = 100
model = pyhf.simplemodels.hepdata_like([10] * n_bins, [50] * n_bins, [1] * n_bins)
init_pars = model.config.suggested_init()
observations = np.random.randint(50, 60, size=n_bins).tolist()
data = observations + model.config.auxdata
return model, data, init_pars
def test_pdf_inputs(backend):
source = {
"binning": [2, -0.5, 1.5],
"bindata": {"data": [55.0], "bkg": [50.0], "bkgerr": [7.0], "sig": [10.0]},
}
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']
)
pars = pdf.config.suggested_init()
data = source['bindata']['data'] + pdf.config.auxdata
tensorlib, _ = backend
assert tensorlib.shape(tensorlib.astensor(data)) == (2,)
assert tensorlib.shape(tensorlib.astensor(pars)) == (2,)
assert tensorlib.tolist(pdf.pdf(pars, data)) == pytest.approx(
[0.002417160663753748], abs=1e-4
)
assert tensorlib.tolist(pdf.logpdf(pars, data)) == pytest.approx(
[-6.025179228209936], abs=1e-4
)
def test_pdf_basicapi_tests(backend):
source = {
"binning": [2, -0.5, 1.5],
"bindata": {"data": [55.0], "bkg": [50.0], "bkgerr": [7.0], "sig": [10.0]},
}
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']
)
pars = pdf.config.suggested_init()
data = source['bindata']['data'] + pdf.config.auxdata
tensorlib, _ = backend
assert tensorlib.tolist(pdf.pdf(pars, data)) == pytest.approx(
[0.002417118312751542], 2.5e-05
)
assert tensorlib.tolist(pdf.expected_data(pars)) == pytest.approx(
[60.0, 51.020408630], 1e-08
)
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'],
def test_smoketest_toys(backend):
tb, _ = pyhf.get_backend()
m = pyhf.simplemodels.hepdata_like([6], [9], [3])
s = m.make_pdf(pyhf.tensorlib.astensor(m.config.suggested_init()))
assert np.asarray(tb.tolist(s.log_prob(s.sample((1000,))))).shape == (1000,)
tb, _ = pyhf.get_backend()
m = pyhf.simplemodels.hepdata_like([6, 6], [9, 9], [3, 3], batch_size=13)
s = m.make_pdf(pyhf.tensorlib.astensor(m.batch_size * [m.config.suggested_init()]))
assert np.asarray(tb.tolist(s.sample((10,)))).shape == (10, 13, 4)
def test_smoketest_toys(backend):
tb, _ = pyhf.get_backend()
m = pyhf.simplemodels.hepdata_like([6], [9], [3])
s = m.make_pdf(pyhf.tensorlib.astensor(m.config.suggested_init()))
assert np.asarray(tb.tolist(s.log_prob(s.sample((1000,))))).shape == (1000,)
tb, _ = pyhf.get_backend()
m = pyhf.simplemodels.hepdata_like([6, 6], [9, 9], [3, 3], batch_size=13)
s = m.make_pdf(pyhf.tensorlib.astensor(m.batch_size * [m.config.suggested_init()]))
assert np.asarray(tb.tolist(s.sample((10,)))).shape == (10, 13, 4)
def test_hypotest(benchmark, backend, n_bins):
"""
Benchmark the performance of pyhf.utils.hypotest()
for various numbers of bins and different backends
Args:
benchmark: pytest benchmark
backend: `pyhf` tensorlib given by pytest parameterization
n_bins: `list` of number of bins given by pytest parameterization
Returns:
None
"""
source = generate_source_static(n_bins)
pdf = hepdata_like(
source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']
)
data = source['bindata']['data'] + pdf.config.auxdata
assert benchmark(hypotest, pdf, data)
def test_pdf_batched(backend):
tb, _ = backend
source = {
"binning": [2, -0.5, 1.5],
"bindata": {"data": [55.0], "bkg": [50.0], "bkgerr": [7.0], "sig": [10.0]},
}
model = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'],
source['bindata']['bkg'],
source['bindata']['bkgerr'],
batch_size=2,
)
pars = [model.config.suggested_init()] * 2
data = source['bindata']['data'] + model.config.auxdata
model.pdf(pars, data)
model.expected_data(pars)
def test_invalid_pdf_pars():
source = {
"binning": [2, -0.5, 1.5],
"bindata": {"data": [55.0], "bkg": [50.0], "bkgerr": [7.0], "sig": [10.0]},
}
pdf = pyhf.simplemodels.hepdata_like(
source['bindata']['sig'], source['bindata']['bkg'], source['bindata']['bkgerr']
)
pars = pdf.config.suggested_init() + [1.0]
data = source['bindata']['data'] + pdf.config.auxdata
with pytest.raises(pyhf.exceptions.InvalidPdfParameters):
pdf.logpdf(pars, data)