-
Notifications
You must be signed in to change notification settings - Fork 125
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
Feature/impact write from hdf5 #606
Conversation
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 gets the job done, but I have to say that I find the overall structure of the functions a bit hard to understand. It currently iterates over all __dict__
entries, and then uses a large if/elif/else tree to do specific things with specific items. I would much prefer to instead define local "writer functions" and a mapping from attribute name or type to writer function. Then we can iterate over all attributes, check for a writer function or fall back to a default one. This would also help with some code duplications, specifically for writing all the tags. I'm thinking of something like this:
def write_str(name, value):
data.attrs[name] = value
def write_csr(name, value):
data.create_dataset(name, data=value.toarray())
def write_default(name, value):
data.create_dataset(name, data=value)
attr_writers = {str: write_str, sparse.csr_matrix: write_csr}
for (var_name, var_val) in self.__dict__.items():
# Fetch the write function if it exists for the type. If not, use the default one.
write_func = attr_writers.get(type(var_val), write_default)
# Call it with the attr name and value
write_func(var_name, var_val)
(The same goes for the reader)
@ThomasRoosli I think I am finished. Would you have a look? |
Thanks @peanutfun for restructuring especially the write_hdf5 function using type specific writers. And thanks for extending the tests. I am now very happy with the status of the code and the functionality it adds to the impact class. I am happy if we can merge this. |
@emanuel-schmid This new |
Very nice! 😁
My preference would be to make it a |
@emanuel-schmid I don't want to over-complicate this PR. I like your proposal a lot, but I would want to discuss it first and then have a follow-up PR. |
Changes proposed in this PR:
This PR fixes #
PR Author Checklist
develop
)PR Reviewer Checklist