Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Recursive to/from_dict #1602

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions pyiron_base/interfaces/has_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
from typing import Any

from pyiron_base.interfaces.has_hdf import HasHDF
from pyiron_base.storage.hdfio import DummyHDFio
from pyiron_base.storage.hdfio import (
DummyHDFio,
_extract_module_class_name,
_import_class,
)

__author__ = "Jan Janssen"
__copyright__ = (
Expand All @@ -33,11 +37,50 @@
__date__ = "Dec 20, 2023"


def create_from_dict(obj_dict):
"""
Create and restores an object previously written as a dictionary.

Args:
obj_dict (dict): must be the output of HasDict.to_dict()

Returns:
object: restored object
"""
if "TYPE" not in obj_dict:
raise ValueError(
"invalid obj_dict! must contain type information and be the output of HasDict.to_dict!"
)
type_field = obj_dict["TYPE"]
module_path, class_name = _extract_module_class_name(type_field)
class_object = _import_class(module_path, class_name)
version = obj_dict.get("VERSION", None)
obj = class_object.instantiate(obj_dict, version)
obj.from_dict(obj_dict, version)
return obj


class HasDict(ABC):
__dict_version__ = "0.1.0"

@abstractmethod
@classmethod
def instantiate(cls, obj_dict: dict, version: str = None) -> "Self":
return cls()

def from_dict(self, obj_dict: dict, version: str = None):
def load(inner_dict):
if not isinstance(inner_dict, dict):
return inner_dict
if not all(
k in inner_dict for k in ("NAME", "TYPE", "OBJECT", "DICT_VERSION")
):
return {k: load(v) for k, v in inner_dict.items()}
return create_from_dict(inner_dict)
Comment on lines +116 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I understand that the conversion is handled here, still I am wondering if this is what we want. I thought the intention was to get towards using __getstate__() and __setstate__(). Now with the executable and the server object using datacalsses for data storage internally, I was hoping that we could use a simple dictionary representation there where the class is stored by storing the attached dataclass.


self._from_dict({k: load(v) for k, v in obj_dict.items()}, version)

@abstractmethod
def _from_dict(self, obj_dict: dict, version: str = None):
pass

@abstractmethod
Expand Down Expand Up @@ -66,7 +109,17 @@ def _type_to_dict(self):
return type_dict

def to_dict(self):
return self._to_dict() | self._type_to_dict()
type_dict = self._type_to_dict()
data_dict = {}
child_dict = {}
for k, v in self._to_dict().items():
if isinstance(v, HasDict):
child_dict[k] = v.to_dict()
elif isinstance(v, HasHDF):
child_dict[k] = HasDictfromHDF.to_dict(v)
else:
data_dict[k] = v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify this, this part currently does not work for the Atoms object in pyiron_atomistics, as it is neither derived from HasDict nor from HasHDF, correct? It only works when the Atoms object was stored as part of a DataContainer, correct?

return data_dict | self._join_children_dict(child_dict) | type_dict

@staticmethod
def _join_children_dict(children: dict[str, dict[str, Any]]) -> dict[str, Any]:
Expand Down Expand Up @@ -114,12 +167,21 @@ class HasDictfromHDF(HasDict, HasHDF):
their children to implmement it.
"""

def from_dict(self, obj_dict: dict, version: str = None):
@classmethod
def instantiate(cls, obj_dict: dict, version: str = None) -> "Self":
hdf = DummyHDFio(None, "/", obj_dict)
return cls(**cls.from_hdf_args(hdf))

def _from_dict(self, obj_dict: dict, version: str = None):
# DummyHDFio(project=None) looks a bit weird, but it was added there
# only to support saving/loading jobs which already use the HasDict
# interface
hdf = DummyHDFio(None, "/", obj_dict)
self.from_hdf(hdf, group_name=self._get_hdf_group_name())
group_name = self._get_hdf_group_name()
if group_name is not None:
hdf = DummyHDFio(None, "/", {group_name: obj_dict})
else:
hdf = DummyHDFio(None, "/", obj_dict)
self.from_hdf(hdf)

def _to_dict(self):
hdf = DummyHDFio(None, "/")
Expand Down
24 changes: 12 additions & 12 deletions pyiron_base/jobs/datamining.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ def _save_output(self):
hdf5_output.file_name, key=hdf5_output.h5_path + "/table"
)

def to_dict(self):
job_dict = super().to_dict()
def _to_dict(self):
job_dict = super()._to_dict()
job_dict["input/bool_dict"] = {
"enforce_update": self._enforce_update,
"convert_to_object": self._pyiron_table.convert_to_object,
Expand All @@ -683,10 +683,10 @@ def to_dict(self):
)
return job_dict

def from_dict(self, job_dict):
super().from_dict(job_dict=job_dict)
if "project" in job_dict["input"].keys():
project_dict = job_dict["input"]["project"]
def _from_dict(self, obj_dict, version=None):
super()._from_dict(obj_dict=obj_dict, version=version)
if "project" in obj_dict["input"].keys():
project_dict = obj_dict["input"]["project"]
if os.path.exists(project_dict["path"]):
project = self.project.__class__(
path=project_dict["path"],
Expand All @@ -700,18 +700,18 @@ def from_dict(self, job_dict):
self._logger.warning(
f"Could not instantiate analysis_project, no such path {project_dict['path']}."
)
if "filter" in job_dict["input"].keys():
if "filter" in obj_dict["input"].keys():
self.pyiron_table.filter_function = _from_pickle(
job_dict["input"], "filter"
obj_dict["input"], "filter"
)
if "db_filter" in job_dict["input"].keys():
if "db_filter" in obj_dict["input"].keys():
self.pyiron_table.db_filter_function = _from_pickle(
job_dict["input"], "db_filter"
obj_dict["input"], "db_filter"
)
bool_dict = job_dict["input"]["bool_dict"]
bool_dict = obj_dict["input"]["bool_dict"]
self._enforce_update = bool_dict["enforce_update"]
self._pyiron_table.convert_to_object = bool_dict["convert_to_object"]
self._pyiron_table.add._from_hdf(job_dict["input"])
self._pyiron_table.add._from_hdf(obj_dict["input"])

def to_hdf(self, hdf=None, group_name=None):
"""
Expand Down
18 changes: 9 additions & 9 deletions pyiron_base/jobs/flex/executablecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ def write_input_combo_funct(working_directory, input_dict):
collect_output_funct=self._collect_output_funct,
)

