diff --git a/src/pyhf/experimental/modifiers.py b/src/pyhf/experimental/modifiers.py index 42793575f7..167a446d25 100644 --- a/src/pyhf/experimental/modifiers.py +++ b/src/pyhf/experimental/modifiers.py @@ -19,13 +19,13 @@ def _allocate_new_param( p: dict[str, Sequence[float]] ) -> dict[str, str | bool | int | Sequence[float]]: return { - 'paramset_type': 'unconstrained', - 'n_parameters': 1, - 'is_shared': True, - 'inits': p['inits'], - 'bounds': p['bounds'], - 'is_scalar': True, - 'fixed': False, + "paramset_type": "unconstrained", + "n_parameters": 1, + "is_shared": True, + "inits": p["inits"], + "bounds": p["bounds"], + "is_scalar": True, + "fixed": False, } @@ -45,30 +45,30 @@ class _builder(BaseBuilder): is_shared = False def __init__(self, config): - self.builder_data = {'funcs': {}} + self.builder_data = {"funcs": {}} self.config = config def collect(self, thismod, nom): maskval = True if thismod else False mask = [maskval] * len(nom) - return {'mask': mask} + return {"mask": mask} def append(self, key, channel, sample, thismod, defined_samp): self.builder_data.setdefault(key, {}).setdefault(sample, {}).setdefault( - 'data', {'mask': []} + "data", {"mask": []} ) nom = ( - defined_samp['data'] + defined_samp["data"] if defined_samp else [0.0] * self.config.channel_nbins[channel] ) moddata = self.collect(thismod, nom) - self.builder_data[key][sample]['data']['mask'] += moddata['mask'] + self.builder_data[key][sample]["data"]["mask"] += moddata["mask"] if thismod: - if thismod['name'] != funcname: + if thismod["name"] != funcname: print(thismod) - self.builder_data['funcs'].setdefault( - thismod['name'], thismod['data']['expr'] + self.builder_data["funcs"].setdefault( + thismod["name"], thismod["data"]["expr"] ) self.required_parsets = { k: [_allocate_new_param(v)] for k, v in newparams.items() @@ -85,14 +85,14 @@ def make_applier( ) -> BaseApplier: class _applier(BaseApplier): name = funcname - op_code = 'multiplication' + op_code = "multiplication" def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None): - self.funcs = [make_func(v, deps) for v in builder_data['funcs'].values()] + self.funcs = [make_func(v, deps) for v in builder_data["funcs"].values()] self.batch_size = batch_size pars_for_applier = deps - _modnames = [f'{mtype}/{m}' for m, mtype in modifiers] + _modnames = [f"{mtype}/{m}" for m, mtype in modifiers] parfield_shape = ( (self.batch_size, pdfconfig.npars) @@ -103,11 +103,11 @@ def __init__(self, modifiers, pdfconfig, builder_data, batch_size=None): parfield_shape, pdfconfig.par_map, pars_for_applier ) self._custommod_mask = [ - [[builder_data[modname][s]['data']['mask']] for s in pdfconfig.samples] + [[builder_data[modname][s]["data"]["mask"]] for s in pdfconfig.samples] for modname in _modnames ] self._precompute() - events.subscribe('tensorlib_changed')(self._precompute) + events.subscribe("tensorlib_changed")(self._precompute) def _precompute(self): tensorlib, _ = get_backend() @@ -132,15 +132,15 @@ def apply(self, pars): tensorlib, _ = get_backend() if self.batch_size is None: deps = self.param_viewer.get(pars) - print('deps', deps.shape) + print("deps", deps.shape) results = tensorlib.astensor([f(deps) for f in self.funcs]) - results = tensorlib.einsum('msab,m->msab', self.custommod_mask, results) + results = tensorlib.einsum("msab,m->msab", self.custommod_mask, results) else: deps = self.param_viewer.get(pars) - print('deps', deps.shape) + print("deps", deps.shape) results = tensorlib.astensor([f(deps) for f in self.funcs]) results = tensorlib.einsum( - 'msab,ma->msab', self.custommod_mask, results + "msab,ma->msab", self.custommod_mask, results ) results = tensorlib.where( self.custommod_mask_bool, results, self.custommod_default diff --git a/tests/test_experimental.py b/tests/test_experimental.py index 32ca49861c..3f83e52aa0 100644 --- a/tests/test_experimental.py +++ b/tests/test_experimental.py @@ -6,38 +6,38 @@ def test_add_custom_modifier(backend): tensorlib, _ = backend new_params = { - 'm1': {'inits': (1.0,), 'bounds': ((-5.0, 5.0),)}, - 'm2': {'inits': (1.0,), 'bounds': ((-5.0, 5.0),)}, + "m1": {"inits": (1.0,), "bounds": ((-5.0, 5.0),)}, + "m2": {"inits": (1.0,), "bounds": ((-5.0, 5.0),)}, } expanded_pyhf = pyhf.experimental.modifiers.add_custom_modifier( - 'customfunc', ['m1', 'm2'], new_params + "customfunc", ["m1", "m2"], new_params ) model = pyhf.Model( { - 'channels': [ + "channels": [ { - 'name': 'singlechannel', - 'samples': [ + "name": "singlechannel", + "samples": [ { - 'name': 'signal', - 'data': [10] * 20, - 'modifiers': [ + "name": "signal", + "data": [10] * 20, + "modifiers": [ { - 'name': 'f2', - 'type': 'customfunc', - 'data': {'expr': 'm1'}, + "name": "f2", + "type": "customfunc", + "data": {"expr": "m1"}, }, ], }, { - 'name': 'background', - 'data': [100] * 20, - 'modifiers': [ + "name": "background", + "data": [100] * 20, + "modifiers": [ { - 'name': 'f1', - 'type': 'customfunc', - 'data': {'expr': 'm1+(m2**2)'}, + "name": "f1", + "type": "customfunc", + "data": {"expr": "m1+(m2**2)"}, }, ], }, @@ -46,7 +46,7 @@ def test_add_custom_modifier(backend): ] }, modifier_set=expanded_pyhf, - poi_name='m1', + poi_name="m1", validate=False, batch_size=1, )