How to use the pyhf.get_backend 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_toys.py View on Github external
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)
github scikit-hep / pyhf / tests / test_toys.py View on Github external
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)
github scikit-hep / pyhf / tests / test_constraints.py View on Github external
def slow(self, auxdata, pars):
        tensorlib, _ = pyhf.get_backend()
        # iterate over all constraints order doesn't matter....
        start_index = 0
        summands = None
        for cname in self.config.auxdata_order:
            parset, parslice = (
                self.config.param_set(cname),
                self.config.par_slice(cname),
            )
            end_index = start_index + parset.n_parameters
            thisauxdata = auxdata[start_index:end_index]
            start_index = end_index
            if parset.pdf_type == 'normal':
                paralphas = pars[parslice]
                sigmas = (
                    parset.sigmas
                    if hasattr(parset, 'sigmas')
github scikit-hep / pyhf / tests / test_top_level.py View on Github external
def test_set_backend(backend):
    tb, _ = pyhf.get_backend()
    pyhf.set_backend(tb.name)
github scikit-hep / pyhf / pyhf / interpolate.py View on Github external
def _precompute_alphasets(self, alphasets_shape):
        if alphasets_shape == self.alphasets_shape:
            return
        tensorlib, _ = get_backend()
        self.mask_on = tensorlib.ones(alphasets_shape)
        self.mask_off = tensorlib.zeros(alphasets_shape)
        self.alphasets_shape = alphasets_shape
github scikit-hep / pyhf / pyhf / modifiers / normsys.py View on Github external
def apply(self, pars):
        tensorlib, _ = get_backend()
        if not tensorlib.shape(self.normsys_indices)[0]:
            return
        flat_pars = tensorlib.reshape(pars,-1)
        normsys_alphaset = tensorlib.gather(flat_pars, self.normsys_indices)
        results_norm = self.interpolator(normsys_alphaset)

        # either rely on numerical no-op or force with line below
        results_norm = tensorlib.where(
            self.normsys_mask, results_norm, self.normsys_default
        )
        return results_norm
github scikit-hep / pyhf / pyhf / interpolate.py View on Github external
def _precompute(self):
        tensorlib, _ = get_backend()
        self.deltas_up = tensorlib.astensor(
            default_backend.divide(
                self._histogramssets[:, :, 2], self._histogramssets[:, :, 1]
            )
        )
        self.deltas_dn = tensorlib.astensor(
            default_backend.divide(
                self._histogramssets[:, :, 0], self._histogramssets[:, :, 1]
            )
        )
        self.broadcast_helper = tensorlib.ones(self.deltas_up.shape)
        self.bases_up = tensorlib.einsum(
            'sa,shb->shab', tensorlib.ones(self.alphasets_shape), self.deltas_up
        )
        self.bases_dn = tensorlib.einsum(
            'sa,shb->shab', tensorlib.ones(self.alphasets_shape), self.deltas_dn
github scikit-hep / pyhf / pyhf / interpolate.py View on Github external
def _hfinterp_code1(histogramssets, alphasets):
    tensorlib, _ = get_backend()
    interpolator = _hfinterpolator_code1(tensorlib.tolist(histogramssets))
    return interpolator(alphasets)