def to_dict(self) -> dict:
def _to_dict(self) -> dict:
"""
Convert the job object to a dictionary representation.

Returns:
dict: A dictionary representation of the job object.
"""
job_dict = super().to_dict()
job_dict = super()._to_dict()
if self._write_input_funct is not None:
job_dict["write_input_function"] = np.void(
cloudpickle.dumps(self._write_input_funct)
Expand All @@ -159,20 +159,20 @@ def to_dict(self) -> dict:
)
return job_dict

def from_dict(self, job_dict: dict):
def _from_dict(self, obj_dict: dict, version=None):
"""
Load the job attributes from a dictionary representation.

Args:
job_dict (dict): A dictionary containing the job attributes.
obj_dict (dict): A dictionary containing the job attributes.

"""
super().from_dict(job_dict=job_dict)
if "write_input_function" in job_dict.keys():
super()._from_dict(obj_dict=obj_dict)
if "write_input_function" in obj_dict.keys():
self._write_input_funct = cloudpickle.loads(
job_dict["write_input_function"]
obj_dict["write_input_function"]
)
if "write_input_function" in job_dict.keys():
if "write_input_function" in obj_dict.keys():
self._collect_output_funct = cloudpickle.loads(
job_dict["collect_output_function"]
obj_dict["collect_output_function"]
)
14 changes: 7 additions & 7 deletions pyiron_base/jobs/flex/pythonfunctioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,31 @@ def __call__(self, *args, **kwargs):
self.run()
return self.output["result"]

