Skip to content

Commit 0b05b53

Browse files
lukasheinrichkratsg
authored andcommitted
first attempt
1 parent 89a6c13 commit 0b05b53

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/pyhf/infer/calculators.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ def __init__(
675675
test_stat="qtilde",
676676
ntoys=2000,
677677
track_progress=True,
678+
skip_failing_toys = False,
678679
):
679680
r"""
680681
Toy-based Calculator.
@@ -705,6 +706,7 @@ def __init__(
705706
~pyhf.infer.calculators.ToyCalculator: The calculator for toy-based quantities.
706707
707708
"""
709+
self.skip_failing_toys = skip_failing_toys
708710
self.ntoys = ntoys
709711
self.data = data
710712
self.pdf = pdf
@@ -754,6 +756,9 @@ def distributions(self, poi_test, track_progress=None):
754756
Tuple (~pyhf.infer.calculators.EmpiricalDistribution): The distributions under the hypotheses.
755757
756758
"""
759+
760+
print('skip?',self.skip_failing_toys)
761+
757762
tensorlib, _ = get_backend()
758763
sample_shape = (self.ntoys,)
759764

@@ -792,29 +797,47 @@ def distributions(self, poi_test, track_progress=None):
792797

793798
signal_teststat = []
794799
for sample in tqdm.tqdm(signal_sample, **tqdm_options, desc='Signal-like'):
795-
signal_teststat.append(
796-
teststat_func(
797-
poi_test,
798-
sample,
799-
self.pdf,
800-
self.init_pars,
801-
self.par_bounds,
802-
self.fixed_params,
800+
try:
801+
value = teststat_func(
802+
poi_test,
803+
sample,
804+
self.pdf,
805+
self.init_pars,
806+
self.par_bounds,
807+
self.fixed_params,
808+
)
809+
except RuntimeError:
810+
if self.skip_failing_toys:
811+
value = None
812+
else:
813+
raise
814+
815+
if (value is not None) and (tensorlib.isfinite(value)):
816+
signal_teststat.append(
817+
value
803818
)
804-
)
805819

806820
bkg_teststat = []
807821
for sample in tqdm.tqdm(bkg_sample, **tqdm_options, desc='Background-like'):
808-
bkg_teststat.append(
809-
teststat_func(
810-
poi_test,
811-
sample,
812-
self.pdf,
813-
self.init_pars,
814-
self.par_bounds,
815-
self.fixed_params,
822+
try:
823+
value = teststat_func(
824+
poi_test,
825+
sample,
826+
self.pdf,
827+
self.init_pars,
828+
self.par_bounds,
829+
self.fixed_params,
830+
)
831+
except RuntimeError:
832+
if self.skip_failing_toys:
833+
value = None
834+
else:
835+
raise
836+
837+
if (value is not None) and (tensorlib.isfinite(value)):
838+
bkg_teststat.append(
839+
value
816840
)
817-
)
818841

819842
s_plus_b = EmpiricalDistribution(tensorlib.astensor(signal_teststat))
820843
b_only = EmpiricalDistribution(tensorlib.astensor(bkg_teststat))

0 commit comments

Comments
 (0)