def test_get_channels(tmp_path): example_config = { "Regions": [{"Name": "region_1"}], "Samples": [{"Name": "signal"}], "NormFactors": [], } # create a histogram for testing histo_path = tmp_path / "signal_region_1_nominal.npz" histogram = histo.to_dict( np.asarray([1.0, 2.0]), np.asarray([1.0, 1.0]), np.asarray([0.0, 1.0, 2.0]) ) histo.save(histogram, histo_path) channels = workspace.get_channels(example_config, tmp_path) expected_channels = [ { "name": "region_1", "samples": [ { "name": "signal", "data": [1.0, 2.0], "modifiers": [ { "name": "staterror_region_1", "type": "staterror", "data": [1.0, 1.0], } ], } ], } ] assert channels == expected_channels
def test_to_dict(): yields = np.asarray([1.0, 2.0]) sumw2 = np.asarray([0.1, 0.2]) bins = np.asarray([1, 2, 3]) assert histo.to_dict(yields, sumw2, bins) == { "yields": yields, "sumw2": sumw2, "bins": bins, }
def test_data_MC_matplotlib(tmp_path): fname = tmp_path / "subdir" / "fig.pdf" bg_hist = histo.to_dict( np.asarray([12.5, 14]), np.asarray([0.4, 0.5]), np.asarray([1, 2, 3]) ) sig_hist = histo.to_dict( np.asarray([2, 5]), np.asarray([0.1, 0.2]), np.asarray([1, 2, 3]) ) data_hist = histo.to_dict( np.asarray([13, 15]), np.asarray([3.61, 3.87]), np.asarray([1, 2, 3]) ) histo_dict_list = [ {"label": "Background", "isData": False, "hist": bg_hist, "variable": "x"}, {"label": "Signal", "isData": False, "hist": sig_hist, "variable": "x"}, {"label": "Data", "isData": True, "hist": data_hist, "variable": "x"}, ] histogram_drawing.data_MC_matplotlib(histo_dict_list, fname) assert ( compare_images("tests/contrib/reference/ref_data_MC.pdf", str(fname), 0) is None )
def test_get_observations(tmp_path): histo_path = tmp_path / "Data_test_region_nominal.npz" # build a test histogram and save it histogram = histo.to_dict( np.asarray([1.0, 2.0]), np.asarray([1.0, 1.0]), np.asarray([0.0, 1.0, 2.0]) ) histo.save(histogram, histo_path) # create observations list from config config = { "Samples": [{"Name": "Data", "Tree": "tree", "Path": tmp_path, "Data": True}], "Regions": [{"Name": "test_region"}], } obs = workspace.get_observations(config, tmp_path) expected_obs = [{"name": "test_region", "data": [1.0, 2.0]}] assert obs == expected_obs
def test_run(tmp_path): config = { "Samples": [{ "Name": "signal" }], "Regions": [{ "Name": "region_1" }] } # create an input histogram histo_path = tmp_path / "signal_region_1_nominal.npz" histogram = histo.to_dict(np.asarray([1.0, 2.0]), np.asarray([1.0, 1.0]), np.asarray([0.0, 1.0, 2.0])) histo.save(histogram, histo_path) template_postprocessor.run(config, tmp_path) modified_histo = histo._load(histo_path, modified=True) assert np.allclose(modified_histo["yields"], histogram["yields"]) assert np.allclose(modified_histo["sumw2"], histogram["sumw2"]) assert np.allclose(modified_histo["bins"], histogram["bins"])
def nan_sumw2_nonempty_bin(): yields = np.asarray([1.0, 2.0]) sumw2 = np.asarray([0.1, float("NaN")]) bins = np.asarray([1, 2, 3]) return histo.to_dict(yields, sumw2, bins)
def empty_bin(): yields = np.asarray([1.0, 0.0]) sumw2 = np.asarray([0.1, 0.2]) bins = np.asarray([1, 2, 3]) return histo.to_dict(yields, sumw2, bins)
def single_bin(): yields = np.asarray([1.0]) sumw2 = np.asarray([0.1]) bins = np.asarray([1, 2]) return histo.to_dict(yields, sumw2, bins)
def normal(): yields = np.asarray([1.0, 2.0]) sumw2 = np.asarray([0.1, 0.2]) bins = np.asarray([1, 2, 3]) return histo.to_dict(yields, sumw2, bins)