def test_limit(mock_draw):
    folder_path = "tmp"
    figure_path = pathlib.Path(folder_path) / "limit.pdf"

    observed_CLs = np.asarray([0.75, 0.32, 0.02])
    expected_CLs = np.asarray([[0.1, 0.2, 0.3, 0.4, 0.5] for _ in range(3)])
    poi_values = np.asarray([0, 1, 2])
    limit_results = fit.LimitResults(3.0, np.empty(5), observed_CLs,
                                     expected_CLs, poi_values)

    visualize.limit(limit_results, figure_folder=folder_path)

    assert mock_draw.call_count == 1
    assert np.allclose(mock_draw.call_args[0][0], limit_results.observed_CLs)
    assert np.allclose(mock_draw.call_args[0][1], limit_results.expected_CLs)
    assert np.allclose(mock_draw.call_args[0][2], limit_results.poi_values)
    assert mock_draw.call_args[0][3] == figure_path
    assert mock_draw.call_args[1] == {"close_figure": False}

    # close figure
    visualize.limit(limit_results,
                    figure_folder=folder_path,
                    close_figure=True)
    assert mock_draw.call_args[1] == {"close_figure": True}

    # unknown plotting method
    with pytest.raises(NotImplementedError, match="unknown backend: unknown"):
        visualize.limit(limit_results,
                        figure_folder=folder_path,
                        method="unknown")
Exemple #2
0
def test_limit(mock_limit, mock_vis, tmp_path):
    workspace = {"workspace": "mock"}
    workspace_path = str(tmp_path / "workspace.json")

    # need to save workspace to file since click looks for it
    with open(workspace_path, "w") as f:
        f.write('{"workspace": "mock"}')

    limit_results = fit.LimitResults(
        3.0,
        np.asarray([1.0, 2.0, 3.0, 4.0, 5.0]),
        np.asarray([0.05]),
        np.asarray([0.01, 0.02, 0.05, 0.07, 0.10]),
        np.asarray([3.0]),
    )

    runner = CliRunner()

    # default
    result = runner.invoke(cli.limit, [workspace_path])
    assert result.exit_code == 0
    assert mock_limit.call_args_list == [((workspace, ), {
        "asimov": False,
        "tolerance": 0.01
    })]
    assert mock_vis.call_count == 1
    assert np.allclose(mock_vis.call_args[0][0].observed_limit,
                       limit_results.observed_limit)
    assert np.allclose(mock_vis.call_args[0][0].expected_limit,
                       limit_results.expected_limit)
    assert np.allclose(mock_vis.call_args[0][0].observed_CLs,
                       limit_results.observed_CLs)
    assert np.allclose(mock_vis.call_args[0][0].expected_CLs,
                       limit_results.expected_CLs)
    assert np.allclose(mock_vis.call_args[0][0].poi_values,
                       limit_results.poi_values)
    assert mock_vis.call_args[1] == {"figure_folder": "figures"}

    # Asimov, tolerance, custom folder
    result = runner.invoke(
        cli.limit,
        [
            "--asimov", "--tolerance", "0.1", "--figfolder", "folder",
            workspace_path
        ],
    )
    assert result.exit_code == 0
    assert mock_limit.call_args_list[-1] == (
        (workspace, ),
        {
            "asimov": True,
            "tolerance": 0.1
        },
    )
    assert mock_vis.call_args_list[-1][1] == {"figure_folder": "folder"}
Exemple #3
0
def test_LimitResults():
    observed_limit = 3.0
    expected_limit = np.asarray([1.0, 2.0, 3.0, 4.0, 5.0])
    observed_CLs = np.asarray([0.05])
    expected_CLs = np.asarray([0.01, 0.02, 0.05, 0.07, 0.10])
    poi_values = np.asarray([3.0])
    limit_results = fit.LimitResults(observed_limit, expected_limit,
                                     observed_CLs, expected_CLs, poi_values)
    assert limit_results.observed_limit == observed_limit
    assert np.allclose(limit_results.expected_limit, expected_limit)
    assert np.allclose(limit_results.observed_CLs, observed_CLs)
    assert np.allclose(limit_results.expected_CLs, expected_CLs)
    assert np.allclose(limit_results.poi_values, poi_values)
Exemple #4
0
        ("model", "data", par_name),
        {
            "par_range": (0.0, 2.0),
            "n_steps": 21
        },
    )
    assert mock_vis.call_args[1] == {"figure_folder": "folder"}


@mock.patch("cabinetry.visualize.limit", autospec=True)
@mock.patch(
    "cabinetry.fit.limit",
    return_value=fit.LimitResults(
        3.0,
        np.asarray([1.0, 2.0, 3.0, 4.0, 5.0]),
        np.asarray([0.05]),
        np.asarray([0.01, 0.02, 0.05, 0.07, 0.10]),
        np.asarray([3.0]),
    ),
    autospec=True,
)
@mock.patch(
    "cabinetry.model_utils.model_and_data",
    return_value=("model", "data"),
    autospec=True,
)
def test_limit(mock_util, mock_limit, mock_vis, tmp_path):
    workspace = {"workspace": "mock"}
    workspace_path = str(tmp_path / "workspace.json")

    # need to save workspace to file since click looks for it