Skip to content

Commit 362e47c

Browse files
kratsglukasheinrich
authored andcommitted
Add additional pdf information to aid with refactoring (scikit-hep#305)
* add additional pdf information in preparation for megachannel
1 parent 4faae86 commit 362e47c

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

.ackrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--ignore-directory=docs/_generated
2+
--ignore-directory=htmlcov

pyhf/pdf.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,47 @@
1010

1111

1212
class _ModelConfig(object):
13-
@classmethod
14-
def from_spec(cls,spec,poiname = 'mu', qualify_names = False):
15-
channels = []
16-
samples = []
17-
modifiers = []
13+
def __init__(self, spec, poiname = 'mu', qualify_names = False):
14+
self.poi_index = None
15+
self.par_map = {}
16+
self.par_order = []
17+
self.auxdata = []
18+
self.auxdata_order = []
19+
self.next_index = 0
20+
21+
self.channels = []
22+
self.samples = []
23+
self.parameters = []
24+
self.modifiers = []
25+
self.channel_nbins = {}
1826
# hacky, need to keep track in which order we added the constraints
1927
# so that we can generate correctly-ordered data
20-
instance = cls()
2128
for channel in spec['channels']:
22-
channels.append(channel['name'])
29+
self.channels.append(channel['name'])
30+
self.channel_nbins[channel['name']] = len(channel['samples'][0]['data'])
2331
for sample in channel['samples']:
24-
samples.append(sample['name'])
32+
self.samples.append(sample['name'])
2533
# we need to bookkeep a list of modifiers by type so that we
2634
# can loop over them on a type-by-type basis
2735
# types like histosys, normsys, etc...
2836
sample['modifiers_by_type'] = {}
2937
for modifier_def in sample['modifiers']:
38+
self.parameters.append(modifier_def['name'])
3039
if qualify_names:
3140
fullname = '{}/{}'.format(modifier_def['type'],modifier_def['name'])
3241
if modifier_def['name'] == poiname:
3342
poiname = fullname
3443
modifier_def['name'] = fullname
35-
modifier = instance.add_or_get_modifier(channel, sample, modifier_def)
44+
modifier = self.add_or_get_modifier(channel, sample, modifier_def)
3645
modifier.add_sample(channel, sample, modifier_def)
37-
modifiers.append(modifier_def['name'])
46+
self.modifiers.append(modifier_def['name'])
3847
sample['modifiers_by_type'].setdefault(modifier_def['type'],[]).append(modifier_def['name'])
39-
instance.channels = list(set(channels))
40-
instance.samples = list(set(samples))
41-
instance.modifiers = list(set(modifiers))
42-
instance.set_poi(poiname)
43-
return instance
44-
45-
def __init__(self):
46-
# set up all other bookkeeping variables
47-
self.poi_index = None
48-
self.par_map = {}
49-
self.par_order = []
50-
self.auxdata = []
51-
self.auxdata_order = []
52-
self.next_index = 0
48+
self.channels = list(set(self.channels))
49+
self.samples = list(set(self.samples))
50+
self.parameters = list(set(self.parameters))
51+
self.modifiers = list(set(self.modifiers))
52+
self.channel_nbins = self.channel_nbins
53+
self.set_poi(poiname)
5354

5455
def suggested_init(self):
5556
init = []
@@ -135,13 +136,12 @@ def add_or_get_modifier(self, channel, sample, modifier_def):
135136
class Model(object):
136137
def __init__(self, spec, **config_kwargs):
137138
self.spec = copy.deepcopy(spec) #may get modified by config
138-
self.schema = config_kwargs.get('schema', utils.get_default_schema())
139+
self.schema = config_kwargs.pop('schema', utils.get_default_schema())
139140
# run jsonschema validation of input specification against the (provided) schema
140141
log.info("Validating spec against schema: {0:s}".format(self.schema))
141142
utils.validate(self.spec, self.schema)
142143
# build up our representation of the specification
143-
self.config = _ModelConfig.from_spec(self.spec,**config_kwargs)
144-
144+
self.config = _ModelConfig(self.spec, **config_kwargs)
145145

146146
for m in self.config.modifiers:
147147
mod = self.config.modifier(m)

tests/test_pdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def test_invalid_modifier():
257257
]
258258
}
259259
with pytest.raises(pyhf.exceptions.InvalidModifier):
260-
pyhf.pdf._ModelConfig.from_spec(spec)
260+
pyhf.pdf._ModelConfig(spec)
261261

262262

263263
def test_invalid_modifier_name_resuse():

0 commit comments

Comments
 (0)