Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow failing toys #2128

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/pyhf/infer/calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@
test_stat="qtilde",
ntoys=2000,
track_progress=True,
skip_failing_toys=False,
):
r"""
Toy-based Calculator.
Expand Down Expand Up @@ -705,6 +706,7 @@
~pyhf.infer.calculators.ToyCalculator: The calculator for toy-based quantities.

"""
self.skip_failing_toys = skip_failing_toys
self.ntoys = ntoys
self.data = data
self.pdf = pdf
Expand Down Expand Up @@ -754,6 +756,9 @@
Tuple (~pyhf.infer.calculators.EmpiricalDistribution): The distributions under the hypotheses.

"""

print('skip?', self.skip_failing_toys)

tensorlib, _ = get_backend()
sample_shape = (self.ntoys,)

Expand Down Expand Up @@ -792,29 +797,45 @@

signal_teststat = []
for sample in tqdm.tqdm(signal_sample, **tqdm_options, desc='Signal-like'):
signal_teststat.append(
teststat_func(
try:
value = teststat_func(
poi_test,
sample,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
)
except:

Check notice on line 809 in src/pyhf/infer/calculators.py

View check run for this annotation

codefactor.io / CodeFactor

src/pyhf/infer/calculators.py#L809

Do not use bare 'except'. (E722)
if self.skip_failing_toys:
value = None
print(f'signal failed for: {sample}')
else:
raise

if (value is not None) and (tensorlib.isfinite(value)):
signal_teststat.append(value)

bkg_teststat = []
for sample in tqdm.tqdm(bkg_sample, **tqdm_options, desc='Background-like'):
bkg_teststat.append(
teststat_func(
try:
value = teststat_func(
poi_test,
sample,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
)
except:

Check notice on line 830 in src/pyhf/infer/calculators.py

View check run for this annotation

codefactor.io / CodeFactor

src/pyhf/infer/calculators.py#L830

Do not use bare 'except'. (E722)
if self.skip_failing_toys:
value = None
print(f'background failed for: {sample}')
else:
raise

if (value is not None) and (tensorlib.isfinite(value)):
bkg_teststat.append(value)

s_plus_b = EmpiricalDistribution(tensorlib.astensor(signal_teststat))
b_only = EmpiricalDistribution(tensorlib.astensor(bkg_teststat))
Expand Down
Loading