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: Toy Calculator executor support #1158

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
33 changes: 33 additions & 0 deletions src/pyhf/futures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Defines a synchronous Python-like Executor for pyhf: :class:`TrivialExecutor`
"""

from concurrent import futures


class TrivialExecutor(futures.Executor):
"""
Formally satisfies the interface for a :class:`concurrent.futures.Executor`
but the :func:`TrivialExecutor.submit` method computes its ``task``
synchronously.
"""

def __repr__(self):
"""Representation of the object"""
module = type(self).__module__
qualname = type(self).__qualname__
return f"<{module}.{qualname} object at {hex(id(self))}>"

def submit(self, task, *args, **kwargs):
"""
Immediately runs ``task(*args)``.
"""
_f = futures.Future()
_f.set_running_or_notify_cancel()
try:
result = task(*args)
except BaseException as exc:
_f.set_exception(exc)
else:
_f.set_result(result)
return _f
99 changes: 75 additions & 24 deletions src/pyhf/infer/calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pyhf.infer.mle import fixed_poi_fit
from pyhf import get_backend
from pyhf.infer import utils
from pyhf import futures
import tqdm

from dataclasses import dataclass
Expand Down Expand Up @@ -675,6 +676,7 @@ def __init__(
test_stat="qtilde",
ntoys=2000,
track_progress=True,
executor=None,
):
r"""
Toy-based Calculator.
Expand All @@ -700,6 +702,7 @@ def __init__(
:math:`q_{0}` (:func:`~pyhf.infer.test_statistics.q0`).
ntoys (:obj:`int`): Number of toys to use (how many times to sample the underlying distributions).
track_progress (:obj:`bool`): Whether to display the `tqdm` progress bar or not (outputs to `stderr`).
executor (:class:`concurrent.futures.Executor`): Executor for job dispatch. ``None`` by default.

Returns:
~pyhf.infer.calculators.ToyCalculator: The calculator for toy-based quantities.
Expand All @@ -713,6 +716,7 @@ def __init__(
self.fixed_params = fixed_params or pdf.config.suggested_fixed()
self.test_stat = test_stat
self.track_progress = track_progress
self.executor = executor or futures.TrivialExecutor()

def distributions(self, poi_test, track_progress=None):
"""
Expand Down Expand Up @@ -790,34 +794,81 @@ def distributions(self, poi_test, track_progress=None):
unit='toy',
)

signal_teststat = []
for sample in tqdm.tqdm(signal_sample, **tqdm_options, desc='Signal-like'):
signal_teststat.append(
teststat_func(
poi_test,
sample,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
if not isinstance(self.executor, futures.TrivialExecutor):
signal_teststat = self.executor.map(
teststat_func,
*zip(
*(
(
poi_test,
sample,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
)
for sample in signal_sample
)
),
)

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

signal_teststat = tqdm.tqdm(
signal_teststat, **tqdm_options, desc='Signal-like'
)
s_plus_b = EmpiricalDistribution(tensorlib.astensor(list(signal_teststat)))
bkg_teststat = tqdm.tqdm(
bkg_teststat, **tqdm_options, desc='Background-like'
)
b_only = EmpiricalDistribution(tensorlib.astensor(list(bkg_teststat)))
else:
signal_teststat = []
for sample in tqdm.tqdm(signal_sample, **tqdm_options, desc='Signal-like'):
signal_teststat.append(
self.executor.submit(
teststat_func,
poi_test,
sample,
self.pdf,
self.init_pars,
self.par_bounds,
self.fixed_params,
).result()
)

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

s_plus_b = EmpiricalDistribution(tensorlib.astensor(list(signal_teststat)))
b_only = EmpiricalDistribution(tensorlib.astensor(list(bkg_teststat)))

s_plus_b = EmpiricalDistribution(tensorlib.astensor(signal_teststat))
b_only = EmpiricalDistribution(tensorlib.astensor(bkg_teststat))
return s_plus_b, b_only

def pvalues(self, teststat, sig_plus_bkg_distribution, bkg_only_distribution):
Expand Down
Loading