Skip to content

Commit

Permalink
tests should pass
Browse files Browse the repository at this point in the history
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

string-based constraints and more dedubpe in builders

pyflakes

[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
  • Loading branch information
lukasheinrich committed Oct 12, 2021
1 parent 1ae91c3 commit 14a3c68
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 98 deletions.
56 changes: 30 additions & 26 deletions docs/examples/notebooks/custom_modifiers.ipynb

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/pyhf/cli/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyhf.workspace import Workspace
from pyhf import modifiers
from pyhf import utils
from pyhf import parameters

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,10 +82,7 @@ def inspect(workspace, output_file, measurement):
model = ws.model()

result['parameters'] = sorted(
[
(k, parset_descr[type(v['paramset'])])
for k, v in model.config.par_map.items()
]
(k, parset_descr[type(v['paramset'])]) for k, v in model.config.par_map.items()
)
result['systematics'] = [
(
Expand Down
34 changes: 32 additions & 2 deletions src/pyhf/modifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@
from .shapefactor import shapefactor_builder, shapefactor_combined
from .normsys import normsys_builder, normsys_combined
from .shapesys import shapesys_builder, shapesys_combined
from .staterror import staterr_builder, staterror_combined
from .staterror import staterror_builder, staterror_combined

__all__ = [
'histosys',
'histosys_builder',
'histosys_combined',
'lumi',
'lumi_builder',
'lumi_combined',
'normfactor',
'normfactor_builder',
'normfactor_combined',
'normsys',
'normsys_builder',
'normsys_combined',
'pyhfset',
'shapefactor',
'shapefactor_builder',
'shapefactor_combined',
'shapesys',
'shapesys_builder',
'shapesys_combined',
'staterror',
'staterror_builder',
'staterror_combined',
]


def __dir__():
return __all__


pyhfset = {
'histosys': (histosys_builder, histosys_combined),
Expand All @@ -13,5 +43,5 @@
'normsys': (normsys_builder, normsys_combined),
'shapefactor': (shapefactor_builder, shapefactor_combined),
'shapesys': (shapesys_builder, shapesys_combined),
'staterror': (staterr_builder, staterror_combined),
'staterror': (staterror_builder, staterror_combined),
}
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/histosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

from pyhf import get_backend, events
from pyhf import interpolators
from pyhf.parameters import constrained_by_normal, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)


def required_parset(sample_data, modifier_data):
return {
'paramset_type': constrained_by_normal,
'paramset_type': 'constrained_by_normal',
'n_parameters': 1,
'is_shared': True,
'is_scalar': True,
'inits': (0.0,),
'bounds': ((-5.0, 5.0),),
'fixed': False,
Expand Down Expand Up @@ -48,20 +49,22 @@ def append(self, key, channel, sample, thismod, defined_samp):
self._mega_mods[key][sample]['data']['mask'] += moddata['mask']

if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class histosys_combined:
name = 'histosys'
op_code = 'addition'

def __init__(
self, modifiers, pdfconfig, builder_data, interpcode='code0', batch_size=None
):
self.name = 'histosys'
self.op_code = 'addition'
self.batch_size = batch_size
self.interpcode = interpcode
assert self.interpcode in ['code0', 'code2', 'code4p']
Expand Down
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/lumi.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging

from pyhf import get_backend, events
from pyhf.parameters import constrained_by_normal, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)


def required_parset(sample_data, modifier_data):
return {
'paramset_type': constrained_by_normal,
'paramset_type': 'constrained_by_normal',
'n_parameters': 1,
'is_shared': True,
'is_scalar': True,
'inits': None, # lumi
'bounds': None, # (0, 10*lumi)
'fixed': False,
Expand Down Expand Up @@ -42,19 +43,21 @@ def append(self, key, channel, sample, thismod, defined_samp):
moddata = self.collect(thismod, nom)
self._mega_mods[key][sample]['data']['mask'] += moddata['mask']
if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class lumi_combined:
name = 'lumi'
op_code = 'multiplication'

def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None):
self.batch_size = batch_size
self.name = 'lumi'
self.op_code = 'multiplication'

keys = [f'{mtype}/{m}' for m, mtype in modifiers]
lumi_mods = [m for m, _ in modifiers]
Expand Down
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/normfactor.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging

from pyhf import get_backend, events
from pyhf.parameters import unconstrained, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)


def required_parset(sample_data, modifier_data):
return {
'paramset_type': unconstrained,
'paramset_type': 'unconstrained',
'n_parameters': 1,
'is_shared': True,
'is_scalar': True,
'inits': (1.0,),
'bounds': ((0, 10),),
'fixed': False,
Expand Down Expand Up @@ -40,19 +41,21 @@ def append(self, key, channel, sample, thismod, defined_samp):
moddata = self.collect(thismod, nom)
self._mega_mods[key][sample]['data']['mask'] += moddata['mask']
if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class normfactor_combined:
name = 'normfactor'
op_code = 'multiplication'

def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None):
self.batch_size = batch_size
self.name = 'normfactor'
self.op_code = 'multiplication'

keys = [f'{mtype}/{m}' for m, mtype in modifiers]
normfactor_mods = [m for m, _ in modifiers]
Expand Down
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/normsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

from pyhf import get_backend, events
from pyhf import interpolators
from pyhf.parameters import constrained_by_normal, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)


