@@ -43,6 +43,7 @@ def lambda_handler(event, context):
43
43
jobTimestamp = event ['jobTimestamp' ]
44
44
param_dict = event ['params' ]
45
45
samples_int = event ['samples' ]
46
+ sim_type = event ['sim_type' ]
46
47
47
48
model = event ['model' ].lower ()
48
49
if model == 'cornstover' :
@@ -56,8 +57,6 @@ def lambda_handler(event, context):
56
57
# print("invalid model " + model)
57
58
all_parameters = {i .name : i for i in model .parameters }
58
59
print (all_parameters )
59
- # Rerun model at baseline to reset cache
60
- baseline_metrics = model .metrics_at_baseline ()
61
60
62
61
63
62
parameters = []
@@ -71,45 +70,78 @@ def lambda_handler(event, context):
71
70
72
71
parameters .append (parameter )
73
72
values = item ['values' ]
74
- distribution = item ['distribution' ].capitalize ()
75
- if distribution == 'Triangular' :
76
- lower = values ['lower' ]
77
- midpoint = values ['midpoint' ]
78
- upper = values ['upper' ]
79
- parameter .distribution = shape .Triangle (lower = lower , midpoint = midpoint , upper = upper )
80
- elif distribution == 'Uniform' :
81
- lower = values ['lower' ]
82
- upper = values ['upper' ]
83
- parameter .distribution = shape .Uniform (lower = lower , upper = upper )
73
+ if sim_type == 'uncertainty' :
74
+ distribution = item ['distribution' ].capitalize ()
75
+ parameter .baseline = values ['baseline' ]
76
+ if distribution == 'Triangular' :
77
+ lower = values ['lower' ]
78
+ midpoint = values ['mode' ]
79
+ upper = values ['upper' ]
80
+ parameter .distribution = shape .Triangle (lower = lower , midpoint = midpoint , upper = upper )
81
+ elif distribution == 'Uniform' :
82
+ lower = values ['lower' ]
83
+ upper = values ['upper' ]
84
+ parameter .distribution = shape .Uniform (lower = lower , upper = upper )
85
+ else :
86
+ raise RuntimeError (f"distribution { distribution } not available yet" )
87
+ elif sim_type == 'single' :
88
+ parameter .baseline = values ['baseline' ]
84
89
else :
85
- raise RuntimeError (f"distribution { distribution } not available yet" )
90
+ print ('Simulation type not implemented:' , sim_type )
91
+
92
+ # Rerun model at baseline to reset cache
93
+ baseline_metrics = model .metrics_at_baseline ()
86
94
87
95
# Run model
88
- try :
89
- model .parameters = parameters
90
- samples = model .sample (N = samples_int , rule = 'L' )
91
- model .load_samples (samples )
92
- model .evaluate ()
93
- except Exception as e :
94
- raise e
95
- else :
96
+
97
+ # results_payload={
98
+ # 'jobId': jobId,
99
+ # 'jobTimestamp': jobTimestamp,
100
+ # # 'results': results_json,
101
+ # # 'spearmanResults': spearman_rhos_json,
102
+ # }
103
+
104
+ if sim_type == 'uncertainty' :
105
+ try :
106
+ model .parameters = parameters
107
+ samples = model .sample (N = samples_int , rule = 'L' )
108
+ model .load_samples (samples )
109
+ model .evaluate ()
110
+ except Exception as e :
111
+ raise e
112
+ else :
113
+ def get_name (metric ):
114
+ name = metric .name
115
+ if metric .units : name += f" [{ metric .units } ]"
116
+ return name
117
+ results = model .table
118
+ spearman_rhos , ps = model .spearman_r ()
119
+ param_names = [get_name (i ) for i in parameters ]
120
+ metric_names = [get_name (i ) for i in model .metrics ]
121
+ names = param_names + metric_names
122
+ results_dict = {i : j .tolist () for i , j in zip (names , results .values .transpose ())}
123
+ results_json = json .dumps (results_dict )
124
+ spearman_rhos_dict = {col : {row : float (value ) for row , value in zip (param_names , values )}
125
+ for col , values in zip (metric_names , spearman_rhos .values .transpose ())}
126
+ spearman_rhos_json = json .dumps (spearman_rhos_dict )
127
+ # results_payload['results'] = json.dumps(json.load(results_json, parse_float=Decimal))
128
+ # results_payload['spearmanResults'] = json.dumps(json.load(spearman_rhos_json, parse_float=Decimal))
129
+
130
+ finally :
131
+ model .parameters = tuple (all_parameters .values ())
132
+ elif sim_type == 'single' :
96
133
def get_name (metric ):
97
- name = metric .name
98
- if metric .units : name += f" [{ metric .units } ]"
99
- return name
100
- results = model .table
101
- spearman_rhos , ps = model .spearman_r ()
102
- param_names = [get_name (i ) for i in parameters ]
134
+ name = metric .name
135
+ if metric .units : name += f" [{ metric .units } ]"
136
+ return name
137
+ baseline_metrics = model .metrics_at_baseline ()
103
138
metric_names = [get_name (i ) for i in model .metrics ]
104
- names = param_names + metric_names
105
- results_dict = {i : j .tolist () for i , j in zip (names , results .values .transpose ())}
106
- results_json = json .dumps (results_dict )
107
- spearman_rhos_dict = {col : {row : float (value ) for row , value in zip (param_names , values )}
108
- for col , values in zip (metric_names , spearman_rhos .values .transpose ())}
109
- spearman_rhos_json = json .dumps (spearman_rhos_dict )
110
-
111
- finally :
112
- model .parameters = tuple (all_parameters .values ())
139
+ single_results = {i : j for i , j in zip (metric_names , baseline_metrics .values )}
140
+ single_results = json .dumps (single_results )
141
+ # results_payload['singleResults'] = json.dumps(json.load(single_results, parse_float=Decimal))
142
+
143
+ else :
144
+ print ('Simulation type not implemented:' , sim_type )
113
145
114
146
# Add outputs to DynamoDB table: biosteamJobResults
115
147
jobTimestamp = int (jobTimestamp )
@@ -119,14 +151,31 @@ def get_name(metric):
119
151
print (table .creation_date_time )
120
152
121
153
# Add biosteam results to table
122
- table .put_item (
123
- Item = {
124
- 'jobId' : jobId ,
125
- 'jobTimestamp' : jobTimestamp ,
126
- 'results' : results_json ,
127
- 'spearmanResults' : spearman_rhos_json ,
128
- }
129
- )
154
+ # print(results_payload)
155
+ # from warnings import warn
156
+ # warn(str(results_payload))
157
+ if sim_type == 'uncertainty' :
158
+ table .put_item (
159
+ Item = {
160
+ 'jobId' : jobId ,
161
+ 'jobTimestamp' : jobTimestamp ,
162
+ 'results' : results_json ,
163
+ 'spearmanResults' : spearman_rhos_json ,
164
+ # 'singleResults': single_results
165
+ }
166
+ )
167
+ elif sim_type == 'single' :
168
+ table .put_item (
169
+ Item = {
170
+ 'jobId' : jobId ,
171
+ 'jobTimestamp' : jobTimestamp ,
172
+ # 'results': results_json,
173
+ # 'spearmanResults': spearman_rhos_json,
174
+ 'singleResults' : single_results
175
+ }
176
+ )
177
+ else :
178
+ print ("Simulation type not implemented" )
130
179
131
180
# Return job status
132
181
return {
0 commit comments