How to use the pyhf.exceptions function in pyhf

To help you get started, we’ve selected a few pyhf 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 scikit-hep / pyhf / tests / test_pdf.py View on Github external
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)
github scikit-hep / pyhf / tests / test_optim.py View on Github external
def test_get_invalid_optimizer():
    with pytest.raises(pyhf.exceptions.InvalidOptimizer):
        assert pyhf.optimize.scipy
github scikit-hep / pyhf / tests / test_interpolate.py View on Github external
def test_invalid_interpcode():
    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get('fake')

    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get(1.2)

    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get(-1)
github scikit-hep / pyhf / tests / test_workspace.py View on Github external
def test_combine_workspace_same_channels(workspace_factory):
    ws = workspace_factory()
    new_ws = ws.rename(channels={'channel2': 'channel3'})
    with pytest.raises(pyhf.exceptions.InvalidWorkspaceOperation) as excinfo:
        combined = pyhf.Workspace.combine(ws, new_ws)
    assert 'channel1' in str(excinfo.value)
    assert 'channel2' not in str(excinfo.value)
github scikit-hep / pyhf / tests / test_interpolate.py View on Github external
def test_invalid_interpcode():
    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get('fake')

    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get(1.2)

    with pytest.raises(pyhf.exceptions.InvalidInterpCode):
        pyhf.interpolators.get(-1)
github scikit-hep / pyhf / tests / test_init.py View on Github external
            pytest.raises(pyhf.exceptions.ImportBackendError),
        ],
    ],
    ids=[
        "numpy",
        "pytorch",
        "tensorflow",
        "mxnet"
    ],
)
def test_missing_backends(isolate_modules, param):
    backend_name, module_name, import_name, expectation = param

    # hide
    CACHE_BACKEND, sys.modules[backend_name] = sys.modules[backend_name], None
    sys.modules.setdefault('pyhf.tensor.{}'.format(import_name), None)
    CACHE_MODULE, sys.modules['pyhf.tensor.{}'.format(module_name)] = (
github scikit-hep / pyhf / tests / test_workspace.py View on Github external
def test_prune_measurements(workspace_factory):
    ws = workspace_factory()
    measurement = ws.measurement_names[0]

    if len(ws.measurement_names) == 1:
        with pytest.raises(pyhf.exceptions.InvalidSpecification):
            new_ws = ws.prune(measurements=measurement)
        with pytest.raises(pyhf.exceptions.InvalidSpecification):
            new_ws = ws.prune(measurements=[measurement])
    else:
        new_ws = ws.prune(measurements=[measurement])
        assert new_ws
        assert measurement not in new_ws.measurement_names

        new_ws_list = ws.prune(measurements=[measurement])
        assert new_ws_list == new_ws
github scikit-hep / pyhf / tests / test_public_api.py View on Github external
def test_supported_backends(backend_name):
    with pytest.raises(pyhf.exceptions.InvalidBackend):
        pyhf.set_backend(backend_name)
github scikit-hep / pyhf / tests / test_schema.py View on Github external
def test_no_channels():
    spec = {'channels': []}
    with pytest.raises(pyhf.exceptions.InvalidSpecification):
        pyhf.Model(spec)
github scikit-hep / pyhf / pyhf / interpolate.py View on Github external
def interpolator(interpcode, do_tensorized_calc=True):
    interpcodes = {
        0: _hfinterp_code0 if do_tensorized_calc else _slow_hfinterp_code0,
        1: _hfinterp_code1 if do_tensorized_calc else _slow_hfinterp_code1,
    }

    try:
        return interpcodes[interpcode]
    except KeyError:
        raise exceptions.InvalidInterpCode