-
Notifications
You must be signed in to change notification settings - Fork 81
/
spec.py
400 lines (335 loc) · 11.9 KB
/
spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""The pyhf spec CLI subcommand."""
import logging
import click
import json
from ..workspace import Workspace
from .. import modifiers
from .. import utils
log = logging.getLogger(__name__)
@click.group(name='spec')
def cli():
"""Spec CLI group."""
@cli.command()
@click.argument('workspace', default='-')
@click.option(
'--output-file',
help='The location of the output json file. If not specified, prints to screen.',
default=None,
)
@click.option('--measurement', default=None)
def inspect(workspace, output_file, measurement):
"""
Inspect a pyhf JSON document.
Example:
.. code-block:: shell
$ curl -sL https://raw.githubusercontent.com/scikit-hep/pyhf/master/docs/examples/json/2-bin_1-channel.json | pyhf inspect
Summary
------------------
channels 1
samples 2
parameters 2
modifiers 2
channels nbins
---------- -----
singlechannel 2
samples
----------
background
signal
parameters constraint modifiers
---------- ---------- ----------
mu unconstrained normfactor
uncorr_bkguncrt constrained_by_poisson shapesys
measurement poi parameters
---------- ---------- ----------
(*) Measurement mu (none)
"""
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
ws = Workspace(spec)
default_measurement = ws.get_measurement()
result = {}
result['samples'] = ws.samples
result['channels'] = [
(channel, ws.channel_nbins[channel]) for channel in ws.channels
]
result['modifiers'] = dict(ws.modifiers)
result['parameters'] = sorted(
(
parname,
modifiers.registry[result['modifiers'][parname]]
.required_parset([], [])['paramset_type']
.__name__,
)
for parname in ws.parameters
)
result['systematics'] = [
(
parameter[0],
parameter[1],
[modifier[1] for modifier in ws.modifiers if modifier[0] == parameter[0]],
)
for parameter in result['parameters']
]
result['measurements'] = [
(m['name'], m['config']['poi'], [p['name'] for p in m['config']['parameters']])
for m in ws.get('measurements')
]
maxlen_channels = max(map(len, ws.channels))
maxlen_samples = max(map(len, ws.samples))
maxlen_parameters = max(map(len, ws.parameters))
maxlen_measurements = max(map(lambda x: len(x[0]), result['measurements']))
maxlen = max(
[maxlen_channels, maxlen_samples, maxlen_parameters, maxlen_measurements]
)
# summary
fmtStr = '{{0: >{0:d}s}} {{1:s}}'.format(maxlen + len('Summary'))
click.echo(fmtStr.format(' Summary ', ''))
click.echo(fmtStr.format('-' * 18, ''))
fmtStr = '{{0: >{0:d}s}} {{1:s}}'.format(maxlen)
for key in ['channels', 'samples', 'parameters', 'modifiers']:
click.echo(fmtStr.format(key, str(len(result[key]))))
click.echo()
fmtStr = '{{0: >{0:d}s}} {{1: ^5s}}'.format(maxlen)
click.echo(fmtStr.format('channels', 'nbins'))
click.echo(fmtStr.format('-' * 10, '-' * 5))
for channel, nbins in result['channels']:
click.echo(fmtStr.format(channel, str(nbins)))
click.echo()
fmtStr = '{{0: >{0:d}s}}'.format(maxlen)
click.echo(fmtStr.format('samples'))
click.echo(fmtStr.format('-' * 10))
for sample in result['samples']:
click.echo(fmtStr.format(sample))
click.echo()
# print parameters, constraints, modifiers
fmtStr = '{{0: >{0:d}s}} {{1: <22s}} {{2:s}}'.format(maxlen)
click.echo(fmtStr.format('parameters', 'constraint', 'modifiers'))
click.echo(fmtStr.format('-' * 10, '-' * 10, '-' * 10))
for parname, constraint, modtypes in result['systematics']:
click.echo(fmtStr.format(parname, constraint, ','.join(sorted(set(modtypes)))))
click.echo()
fmtStr = '{{0: >{0:d}s}} {{1: ^22s}} {{2:s}}'.format(maxlen)
click.echo(fmtStr.format('measurement', 'poi', 'parameters'))
click.echo(fmtStr.format('-' * 10, '-' * 10, '-' * 10))
for measurement_name, measurement_poi, measurement_parameters in result[
'measurements'
]:
click.echo(
fmtStr.format(
('(*) ' if measurement_name == default_measurement['name'] else '')
+ measurement_name,
measurement_poi,
','.join(measurement_parameters)
if measurement_parameters
else '(none)',
)
)
click.echo()
if output_file:
with open(output_file, 'w+') as out_file:
json.dump(result, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
@cli.command()
@click.argument('workspace', default='-')
@click.option(
'--output-file',
help='The location of the output json file. If not specified, prints to screen.',
default=None,
)
@click.option('-c', '--channel', default=[], multiple=True, metavar='<CHANNEL>...')
@click.option('-s', '--sample', default=[], multiple=True, metavar='<SAMPLE>...')
@click.option('-m', '--modifier', default=[], multiple=True, metavar='<MODIFIER>...')
@click.option(
'-t',
'--modifier-type',
default=[],
multiple=True,
type=click.Choice(modifiers.uncombined.keys()),
)
@click.option('--measurement', default=[], multiple=True, metavar='<MEASUREMENT>...')
def prune(
workspace, output_file, channel, sample, modifier, modifier_type, measurement
):
"""
Prune components from the workspace.
See :func:`pyhf.workspace.Workspace.prune` for more information.
"""
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
ws = Workspace(spec)
pruned_ws = ws.prune(
channels=channel,
samples=sample,
modifiers=modifier,
modifier_types=modifier_type,
measurements=measurement,
)
if output_file is None:
click.echo(json.dumps(pruned_ws, indent=4, sort_keys=True))
else:
with open(output_file, 'w+') as out_file:
json.dump(pruned_ws, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
@cli.command()
@click.argument('workspace', default='-')
@click.option(
'--output-file',
help='The location of the output json file. If not specified, prints to screen.',
default=None,
)
@click.option(
'-c',
'--channel',
default=[],
multiple=True,
type=click.Tuple([str, str]),
metavar='<PATTERN> <REPLACE>...',
)
@click.option(
'-s',
'--sample',
default=[],
multiple=True,
type=click.Tuple([str, str]),
metavar='<PATTERN> <REPLACE>...',
)
@click.option(
'-m',
'--modifier',
default=[],
multiple=True,
type=click.Tuple([str, str]),
metavar='<PATTERN> <REPLACE>...',
)
@click.option(
'--measurement',
default=[],
multiple=True,
type=click.Tuple([str, str]),
metavar='<PATTERN> <REPLACE>...',
)
def rename(workspace, output_file, channel, sample, modifier, measurement):
"""
Rename components of the workspace.
See :func:`pyhf.workspace.Workspace.rename` for more information.
"""
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
ws = Workspace(spec)
renamed_ws = ws.rename(
channels=dict(channel),
samples=dict(sample),
modifiers=dict(modifier),
measurements=dict(measurement),
)
if output_file is None:
click.echo(json.dumps(renamed_ws, indent=4, sort_keys=True))
else:
with open(output_file, 'w+') as out_file:
json.dump(renamed_ws, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
@cli.command()
@click.argument('workspace-one', default='-')
@click.argument('workspace-two', default='-')
@click.option(
'-j',
'--join',
default='none',
type=click.Choice(Workspace.valid_joins),
help='The join operation to apply when combining the two workspaces.',
)
@click.option(
'--output-file',
help='The location of the output json file. If not specified, prints to screen.',
default=None,
)
def combine(workspace_one, workspace_two, join, output_file):
"""
Combine two workspaces into a single workspace.
See :func:`pyhf.workspace.Workspace.combine` for more information.
"""
with click.open_file(workspace_one, 'r') as specstream:
spec_one = json.load(specstream)
with click.open_file(workspace_two, 'r') as specstream:
spec_two = json.load(specstream)
ws_one = Workspace(spec_one)
ws_two = Workspace(spec_two)
combined_ws = Workspace.combine(ws_one, ws_two, join=join)
if output_file is None:
click.echo(json.dumps(combined_ws, indent=4, sort_keys=True))
else:
with open(output_file, 'w+') as out_file:
json.dump(combined_ws, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
@cli.command()
@click.argument('workspace', default='-')
@click.option(
'-a',
'--algorithm',
default=['sha256'],
multiple=True,
help='The hashing algorithm used to compute the workspace digest.',
)
@click.option(
'-j/-p',
'--json/--plaintext',
'output_json',
help='Output the hash values as a JSON dictionary or plaintext strings',
)
def digest(workspace, algorithm, output_json):
"""
Use hashing algorithm to calculate the workspace digest.
Returns:
digests (:obj:`dict`): A mapping of the hashing algorithms used to the computed digest for the workspace.
Example:
.. code-block:: shell
$ curl -sL https://raw.githubusercontent.com/scikit-hep/pyhf/master/docs/examples/json/2-bin_1-channel.json | pyhf digest
sha256:dad8822af55205d60152cbe4303929042dbd9d4839012e055e7c6b6459d68d73
"""
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
workspace = Workspace(spec)
digests = {
hash_alg: utils.digest(workspace, algorithm=hash_alg) for hash_alg in algorithm
}
if output_json:
output = json.dumps(
digests,
indent=4,
sort_keys=True,
)
else:
output = '\n'.join(
f"{hash_alg}:{digest}" for hash_alg, digest in digests.items()
)
click.echo(output)
@cli.command()
@click.argument('workspace', default='-')
@click.option(
'--output-file',
help='The location of the output json file. If not specified, prints to screen.',
default=None,
)
def sort(workspace, output_file):
"""
Sort the workspace.
See :func:`pyhf.workspace.Workspace.sorted` for more information.
Example:
.. code-block:: shell
$ curl -sL https://raw.githubusercontent.com/scikit-hep/pyhf/master/docs/examples/json/2-bin_1-channel.json | pyhf sort | jq '.' | md5
8be5186ec249d2704e14dd29ef05ffb0
.. code-block:: shell
$ curl -sL https://raw.githubusercontent.com/scikit-hep/pyhf/master/docs/examples/json/2-bin_1-channel.json | jq -S '.channels|=sort_by(.name)|.channels[].samples|=sort_by(.name)|.channels[].samples[].modifiers|=sort_by(.name,.type)|.observations|=sort_by(.name)' | md5
8be5186ec249d2704e14dd29ef05ffb0
"""
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
workspace = Workspace(spec)
sorted_ws = Workspace.sorted(workspace)
if output_file is None:
click.echo(json.dumps(sorted_ws, indent=4, sort_keys=True))
else:
with open(output_file, 'w+') as out_file:
json.dump(sorted_ws, out_file, indent=4, sort_keys=True)
log.debug(f"Written to {output_file}")