Ejemplo n.º 1
0
def test_ranking(mock_draw):
    bestfit = np.asarray([1.2, 0.1])
    uncertainty = np.asarray([0.2, 0.8])
    labels = ["staterror_a", "modeling"]
    impact_prefit_up = np.asarray([0.1, 0.5])
    impact_prefit_down = np.asarray([-0.2, -0.4])
    impact_postfit_up = np.asarray([0.1, 0.4])
    impact_postfit_down = np.asarray([-0.2, -0.3])
    ranking_results = fit.RankingResults(
        bestfit,
        uncertainty,
        labels,
        impact_prefit_up,
        impact_prefit_down,
        impact_postfit_up,
        impact_postfit_down,
    )
    folder_path = "tmp"

    figure_path = pathlib.Path(folder_path) / "ranking.pdf"
    bestfit_expected = np.asarray([0.1, 1.2])
    uncertainty_expected = np.asarray([0.8, 0.2])
    labels_expected = ["modeling", "staterror_a"]

    visualize.ranking(ranking_results, figure_folder=folder_path)
    assert mock_draw.call_count == 1
    assert np.allclose(mock_draw.call_args[0][0], bestfit_expected)
    assert np.allclose(mock_draw.call_args[0][1], uncertainty_expected)
    for i_lab, label in enumerate(mock_draw.call_args[0][2]):
        assert label == labels_expected[i_lab]
    assert np.allclose(mock_draw.call_args[0][3], impact_prefit_up[::-1])
    assert np.allclose(mock_draw.call_args[0][4], impact_prefit_down[::-1])
    assert np.allclose(mock_draw.call_args[0][5], impact_postfit_up[::-1])
    assert np.allclose(mock_draw.call_args[0][6], impact_postfit_down[::-1])
    assert mock_draw.call_args[0][7] == figure_path
    assert mock_draw.call_args[1] == {"close_figure": False}

    # maximum parameter amount specified, close figure
    visualize.ranking(ranking_results,
                      figure_folder=folder_path,
                      max_pars=1,
                      close_figure=True)
    assert mock_draw.call_count == 2
    assert np.allclose(mock_draw.call_args[0][0], bestfit_expected[0])
    assert np.allclose(mock_draw.call_args[0][1], uncertainty_expected[0])
    assert mock_draw.call_args[0][2] == labels_expected[0]
    assert np.allclose(mock_draw.call_args[0][3], impact_prefit_up[1])
    assert np.allclose(mock_draw.call_args[0][4], impact_prefit_down[1])
    assert np.allclose(mock_draw.call_args[0][5], impact_postfit_up[1])
    assert np.allclose(mock_draw.call_args[0][6], impact_postfit_down[1])
    assert mock_draw.call_args[0][7] == figure_path
    assert mock_draw.call_args[1] == {"close_figure": True}

    # unknown plotting method
    with pytest.raises(NotImplementedError, match="unknown backend: unknown"):
        visualize.ranking(ranking_results,
                          figure_folder=folder_path,
                          method="unknown")
Ejemplo n.º 2
0
def test_RankingResults():
    bestfit = np.asarray([1.0])
    uncertainty = np.asarray([0.1])
    labels = ["par_a"]
    prefit_up = np.asarray([0.3])
    prefit_down = np.asarray([-0.3])
    postfit_up = np.asarray([0.2])
    postfit_down = np.asarray([-0.2])
    ranking_results = fit.RankingResults(bestfit, uncertainty, labels,
                                         prefit_up, prefit_down, postfit_up,
                                         postfit_down)
    assert np.allclose(ranking_results.bestfit, bestfit)
    assert np.allclose(ranking_results.uncertainty, uncertainty)
    assert ranking_results.labels == labels
    assert np.allclose(ranking_results.prefit_up, prefit_up)
    assert np.allclose(ranking_results.prefit_down, prefit_down)
    assert np.allclose(ranking_results.postfit_up, postfit_up)
    assert np.allclose(ranking_results.postfit_down, postfit_down)
Ejemplo n.º 3
0
    assert mock_pulls.call_args_list[-1] == (
        (fit_results, ),
        {
            "figure_folder": "folder"
        },
    )


@mock.patch("cabinetry.visualize.ranking", autospec=True)
@mock.patch(
    "cabinetry.fit.ranking",
    return_value=fit.RankingResults(
        np.asarray([1.0]),
        np.asarray([0.1]),
        ["label"],
        np.asarray([[1.2]]),
        np.asarray([[0.8]]),
        np.asarray([[1.1]]),
        np.asarray([[0.9]]),
    ),
    autospec=True,
)
@mock.patch(
    "cabinetry.fit.fit",
    return_value=fit.FitResults(np.asarray([1.0]), np.asarray([0.1]),
                                ["label"], np.asarray([[1.0]]), 1.0),
    autospec=True,
)
@mock.patch(
    "cabinetry.model_utils.model_and_data",
    return_value=("model", "data"),