How to use the pyhf.tensorlib.tolist 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_constraints.py View on Github external
)

                constraint_term = tensorlib.poisson_logpdf(thisauxdata, paralphas)
            summands = (
                constraint_term
                if summands is None
                else tensorlib.concatenate([summands, constraint_term])
            )
        return tensorlib.sum(summands) if summands is not None else 0

    def fast(self, auxdata, pars):
        return self.constraint_logpdf(auxdata, pars)

    auxd = pyhf.tensorlib.astensor(m.config.auxdata)
    pars = pyhf.tensorlib.astensor(m.config.suggested_init())
    slow_result = pyhf.tensorlib.tolist(slow(m, auxd, pars))
    fast_result = pyhf.tensorlib.tolist(fast(m, auxd, pars))
    assert pytest.approx(slow_result) == fast_result
github scikit-hep / pyhf / tests / test_optim.py View on Github external
def test_optim_with_value(backend, source, spec, mu):
    pdf = pyhf.Model(spec)
    data = source['bindata']['data'] + pdf.config.auxdata

    init_pars = pdf.config.suggested_init()
    par_bounds = pdf.config.suggested_bounds()

    optim = pyhf.optimizer

    result = optim.minimize(pyhf.infer.mle.twice_nll, data, pdf, init_pars, par_bounds)
    assert pyhf.tensorlib.tolist(result)

    result, fitted_val = optim.minimize(
        pyhf.infer.mle.twice_nll,
        data,
        pdf,
        init_pars,
        par_bounds,
        [(pdf.config.poi_index, mu)],
        return_fitted_val=True,
    )
    assert pyhf.tensorlib.tolist(result)
