-
Notifications
You must be signed in to change notification settings - Fork 91
feat: HiFa v1.1.0 schema development #1978
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
base: main
Are you sure you want to change the base?
Changes from all commits
4934ed9
2569988
bb7b416
3dd5766
81c5816
9a67f9f
c9da4e3
bc8de57
475c08e
a4358c6
8d67cc4
f30da27
a2f4899
41a0b23
58c79f0
7b60642
58fe1b7
9b8ea83
691a3b5
26a5f32
b75ab60
b15850c
b91ee76
c89eff0
8f10cd8
b6edb60
f854c23
4f84a65
06f13a9
4563ae3
3bb4d0e
fcfab82
862aabb
6249ffa
f05cb95
531e0ba
bc7fffa
e66d252
f1df51f
372c8d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """The pyhf upgrade CLI subcommand.""" | ||
| import logging | ||
|
|
||
| import click | ||
| import json | ||
|
|
||
| from pyhf.schema.upgrader import upgrade | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @click.group(name='upgrade') | ||
| def cli(): | ||
| """Operations for upgrading specifications.""" | ||
|
|
||
|
|
||
| @cli.command() | ||
| @click.argument('workspace', default='-') | ||
| @click.option( | ||
| '--version', | ||
| help='The version to upgrade to', | ||
| default=None, | ||
| ) | ||
| @click.option( | ||
| '--output-file', | ||
| help='The location of the output json file. If not specified, prints to screen.', | ||
| default=None, | ||
| ) | ||
| def workspace(workspace, version, output_file): | ||
| """ | ||
| Upgrade a HistFactory JSON workspace. | ||
| """ | ||
| with click.open_file(workspace, 'r', encoding="utf-8") as specstream: | ||
| spec = json.load(specstream) | ||
|
|
||
| ws = upgrade(to_version=version).workspace(spec) | ||
|
|
||
| if output_file is None: | ||
| click.echo(json.dumps(ws, indent=4, sort_keys=True)) | ||
| else: | ||
| with open(output_file, 'w+', encoding="utf-8") as out_file: | ||
| json.dump(ws, out_file, indent=4, sort_keys=True) | ||
| log.debug(f"Written to {output_file:s}") | ||
|
|
||
|
|
||
| @cli.command() | ||
| @click.argument('patchset', default='-') | ||
| @click.option( | ||
| '--version', | ||
| help='The version to upgrade to', | ||
| default=None, | ||
| ) | ||
| @click.option( | ||
| '--output-file', | ||
| help='The location of the output json file. If not specified, prints to screen.', | ||
| default=None, | ||
| ) | ||
| def patchset(patchset, version, output_file): | ||
| """ | ||
| Upgrade a pyhf JSON PatchSet. | ||
| """ | ||
| with click.open_file(patchset, 'r', encoding="utf-8") as specstream: | ||
| spec = json.load(specstream) | ||
|
|
||
| ps = upgrade(to_version=version).patchset(spec) | ||
|
|
||
| if output_file is None: | ||
| click.echo(json.dumps(ps, indent=4, sort_keys=True)) | ||
| else: | ||
| with open(output_file, 'w+', encoding="utf-8") as out_file: | ||
| json.dump(ps, out_file, indent=4, sort_keys=True) | ||
| log.debug(f"Written to {output_file:s}") |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from pyhf.typing import Workspace, PatchSet, SchemaVersion, UpgradeProtocol | ||||||||||||||||||||||
| import copy | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class Upgrade_1_0_1: | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Used for testing functionality of upgrade. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| version: SchemaVersion = '1.0.1' | ||||||||||||||||||||||
|
Comment on lines
+7
to
+12
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe I'm missing something about what the purpose of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need an
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kratsg Ah okay, so the real reason is that we can't deterministically know what version we're upgrading from in advance? Do we have a plan for how to handle upgrading multiple versions in one go? e.g. Let's say we have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have ideas on how to do this with python's |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||
| def workspace(cls, spec: Workspace) -> Workspace: | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Upgrade the provided workspace specification. | ||||||||||||||||||||||
| Args: | ||||||||||||||||||||||
| spec (dict): The specification to validate. | ||||||||||||||||||||||
| schema_name (str): The name of the schema to upgrade. | ||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||
| upgraded_spec (dict): Upgraded workspace specification. | ||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||
| pyhf.exceptions.InvalidSpecification: the specification is invalid | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| new_spec = copy.deepcopy(spec) | ||||||||||||||||||||||
| if spec['version'] == '1.0.0': | ||||||||||||||||||||||
| new_spec['version'] = cls.version | ||||||||||||||||||||||
| return new_spec | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||
| def patchset(cls, spec: PatchSet) -> PatchSet: | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Upgrade the provided patchset specification. | ||||||||||||||||||||||
| Args: | ||||||||||||||||||||||
| spec (dict): The specification to validate. | ||||||||||||||||||||||
| schema_name (str): The name of the schema to upgrade. | ||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||
| upgraded_spec (dict): Upgraded patchset specification. | ||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||
| pyhf.exceptions.InvalidSpecification: the specification is invalid | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| new_spec = copy.deepcopy(spec) | ||||||||||||||||||||||
| if spec['version'] == '1.0.0': | ||||||||||||||||||||||
| new_spec['version'] = cls.version | ||||||||||||||||||||||
| return new_spec | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def upgrade(*, to_version: SchemaVersion | None = None) -> type[UpgradeProtocol]: | ||||||||||||||||||||||
| if to_version is None or to_version == '1.0.1': | ||||||||||||||||||||||
| return Upgrade_1_0_1 | ||||||||||||||||||||||
|
Comment on lines
+57
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Follow up to the above. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| raise ValueError(f'{to_version} is not a valid version to upgrade to.') | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.