def experiment_experiments_registry(): registry = Registry("experiments") @registry.dumper(Experiment, "Experiment", version=1) def _exp_dump(experiment): return GroupContainer(dataset=DatasetContainer(data=experiment.data), attrs={"time started": experiment.time_started}) @registry.loader("Experiment", version=1) def _exp_load(dataset): return Experiment(data=dataset["dataset"]["data"], time_started=dataset.attrs["time started"]) @registry.dumper(Experiments, "Experiments", version=1) def _exp_dump(experiments): return GroupContainer(runs=GroupContainer(**experiments.runs), attrs={"name": experiments.name}) @registry.loader("Experiments", version=1) def _exp_load(group): return Experiments( runs={run: exp for run, exp in group["runs"].items()}, name=group.attrs["name"]) return registry
def invalid_dumper_experiment_registry(): registry = Registry("incorrect dumper experiment") @registry.dumper(Experiment, "Experiment", version=1) def _exp_dump(experiment): return (experiment.data, {"time started": experiment.time_started}) return registry
def no_loader_experiment_registry(): registry = Registry("experiment") @registry.dumper(Experiment, "Experiment", version=1) def _exp_dump(experiment): return DatasetContainer( data=experiment.data, attrs={"time started": experiment.time_started}) return registry
def invalid_loader_experiment_registry(): registry = Registry("incorrect loader experiment") @registry.dumper(Experiment, "Experiment", version=1) def _exp_dump(experiment): return DatasetContainer( data=experiment.data, attrs={"time started": experiment.time_started}) @registry.loader("Experiment", version=1) def _exp_load(dataset): return Experiment(data=dataset["data"], ) return registry
def experiment_registry_as_group(): registry = Registry("experiment") @registry.dumper(Experiment, "Experiment", version=1) def _exp_dump(experiment): return GroupContainer(dataset=DatasetContainer(data=experiment.data), attrs={"time started": experiment.time_started}) @registry.loader("Experiment", version=1) def _exp_load(dataset): return Experiment(data=dataset["dataset"]["data"], time_started=dataset.attrs["time started"]) return registry
def None_version_experiment_registry(): registry = Registry("None experiment") @registry.dumper(Experiment, "Experiment", version=None) def _exp_dump(experiment): return DatasetContainer( data=experiment.data, attrs={"time started": experiment.time_started}) @registry.loader("Experiment", version=None) def _exp_load(dataset): return Experiment(data=dataset["data"], time_started=dataset["attrs"]["time started"]) return registry
def run_registry(): registry = Registry("run") @registry.dumper(InternalData, "InternalData", version=1) def _internal_dump(internal_data): return GroupContainer( derivs=internal_data.derivs, params=internal_data.params, angles=internal_data.angles, v_r_normal=internal_data.v_r_normal, v_phi_normal=internal_data.v_phi_normal, rho_normal=internal_data.rho_normal, v_r_taylor=internal_data.v_r_taylor, v_phi_taylor=internal_data.v_phi_taylor, rho_taylor=internal_data.rho_taylor, ) @registry.loader("InternalData", version=1) def _internal_load(group): return InternalData( derivs=group["derivs"]["data"], params=group["params"]["data"], angles=group["angles"]["data"], v_r_normal=group["v_r_normal"]["data"], v_phi_normal=group["v_phi_normal"]["data"], rho_normal=group["rho_normal"]["data"], v_r_taylor=group["v_r_taylor"]["data"], v_phi_taylor=group["v_phi_taylor"]["data"], rho_taylor=group["rho_taylor"]["data"], ) @registry.dumper(InitialConditions, "InitialConditions", version=1) def _initial_dump(initial_conditions): return GroupContainer( attrs={ "norm_kepler_sq": initial_conditions.norm_kepler_sq, "c_s": initial_conditions.c_s, "eta_O": initial_conditions.eta_O, "eta_A": initial_conditions.eta_A, "eta_H": initial_conditions.eta_H, "beta": initial_conditions.beta, "init_con": initial_conditions.init_con, }, angles=initial_conditions.angles, ) @registry.loader("InitialConditions", version=1) def _initial_load(group): return InitialConditions( norm_kepler_sq=group.attrs["norm_kepler_sq"], c_s=group.attrs["c_s"], eta_O=group.attrs["eta_O"], eta_A=group.attrs["eta_A"], eta_H=group.attrs["eta_H"], beta=group.attrs["beta"], init_con=group.attrs["init_con"], angles=group["angles"]["data"], ) @registry.dumper(Solution, "Solution", version=1) def _solution_dumper(solution): return GroupContainer( attrs={ "flag": solution.flag, "coordinate_system": solution.coordinate_system, }, angles=solution.angles, solution=solution.solution, internal_data=solution.internal_data, initial_conditions=solution.initial_conditions, t_roots=solution.t_roots, y_roots=solution.y_roots, ) @registry.loader("Solution", version=1) def _solution_loader(group): return Solution( flag=group.attrs["flag"], coordinate_system=group.attrs["coordinate_system"], angles=group["angles"]["data"], solution=group["solution"]["data"], t_roots=group["t_roots"]["data"], y_roots=group["y_roots"]["data"], internal_data=group["internal_data"], initial_conditions=group["initial_conditions"], ) @registry.dumper(Solutions, "Solutions", version=1) def _solutions_dumper(solutions): return OnDemandGroupContainer(**solutions) @registry.loader("Solutions", version=1) def _solutions_loader(group): return Solutions(**group) @registry.dumper(Run, "Run", version=1) def _run_dumper(run): return GroupContainer( solutions=run.solutions, final_solution=run.final_solution, ) @registry.loader("Run", version=1) def _run_loader(group): return Run( solutions=group["solutions"], final_solution=group["final_solution"], ) return registry
def empty_registry(): return Registry("empty registry")
""" Old registry for dictionaries (since it's been dropped from h5preserve """ from h5preserve import Registry, GroupContainer dict_as_group_registry = Registry("Python: dict as group") @dict_as_group_registry.dumper(dict, "dict", version=None) def _dict_dumper(d): # pylint: disable=missing-docstring return GroupContainer(**d) @dict_as_group_registry.loader("dict", version=None) def _dict_loader(group): # pylint: disable=missing-docstring new_dict = {} new_dict.update(group) if group.attrs: new_dict["attrs"] = group.attrs return new_dict
""" Utility functions for data strutures """ from fractions import Fraction import attr from h5preserve import Registry ds_registry = Registry("disc_solver") def _str_β_to_γ(β): """ Convert β to γ where it appears as a string """ return str(Fraction("5/4") - Fraction(β)) def get_fields(cls): """ Get the list of field names from an attr.s class """ return tuple(field.name for field in attr.fields(cls))