def test_shapesys_zero(backend):
    mc = MockConfig(
        par_map={
            'SigXsecOverSM': {
                'paramset':
                paramset(
                    name='SigXsecOverSM',
                    is_scalar=True,
                    n_parameters=1,
                    inits=[0],
                    bounds=[[0, 10]],
                    fixed=False,
                ),
                'slice':
                slice(0, 1),
            },
            'syst': {
                'paramset':
                constrained_by_poisson(
                    name='syst',
                    is_scalar=False,
                    n_parameters=5,
                    inits=[0] * 5,
                    bounds=[[0, 10]] * 5,
                    fixed=False,
                    auxdata=[None] * 5,
                    factors=[None] * 5,
                ),
                'slice':
                slice(1, 6),
            },
        },
        channels=['channel1'],
        channel_nbins={'channel1': 6},
        par_order=['SigXsecOverSM', 'syst'],
        samples=['signal', 'background'],
    )

    mega_mods = {
        'shapesys/syst': {
            'background': {
                'type': 'shapesys',
                'name': 'syst',
                'data': {
                    'mask': [True, True, False, True, True, True],
                    'nom_data': [100.0, 90.0, 0.0, 70, 0.1, 50],
                    'uncrt': [10, 9, 1, 0.0, 0.1, 5],
                },
            },
            'signal': {
                'type': 'shapesys',
                'name': 'syst',
                'data': {
                    'mask': [False, False, False, False, False, False],
                    'nom_data': [20.0, 10.0, 5.0, 3.0, 2.0, 1.0],
                    'uncrt': [10, 9, 1, 0.0, 0.1, 5],
                },
            },
        }
    }
    hsc = shapesys_combined([('syst', 'shapesys')], mc, mega_mods)

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

    # expect the 'background' sample to have a single masked bin for 'syst'
    assert mod[0, 1, 0, 2] == 1.0
示例#2
0
def test_shapesys(backend):
    mc = MockConfig(
        par_map={
            'dummy1': {
                'paramset': paramset(n_parameters=1, inits=[0], bounds=[[0, 10]]),
                'slice': slice(0, 1),
            },
            'shapesys1': {
                'paramset': paramset(n_parameters=1, inits=[0], bounds=[[0, 10]]),
                'slice': slice(1, 2),
            },
            'shapesys2': {
                'paramset': paramset(
                    n_parameters=2, inits=[0, 0], bounds=[[0, 10], [0, 10]]
                ),
                'slice': slice(2, 4),
            },
            'dummy2': {
                'paramset': paramset(n_parameters=1, inits=[0], bounds=[[0, 10]]),
                'slice': slice(4, 5),
            },
        },
        channels=['chan1', 'chan2'],
        channel_nbins={'chan1': 1, 'chan2': 2},
        par_order=['dummy1', 'shapesys1', 'shapesys2', 'dummy2'],
        samples=['signal', 'background'],
    )

    mega_mods = {
        'signal': {
            'shapesys/shapesys1': {
                'type': 'shapesys',
                'name': 'shapesys1',
                'data': {
                    'mask': [True, False, False],
                    'nom_data': [10, 10, 10],
                    'uncrt': [1, 0, 0],
                },
            },
            'shapesys/shapesys2': {
                'type': 'shapesys',
                'name': 'shapesys1',
                'data': {
                    'mask': [False, True, True],
                    'nom_data': [10, 10, 10],
                    'uncrt': [0, 1, 1],
                },
            },
        },
        'background': {
            'shapesys/shapesys1': {
                'type': 'shapesys',
                'name': 'shapesys1',
                'data': {
                    'mask': [True, False, False],
                    'nom_data': [10, 10, 10],
                    'uncrt': [1, 0, 0],
                },
            },
            'shapesys/shapesys2': {
                '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])
def test_shapesys_zero(backend):
    mc = MockConfig(
        par_map={
            'SigXsecOverSM': {
                'paramset': paramset(n_parameters=1, inits=[0], bounds=[[0, 10]]),
                'slice': slice(0, 1),
            },
            'syst': {
                'paramset': paramset(
                    n_parameters=5, inits=[0] * 5, bounds=[[0, 10]] * 5
                ),
                'slice': slice(1, 6),
            },
            'syst_lowstats': {
                'paramset': paramset(
                    n_parameters=0, inits=[0] * 0, bounds=[[0, 10]] * 0
                ),
                'slice': slice(6, 6),
            },
        },
        channels=['channel1'],
        channel_nbins={'channel1': 6},
        par_order=['SigXsecOverSM', 'syst', 'syst_lowstats'],
        samples=['signal', 'background'],
    )

    mega_mods = {
        'shapesys/syst': {
            'background': {
                'type': 'shapesys',
                'name': 'syst',
                'data': {
                    'mask': [True, True, False, True, True, True],
                    'nom_data': [100.0, 90.0, 0.0, 70, 0.1, 50],
                    'uncrt': [10, 9, 1, 0.0, 0.1, 5],
                },
            },
            'signal': {
                'type': 'shapesys',
                'name': 'syst',
                'data': {
                    'mask': [False, False, False, False, False, False],
                    'nom_data': [20.0, 10.0, 5.0, 3.0, 2.0, 1.0],
                    'uncrt': [10, 9, 1, 0.0, 0.1, 5],
                },
            },
        },
        'shapesys/syst_lowstats': {
            'background': {
                'type': 'shapesys',
                'name': 'syst_lowstats',
                'data': {
                    'mask': [False, False, False, False, False, False],
                    'nom_data': [100.0, 90.0, 0.0, 70, 0.1, 50],
                    'uncrt': [0, 0, 0, 0, 0, 0],
                },
            },
            'signal': {
                'type': 'shapesys',
                'name': 'syst',
                'data': {
                    'mask': [False, False, False, False, False, False],
                    'nom_data': [20.0, 10.0, 5.0, 3.0, 2.0, 1.0],
                    'uncrt': [10, 9, 1, 0.0, 0.1, 5],
                },
            },
        },
    }
    hsc = shapesys_combined(
        [('syst', 'shapesys'), ('syst_lowstats', 'shapesys')], mc, mega_mods
    )

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

    # expect the 'background' sample to have a single masked bin for 'syst'
    assert mod[0, 1, 0, 2] == 1.0
    # expect the 'background' sample to have all bins masked for 'syst_lowstats'
    assert np.all(kappa == 1 for kappa in mod[1, 1, 0])