def required_parset(sample_data, modifier_data):
return {
'paramset_type': constrained_by_normal,
'paramset_type': 'constrained_by_normal',
'n_parameters': 1,
'is_shared': True,
'is_scalar': True,
'inits': (0.0,),
'bounds': ((-5.0, 5.0),),
'fixed': False,
Expand Down Expand Up @@ -52,20 +53,22 @@ def append(self, key, channel, sample, thismod, defined_samp):
self._mega_mods[key][sample]['data']['mask'] += moddata['mask']

if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class normsys_combined:
name = 'normsys'
op_code = 'multiplication'

def __init__(
self, modifiers, pdfconfig, builder_data, interpcode='code1', batch_size=None
):
self.name = 'normsys'
self.op_code = 'multiplication'

self.interpcode = interpcode
assert self.interpcode in ['code1', 'code4']
Expand Down
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/shapefactor.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging

from pyhf import get_backend, default_backend, events
from pyhf.parameters import unconstrained, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)


def required_parset(sample_data, modifier_data):
return {
'paramset_type': unconstrained,
'paramset_type': 'unconstrained',
'n_parameters': len(sample_data),
'is_shared': True,
'is_scalar': False,
'inits': (1.0,) * len(sample_data),
'bounds': ((0.0, 10.0),) * len(sample_data),
'fixed': False,
Expand Down Expand Up @@ -40,15 +41,19 @@ def append(self, key, channel, sample, thismod, defined_samp):
moddata = self.collect(thismod, nom)
self._mega_mods[key][sample]['data']['mask'] += moddata['mask']
if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class shapefactor_combined:
name = 'shapefactor'
op_code = 'multiplication'

def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None):
"""
Imagine a situation where we have 2 channels (SR, CR), 3 samples (sig1,
Expand Down Expand Up @@ -87,8 +92,6 @@ def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None):
and at that point can be used to compute the effect of shapefactor.
"""
self.name = 'shapefactor'
self.op_code = 'multiplication'

self.batch_size = batch_size
keys = [f'{mtype}/{m}' for m, mtype in modifiers]
Expand Down
15 changes: 9 additions & 6 deletions src/pyhf/modifiers/shapesys.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from pyhf import get_backend, default_backend, events
from pyhf.parameters import constrained_by_poisson, ParamViewer
from pyhf.parameters import ParamViewer

log = logging.getLogger(__name__)

Expand All @@ -14,9 +14,10 @@ def required_parset(sample_data, modifier_data):
]
n_parameters = sum(valid_bins)
return {
'paramset_type': constrained_by_poisson,
'paramset_type': 'constrained_by_poisson',
'n_parameters': n_parameters,
'is_shared': False,
'is_scalar': False,
'inits': (1.0,) * n_parameters,
'bounds': ((1e-10, 10.0),) * n_parameters,
'fixed': False,
Expand Down Expand Up @@ -53,18 +54,20 @@ def append(self, key, channel, sample, thismod, defined_samp):
self._mega_mods[key][sample]['data']['nom_data'] += moddata['nom_data']

if thismod:
self.required_parsets.setdefault(thismod['name'], []).append(
required_parset(defined_samp['data'], thismod['data'])
self.required_parsets.setdefault(
thismod['name'],
[required_parset(defined_samp['data'], thismod['data'])],
)

def finalize(self):
return self._mega_mods


class shapesys_combined:
name = 'shapesys'
op_code = 'multiplication'

def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None):
self.name = 'shapesys'
self.op_code = 'multiplication'
self.batch_size = batch_size

keys = [f'{mtype}/{m}' for m, mtype in modifiers]
Expand Down
Loading

0 comments on commit 14a3c68

Please sign in to comment.