def record(self, params, unknowns, resids, metadata): """ Stores the provided data in the shelve file using the iteration coordinate for the key. Args ---- params : dict Dictionary containing parameters. (p) unknowns : dict Dictionary containing outputs and states. (u) resids : dict Dictionary containing residuals. (r) metadata : dict, optional Dictionary containing execution metadata (e.g. iteration coordinate). """ iteration_coordinate = metadata['coord'] group_name = format_iteration_coordinate(iteration_coordinate) self.order.append(group_name) f = self.out data = OrderedDict([('Parameters', params), ('Unknowns', unknowns), ('Residuals', resids)]) f[group_name] = data f['order'] = self.order
def assertDatasetEquals(self, expected, tolerance): # Close the file to ensure it is written to disk. self.recorder.out.close() self.recorder.out = None sentinel = object() f = shelve.open(self.filename) order = f['order'] del f['order'] for coord, expect in expected: iter_coord = format_iteration_coordinate(coord) self.assertEqual(order.pop(0), iter_coord) groupings = ( ("Parameters", expect[0]), ("Unknowns", expect[1]), ("Residuals", expect[2]) ) actual_group = f[iter_coord] for label, values in groupings: actual = actual_group[label] # If len(actual) == len(expected) and actual <= expected, then # actual == expected. self.assertEqual(len(actual), len(values)) for key, val in values: found_val = actual.get(key, sentinel) if found_val is sentinel: self.fail("Did not find key '{0}'".format(key)) assert_rel_error(self, found_val, val, tolerance) del f[iter_coord] # Having deleted all found values, the file should now be empty. self.assertEqual(len(f), 0) # As should the ordering. self.assertEqual(len(order), 0) f.close()
def record(self, params, unknowns, resids, metadata): """Dump the given run data in a "pretty" form.""" if not self.out: # if self.out is None, just do nothing return write = self.out.write write("Iteration Coordinate: {0:s}\n".format(format_iteration_coordinate(metadata['coord']))) write("Params:\n") for param, val in sorted(params.items()): write(" {0}: {1}\n".format(param, str(val))) write("Unknowns:\n") for unknown, val in sorted(unknowns.items()): write(" {0}: {1}\n".format(unknown, str(val))) write("Resids:\n") for resid, val in sorted(resids.items()): write(" {0}: {1}\n".format(resid, str(val)))
def record(self, params, unknowns, resids, metadata): """ Stores the provided data in the HDF5 file using the iteration coordinate for the Group name. Args ---- params : dict Dictionary containing parameters. (p) unknowns : dict Dictionary containing outputs and states. (u) resids : dict Dictionary containing residuals. (r) metadata : dict, optional Dictionary containing execution metadata (e.g. iteration coordinate). """ iteration_coordinate = metadata['coord'] group_name = format_iteration_coordinate(iteration_coordinate) f = self.out group = f.require_group(group_name) p_group = group.create_group("Parameters") u_group = group.create_group("Unknowns") r_group = group.create_group("Residuals") pairings = ((p_group, params), (u_group, unknowns), (r_group, resids)) for grp, data in pairings: for key, val in data.items(): if isinstance(val, (np.ndarray, Number)): grp.create_dataset(key, data=val) # TODO: Compression/Checksum? else: # TODO: Handling non-numeric data msg = "HDF5 Recorder does not support data of type '{0}'".format(type(val)) raise NotImplementedError(msg)
def assertDatasetEquals(self, expected, tolerance): for coord, expect in expected: icoord = format_iteration_coordinate(coord) f = self.recorder.out[icoord] params = f['Parameters'] unknowns = f['Unknowns'] resids = f['Residuals'] sentinel = object() # If len(actual) == len(expected) and actual <= expected, then # actual == expected. for actual, exp in zip((params, unknowns, resids), expect): self.assertEqual(len(actual), len(exp)) for key, val in exp: found_val = actual.get(key, sentinel) if found_val is sentinel: self.fail("Did not find key '{0}'.".format(key)) assert_rel_error(self, found_val[()], val, tolerance)
def record(self, params, unknowns, resids, metadata): """Dump the given run data in a "pretty" form. Args ---- params : `VecWrapper` `VecWrapper` containing parameters. (p) unknowns : `VecWrapper` `VecWrapper` containing outputs and states. (u) resids : `VecWrapper` `VecWrapper` containing residuals. (r) metadata : dict Dictionary containing execution metadata (e.g. iteration coordinate). """ if not self.out: # if self.out is None, just do nothing return write = self.out.write fmat = "Iteration Coordinate: {0:s}\n" write(fmat.format(format_iteration_coordinate(metadata['coord']))) write("Params:\n") for param, val in sorted(params.items()): write(" {0}: {1}\n".format(param, str(val))) write("Unknowns:\n") for unknown, val in sorted(unknowns.items()): write(" {0}: {1}\n".format(unknown, str(val))) write("Resids:\n") for resid, val in sorted(resids.items()): write(" {0}: {1}\n".format(resid, str(val)))