def test_scan(mock_draw): folder_path = "tmp" figure_path = pathlib.Path(folder_path) / "scan_a_0.pdf" par_name = "a[0]" par_mle = 1.5 par_unc = 0.2 par_vals = np.asarray([1.3, 1.5, 1.7]) par_nlls = np.asarray([0.9, 0.0, 1.1]) scan_results = fit.ScanResults(par_name, par_mle, par_unc, par_vals, par_nlls) visualize.scan(scan_results, figure_folder=folder_path) assert mock_draw.call_count == 1 assert mock_draw.call_args[0][0] == par_name assert mock_draw.call_args[0][1] == par_mle assert mock_draw.call_args[0][2] == par_unc assert np.allclose(mock_draw.call_args[0][3], par_vals) assert np.allclose(mock_draw.call_args[0][4], par_nlls) assert mock_draw.call_args[0][5] == figure_path assert mock_draw.call_args[1] == {} # unknown plotting method with pytest.raises(NotImplementedError, match="unknown backend: unknown"): visualize.scan(scan_results, figure_folder=folder_path, method="unknown")
def test_ScanResults(): name = "par_a" bestfit = 1.2 uncertainty = 0.3 parameter_values = np.asarray([0.9, 1.2, 1.5]) delta_nlls = np.asarray([1.0, 0.0, 1.0]) scan_results = fit.ScanResults(name, bestfit, uncertainty, parameter_values, delta_nlls) assert scan_results.name == name assert scan_results.bestfit == bestfit assert scan_results.uncertainty == uncertainty assert np.allclose(scan_results.parameter_values, parameter_values) assert np.allclose(scan_results.delta_nlls, delta_nlls)
def test_scan(mock_util, mock_scan, 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"}') par_name = "par" scan_results = fit.ScanResults(par_name, 1.0, 0.1, np.asarray([1.5]), np.asarray([3.5])) runner = CliRunner() # default result = runner.invoke(cli.scan, [workspace_path, par_name]) assert result.exit_code == 0 assert mock_util.call_args_list == [((workspace, ), {"asimov": False})] assert mock_scan.call_args_list == [(("model", "data", par_name), { "par_range": None, "n_steps": 11 })] assert mock_vis.call_count == 1 assert mock_vis.call_args[0][0].name == scan_results.name assert mock_vis.call_args[0][0].bestfit == scan_results.bestfit assert mock_vis.call_args[0][0].uncertainty == scan_results.uncertainty assert np.allclose(mock_vis.call_args[0][0].parameter_values, scan_results.parameter_values) assert np.allclose(mock_vis.call_args[0][0].delta_nlls, scan_results.delta_nlls) assert mock_vis.call_args[1] == {"figure_folder": "figures"} # only one bound with pytest.raises( ValueError, match= "Need to either specify both lower_bound and upper_bound, or neither.", ): runner.invoke( cli.scan, ["--lower_bound", 1.0, workspace_path, par_name], catch_exceptions=False, ) with pytest.raises( ValueError, match= "Need to either specify both lower_bound and upper_bound, or neither.", ): runner.invoke( cli.scan, ["--upper_bound", 1.0, workspace_path, par_name], catch_exceptions=False, ) # custom bounds, number of steps and Asimov result = runner.invoke( cli.scan, [ "--lower_bound", 0.0, "--upper_bound", 2.0, "--n_steps", 21, "--asimov", "--figfolder", "folder", workspace_path, par_name, ], ) assert result.exit_code == 0 assert mock_util.call_args_list[-1] == ((workspace, ), {"asimov": True}) assert mock_scan.call_args_list[-1] == ( ("model", "data", par_name), { "par_range": (0.0, 2.0), "n_steps": 21 }, ) assert mock_vis.call_args[1] == {"figure_folder": "folder"}
assert mock_rank.call_args_list[-1] == ( ("model", "data"), { "fit_results": fit_results }, ) assert mock_vis.call_args_list[-1][1] == { "figure_folder": "folder", "max_pars": 3 } @mock.patch("cabinetry.visualize.scan", autospec=True) @mock.patch( "cabinetry.fit.scan", return_value=fit.ScanResults("par", 1.0, 0.1, np.asarray([1.5]), np.asarray([3.5])), autospec=True, ) @mock.patch( "cabinetry.model_utils.model_and_data", return_value=("model", "data"), autospec=True, ) def test_scan(mock_util, mock_scan, 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"}')