-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path__init__.py
652 lines (572 loc) · 24.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""High-level entry point for visualizing fit models and inference results."""
import fnmatch
import glob
import logging
import pathlib
from typing import Any, Dict, List, Optional, Tuple, Union
import matplotlib as mpl
import numpy as np
from cabinetry import configuration
from cabinetry import fit
from cabinetry import histo
from cabinetry import model_utils
from cabinetry.templates import builder
from cabinetry.visualize import plot_model
from cabinetry.visualize import plot_result
log = logging.getLogger(__name__)
def _figure_name(region_name: str, label: str) -> str:
"""Constructs a file name for a figure.
Args:
region_name (str): name of the region shown in the figure
label (str): additional label, e.g. from model prediction
Returns:
str: name of the file the figure should be saved to
"""
if label in ["pre-fit", "post-fit"]:
label = label.replace("-", "") # replace pre-fit by prefit, post-fit by postfit
figure_name = f"{region_name.replace(' ', '-')}_{label}.pdf"
return figure_name
def _total_yield_uncertainty(stdev_list: List[np.ndarray]) -> np.ndarray:
"""Calculates the absolute statistical uncertainty of a stack of MC.
Args:
stdev_list (List[np.ndarray]): list of absolute stat. uncertainty per sample
Returns:
np.array: absolute stat. uncertainty of stack of samples
"""
tot_unc = np.sqrt(np.sum(np.power(np.asarray(stdev_list), 2), axis=0))
return tot_unc
def data_mc_from_histograms(
config: Dict[str, Any],
*,
figure_folder: Union[str, pathlib.Path] = "figures",
log_scale: Optional[bool] = None,
log_scale_x: bool = False,
close_figure: bool = False,
save_figure: bool = True,
) -> List[Dict[str, Any]]:
"""Draws pre-fit data/MC histograms, using histograms created by cabinetry.
The uncertainty band drawn includes only statistical uncertainties.
Args:
config (Dict[str, Any]): cabinetry configuration
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
log_scale (Optional[bool], optional): whether to use logarithmic vertical axis,
defaults to None (automatically determine whether to use linear/log scale)
log_scale_x (bool, optional): whether to use logarithmic horizontal axis,
defaults to False
close_figure (bool, optional): whether to close each figure, defaults to False
(enable when producing many figures to avoid memory issues, prevents
automatic rendering in notebooks)
save_figure (bool, optional): whether to save figures, defaults to True
Returns:
List[Dict[str, Any]]: list of dictionaries, where each dictionary contains a
figure and the associated region name
"""
log.info("visualizing histogram")
histogram_folder = pathlib.Path(config["General"]["HistogramFolder"])
figure_dict_list = []
for region in config["Regions"]:
histogram_dict_list = []
model_stdevs = []
# loop over samples in reverse order, such that samples that appear first in the
# config will appear at the top of the stack in the plot (they are plotted last)
for sample in config["Samples"][::-1]:
is_data = sample.get("Data", False)
histogram = histo.Histogram.from_config(
histogram_folder, region, sample, {}, modified=True
)
histogram_dict_list.append(
{
"label": sample["Name"],
"isData": is_data,
"yields": histogram.yields,
"variable": region["Variable"],
}
)
if not is_data:
model_stdevs.append(histogram.stdev)
figure_name = _figure_name(region["Name"], "pre-fit")
total_model_unc = _total_yield_uncertainty(model_stdevs)
bin_edges = histogram.bins
label = f"{region['Name']}\npre-fit"
# path is None if figure should not be saved
figure_path = pathlib.Path(figure_folder) / figure_name if save_figure else None
fig = plot_model.data_mc(
histogram_dict_list,
total_model_unc,
bin_edges,
figure_path=figure_path,
log_scale=log_scale,
log_scale_x=log_scale_x,
label=label,
close_figure=close_figure,
)
figure_dict_list.append({"figure": fig, "region": region["Name"]})
return figure_dict_list
def data_mc(
model_prediction: model_utils.ModelPrediction,
data: List[float],
*,
config: Optional[Dict[str, Any]] = None,
figure_folder: Union[str, pathlib.Path] = "figures",
log_scale: Optional[bool] = None,
log_scale_x: bool = False,
channels: Optional[Union[str, List[str]]] = None,
close_figure: bool = False,
save_figure: bool = True,
) -> Optional[List[Dict[str, Any]]]:
"""Draws pre- and post-fit data/MC histograms for a ``pyhf`` model and data.
The ``config`` argument is optional, but required to determine correct axis labels
and binning. The information is not stored in the model, and default values are
used if no ``config`` is supplied. This allows quickly plotting distributions for
models that were not created with ``cabinetry``, and for which no config exists.
Args:
model_prediction (model_utils.ModelPrediction): model prediction to show
data (List[float]): data to include in visualization, can either include auxdata
(the auxdata is then stripped internally) or only observed yields
config (Optional[Dict[str, Any]], optional): cabinetry configuration needed for
binning and axis labels, defaults to None (uses a default binning and labels
then)
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
log_scale (Optional[bool], optional): whether to use logarithmic vertical axis,
defaults to None (automatically determine whether to use linear/log scale)
log_scale_x (bool, optional): whether to use logarithmic horizontal axis,
defaults to False
channels (Optional[Union[str, List[str]]], optional): name of channel to show,
or list of names to include, defaults to None (uses all channels)
close_figure (bool, optional): whether to close each figure, defaults to False
(enable when producing many figures to avoid memory issues, prevents
automatic rendering in notebooks)
save_figure (bool, optional): whether to save figures, defaults to True
Returns:
Optional[List[Dict[str, Any]]]: list of dictionaries, where each dictionary
contains a figure and the associated region name, or None if no figure was
produced
"""
# strip off auxdata (if needed) and obtain data indexed by channel (and bin)
data_yields = model_utils._data_per_channel(model_prediction.model, data)
# channels to include in table, with optional filtering applied
filtered_channels = model_utils._filter_channels(model_prediction.model, channels)
if filtered_channels == []:
# nothing to include in tables, warning already raised via _filter_channels
return None
# indices of included channels
channel_indices = [
model_prediction.model.config.channels.index(ch) for ch in filtered_channels
]
# process channel by channel
figure_dict_list = []
for i_chan, channel_name in zip(channel_indices, filtered_channels):
histogram_dict_list = [] # one dict per region/channel
if config is not None:
# get the region dictionary from the config for binning / variable name
# for histogram inputs this information is not available, so need to fall
# back to defaults
region_dict = configuration.region_dict(config, channel_name)
if region_dict.get("Binning", None) is not None:
bin_edges = builder._binning(region_dict)
else:
bin_edges = np.arange(len(data_yields[i_chan]) + 1)
variable = region_dict.get("Variable", "bin")
else:
# fall back to defaults if no config is specified
bin_edges = np.arange(len(data_yields[i_chan]) + 1)
variable = "bin"
for i_sam, sample_name in enumerate(model_prediction.model.config.samples):
histogram_dict_list.append(
{
"label": sample_name,
"isData": False,
"yields": model_prediction.model_yields[i_chan][i_sam],
"variable": variable,
}
)
# add data sample
histogram_dict_list.append(
{
"label": "Data",
"isData": True,
"yields": data_yields[i_chan],
"variable": variable,
}
)
# path is None if figure should not be saved
figure_path = (
pathlib.Path(figure_folder)
/ _figure_name(channel_name, model_prediction.label)
if save_figure
else None
)
label = f"{channel_name}\n{model_prediction.label}"
fig = plot_model.data_mc(
histogram_dict_list,
np.asarray(model_prediction.total_stdev_model_bins[i_chan]),
bin_edges,
figure_path=figure_path,
log_scale=log_scale,
log_scale_x=log_scale_x,
label=label,
close_figure=close_figure,
)
figure_dict_list.append({"figure": fig, "region": channel_name})
return figure_dict_list
def templates(
config: Dict[str, Any],
*,
figure_folder: Union[str, pathlib.Path] = "figures",
close_figure: bool = False,
save_figure: bool = True,
) -> List[Dict[str, Any]]:
"""Visualizes template histograms (after post-processing) for systematic variations.
The original template histogram for systematic variations (before post-processing)
is also included in the visualization.
Args:
config (Dict[str, Any]): cabinetry configuration
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
close_figure (bool, optional): whether to close each figure, defaults to False
(enable when producing many figures to avoid memory issues, prevents
automatic rendering in notebooks)
save_figure (bool, optional): whether to save figures, defaults to True
Returns:
List[Dict[str, Any]]: list of dictionaries, where each dictionary contains a
figure and the associated region / sample / systematic names
"""
log.info("visualizing systematic templates")
histogram_folder = pathlib.Path(config["General"]["HistogramFolder"])
figure_folder = pathlib.Path(figure_folder) / "templates"
# could do this via the route module instead
figure_dict_list = []
for region in config["Regions"]:
for sample in config["Samples"]:
if sample.get("Data", False):
# skip data
continue
# loop over systematics (if they exist)
for systematic in config.get("Systematics", []):
histo_name = (
region["Name"]
+ "_"
+ sample["Name"]
+ "_"
+ systematic["Name"]
+ "*_modified*"
)
# create a list of paths to histograms matching the pattern
variation_paths = [
pathlib.Path(h_name)
for h_name in glob.glob(str(histogram_folder / histo_name))
]
# only keep up/down variations, and sort alphabetically
# (sorting to have consistent order, and simplified debugging)
variation_paths = sorted(
v for v in variation_paths if ("Up" in v.name or "Down" in v.name)
)
if len(variation_paths) == 0:
# no associated templates (normalization systematics)
continue
# extract nominal histogram
nominal_histo = histo.Histogram.from_config(
histogram_folder, region, sample, {}
)
bins = nominal_histo.bins
# variable is a required config setting for ntuple inputs, but not for
# histogram inputs, so default to "observable" for these cases (the
# actual bin edges are preserved here, so not using "bin" as in data_mc)
variable = region.get("Variable", "observable")
nominal = {"yields": nominal_histo.yields, "stdev": nominal_histo.stdev}
# extract original and modified (after post-processing) variation
# histograms, if they exist
up_orig = {}
down_orig = {}
up_mod = {}
down_mod = {}
for variation_path in variation_paths:
# original variation, before post-processing
variation_path_orig = pathlib.Path(
str(variation_path).replace("_modified", "")
)
var_histo_orig = histo.Histogram.from_path(variation_path_orig)
var_orig = {
"yields": var_histo_orig.yields,
"stdev": var_histo_orig.stdev,
}
# variation after post-processing
var_histo_mod = histo.Histogram.from_path(variation_path)
var_mod = {
"yields": var_histo_mod.yields,
"stdev": var_histo_mod.stdev,
}
if "Up" in variation_path.parts[-1]:
up_orig.update(var_orig)
up_mod.update(var_mod)
else:
down_orig.update(var_orig)
down_mod.update(var_mod)
figure_label = (
f"region: {region['Name']}\nsample: {sample['Name']}"
f"\nsystematic: {systematic['Name']}"
)
figure_name = (
f"{region['Name']}_{sample['Name']}_{systematic['Name']}.pdf"
)
figure_path = figure_folder / figure_name if save_figure else None
fig = plot_model.templates(
nominal,
up_orig,
down_orig,
up_mod,
down_mod,
bins,
variable,
figure_path=figure_path,
label=figure_label,
close_figure=close_figure,
)
figure_dict_list.append(
{
"figure": fig,
"region": region["Name"],
"sample": sample["Name"],
"systematic": systematic["Name"],
}
)
return figure_dict_list
def correlation_matrix(
fit_results: fit.FitResults,
*,
figure_folder: Union[str, pathlib.Path] = "figures",
pruning_threshold: float = 0.0,
close_figure: bool = True,
save_figure: bool = True,
) -> mpl.figure.Figure:
"""Draws a correlation matrix.
Args:
fit_results (fit.FitResults): fit results, including correlation matrix and
parameter labels
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
pruning_threshold (float, optional): minimum correlation for a parameter to
have with any other parameters to not get pruned, defaults to 0.0
close_figure (bool, optional): whether to close figure, defaults to True
save_figure (bool, optional): whether to save figure, defaults to True
Returns:
matplotlib.figure.Figure: the correlation matrix figure
"""
# path is None if figure should not be saved
figure_path = (
pathlib.Path(figure_folder) / "correlation_matrix.pdf" if save_figure else None
)
# create a matrix that is True if a correlation is below threshold, and True on the
# diagonal
below_threshold = np.where(
np.abs(fit_results.corr_mat) < pruning_threshold, True, False
)
np.fill_diagonal(below_threshold, True)
# get list of booleans specifying if everything in rows/columns is below threshold
all_below_threshold = np.all(below_threshold, axis=0)
# get list of booleans specifying if rows/columns correspond to fixed parameter
# (0 correlations)
fixed_parameter = np.all(np.equal(fit_results.corr_mat, 0.0), axis=0)
# get indices of rows/columns where everything is below threshold, or the parameter
# is fixed
delete_indices = np.where(np.logical_or(all_below_threshold, fixed_parameter))
# delete rows and columns where all correlations are below threshold / parameter is
# fixed
corr_mat = np.delete(
np.delete(fit_results.corr_mat, delete_indices, axis=1), delete_indices, axis=0
)
labels = np.delete(fit_results.labels, delete_indices)
fig = plot_result.correlation_matrix(
corr_mat, labels, figure_path=figure_path, close_figure=close_figure
)
return fig
def pulls(
fit_results: fit.FitResults,
*,
figure_folder: Union[str, pathlib.Path] = "figures",
exclude: Optional[Union[str, List[str], Tuple[str, ...]]] = None,
exclude_by_type: Optional[List[str]] = None,
close_figure: bool = True,
save_figure: bool = True,
) -> mpl.figure.Figure:
"""Draws a pull plot of parameter results and uncertainties.
Args:
fit_results (fit.FitResults): fit results, including correlation matrix and
parameter labels
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
exclude (Optional[Union[str, List[str], Tuple[str, ...]]], optional): parameter
or parameters to exclude from plot, defaults to None (nothing excluded),
compatible with unix wildcards
exclude_by_type (Optional[Union[str, List[str], Tuple[str, ...]]], optional):
exclude parameters of the given type, defaults ``['staterror']`` filtering
out mc_stat uncertainties which are centered on 1
close_figure (bool, optional): whether to close figure, defaults to True
save_figure (bool, optional): whether to save figure, defaults to True
Returns:
matplotlib.figure.Figure: the pull figure
"""
# path is None if figure should not be saved
figure_path = pathlib.Path(figure_folder) / "pulls.pdf" if save_figure else None
labels_np = np.asarray(fit_results.labels)
numeric = np.array(
[True if ty in ["normfactor"] else False for ty in fit_results.types]
)
if exclude is None:
exclude_set = set()
elif isinstance(exclude, str):
exclude_set = set(fnmatch.filter(fit_results.labels, exclude))
else:
exclude_set = set(exclude)
# exclude fixed parameters from pull plot
exclude_set.update(
[
label
for i_np, label in enumerate(labels_np)
if fit_results.uncertainty[i_np] == 0.0
]
)
# exclude by type
if exclude_by_type is None:
exclude_by_type = ["staterror"]
exclude_set.update(
[
label
for label, kind in zip(labels_np, fit_results.types)
if kind in exclude_by_type
]
)
# filter out user-specified parameters
mask = [True if label not in exclude_set else False for label in labels_np]
bestfit = fit_results.bestfit[mask]
uncertainty = fit_results.uncertainty[mask]
labels_np = labels_np[mask]
numeric = numeric[mask]
fig = plot_result.pulls(
bestfit,
uncertainty,
labels_np,
numeric=numeric,
figure_path=figure_path,
close_figure=close_figure,
)
return fig
def ranking(
ranking_results: fit.RankingResults,
*,
figure_folder: Union[str, pathlib.Path] = "figures",
max_pars: Optional[int] = None,
close_figure: bool = True,
save_figure: bool = True,
) -> mpl.figure.Figure:
"""Produces a ranking plot showing the impact of parameters on the POI.
Args:
ranking_results (fit.RankingResults): fit results, and pre- and post-fit impacts
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
max_pars (Optional[int], optional): number of parameters to include, defaults to
None (which means all parameters are included)
close_figure (bool, optional): whether to close figure, defaults to True
save_figure (bool, optional): whether to save figure, defaults to True
Returns:
matplotlib.figure.Figure: the ranking figure
"""
# path is None if figure should not be saved
figure_path = pathlib.Path(figure_folder) / "ranking.pdf" if save_figure else None
# sort parameters by decreasing average post-fit impact
avg_postfit_impact = (
np.abs(ranking_results.postfit_up) + np.abs(ranking_results.postfit_down)
) / 2
# get indices to sort by decreasing impact
sorted_indices = np.argsort(avg_postfit_impact)[::-1]
bestfit = ranking_results.bestfit[sorted_indices]
uncertainty = ranking_results.uncertainty[sorted_indices]
labels = np.asarray(ranking_results.labels)[sorted_indices] # labels are list
prefit_up = ranking_results.prefit_up[sorted_indices]
prefit_down = ranking_results.prefit_down[sorted_indices]
postfit_up = ranking_results.postfit_up[sorted_indices]
postfit_down = ranking_results.postfit_down[sorted_indices]
if max_pars is not None:
# only keep leading parameters in ranking
bestfit = bestfit[:max_pars]
uncertainty = uncertainty[:max_pars]
labels = labels[:max_pars]
prefit_up = prefit_up[:max_pars]
prefit_down = prefit_down[:max_pars]
postfit_up = postfit_up[:max_pars]
postfit_down = postfit_down[:max_pars]
fig = plot_result.ranking(
bestfit,
uncertainty,
labels,
prefit_up,
prefit_down,
postfit_up,
postfit_down,
figure_path=figure_path,
close_figure=close_figure,
)
return fig
def scan(
scan_results: fit.ScanResults,
*,
figure_folder: Union[str, pathlib.Path] = "figures",
close_figure: bool = True,
save_figure: bool = True,
) -> mpl.figure.Figure:
"""Visualizes the results of a likelihood scan.
Args:
scan_results (fit.ScanResults): results of a likelihood scan
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
close_figure (bool, optional): whether to close figure, defaults to True
save_figure (bool, optional): whether to save figure, defaults to True
Returns:
matplotlib.figure.Figure: the likelihood scan figure
"""
# replace [], needed for staterrors
figure_name = (
"scan_" + scan_results.name.replace("[", "_").replace("]", "") + ".pdf"
)
# path is None if figure should not be saved
figure_path = pathlib.Path(figure_folder) / figure_name if save_figure else None
fig = plot_result.scan(
scan_results.name,
scan_results.bestfit,
scan_results.uncertainty,
scan_results.parameter_values,
scan_results.delta_nlls,
figure_path=figure_path,
close_figure=close_figure,
)
return fig
def limit(
limit_results: fit.LimitResults,
*,
figure_folder: Union[str, pathlib.Path] = "figures",
close_figure: bool = True,
save_figure: bool = True,
) -> mpl.figure.Figure:
"""Visualizes observed and expected CLs values as a function of the POI.
Args:
limit_results (fit.LimitResults): results of upper limit determination
figure_folder (Union[str, pathlib.Path], optional): path to the folder to save
figures in, defaults to "figures"
close_figure (bool, optional): whether to close figure, defaults to True
save_figure (bool, optional): whether to save figure, defaults to True
Returns:
matplotlib.figure.Figure: the CLs figure
"""
# path is None if figure should not be saved
figure_path = pathlib.Path(figure_folder) / "limit.pdf" if save_figure else None
fig = plot_result.limit(
limit_results.observed_CLs,
limit_results.expected_CLs,
limit_results.poi_values,
1 - limit_results.confidence_level,
figure_path=figure_path,
close_figure=close_figure,
)
return fig