def to_dict(self) -> dict:
def _to_dict(self) -> dict:
"""
Convert the job object to a dictionary representation.

Returns:
dict: The dictionary representation of the job object.
"""
job_dict = super().to_dict()
job_dict = super()._to_dict()
job_dict["function"] = np.void(cloudpickle.dumps(self._function))
job_dict["_automatically_rename_on_save_using_input"] = (
self._automatically_rename_on_save_using_input
)
return job_dict

def from_dict(self, job_dict: dict) -> None:
def _from_dict(self, obj_dict: dict, version=None) -> None:
"""
Load the job object from a dictionary representation.

Args:
job_dict (dict): The dictionary representation of the job object.
obj_dict (dict): The dictionary representation of the job object.
"""
super().from_dict(job_dict=job_dict)
self._function = cloudpickle.loads(job_dict["function"])
super()._from_dict(obj_dict=obj_dict)
self._function = cloudpickle.loads(obj_dict["function"])
self._automatically_rename_on_save_using_input = bool(
job_dict["_automatically_rename_on_save_using_input"]
obj_dict["_automatically_rename_on_save_using_input"]
)

def save(self):
Expand Down
14 changes: 8 additions & 6 deletions pyiron_base/jobs/job/extension/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ def executable_path(self, new_path):
else:
self.storage.mpi = False

@classmethod
def instantiate(cls, obj_dict: dict, version: str = None) -> "Self":
return cls(codename=obj_dict["name"])

def _to_dict(self):
return asdict(self.storage)

def from_dict(self, executable_dict):
def _from_dict(self, obj_dict, version=None):
data_container_keys = [
"version",
"name",
Expand All @@ -220,12 +224,10 @@ def from_dict(self, executable_dict):
]
executable_class_dict = {}
# Backwards compatibility; dict state used to be nested one level deeper
if "executable" in executable_dict.keys() and isinstance(
executable_dict["executable"], dict
):
executable_dict = executable_dict["executable"]
if "executable" in obj_dict.keys() and isinstance(obj_dict["executable"], dict):
obj_dict = obj_dict["executable"]
for key in data_container_keys:
executable_class_dict[key] = executable_dict.get(key, None)
executable_class_dict[key] = obj_dict.get(key, None)
self.storage = ExecutableDataClass(**executable_class_dict)

def get_input_for_subprocess_call(self, cores, threads, gpus=None):
Expand Down
28 changes: 14 additions & 14 deletions pyiron_base/jobs/job/extension/server/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,23 +566,23 @@ def _to_dict(self):
return asdict(self._data)
return server_dict

def from_dict(self, server_dict):
def _from_dict(self, obj_dict, version=None):
# backwards compatibility
if "new_h5" in server_dict.keys():
server_dict["new_hdf"] = server_dict.pop("new_h5") == 1
if "new_h5" in obj_dict.keys():
obj_dict["new_hdf"] = obj_dict.pop("new_h5") == 1
for key in ["conda_environment_name", "conda_environment_path", "qid"]:
if key not in server_dict.keys():
server_dict[key] = None
if "accept_crash" not in server_dict.keys():
server_dict["accept_crash"] = False
if "additional_arguments" not in server_dict.keys():
server_dict["additional_arguments"] = {}
if key not in obj_dict.keys():
obj_dict[key] = None
if "accept_crash" not in obj_dict.keys():
obj_dict["accept_crash"] = False
if "additional_arguments" not in obj_dict.keys():
obj_dict["additional_arguments"] = {}

# Reload dataclass
for key in ["NAME", "TYPE", "OBJECT", "VERSION", "DICT_VERSION"]:
if key in server_dict.keys():
del server_dict[key]
self._data = ServerDataClass(**server_dict)
if key in obj_dict.keys():
del obj_dict[key]
self._data = ServerDataClass(**obj_dict)
self._run_mode = Runmode(mode=self._data.run_mode)

