-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_utils.py
142 lines (121 loc) · 6.2 KB
/
save_utils.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
import json
import h5py
from typing import List
from lume_model.utils import save_variables
from lume_model.variables import ImageInputVariable, ImageOutputVariable, ScalarInputVariable, ScalarOutputVariable
def build_variables_from_description_file(
description_file, input_extras=None, output_extras=None
):
"""
Utility function for creating variables from a JSON model description file.
Args:
description_file (str): Description filename.
input_extras (dict): Dictionary of extra attributes to add to input variables.
For example, default images.
output_extras (dict): Dictionary of extra attributes to add to output variables.
For example, default images. input_extras = {"default": np.load(image_file)}
"""
input_variables = {}
output_variables = {}
with open(description_file) as f:
data = json.load(f)
# Add extras to the description dicts
if input_extras:
for input_item in input_extras:
data["input_variables"][input_item].update(input_extras[input_item])
if output_extras:
for output_item in output_extras:
data["output_variables"][output_item].update(output_extras[output_item])
# parse input variables
for variable in data["input_variables"]:
variable_data = data["input_variables"][variable]
if variable_data["variable_type"] == "scalar":
input_variables[variable] = ScalarInputVariable(
name=variable,
default=variable_data["default"],
units=variable_data["units"],
value_range=variable_data["range"],
parent=variable_data.get("parent"),
)
elif variable_data["variable_type"] == "image":
if variable_data.get("x_min_variable"):
x_min = data["input_variables"][variable_data["x_min_variable"]]["default"]
elif variable_data.get("x_min"):
x_min = variable_data["x_min"]
else:
print("Image variable requires x_min or x_min_variable definition.")
raise Exception
if variable_data.get("y_min_variable"):
y_min = data["input_variables"][variable_data["y_min_variable"]]["default"]
elif variable_data.get("y_min"):
y_min = variable_data["y_min"]
else:
print("Image variable requires y_min or y_min_variable definition.")
raise Exception
if variable_data.get("x_max_variable"):
x_max = data["input_variables"][variable_data["x_max_variable"]]["default"]
elif variable_data.get("x_max"):
x_max = variable_data["x_max"]
else:
print("Image variable requires x_max or x_max_variable definition.")
raise Exception
if variable_data.get("y_max_variable"):
y_max = data["input_variables"][variable_data["y_max_variable"]]["default"]
elif variable_data.get("y_max"):
y_max = variable_data["y_max"]
else:
print("Image variable requires y_max or y_max_variable definition.")
raise Exception
input_variables[variable] = ImageInputVariable(
name=variable,
shape=tuple(variable_data["shape"]),
default=variable_data["default"],
axis_labels=variable_data["axis_labels"],
axis_units=variable_data.get("axis_units"),
value_range=variable_data.get("range"),
x_min_variable=variable_data.get("x_min_variable"),
x_max_variable=variable_data.get("x_max_variable"),
y_min_variable=variable_data.get("y_min_variable"),
y_max_variable=variable_data.get("y_max_variable"),
x_min = x_min,
x_max=x_max,
y_min=y_min,
y_max=y_max,
)
elif not variable_data["variable_type"]:
logger.exception("No variable type provided for %s", variable)
else:
logger.exception(
'%s variable type (%s) is not an allowed variable type. Variables may be "image" or "scalar"',
variable,
variable_data["variable_type"],
)
# parse output variables
for variable in data["output_variables"]:
variable_data = data["output_variables"][variable]
if variable_data["variable_type"] == "scalar":
output_variables[variable] = ScalarOutputVariable(
name=variable,
default=variable_data.get("default"), units=variable_data.get("units"),
parent=variable_data.get("parent"),
)
elif variable_data["variable_type"] == "image":
output_variables[variable] = ImageOutputVariable(
name=variable,
# shape=tuple(variable_data["shape"]),
default=variable_data.get("default"),
axis_labels=variable_data["axis_labels"],
axis_units=variable_data.get("axis_units"),
value_range=variable_data.get("range"),
x_min_variable=variable_data.get("x_min_variable"),
x_max_variable=variable_data.get("x_max_variable"),
y_min_variable=variable_data.get("y_min_variable"),
y_max_variable=variable_data.get("y_max_variable"),
)
return input_variables, output_variables
if __name__ == "__main__":
import numpy as np
var_file = "files/surrogate_model_variables_2.pickle"
from lume_model.utils import save_variables
input_variables, output_variables = build_variables_from_description_file("files/LCLS_CU_INJ_SC2IMSC_my_test_description.json")
save_variables(input_variables, output_variables, var_file)