forked from cms-nanoAOD/correctionlib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
schemav1.py
133 lines (103 loc) · 3.8 KB
/
schemav1.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
import sys; assert sys.version_info>=(3,8),"Python version must be newer than 3.8, currently %s.%s"%(sys.version_info[:2])
from typing import (
List,
Optional,
Union,
ForwardRef,
Literal,
)
from pydantic import BaseModel, validator
def RedValueError(*args): return ValueError("\033[91m"+''.join(args)+"\033[0m")
VERSION = 1
class Model(BaseModel):
class Config:
extra = "forbid"
class Variable(Model):
name: str
type: Literal["string", "int", "real"]
"Implicitly 64 bit integer and double-precision floating point?"
description: Optional[str]
class Formula(Model):
expression: str
parser: Literal["TFormula", "numexpr"]
parameters: List[int]
"Index to Correction.inputs[]"
Value = Union[Formula, float]
Binning = ForwardRef("Binning")
MultiBinning = ForwardRef("MultiBinning")
Category = ForwardRef("Category")
Content = Union[Binning, MultiBinning, Category, Value]
class Binning(Model):
nodetype: Literal["binning"]
input: str
edges: List[float]
"Edges of the binning, where edges[i] <= x < edges[i+1] => f(x, ...) = content[i](...)"
content: List[Content]
@validator('edges')
def validate_edges(cls,edges,values):
for i, lowedge in enumerate(edges[:-1]):
if lowedge>=edges[i+1]:
raise RedValueError("bin edges not in increasing order: %s"%(edges))
return edges
@validator('content')
def match_content_bins(cls,content,values):
if 'edges' in values:
nbins = len(values['edges'])-1
if len(content)!=nbins:
raise RedValueError("number of content elements (%s) must match number of bins (%s)"%(len(content),nbins))
return content
class MultiBinning(Model):
"""N-dimensional rectangular binning"""
nodetype: Literal["multibinning"]
inputs: List[str]
edges: List[List[float]]
"Bin edges for each input"
content: List[Content]
@validator('edges')
def match_bins_input(cls,edges,values):
for edges_ in edges:
for i, lowedge in enumerate(edges_[:-1]):
if lowedge>=edges_[i+1]:
raise RedValueError("bin edges not in increasing order: %s"%(edges_))
if 'inputs' in values and len(edges)!=len(values['inputs']):
raise RedValueError("number of axes (%s) must number of inputs (%s: %s)"%(len(edges),len(values['inputs']),values['inputs']))
return edges
@validator('content')
def match_content_bins(cls,content,values):
if 'edges' in values:
nbins = 1
for e in values['edges']: nbins *= len(e)-1
if len(content)!=nbins:
raise RedValueError("number of content elements (%s) must number of bins (%s)"%(len(content),nbins))
return content
class Category(Model):
nodetype: Literal["category"]
input: str
keys: List[Union[str,int]]
content: List[Content]
default: Content = None
@validator('content')
def match_content_key(cls,content,values):
if 'keys' in values and len(content)!=len(values['keys']):
raise RedValueError("number of content elements (%s) must number of keys (%s: %s)"%(len(content),len(values['keys']),values['keys']))
return content
Binning.update_forward_refs()
MultiBinning.update_forward_refs()
Category.update_forward_refs()
class Correction(Model):
name: str
"A useful name"
description: Optional[str]
"Detailed description of the correction"
version: int
"Version"
inputs: List[Variable]
output: Variable
data: Content
class CorrectionSet(Model):
schema_version: Literal[VERSION]
"Schema version"
corrections: List[Correction]
if __name__ == "__main__":
with open(f"data/schemav{VERSION}.json", "w") as fout:
fout.write(CorrectionSet.schema_json(indent=4))