Skip to content

Commit fc3d344

Browse files
committed
fix: update tests
1 parent ba7a6f8 commit fc3d344

File tree

9 files changed

+38
-36
lines changed

9 files changed

+38
-36
lines changed

Diff for: src/cabinetry/fit/results_containers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Provides containers for inference results."""
22

3-
from typing import Dict, List, NamedTuple, Optional, Tuple
3+
from typing import Dict, List, NamedTuple, Tuple
44

55
import numpy as np
66

@@ -24,7 +24,7 @@ class FitResults(NamedTuple):
2424
bestfit: np.ndarray
2525
uncertainty: np.ndarray
2626
labels: List[str]
27-
types: List[Optional[str]]
27+
types: List[List[str]]
2828
corr_mat: np.ndarray
2929
best_twice_nll: float
3030
goodness_of_fit: float = -1

Diff for: src/cabinetry/model_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def _filter_channels(
518518

519519
def _labels_modifiers(
520520
model: pyhf.pdf.Model,
521-
) -> Tuple[List[str], List[Optional[str]]]:
521+
) -> Tuple[List[str], List[List[str]]]:
522522
""" """
523523
labels = model.config.par_names()
524524
types = []
@@ -528,9 +528,9 @@ def _labels_modifiers(
528528
mod_type
529529
for par_name, mod_type in model.config.modifiers
530530
if par_name == parameter
531-
][:1]
531+
]
532532
] * model.config.param_set(parameter).n_parameters
533-
return labels, sum(types, []) # flatten types
533+
return labels, types # flatten types
534534

535535

536536
def match_fit_results(model: pyhf.pdf.Model, fit_results: FitResults) -> FitResults:

Diff for: src/cabinetry/visualize/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,7 @@ def pulls(
483483
# path is None if figure should not be saved
484484
figure_path = pathlib.Path(figure_folder) / "pulls.pdf" if save_figure else None
485485
labels_np = np.asarray(fit_results.labels)
486-
numeric = np.array(
487-
[True if ty in ["normfactor"] else False for ty in fit_results.types]
488-
)
486+
numeric = np.array([bool(set(ty) & {"normfactor"}) for ty in fit_results.types])
489487

490488
exclude_set = _exclude_matching(
491489
fit_results, exclude=exclude, exclude_by_type=exclude_by_type

Diff for: src/cabinetry/visualize/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def _exclude_matching(
7777
exclude_by_type = ["staterror"]
7878

7979
exclude_set.update(
80-
[label for label, kind in zip(labels, types) if kind in exclude_by_type]
80+
[
81+
label
82+
for label, kinds in zip(labels, types)
83+
if bool(set(kinds) & set(exclude_by_type))
84+
]
8185
)
8286
return exclude_set

Diff for: tests/cli/test_cli.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_workspace(mock_validate, mock_build, cli_helpers, tmp_path):
9898
np.asarray([1.0]),
9999
np.asarray([0.1]),
100100
["label"],
101-
["type"],
101+
[["type"]],
102102
np.asarray([[1.0]]),
103103
1.0,
104104
),
@@ -114,7 +114,7 @@ def test_fit(mock_utils, mock_fit, mock_pulls, mock_corrmat, tmp_path):
114114
bestfit = np.asarray([1.0])
115115
uncertainty = np.asarray([0.1])
116116
labels = ["label"]
117-
types = ["type"]
117+
types = [["type"]]
118118
corr_mat = np.asarray([[1.0]])
119119
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, corr_mat, 1.0)
120120

@@ -213,7 +213,7 @@ def test_fit(mock_utils, mock_fit, mock_pulls, mock_corrmat, tmp_path):
213213
np.asarray([1.0]),
214214
np.asarray([0.1]),
215215
["label"],
216-
["type"],
216+
[["type"]],
217217
np.asarray([[1.0]]),
218218
1.0,
219219
),
@@ -229,7 +229,7 @@ def test_ranking(mock_utils, mock_fit, mock_rank, mock_vis, tmp_path):
229229
bestfit = np.asarray([1.0])
230230
uncertainty = np.asarray([0.1])
231231
labels = ["label"]
232-
types = ["type"]
232+
types = [["type"]]
233233
corr_mat = np.asarray([[1.0]])
234234
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, corr_mat, 1.0)
235235

@@ -482,7 +482,7 @@ def test_significance(mock_utils, mock_sig, tmp_path):
482482
np.asarray([1.0]),
483483
np.asarray([0.1]),
484484
["label"],
485-
["type"],
485+
[["type"]],
486486
np.asarray([[1.0]]),
487487
1.0,
488488
),
@@ -528,7 +528,7 @@ def test_data_mc(
528528
np.asarray([1.0]),
529529
np.asarray([0.1]),
530530
["label"],
531-
["type"],
531+
[["type"]],
532532
np.asarray([[1.0]]),
533533
1.0,
534534
)

Diff for: tests/fit/test_fit.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_print_results(caplog):
1616
bestfit = np.asarray([1.0, 2.0])
1717
uncertainty = np.asarray([0.1, 0.3])
1818
labels = ["param_A", "param_B"]
19-
types = ["normsys", "shapesys"]
19+
types = [["normsys"], ["shapesys"]]
2020
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, np.empty(0), 0.0)
2121

2222
fit.print_results(fit_results)
@@ -144,13 +144,13 @@ def test__fit_model_custom(mock_minos, example_spec, example_spec_multibin):
144144
@mock.patch(
145145
"cabinetry.fit._fit_model_custom",
146146
return_value=fit.FitResults(
147-
np.asarray([1.2]), np.asarray([0.2]), ["par"], ["normsys"], np.empty(0), 2.0
147+
np.asarray([1.2]), np.asarray([0.2]), ["par"], [["normsys"]], np.empty(0), 2.0
148148
),
149149
)
150150
@mock.patch(
151151
"cabinetry.fit._fit_model_pyhf",
152152
return_value=fit.FitResults(
153-
np.asarray([1.1]), np.asarray([0.2]), ["par"], ["normsys"], np.empty(0), 2.0
153+
np.asarray([1.1]), np.asarray([0.2]), ["par"], [["normsys"]], np.empty(0), 2.0
154154
),
155155
)
156156
def test__fit_model(mock_pyhf, mock_custom, example_spec):
@@ -296,7 +296,7 @@ def test__goodness_of_fit(
296296
@mock.patch(
297297
"cabinetry.fit._fit_model",
298298
return_value=fit.FitResults(
299-
np.asarray([1.0]), np.asarray([0.1]), ["par"], ["normsys"], np.empty(0), 2.0
299+
np.asarray([1.0]), np.asarray([0.1]), ["par"], [["normsys"]], np.empty(0), 2.0
300300
),
301301
)
302302
def test_fit(mock_fit, mock_print, mock_gof):
@@ -386,31 +386,31 @@ def test_fit(mock_fit, mock_print, mock_gof):
386386
np.asarray([0.9, 1.3]),
387387
np.asarray([0.1, 0.1]),
388388
["a", "b"],
389-
["normsys", "normsys"],
389+
[["normsys"], ["normsys"]],
390390
np.empty(0),
391391
0.0,
392392
),
393393
fit.FitResults(
394394
np.asarray([0.9, 0.7]),
395395
np.asarray([0.1, 0.1]),
396396
["a", "b"],
397-
["normsys", "normsys"],
397+
[["normsys"], ["normsys"]],
398398
np.empty(0),
399399
0.0,
400400
),
401401
fit.FitResults(
402402
np.asarray([0.9, 1.2]),
403403
np.asarray([0.1, 0.1]),
404404
["a", "b"],
405-
["normsys", "normsys"],
405+
[["normsys"], ["normsys"]],
406406
np.empty(0),
407407
0.0,
408408
),
409409
fit.FitResults(
410410
np.asarray([0.9, 0.8]),
411411
np.asarray([0.1, 0.1]),
412412
["a", "b"],
413-
["normsys", "normsys"],
413+
[["normsys"], ["normsys"]],
414414
np.empty(0),
415415
0.0,
416416
),
@@ -419,15 +419,15 @@ def test_fit(mock_fit, mock_print, mock_gof):
419419
np.asarray([0.9, 1.2]),
420420
np.asarray([0.1, 0.1]),
421421
["a", "b"],
422-
["normsys", "normsys"],
422+
[["normsys"], ["normsys"]],
423423
np.empty(0),
424424
0.0,
425425
),
426426
fit.FitResults(
427427
np.asarray([0.9, 0.8]),
428428
np.asarray([0.1, 0.1]),
429429
["a", "b"],
430-
["normsys", "normsys"],
430+
[["normsys"], ["normsys"]],
431431
np.empty(0),
432432
0.0,
433433
),
@@ -436,23 +436,23 @@ def test_fit(mock_fit, mock_print, mock_gof):
436436
np.asarray([0.9, 1.0]),
437437
np.asarray([0.3, 0.3]),
438438
["a", "b"],
439-
["normsys", "normsys"],
439+
[["normsys"], ["normsys"]],
440440
np.empty(0),
441441
0.0,
442442
),
443443
fit.FitResults(
444444
np.asarray([0.9, 1.3]),
445445
np.asarray([0.1, 0.1]),
446446
["a", "b"],
447-
["normsys", "normsys"],
447+
[["normsys"], ["normsys"]],
448448
np.empty(0),
449449
0.0,
450450
),
451451
fit.FitResults(
452452
np.asarray([0.9, 0.7]),
453453
np.asarray([0.1, 0.1]),
454454
["a", "b"],
455-
["normsys", "normsys"],
455+
[["normsys"], ["normsys"]],
456456
np.empty(0),
457457
0.0,
458458
),
@@ -463,7 +463,7 @@ def test_ranking(mock_fit, example_spec):
463463
bestfit = np.asarray([0.9, 1.0])
464464
uncertainty = np.asarray([0.02, 0.1])
465465
labels = ["staterror", "mu"]
466-
types = ["staterror", "normfactor"]
466+
types = [["staterror"], ["normfactor"]]
467467
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, np.empty(0), 0.0)
468468
model, data = model_utils.model_and_data(example_spec)
469469
ranking_results = fit.ranking(model, data, fit_results=fit_results)

Diff for: tests/fit/test_fit_results_containers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_FitResults():
99
bestfit = np.asarray([1.0])
1010
uncertainty = np.asarray([0.1])
1111
labels = ["par_a"]
12-
types = [None]
12+
types = [[]]
1313
corr_mat = np.asarray([[1.0]])
1414
best_twice_nll = 2.0
1515
fit_results = fit.FitResults(
@@ -109,7 +109,7 @@ def test_print_results(caplog):
109109
bestfit = np.asarray([1.0, 2.0])
110110
uncertainty = np.asarray([0.1, 0.3])
111111
labels = ["param_A", "param_B"]
112-
types = [None, None]
112+
types = [[], []]
113113
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, np.empty(0), 0.0)
114114

115115
fit.print_results(fit_results)

Diff for: tests/test_model_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_prediction(
265265
np.asarray([1.01, 1.1]),
266266
np.asarray([0.03, 0.1]),
267267
["staterror_Signal-Region[0]", "Signal strength"],
268-
["staterror", "normfactor"],
268+
[["staterror"], ["normfactor"]],
269269
np.asarray([[1.0, 0.2], [0.2, 1.0]]),
270270
0.0,
271271
)
@@ -299,7 +299,7 @@ def test_prediction(
299299
np.asarray([1.01, 1.1]),
300300
np.asarray([0.03, 0.1]),
301301
["a", "b"],
302-
["staterror", "normfactor"],
302+
[["staterror"], ["normfactor"]],
303303
np.asarray([[1.0, 0.2], [0.2, 1.0]]),
304304
0.0,
305305
)
@@ -396,7 +396,7 @@ def test_match_fit_results(mock_pars, mock_uncs):
396396
np.asarray([1.0, 2.0, 3.0]),
397397
np.asarray([0.1, 0.2, 0.3]),
398398
["par_a", "par_b", "par_c"],
399-
[None, None, None],
399+
[[], [], []],
400400
np.asarray([[1.0, 0.2, 0.5], [0.2, 1.0, 0.1], [0.5, 0.1, 1.0]]),
401401
5.0,
402402
0.1,

Diff for: tests/visualize/test_visualize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def test_correlation_matrix(mock_draw):
360360
corr_mat = np.asarray([[1.0, 0.2, 0.1], [0.2, 1.0, 0.1], [0.1, 0.1, 1.0]])
361361
corr_mat_pruned = np.asarray([[1.0, 0.2], [0.2, 1.0]])
362362
labels = ["a", "b", "c"]
363-
types = [None, None, None]
363+
types = [[], [], []]
364364
labels_pruned = ["a", "b"]
365365
folder_path = "tmp"
366366
figure_path = pathlib.Path(folder_path) / "correlation_matrix.pdf"
@@ -408,7 +408,7 @@ def test_pulls(mock_draw):
408408
bestfit = np.asarray([0.8, 1.0, 1.05, 1.1])
409409
uncertainty = np.asarray([0.9, 1.0, 0.03, 0.7])
410410
labels = ["a", "b", "staterror_region[0]", "c"]
411-
types = [None, None, "staterror", None]
411+
types = [[], [], ["staterror"], []]
412412
exclude = ["a"]
413413
folder_path = "tmp"
414414
fit_results = fit.FitResults(bestfit, uncertainty, labels, types, np.empty(0), 1.0)

0 commit comments

Comments
 (0)