Skip to content

Commit

Permalink
fix: handling spaces in histogram names for template visualization (#423
Browse files Browse the repository at this point in the history
)

* fix visualize.templates not correctly handling template histograms with spaces in names
  • Loading branch information
alexander-held authored Jul 20, 2023
1 parent 5ece244 commit 59b49fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/cabinetry/visualize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def templates(
+ "_"
+ systematic["Name"]
+ "*_modified*"
)
).replace(" ", "-")
# create a list of paths to histograms matching the pattern
variation_paths = [
pathlib.Path(h_name)
Expand Down Expand Up @@ -398,7 +398,7 @@ def templates(
)
figure_name = (
f"{region['Name']}_{sample['Name']}_{systematic['Name']}.pdf"
)
).replace(" ", "-")
figure_path = figure_folder / figure_name if save_figure else None

fig = plot_model.templates(
Expand Down
16 changes: 8 additions & 8 deletions tests/visualize/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,34 +286,34 @@ def test_templates(mock_draw, mock_histo_config, mock_histo_path, tmp_path):
# the side effects are repeated for the patched Histogram.from_path
# to check all relevant behavior (including the unknown backend check)
nominal_path = tmp_path / "region_sample_Nominal_modified.npz"
up_path = tmp_path / "region_sample_sys_Up_modified.npz"
down_path = tmp_path / "region_sample_sys_Down_modified.npz"
up_path = tmp_path / "region_sample_sys-name_Up_modified.npz"
down_path = tmp_path / "region_sample_sys-name_Down_modified.npz"
region = {"Name": "region", "Variable": "x"}
sample = {"Name": "sample"}
config = {
"General": {"HistogramFolder": tmp_path},
"Regions": [region],
"Samples": [sample, {"Name": "data", "Data": True}],
"Systematics": [{"Name": "sys"}],
"Systematics": [{"Name": "sys name"}],
}

folder_path = "tmp"
figure_path = pathlib.Path(folder_path) / "templates/region_sample_sys.pdf"
figure_path = pathlib.Path(folder_path) / "templates/region_sample_sys-name.pdf"

# add fake histograms for glob
nominal_path.touch()
up_path.touch()
down_path.touch()

# also add a file that matches pattern but is not needed
(tmp_path / "region_sample_sys_unknown_modified.npz").touch()
(tmp_path / "region_sample_sys-name_unknown_modified.npz").touch()

fig_dict_list = visualize.templates(config, figure_folder=folder_path)
assert len(fig_dict_list) == 1
assert isinstance(fig_dict_list[0]["figure"], matplotlib.figure.Figure)
assert fig_dict_list[0]["region"] == "region"
assert fig_dict_list[0]["sample"] == "sample"
assert fig_dict_list[0]["systematic"] == "sys"
assert fig_dict_list[0]["systematic"] == "sys name"

# nominal histogram loading
assert mock_histo_config.call_args_list == [((tmp_path, region, sample, {}), {})]
Expand All @@ -338,7 +338,7 @@ def test_templates(mock_draw, mock_histo_config, mock_histo_path, tmp_path):
(nominal, up_orig, down_orig, up_mod, down_mod, bins, "x"),
{
"figure_path": figure_path,
"label": "region: region\nsample: sample\nsystematic: sys",
"label": "region: region\nsample: sample\nsystematic: sys name",
"close_figure": False,
},
)
Expand All @@ -355,7 +355,7 @@ def test_templates(mock_draw, mock_histo_config, mock_histo_path, tmp_path):
(nominal, up_orig, down_orig, up_mod, down_mod, bins, "observable"),
{
"figure_path": None,
"label": "region: region\nsample: sample\nsystematic: sys",
"label": "region: region\nsample: sample\nsystematic: sys name",
"close_figure": True,
},
)
Expand Down

0 comments on commit 59b49fb

Please sign in to comment.