aesoptparam 0.3.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aesoptparam/__init__.py +13 -0
- aesoptparam/example.py +110 -0
- aesoptparam/parameterized.py +415 -0
- aesoptparam/parameters.py +641 -0
- aesoptparam/serializer.py +90 -0
- aesoptparam/test/dummy_instance.json +30 -0
- aesoptparam/test/dummy_schema.json +752 -0
- aesoptparam/test/test_parameterized.py +574 -0
- aesoptparam/test/test_parameters.py +519 -0
- aesoptparam/test/test_units.py +369 -0
- aesoptparam/test/test_utils.py +147 -0
- aesoptparam/utils/__init__.py +63 -0
- aesoptparam/utils/html_repr.py +533 -0
- aesoptparam/utils/json_utils.py +127 -0
- aesoptparam/utils/unit_library.ini +233 -0
- aesoptparam/utils/units.py +1060 -0
- aesoptparam-0.3.7.dist-info/METADATA +86 -0
- aesoptparam-0.3.7.dist-info/RECORD +21 -0
- aesoptparam-0.3.7.dist-info/WHEEL +5 -0
- aesoptparam-0.3.7.dist-info/licenses/LICENSE +21 -0
- aesoptparam-0.3.7.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import textwrap
|
|
2
|
+
|
|
3
|
+
import param as pm
|
|
4
|
+
from param.serializer import JSONSerialization, UnserializableException
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def JSONNullable_Ref_Function(json_type, allow_None, allow_Ref):
|
|
8
|
+
"Express a JSON schema type as nullable or string with $ref or $function to easily support Parameters that allow_None"
|
|
9
|
+
if any([allow_None, allow_Ref]):
|
|
10
|
+
json_types = [json_type]
|
|
11
|
+
if allow_Ref:
|
|
12
|
+
json_types.append({"type": "string", "pattern": r"^(\$ref|\$function)"})
|
|
13
|
+
if allow_None:
|
|
14
|
+
json_types.append({"type": "null"})
|
|
15
|
+
return {"anyOf": json_types}
|
|
16
|
+
return json_type
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ASEOptJSONSerialization(JSONSerialization):
|
|
20
|
+
@classmethod
|
|
21
|
+
def schema(cls, pobj, safe=False, subset=None, skip_default_name=True):
|
|
22
|
+
return {
|
|
23
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
24
|
+
"type": "object",
|
|
25
|
+
"properties": cls._schema(pobj, safe, subset, skip_default_name),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def _schema(cls, pobj, safe=False, subset=None, skip_default_name=True):
|
|
30
|
+
schema = {}
|
|
31
|
+
for name, p in pobj.param.objects("existing").items():
|
|
32
|
+
if skip_default_name and (name == "name") and p.constant:
|
|
33
|
+
continue
|
|
34
|
+
if subset is not None and name not in subset:
|
|
35
|
+
continue
|
|
36
|
+
schema[name] = cls.param_schema(p.__class__.__name__, p, safe, subset)
|
|
37
|
+
if p.doc:
|
|
38
|
+
schema[name]["description"] = p.doc
|
|
39
|
+
return schema
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def param_schema(cls, ptype, p, safe=False, subset=None):
|
|
43
|
+
if ptype in cls.unserializable_parameter_types:
|
|
44
|
+
raise UnserializableException
|
|
45
|
+
allow_Ref = False
|
|
46
|
+
if isinstance(p, pm.String):
|
|
47
|
+
schema = {"type": "string"}
|
|
48
|
+
allow_Ref = True
|
|
49
|
+
elif isinstance(p, pm.Number):
|
|
50
|
+
# schema = getattr(cls, "number_schema")(p, safe=safe)
|
|
51
|
+
schema = cls.declare_numeric_bounds(
|
|
52
|
+
{"type": "number"}, p.bounds, p.inclusive_bounds
|
|
53
|
+
)
|
|
54
|
+
allow_Ref = True
|
|
55
|
+
elif isinstance(p, pm.Array):
|
|
56
|
+
schema = cls.array_schema(p, safe)
|
|
57
|
+
if hasattr(p, "dtype"):
|
|
58
|
+
if p.dtype is int:
|
|
59
|
+
schema["items"] = {"type": "integer"}
|
|
60
|
+
else:
|
|
61
|
+
schema["items"] = {"type": "number"}
|
|
62
|
+
if hasattr(p, "bounds"):
|
|
63
|
+
schema["items"] = cls.declare_numeric_bounds(
|
|
64
|
+
schema["items"], p.bounds, p.inclusive_bounds
|
|
65
|
+
)
|
|
66
|
+
allow_Ref = True
|
|
67
|
+
elif isinstance(p, pm.ClassSelector):
|
|
68
|
+
schema = getattr(cls, "classselector_schema")(p, safe=safe)
|
|
69
|
+
elif isinstance(p, pm.List):
|
|
70
|
+
schema = getattr(cls, "list_schema")(p, safe=safe)
|
|
71
|
+
else:
|
|
72
|
+
method = cls._get_method(ptype, "schema")
|
|
73
|
+
if method:
|
|
74
|
+
schema = method(p, safe=safe)
|
|
75
|
+
else:
|
|
76
|
+
schema = {"type": ptype.lower()}
|
|
77
|
+
if p.doc:
|
|
78
|
+
schema["description"] = p.doc
|
|
79
|
+
return JSONNullable_Ref_Function(schema, p.allow_None, allow_Ref)
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def class__schema(cls, class_, safe=False):
|
|
83
|
+
if isinstance(class_, tuple):
|
|
84
|
+
return {"anyOf": [cls.class__schema(cls_) for cls_ in class_]}
|
|
85
|
+
elif class_ in cls.json_schema_literal_types:
|
|
86
|
+
return {"type": cls.json_schema_literal_types[class_]}
|
|
87
|
+
elif issubclass(class_, pm.Parameterized):
|
|
88
|
+
return {"type": "object", "properties": cls._schema(class_, safe)}
|
|
89
|
+
else:
|
|
90
|
+
return {"type": "object"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./dummy_schema.json",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"name": "New main name",
|
|
5
|
+
"a": 0.2,
|
|
6
|
+
"b": "$ref .a",
|
|
7
|
+
"c": "$ref .sub_list[1].a",
|
|
8
|
+
"d": [0.0, 1.0, 2.0, 3.2],
|
|
9
|
+
"e": [1, 2, 10],
|
|
10
|
+
"f": "Test",
|
|
11
|
+
"g": "$ref .h",
|
|
12
|
+
"h": "$function lambda self: self.f + '3'",
|
|
13
|
+
"sub1": {
|
|
14
|
+
"a": 0.1,
|
|
15
|
+
"b": [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
|
|
16
|
+
"c": "$ref .b",
|
|
17
|
+
"e": "$function lambda self: np.full_like(self.b, self['..a'])",
|
|
18
|
+
"f": [0, 1, 2, 3, 4]
|
|
19
|
+
},
|
|
20
|
+
"sub2": {
|
|
21
|
+
"a": 0.3
|
|
22
|
+
},
|
|
23
|
+
"sub_list": [
|
|
24
|
+
{"a": 0.1},
|
|
25
|
+
{"a": 0.3}
|
|
26
|
+
],
|
|
27
|
+
"sub_list2": [
|
|
28
|
+
{"a": 0.1}
|
|
29
|
+
]
|
|
30
|
+
}
|