Skip to content

Commit 3453040

Browse files
lukasheinrichmatthewfeickert
authored andcommitted
first attempt
1 parent b654be9 commit 3453040

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
@@ -674,6 +674,7 @@ def __init__(
674674
test_stat="qtilde",
675675
ntoys=2000,
676676
track_progress=True,
677+
skip_failing_toys = False,
677678
):
678679
r"""
679680
Toy-based Calculator.
@@ -704,6 +705,7 @@ def __init__(
704705
~pyhf.infer.calculators.ToyCalculator: The calculator for toy-based quantities.
705706
706707
"""
708+
self.skip_failing_toys = skip_failing_toys
707709
self.ntoys = ntoys
708710
self.data = data
709711
self.pdf = pdf
@@ -753,6 +755,9 @@ def distributions(self, poi_test, track_progress=None):
753755
Tuple (~pyhf.infer.calculators.EmpiricalDistribution): The distributions under the hypotheses.
754756
755757
"""
758+
759+
print('skip?',self.skip_failing_toys)
760+
756761
tensorlib, _ = get_backend()
757762
sample_shape = (self.ntoys,)
758763

@@ -791,29 +796,47 @@ def distributions(self, poi_test, track_progress=None):
791796

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

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

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

0 commit comments

Comments
 (0)