def assertDriverIterationDataRecorded(test, db_cur, expected, tolerance): """ Expected can be from multiple cases. """ # iterate through the cases for coord, (t0, t1), desvars_expected, responses_expected, objectives_expected, \ constraints_expected, sysincludes_expected in expected: iter_coord = format_iteration_coordinate(coord) # 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, desvars_blob,\ responses_blob, objectives_blob, constraints_blob, sysincludes_blob = row_actual desvars_actual = blob_to_array(desvars_blob) responses_actual = blob_to_array(responses_blob) objectives_actual = blob_to_array(objectives_blob) constraints_actual = blob_to_array(constraints_blob) sysincludes_actual = blob_to_array(sysincludes_blob) # Does the timestamp make sense? test.assertTrue(t0 <= timestamp and timestamp <= t1) test.assertEqual(success, 1) test.assertEqual(msg, '') for vartype, actual, expected in ( ('desvars', desvars_actual, desvars_expected), ('responses', responses_actual, responses_expected), ('objectives', objectives_actual, objectives_expected), ('constraints', constraints_actual, constraints_expected), ('sysincludes', sysincludes_actual, sysincludes_expected), ): if expected is None: 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) return
def assertSystemIterDataRecorded(test, expected, tolerance, prefix=None): """ Expected can be from multiple cases. """ with database_cursor(test.filename) as db_cur: db_cur.execute("SELECT format_version FROM metadata") f_version = db_cur.fetchone()[0] # 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) outputs_actual = json_to_np_array(outputs_text) residuals_actual = json_to_np_array(residuals_text) 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 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 if PY2: abs2meta = pickle.loads(str( row_abs2meta[0])) if row_abs2meta[0] is not None else None else: abs2meta = pickle.loads( row_abs2meta[0]) if row_abs2meta[0] is not None else None 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 assertDriverIterationDataRecorded(test, db_cur, expected, tolerance): """ Expected can be from multiple cases. """ # iterate through the cases for coord, (t0, t1), desvars_expected, responses_expected, objectives_expected, \ constraints_expected, sysincludes_expected in expected: iter_coord = format_iteration_coordinate(coord) # 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, desvars_blob,\ responses_blob, objectives_blob, constraints_blob, sysincludes_blob = row_actual desvars_actual = blob_to_array(desvars_blob) responses_actual = blob_to_array(responses_blob) objectives_actual = blob_to_array(objectives_blob) constraints_actual = blob_to_array(constraints_blob) sysincludes_actual = blob_to_array(sysincludes_blob) # Does the timestamp make sense? test.assertTrue(t0 <= timestamp and timestamp <= t1) test.assertEqual(success, 1) test.assertEqual(msg, '') for vartype, actual, expected in ( ('desvars', desvars_actual, desvars_expected), ('responses', responses_actual, responses_expected), ('objectives', objectives_actual, objectives_expected), ('constraints', constraints_actual, constraints_expected), ('sysincludes', sysincludes_actual, sysincludes_expected), ): if expected is None: 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) return
def assertSolverIterationDataRecorded(test, db_cur, expected, tolerance): """ Expected can be from multiple cases. """ # 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) # 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, \ output_blob, residuals_blob = row_actual output_actual = blob_to_array(output_blob) residuals_actual = blob_to_array(residuals_blob) # 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: 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) return
def assertSolverIterationDataRecorded(test, db_cur, expected, tolerance): """ Expected can be from multiple cases. """ # 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) # 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, \ output_blob, residuals_blob = row_actual output_actual = blob_to_array(output_blob) residuals_actual = blob_to_array(residuals_blob) # 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: 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) return
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 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 = deserialize(output_text, abs2meta) residuals_actual = deserialize(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_near_equal(abs_err, expected_abs_error, tolerance) if expected_rel_error: test.assertTrue(rel_err, 'Expected relative error but none recorded') assert_near_equal(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 expected.items(): # 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_near_equal(actual[0][key], expected[key], tolerance)
def assertDriverIterDataRecorded(test, expected, tolerance, prefix=None): """ Expected can be from multiple cases. """ with database_cursor(test.filename) as db_cur: f_version, abs2meta, prom2abs, conns = get_format_version_abs2meta( db_cur) # iterate through the cases for coord, ( t0, t1 ), outputs_expected, inputs_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 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, residuals_text = row_actual if f_version >= 3: inputs_actual = deserialize(inputs_text, abs2meta, prom2abs, conns) outputs_actual = deserialize(outputs_text, abs2meta, prom2abs, conns) residuals_actual = deserialize(residuals_text, abs2meta, prom2abs, conns) 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), ('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: 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 expected.items(): # ivc sources if vartype == 'outputs' and key in prom2abs['input']: prom_in = prom2abs['input'][key][0] src_key = conns[prom_in] else: src_key = key # Check to see if the keys in the actual and expected match test.assertTrue( src_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_near_equal(actual[src_key], expected[key], tolerance)