-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add a Hdf mixin that takes care of type info + general boilerplate #416
Conversation
I really like this idea a lot! It might make writing new self-hdf-able classes a lot easier! Maybe you should add the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this. WithHDF
took me a second read-through to understand, but I find the use of __enter__
to handle the group name absolutely wonderful.
I posted a couple of minor suggestions to consider.
pyiron_base/interfaces/has_hdf.py
Outdated
@@ -0,0 +1,63 @@ | |||
from abc import ABC, abstractmethod | |||
|
|||
class WithHDF: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class WithHDF: | |
class _WithHDF: |
Or?
pyiron_base/interfaces/has_hdf.py
Outdated
@abstractmethod | ||
def _get_hdf_group_name(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abstractmethod | |
def _get_hdf_group_name(self): | |
@property | |
@abstractmethod | |
def _hdf_group_name(self): |
At least I think that's the right order for the decorators.
Am I missing a side effect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should work, I didn't do it before because it's a private method anyway, but I can change it now.
pyiron_base/interfaces/has_hdf.py
Outdated
hdf["HDF_VERSION"] = self.__hdf_version__ | ||
|
||
def from_hdf(self, hdf, group_name=None): | ||
group_name = group_name or self._get_hdf_group_name() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit check for None
.
pyiron_base/interfaces/has_hdf.py
Outdated
self._to_hdf(hdf) | ||
self._type_to_hdf(hdf) | ||
|
||
def rewrite_hdf(self, hdf, group_name=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Garbage collection for HDF?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Fixed the test, details in the commits. The problem was that previously |
Adds a method to allow sub classes to specify an HDF group name that they want to be written into. Will be used if to_hdf() is not given a group_name. Pass self in abstractmethods
It's unused in all our code, but removing should involve discussion
This is so the objects implementation cannot muck it up accidentally.
Some objects only have an HDF version unlike jobs that also have a code version.
Not copying caused an error in lazy DataContainers, because with the new HasHDF inheritance it would: 1. hdf.open(...) and pass that to DataContainer._from_hdf 2. this method would then pass references to the HDF object to the HDFStub 3. HasHDF.from_hdf then calls hdf.close(), this modifies the hdf object passed to DataContainer in place, changing where it points to in the file 4. At some point HDFStub.load() would be called, but since the place where hdf points to changed under its feet HDFStub.load() then reaches into the wrong location in the file
Rebased to resolve merge conflict. |
Beautiful, tests are passing so I'm happy! If you have no further concerns, please merge it and let's get this thing live :) Since EDIT: spelling of trifecta |
Since |
Yes, this can work. We just need to think a little bit whether (1) (1) Pro: the ABC is satisfied and people can directly make new classes without ever worrying about storage (as long as everything they want serialized gets slapped onto the (2) Pro: I lean towards (1). Thoughts? |
Yeah, (1) sounds cleaner. Overloading the saving stuff should only be necessary for fundamental object and jobs anyway. |
It came up a few times that
DataContainer
was subclassed because of the HDF functionality, but wasn't otherwise very suitable, e.g. here and here and generally we have objects that write themselves to HDF but are not based on a general data structure likeDataContainer
. I've had this sketch for a while now that should make it easier to implement this kind of classes.As a demonstration I've derived
DataContainer
from this new mixin, but generally it should be easy to move old classes to this format (expectGenericParameters
which handles HDF in a slightly different way).Test + docs will follow after discussion whether we find this useful.