aesoptparam 0.3.6__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.
Potentially problematic release.
This version of aesoptparam might be problematic. Click here for more details.
- aesoptparam/__init__.py +13 -0
- aesoptparam/example.py +110 -0
- aesoptparam/parameterized.py +417 -0
- aesoptparam/parameters.py +641 -0
- aesoptparam/serializer.py +94 -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.6.dist-info/METADATA +86 -0
- aesoptparam-0.3.6.dist-info/RECORD +21 -0
- aesoptparam-0.3.6.dist-info/WHEEL +5 -0
- aesoptparam-0.3.6.dist-info/licenses/LICENSE +21 -0
- aesoptparam-0.3.6.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,94 @@
|
|
|
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 (
|
|
33
|
+
skip_default_name
|
|
34
|
+
and (name == "name")
|
|
35
|
+
and (p.doc == "\n String identifier for this object.")
|
|
36
|
+
):
|
|
37
|
+
continue
|
|
38
|
+
if subset is not None and name not in subset:
|
|
39
|
+
continue
|
|
40
|
+
schema[name] = cls.param_schema(p.__class__.__name__, p, safe, subset)
|
|
41
|
+
if p.doc:
|
|
42
|
+
schema[name]["description"] = p.doc
|
|
43
|
+
return schema
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def param_schema(cls, ptype, p, safe=False, subset=None):
|
|
47
|
+
if ptype in cls.unserializable_parameter_types:
|
|
48
|
+
raise UnserializableException
|
|
49
|
+
allow_Ref = False
|
|
50
|
+
if isinstance(p, pm.String):
|
|
51
|
+
schema = {"type": "string"}
|
|
52
|
+
allow_Ref = True
|
|
53
|
+
elif isinstance(p, pm.Number):
|
|
54
|
+
# schema = getattr(cls, "number_schema")(p, safe=safe)
|
|
55
|
+
schema = cls.declare_numeric_bounds(
|
|
56
|
+
{"type": "number"}, p.bounds, p.inclusive_bounds
|
|
57
|
+
)
|
|
58
|
+
allow_Ref = True
|
|
59
|
+
elif isinstance(p, pm.Array):
|
|
60
|
+
schema = cls.array_schema(p, safe)
|
|
61
|
+
if hasattr(p, "dtype"):
|
|
62
|
+
if p.dtype is int:
|
|
63
|
+
schema["items"] = {"type": "integer"}
|
|
64
|
+
else:
|
|
65
|
+
schema["items"] = {"type": "number"}
|
|
66
|
+
if hasattr(p, "bounds"):
|
|
67
|
+
schema["items"] = cls.declare_numeric_bounds(
|
|
68
|
+
schema["items"], p.bounds, p.inclusive_bounds
|
|
69
|
+
)
|
|
70
|
+
allow_Ref = True
|
|
71
|
+
elif isinstance(p, pm.ClassSelector):
|
|
72
|
+
schema = getattr(cls, "classselector_schema")(p, safe=safe)
|
|
73
|
+
elif isinstance(p, pm.List):
|
|
74
|
+
schema = getattr(cls, "list_schema")(p, safe=safe)
|
|
75
|
+
else:
|
|
76
|
+
method = cls._get_method(ptype, "schema")
|
|
77
|
+
if method:
|
|
78
|
+
schema = method(p, safe=safe)
|
|
79
|
+
else:
|
|
80
|
+
schema = {"type": ptype.lower()}
|
|
81
|
+
if p.doc:
|
|
82
|
+
schema["description"] = p.doc
|
|
83
|
+
return JSONNullable_Ref_Function(schema, p.allow_None, allow_Ref)
|
|
84
|
+
|
|
85
|
+
@classmethod
|
|
86
|
+
def class__schema(cls, class_, safe=False):
|
|
87
|
+
if isinstance(class_, tuple):
|
|
88
|
+
return {"anyOf": [cls.class__schema(cls_) for cls_ in class_]}
|
|
89
|
+
elif class_ in cls.json_schema_literal_types:
|
|
90
|
+
return {"type": cls.json_schema_literal_types[class_]}
|
|
91
|
+
elif issubclass(class_, pm.Parameterized):
|
|
92
|
+
return {"type": "object", "properties": cls._schema(class_, safe)}
|
|
93
|
+
else:
|
|
94
|
+
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
|
+
}
|