Exemplo n.º 1
0
def test_model_and_data(example_spec):
    model, data = model_tools.model_and_data(example_spec)
    assert model.spec["channels"] == example_spec["channels"]
    assert model.config.modifier_settings == {
        "normsys": {
            "interpcode": "code4"
        },
        "histosys": {
            "interpcode": "code4p"
        },
    }
    assert data == [691, 1.0]

    # requesting Asimov dataset
    # TODO: request asimov dataset by setting asimove=True
    # TODO: should return [112.429786, 1.0]

    # TODO: Need to test overloaded method as well

    # test handing a workspace instead of JSON
    model, data = model_tools.model_and_data(pyhf.Workspace(example_spec))
    assert model.spec["channels"] == example_spec["channels"]
    assert model.config.modifier_settings == {
        "normsys": {
            "interpcode": "code4"
        },
        "histosys": {
            "interpcode": "code4p"
        },
    }
    assert data == [691, 1.0]

    # without auxdata
    model, data = model_tools.model_and_data(example_spec, with_aux=False)
    assert data == [691]
Exemplo n.º 2
0
def test_model_and_data(example_spec):
    model, data = model_tools.model_and_data(example_spec)
    assert model.spec["channels"] == example_spec["channels"]
    assert model.config.modifier_settings == {
        "normsys": {"interpcode": "code4"},
        "histosys": {"interpcode": "code4p"},
    }
    assert data == [691, 1.0]

    # requesting Asimov dataset
    model, data = model_tools.model_and_data(pyhf.Workspace(example_spec), asimov=True)
    assert model.spec["channels"] == example_spec["channels"]
    assert model.config.modifier_settings == {
        "normsys": {"interpcode": "code4"},
        "histosys": {"interpcode": "code4p"},
    }
    assert data == [112.429786, 1.0]

    # test handing a workspace instead of JSON
    model, data = model_tools.model_and_data(pyhf.Workspace(example_spec))
    assert model.spec["channels"] == example_spec["channels"]
    assert model.config.modifier_settings == {
        "normsys": {"interpcode": "code4"},
        "histosys": {"interpcode": "code4p"},
    }
    assert data == [691, 1.0]

    # without auxdata
    model, data = model_tools.model_and_data(example_spec, include_auxdata=False)
    assert data == [691]
Exemplo n.º 3
0
def test_get_yields(example_spec):
    model, data = model_tools.model_and_data(example_spec)
    fit_results = fitter.fit(model, data)

    y = yld.get_yields(example_spec, fit_results)
    for region in y.regions:
        assert np.allclose(y.yields[region], np.asarray([690.99844915]))
        assert np.allclose(ak.to_list(y.uncertainties[region]),
                           [26.278787667809468])
        assert np.allclose(y.data[region], np.asarray([691]))
    assert y.regions == ['SR']
    assert y.samples == ['signal']
Exemplo n.º 4
0
def test_fit(mock_print, example_spec, example_spec_multibin):
    model, data = model_tools.model_and_data(example_spec)
    fit_results = fitter.fit(model, data)
    mock_print.assert_called_once()
    assert np.allclose(fit_results.bestfit, [1.1, 5.58731303])
    assert np.allclose(fit_results.uncertainty, [0.0, 0.21248646])
    assert fit_results.labels == ["staterror_SR", "mu_Sig"]
    assert np.allclose(fit_results.best_twice_nll, 6.850287450660111)
    assert np.allclose(fit_results.corr_mat, [[0.0, 0.0], [0.0, 1.0]])

    # # TODO: Asimov fit, with fixed gamma (fixed not to Asimov MLE)
    # model, data = model_tools.model_and_data(example_spec, asimov=True)
    # fit_results = fitter.fit(model, data)
    # # the gamma factor is multiplicative and fixed to 1.1, so the
    # # signal strength needs to be 1/1.1 to compensate
    # assert np.allclose(fit_results.bestfit, [1.1, 0.90917877])
    # assert np.allclose(fit_results.uncertainty, [0.0, 0.12623179])
    # assert fit_results.labels == ["staterror_SR", "mu_Sig"]
    # assert np.allclose(fit_results.best_twice_nll, 5.68851093)
    # assert np.allclose(fit_results.corr_mat, [[0.0, 0.0], [0.0, 1.0]])

    # parameters held constant via keyword argument
    model, data = model_tools.model_and_data(example_spec_multibin)
    init_pars = model.config.suggested_init()
    init_pars[0] = 0.9
    init_pars[1] = 1.1
    fixed_pars = model.config.suggested_fixed()
    fixed_pars[0] = True
    fixed_pars[1] = True
    fit_results = fitter.fit(model,
                             data,
                             init_pars=init_pars,
                             fixed_pars=fixed_pars)
    assert np.allclose(fit_results.bestfit, [0.9, 1.1, 1.11996446, 0.96618774])
    assert np.allclose(fit_results.uncertainty,
                       [0.0, 0.0, 0.1476617, 0.17227148])
    assert np.allclose(fit_results.best_twice_nll, 11.2732492)