-
Notifications
You must be signed in to change notification settings - Fork 240
/
sqlite_recorder_test_utils.py
392 lines (305 loc) · 16.4 KB
/
sqlite_recorder_test_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from six import iteritems, PY2, PY3
import sqlite3
import numpy as np
import json
from contextlib import contextmanager
from openmdao.utils.record_util import format_iteration_coordinate, json_to_np_array
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.recorders.sqlite_recorder import blob_to_array, format_version
if PY2:
import cPickle as pickle
else:
import pickle
@contextmanager
def database_cursor(filename):
"""
Context manager managing a cursor for the SQLite database with the given file name.
"""
con = sqlite3.connect(filename)
cur = con.cursor()
yield cur
con.close()
def get_format_version_abs2meta(db_cur):
"""
Return the format version and abs2meta dict from metadata table in the case recorder file.
"""
db_cur.execute("SELECT format_version, abs2meta FROM metadata")
row = db_cur.fetchone()
f_version = row[0]
# Need to also get abs2meta so that we can pass it to json_to_np_array
if f_version >= 3:
abs2meta = json.loads(row[1])
elif f_version in (1, 2):
if PY2:
abs2meta = pickle.loads(str(row[1])) if row[1] is not None else None
if PY3:
try:
abs2meta = pickle.loads(row[1]) if row[1] is not None else None
except TypeError:
# Reading in a python 2 pickle recorded pre-OpenMDAO 2.4.
abs2meta = pickle.loads(row[1].encode()) if row[1] is not None else None
return f_version, abs2meta
def assertDriverIterDataRecorded(test, expected, tolerance, prefix=None):
"""
Expected can be from multiple cases.
"""
with database_cursor(test.filename) as db_cur:
f_version, abs2meta = get_format_version_abs2meta(db_cur)
# iterate through the cases
for coord, (t0, t1), outputs_expected, inputs_expected in expected:
iter_coord = format_iteration_coordinate(coord, prefix=prefix)
# from the database, get the actual data recorded
db_cur.execute("SELECT * FROM driver_iterations WHERE "
"iteration_coordinate=:iteration_coordinate",
{"iteration_coordinate": iter_coord})
row_actual = db_cur.fetchone()
test.assertTrue(row_actual,
'Driver iterations table does not contain the requested '
'iteration coordinate: "{}"'.format(iter_coord))
counter, global_counter, iteration_coordinate, timestamp, success, msg,\
inputs_text, outputs_text = row_actual
if f_version >= 3:
inputs_actual = json_to_np_array(inputs_text, abs2meta)
outputs_actual = json_to_np_array(outputs_text, abs2meta)
elif f_version in (1, 2):
inputs_actual = blob_to_array(inputs_text)
outputs_actual = blob_to_array(outputs_text)
# Does the timestamp make sense?
test.assertTrue(t0 <= timestamp and timestamp <= t1)
test.assertEqual(success, 1)
test.assertEqual(msg, '')
for vartype, actual, expected in (
('outputs', outputs_actual, outputs_expected),
('inputs', inputs_actual, inputs_expected)
):
if expected is None:
if f_version >= 3:
test.assertIsNone(actual)
if f_version in (1, 2):
test.assertEqual(actual, np.array(None, dtype=object))
else:
actual = actual[0]
# Check to see if the number of values in actual and expected match
test.assertEqual(len(actual), len(expected))
for key, value in iteritems(expected):
# Check to see if the keys in the actual and expected match
test.assertTrue(key in actual.dtype.names,
'{} variable not found in actual data'
' from recorder'.format(key))
# Check to see if the values in actual and expected match
assert_rel_error(test, actual[key], expected[key], tolerance)
def assertDriverDerivDataRecorded(test, expected, tolerance, prefix=None):
"""
Expected can be from multiple cases.
"""
with database_cursor(test.filename) as db_cur:
# iterate through the cases
for coord, (t0, t1), totals_expected in expected:
iter_coord = format_iteration_coordinate(coord, prefix=prefix)
# from the database, get the actual data recorded
db_cur.execute("SELECT * FROM driver_derivatives WHERE "
"iteration_coordinate=:iteration_coordinate",
{"iteration_coordinate": iter_coord})
row_actual = db_cur.fetchone()
db_cur.execute("SELECT abs2meta FROM metadata")
row_abs2meta = db_cur.fetchone()
test.assertTrue(row_actual,
'Driver iterations table does not contain the requested '
'iteration coordinate: "{}"'.format(iter_coord))
counter, global_counter, iteration_coordinate, timestamp, success, msg,\
totals_blob = row_actual
abs2meta = json.loads(row_abs2meta[0]) if row_abs2meta[0] is not None else None
test.assertTrue(isinstance(abs2meta, dict))
totals_actual = blob_to_array(totals_blob)
# Does the timestamp make sense?
test.assertTrue(t0 <= timestamp and timestamp <= t1)
test.assertEqual(success, 1)
test.assertEqual(msg, '')
if totals_expected is None:
test.assertEqual(totals_actual, np.array(None, dtype=object))
else:
actual = totals_actual[0]
# Check to see if the number of values in actual and expected match
test.assertEqual(len(actual), len(totals_expected))
for key, value in iteritems(totals_expected):
# Check to see if the keys in the actual and expected match
test.assertTrue(key in actual.dtype.names,
'{} variable not found in actual data'
' from recorder'.format(key))
# Check to see if the values in actual and expected match
assert_rel_error(test, actual[key], totals_expected[key], tolerance)
def assertSystemIterDataRecorded(test, expected, tolerance, prefix=None):
"""
Expected can be from multiple cases.
"""
with database_cursor(test.filename) as db_cur:
f_version, abs2meta = get_format_version_abs2meta(db_cur)
# iterate through the cases
for coord, (t0, t1), inputs_expected, outputs_expected, residuals_expected in expected:
iter_coord = format_iteration_coordinate(coord, prefix=prefix)
# from the database, get the actual data recorded
db_cur.execute("SELECT * FROM system_iterations WHERE "
"iteration_coordinate=:iteration_coordinate",
{"iteration_coordinate": iter_coord})
row_actual = db_cur.fetchone()
test.assertTrue(row_actual, 'System iterations table does not contain the requested '
'iteration coordinate: "{}"'.format(iter_coord))
counter, global_counter, iteration_coordinate, timestamp, success, msg, inputs_text, \
outputs_text, residuals_text = row_actual
if f_version >= 3:
inputs_actual = json_to_np_array(inputs_text, abs2meta)
outputs_actual = json_to_np_array(outputs_text, abs2meta)
residuals_actual = json_to_np_array(residuals_text, abs2meta)
elif f_version in (1, 2):
inputs_actual = blob_to_array(inputs_text)
outputs_actual = blob_to_array(outputs_text)
residuals_actual = blob_to_array(residuals_text)
# Does the timestamp make sense?
test.assertTrue(t0 <= timestamp and timestamp <= t1)
test.assertEqual(success, 1)
test.assertEqual(msg, '')
for vartype, actual, expected in (
('inputs', inputs_actual, inputs_expected),
('outputs', outputs_actual, outputs_expected),
('residuals', residuals_actual, residuals_expected),
):
if expected is None:
if f_version >= 3:
test.assertIsNone(actual)
if f_version in (1, 2):
test.assertEqual(actual, np.array(None, dtype=object))
else:
# Check to see if the number of values in actual and expected match
test.assertEqual(len(actual[0]), len(expected))
for key, value in iteritems(expected):
# Check to see if the keys in the actual and expected match
test.assertTrue(key in actual[0].dtype.names,
'{} variable not found in actual data '
'from recorder'.format(key))
# Check to see if the values in actual and expected match
assert_rel_error(test, actual[0][key], expected[key], tolerance)
def assertSolverIterDataRecorded(test, expected, tolerance, prefix=None):
"""
Expected can be from multiple cases.
"""
with database_cursor(test.filename) as db_cur:
f_version, abs2meta = get_format_version_abs2meta(db_cur)
# iterate through the cases
for coord, (t0, t1), expected_abs_error, expected_rel_error, expected_output, \
expected_solver_residuals in expected:
iter_coord = format_iteration_coordinate(coord, prefix=prefix)
# from the database, get the actual data recorded
db_cur.execute("SELECT * FROM solver_iterations "
"WHERE iteration_coordinate=:iteration_coordinate",
{"iteration_coordinate": iter_coord})
row_actual = db_cur.fetchone()
test.assertTrue(row_actual, 'Solver iterations table does not contain the requested '
'iteration coordinate: "{}"'.format(iter_coord))
counter, global_counter, iteration_coordinate, timestamp, success, msg, \
abs_err, rel_err, input_blob, output_text, residuals_text = row_actual
if f_version >= 3:
output_actual = json_to_np_array(output_text, abs2meta)
residuals_actual = json_to_np_array(residuals_text, abs2meta)
elif f_version in (1, 2):
output_actual = blob_to_array(output_text)
residuals_actual = blob_to_array(residuals_text)
# Does the timestamp make sense?
test.assertTrue(t0 <= timestamp and timestamp <= t1,
'timestamp should be between when the model started and stopped')
test.assertEqual(success, 1)
test.assertEqual(msg, '')
if expected_abs_error:
test.assertTrue(abs_err, 'Expected absolute error but none recorded')
assert_rel_error(test, abs_err, expected_abs_error, tolerance)
if expected_rel_error:
test.assertTrue(rel_err, 'Expected relative error but none recorded')
assert_rel_error(test, rel_err, expected_rel_error, tolerance)
for vartype, actual, expected in (
('outputs', output_actual, expected_output),
('residuals', residuals_actual, expected_solver_residuals),
):
if expected is None:
if f_version >= 3:
test.assertIsNone(actual)
if f_version in (1, 2):
test.assertEqual(actual, np.array(None, dtype=object))
else:
# Check to see if the number of values in actual and expected match
test.assertEqual(len(actual[0]), len(expected))
for key, value in iteritems(expected):
# Check to see if the keys in the actual and expected match
test.assertTrue(key in actual[0].dtype.names,
'{} variable not found in actual data '
'from recorder'.format(key))
# Check to see if the values in actual and expected match
assert_rel_error(test, actual[0][key], expected[key], tolerance)
def assertMetadataRecorded(test, expected_prom2abs, expected_abs2prom):
with database_cursor(test.filename) as db_cur:
db_cur.execute("SELECT format_version, prom2abs, abs2prom FROM metadata")
row = db_cur.fetchone()
format_version_actual = row[0]
format_version_expected = format_version
prom2abs = json.loads(str(row[1]))
abs2prom = json.loads(str(row[2]))
if prom2abs is None:
test.assertIsNone(expected_prom2abs)
else:
for io in ['input', 'output']:
for var in prom2abs[io]:
test.assertEqual(prom2abs[io][var].sort(), expected_prom2abs[io][var].sort())
if abs2prom is None:
test.assertIsNone(expected_abs2prom)
else:
for io in ['input', 'output']:
for var in abs2prom[io]:
test.assertEqual(abs2prom[io][var], expected_abs2prom[io][var])
# this always gets recorded
test.assertEqual(format_version_actual, format_version_expected)
def assertViewerDataRecorded(test, expected):
with database_cursor(test.filename) as db_cur:
db_cur.execute("SELECT format_version FROM metadata")
f_version = db_cur.fetchone()[0]
test.assertTrue(isinstance(f_version, int))
db_cur.execute("SELECT model_viewer_data FROM driver_metadata")
row = db_cur.fetchone()
if expected is None:
test.assertIsNone(row)
return
model_viewer_data = json.loads(row[0])
test.assertTrue(isinstance(model_viewer_data, dict))
test.assertEqual(3, len(model_viewer_data))
test.assertTrue(isinstance(model_viewer_data['connections_list'], list))
test.assertEqual(expected['connections_list_length'],
len(model_viewer_data['connections_list']))
test.assertEqual(expected['tree_length'], len(model_viewer_data['tree']))
tr = model_viewer_data['tree']
test.assertEqual(set(['name', 'type', 'subsystem_type', 'children']),
set(tr.keys()))
test.assertEqual(expected['tree_children_length'],
len(model_viewer_data['tree']['children']))
cl = model_viewer_data['connections_list']
for c in cl:
test.assertTrue(set(c.keys()).issubset(set(['src', 'tgt', 'cycle_arrows'])))
abs2prom = model_viewer_data['abs2prom']
for io in ['input', 'output']:
for var in expected['abs2prom'][io]:
test.assertEqual(abs2prom[io][var], expected['abs2prom'][io][var])
def assertSystemMetadataIdsRecorded(test, ids):
with database_cursor(test.filename) as cur:
for id in ids:
cur.execute("SELECT * FROM system_metadata WHERE id=:id", {"id": id})
row_actual = cur.fetchone()
test.assertTrue(row_actual,
'System metadata table does not contain the '
'requested id: "{}"'.format(id))
def assertSystemIterCoordsRecorded(test, iteration_coordinates):
with database_cursor(test.filename) as cur:
for iteration_coordinate in iteration_coordinates:
cur.execute("SELECT * FROM system_iterations WHERE "
"iteration_coordinate=:iteration_coordinate",
{"iteration_coordinate": iteration_coordinate})
row_actual = cur.fetchone()
test.assertTrue(row_actual,
'System iterations table does not contain the '
'requested iteration coordinate: "{}"'.
format(iteration_coordinate))