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

    mod = hsc.apply(pyhf.tensorlib.astensor([0.5, -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], [0.5, 1.0, 1.5])

    hsc = histosys_combined(
        [('hello', 'histosys'), ('world', 'histosys')], 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], [-1.0, -2.0, -3.0])
    assert np.allclose(mod[0, 0, 1], [1.0, 2.0, 3.0])
    assert np.allclose(mod[0, 0, 2], [-1.0, -2.0, -3.0])
    assert np.allclose(mod[0, 0, 3], [1.0, 2.0, 3.0])
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'data': {
                    'mask': [False, True, True],
                    'nom_data': [10, 10, 10],
                    'uncrt': [0, 1, 1],
                },
            },
        },
    }
    hsc = staterror_combined(
        [('staterror_chan1', 'staterror'), ('staterror_chan2', 'staterror')],
        mc,
        mega_mods,
    )

    mod = hsc.apply(pyhf.tensorlib.astensor([1.1, 1.2, 1.3]))
    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])
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'type': 'normsys',
                'name': 'world',
                '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)
github scikit-hep / pyhf / tests / test_paramviewer.py View on Github external
parshape = pyhf.tensorlib.shape(pars)

    view = ParamViewer(
        parshape,
        {'hello': {'slice': slice(0, 2)}, 'world': {'slice': slice(3, 4)}},
        ['hello', 'world'],
    )
    par_slice = view.get(pars)

    assert isinstance(view.index_selection, list)
    assert all(
        [len(x) == 3 for x in view.index_selection]
    )  # first dimension is batch dim

    assert pyhf.tensorlib.shape(par_slice) == (3, 3)
    assert pyhf.tensorlib.tolist(par_slice[view.slices[0]]) == [[1, 5, 9], [2, 6, 10]]
    assert pyhf.tensorlib.tolist(par_slice[view.slices[1]]) == [[4, 8, 12]]

    assert pyhf.tensorlib.tolist(par_slice) == [[1, 5, 9], [2, 6, 10], [4, 8, 12]]
github scikit-hep / pyhf / tests / test_paramviewer.py View on Github external
def test_paramviewer_simple_nonbatched(backend):
    pars = pyhf.tensorlib.astensor([1, 2, 3, 4, 5, 6, 7])

    parshape = pyhf.tensorlib.shape(pars)

    view = ParamViewer(
        parshape,
        {'hello': {'slice': slice(0, 2)}, 'world': {'slice': slice(5, 7)}},
        ['hello', 'world'],
    )
    par_slice = view.get(pars)
    assert pyhf.tensorlib.tolist(par_slice[view.slices[0]]) == [1, 2]

    assert pyhf.tensorlib.tolist(par_slice[view.slices[1]]) == [6, 7]

    assert pyhf.tensorlib.tolist(par_slice) == [1, 2, 6, 7]
github scikit-hep / pyhf / tests / test_paramviewer.py View on Github external
def test_paramviewer_simple_batched(backend):
    pars = pyhf.tensorlib.astensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

    parshape = pyhf.tensorlib.shape(pars)

    view = ParamViewer(
        parshape,
        {'hello': {'slice': slice(0, 2)}, 'world': {'slice': slice(3, 4)}},
        ['hello', 'world'],
    )
    par_slice = view.get(pars)

    assert isinstance(view.index_selection, list)
    assert all(
        [len(x) == 3 for x in view.index_selection]
    )  # first dimension is batch dim

    assert pyhf.tensorlib.shape(par_slice) == (3, 3)
    assert pyhf.tensorlib.tolist(par_slice[view.slices[0]]) == [[1, 5, 9], [2, 6, 10]]
    assert pyhf.tensorlib.tolist(par_slice[view.slices[1]]) == [[4, 8, 12]]
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'name': 'mu2',
                'data': {'mask': [False, True, True]},
            },
            'background': {
                'type': 'normfactor',
                'name': 'mu2',
                'data': {'mask': [False, True, True]},
            },
        },
    }
    hsc = normfactor_combined(
        [('mu1', 'normfactor'), ('mu2', 'normfactor')], mc, mega_mods
    )

    mod = hsc.apply(pyhf.tensorlib.astensor([2.0, 3.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], [2.0, 1.0, 1.0])
    assert np.allclose(mod[1, 0, 0], [1.0, 3.0, 3.0])

    hsc = normfactor_combined(
        [('mu1', 'normfactor'), ('mu2', 'normfactor')], mc, mega_mods, batch_size=4
    )

    mod = hsc.apply(
        pyhf.tensorlib.astensor([[1.0, 5.0], [2.0, 6.0], [3.0, 7.0], [4.0, 8.0]])
    )
    shape = pyhf.tensorlib.shape(mod)
    assert shape == (2, 2, 4, 3)
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
mod = np.asarray(pyhf.tensorlib.tolist(mod))
    assert np.allclose(mod[0, 0, 0], [2.0, 1.0, 1.0])
    assert np.allclose(mod[1, 0, 0], [1.0, 3.0, 4.0])

    hsc = shapefactor_combined(
        [('shapefac1', 'shapefactor'), ('shapefac2', 'shapefactor')],
        mc,
        mega_mods,
        batch_size=4,
    )
    mod = hsc.apply(
        pyhf.tensorlib.astensor(
            [[2.0, 3.0, 4.0], [5.0, 6.0, 7.0], [8.0, 9.0, 10.0], [11.0, 12.0, 13.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], [2.0, 1.0, 1.0])
    assert np.allclose(mod[0, 0, 1], [5.0, 1.0, 1.0])
    assert np.allclose(mod[0, 0, 2], [8.0, 1.0, 1.0])
    assert np.allclose(mod[0, 0, 3], [11.0, 1.0, 1.0])

    assert np.allclose(mod[1, 0, 0], [1.0, 3.0, 4.0])
    assert np.allclose(mod[1, 0, 1], [1.0, 6.0, 7.0])
    assert np.allclose(mod[1, 0, 2], [1.0, 9.0, 10.0])
    assert np.allclose(mod[1, 0, 3], [1.0, 12.0, 13.0])
github scikit-hep / pyhf / tests / test_combined_modifiers.py View on Github external
'type': 'shapesys',
                'name': 'shapesys1',
                'data': {
                    '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])