github scikit-hep / pyhf / tests / test_backend_consistency.py View on Github external
test_statistic = []
    for backend in backends:
        if backend.name == 'tensorflow':
            tf.reset_default_graph()
            backend.session = tf.compat.v1.Session()
        pyhf.set_backend(backend)

        q_mu = pyhf.infer.hypotest(
            1.0,
            data,
            pdf,
            pdf.config.suggested_init(),
            pdf.config.suggested_bounds(),
            return_test_statistics=True,
        )[-1][0]
        test_statistic.append(pyhf.tensorlib.tolist(q_mu))

    # compare to NumPy/SciPy
    test_statistic = np.array(test_statistic)
    numpy_ratio = np.divide(test_statistic, test_statistic[0])
    numpy_ratio_delta_unity = np.absolute(np.subtract(numpy_ratio, 1))

    # compare tensor libraries to each other
    tensors_ratio = np.divide(test_statistic[1], test_statistic[2])
    tensors_ratio_delta_unity = np.absolute(np.subtract(tensors_ratio, 1))

    try:
        assert (numpy_ratio_delta_unity < tolerance['numpy']).all()
    except AssertionError:
        print(
            'Ratio to NumPy+SciPy exceeded tolerance of {}: {}'.format(
                tolerance['numpy'], numpy_ratio_delta_unity.tolist()
github scikit-hep / pyhf / tests / test_pdf.py View on Github external
pars = [None, None]
    pars[pdf.config.par_slice('mu')], pars[pdf.config.par_slice('bkg_norm')] = [
        [0.0],
        [0.0],
    ]
    assert np.allclose(
        pyhf.tensorlib.tolist(pdf.expected_data(pars, include_auxdata=False)),
        [100, 150],
    )

    pars[pdf.config.par_slice('mu')], pars[pdf.config.par_slice('bkg_norm')] = [
        [0.0],
        [1.0],
    ]
    assert np.allclose(
        pyhf.tensorlib.tolist(pdf.expected_data(pars, include_auxdata=False)),
        [100 * 1.1, 150 * 1.1],
    )

    pars[pdf.config.par_slice('mu')], pars[pdf.config.par_slice('bkg_norm')] = [
        [0.0],
        [-1.0],
    ]
    assert np.allclose(
        pyhf.tensorlib.tolist(pdf.expected_data(pars, include_auxdata=False)),
        [100 * 0.9, 150 * 0.9],
    )
github scikit-hep / pyhf / tests / test_constraints.py View on Github external
default_backend.poisson_logpdf(data, rate)
                        for data, rate in zip(
                            [12, 13, 14], [12 * 0.4, 13 * 0.5, 14 * 0.6]
                        )
                    ],
                ],
                axis=1,
            ),
        )
    )
    assert result.shape == (3,)

    suggested_pars = [1.0] * 3 + [0.0] * 5  # 2 pois 5 norm
    constraint = gaussian_constraint_combined(config, batch_size=1)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )
        )
    )
    assert np.isclose(
        result[0],
        sum(
            [
                default_backend.normal_logpdf(data, mu, sigma)
                for data, mu, sigma in zip(
                    [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1.5, 2.0, 1.0, 1.0, 1.0]
                )
            ]
        ),
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
},
            'background': {
                'type': 'lumi',
                'name': 'lumi',
                'data': {'mask': [True, True, True]},
            },
        },
    }

    hsc = lumi_combined([('lumi', 'lumi')], mc, mega_mods)

    mod = hsc.apply(pyhf.tensorlib.astensor([0.5]))
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (1, 2, 1, 3)

    mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [0.5, 0.5, 0.5])
    assert np.allclose(mod[0, 1, 0], [0.5, 0.5, 0.5])

    hsc = lumi_combined([('lumi', 'lumi')], mc, mega_mods, batch_size=4)

    mod = hsc.apply(pyhf.tensorlib.astensor([[1.0], [2.0], [3.0], [4.0]]))
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (1, 2, 4, 3)

    mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [1.0, 1.0, 1.0])
    assert np.allclose(mod[0, 0, 1], [2.0, 2.0, 2.0])
    assert np.allclose(mod[0, 0, 2], [3.0, 3.0, 3.0])
    assert np.allclose(mod[0, 0, 3], [4.0, 4.0, 4.0])
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
assert np.allclose(mod[0, 0, 0], [1.1, 1.1, 1.1])
    assert np.allclose(mod[0, 1, 0], [1.2, 1.2, 1.2])
    assert np.allclose(mod[1, 0, 0], [0.7, 0.7, 0.7])
    assert np.allclose(mod[1, 1, 0], [0.6, 0.6, 0.6])

    hsc = normsys_combined(
        [('hello', 'normsys'), ('world', 'normsys')], mc, mega_mods, batch_size=4
    )

    mod = hsc.apply(
        pyhf.tensorlib.astensor([[-1.0, -1.0], [1.0, 1.0], [-1.0, -1.0], [1.0, 1.0]])
    )
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (2, 2, 4, 3)

    mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [0.9, 0.9, 0.9])
    assert np.allclose(mod[0, 0, 1], [1.1, 1.1, 1.1])
    assert np.allclose(mod[0, 0, 2], [0.9, 0.9, 0.9])
    assert np.allclose(mod[0, 0, 3], [1.1, 1.1, 1.1])
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'data': {
                    'hi': [1.4] * 3,
                    'lo': [0.6] * 3,
                    'nom_data': [1, 1, 1],
                    'mask': [True, True, True],
                },
            },
        },
    }

    hsc = normsys_combined([('hello', 'normsys'), ('world', 'normsys')], mc, mega_mods)

    mod = hsc.apply(pyhf.tensorlib.astensor([1.0, -1.0]))
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (2, 2, 1, 3)
    mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [1.1, 1.1, 1.1])
    assert np.allclose(mod[0, 1, 0], [1.2, 1.2, 1.2])
    assert np.allclose(mod[1, 0, 0], [0.7, 0.7, 0.7])
    assert np.allclose(mod[1, 1, 0], [0.6, 0.6, 0.6])

    hsc = normsys_combined(
        [('hello', 'normsys'), ('world', 'normsys')], mc, mega_mods, batch_size=4
    )

    mod = hsc.apply(
        pyhf.tensorlib.astensor([[-1.0, -1.0], [1.0, 1.0], [-1.0, -1.0], [1.0, 1.0]])
    )
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (2, 2, 4, 3)

    mod = np.asarray(pyhf.tensorlib.tolist(mod))
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'mask': [False, True, True],
                    'nom_data': [10, 10, 10],
                    'uncrt': [0, 1, 1],
                },
            },
        },
    }
    hsc = shapesys_combined(
        [('shapesys1', 'shapesys'), ('shapesys2', 'shapesys')], mc, mega_mods
    )

    mod = hsc.apply(pyhf.tensorlib.astensor([-10, 1.1, 1.2, 1.3, -20]))
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (2, 2, 1, 3)

    mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [1.1, 1.0, 1.0])
    assert np.allclose(mod[1, 0, 0], [1, 1.2, 1.3])