@deprecate(message="Use job.server.to_dict() instead of to_hdf()", version=0.9)
Expand All @@ -609,9 +609,9 @@ def from_hdf(self, hdf, group_name=None):
"""
if group_name is not None:
with hdf.open(group_name) as hdf_group:
self.from_dict(server_dict=hdf_group["server"])
self.from_dict(obj_dict=hdf_group["server"])
else:
self.from_dict(server_dict=hdf["server"])
self.from_dict(obj_dict=hdf["server"])

def db_entry(self):
"""
Expand Down
18 changes: 9 additions & 9 deletions pyiron_base/jobs/job/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,14 +1174,14 @@ def _to_dict(self):
data_dict["files_to_compress"] = self._files_to_remove
return data_dict

def from_dict(self, job_dict):
self._type_from_dict(type_dict=job_dict)
if "import_directory" in job_dict.keys():
self._import_directory = job_dict["import_directory"]
self._server.from_dict(server_dict=job_dict["server"])
if "executable" in job_dict.keys() and job_dict["executable"] is not None:
self.executable.from_dict(job_dict["executable"])
input_dict = job_dict["input"]
def _from_dict(self, obj_dict, version=None):
self._type_from_dict(type_dict=obj_dict)
if "import_directory" in obj_dict.keys():
self._import_directory = obj_dict["import_directory"]
self._server = obj_dict["server"]
if "executable" in obj_dict.keys() and obj_dict["executable"] is not None:
self._executable = obj_dict["executable"]
Comment on lines +1182 to +1183
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit surprised about this part, when is the dictionary of the executable converted to the executable object? Is this already happening when reading the executable from the HDF5 file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this new setup HasDict.from_dict converts nested objects back from their dictionary form before the implementations HasDict._from_dict is called. So by the time GenericJob._from_dict runs and looks for the executable Executable._from_dict already ran.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle the reverse also works, i.e. it's not always necessary anymore to call to_dict on ones children that are returned in the obj_dict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to document this at least in the HasDict class, so developers understand how to derive their own classes from HasDict and that they only have to implement the _to_dict() and _from_dict() interface. Finally, I am wondering if it makes sense to overload the __getstate__() and __setstate__() function in the HasDict class. Then we can slowly transition from calling to_dict() and from_dict() to using the __getstate__() and __setstate__() interface to reload the pyiron objects. This would simplify the interface for developers who just want to integrate a new code. As long as their classes can be pickled, then we can store their classes in our jobs. If they want to optimise the performance and benefit from the hierarchical nature of the HDF5 file, then they can overload the __getstate__() and __setstate__() method, for example by attaching a dataclass to their class which they use for data storage or by attaching a data container.

input_dict = obj_dict["input"]
if "generic_dict" in input_dict.keys():
generic_dict = input_dict["generic_dict"]
self._restart_file_list = generic_dict["restart_file_list"]
Expand Down Expand Up @@ -1242,7 +1242,7 @@ def from_hdf(self, hdf=None, group_name=None):
exe_dict = self._hdf5["executable/executable"].to_object().to_builtin()
exe_dict["READ_ONLY"] = self._hdf5["executable/executable/READ_ONLY"]
job_dict["executable"] = {"executable": exe_dict}
self.from_dict(job_dict=job_dict)
self.from_dict(obj_dict=job_dict)

def save(self):
"""
Expand Down
12 changes: 6 additions & 6 deletions pyiron_base/jobs/job/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def input(self):
def output(self):
return self.storage.output

def to_dict(self):
job_dict = super().to_dict()
job_dict["input/data"] = self.storage.input.to_builtin()
def _to_dict(self):
job_dict = super()._to_dict()
job_dict["input/data"] = self.storage.input.to_dict()
return job_dict

def from_dict(self, job_dict):
super().from_dict(job_dict=job_dict)
input_dict = job_dict["input"]
def _from_dict(self, obj_dict, version=None):
super()._from_dict(obj_dict=obj_dict, version=version)
input_dict = obj_dict["input"]
if "data" in input_dict.keys():
self.storage.input.update(input_dict["data"])

Expand Down
Loading
Loading