-
Notifications
You must be signed in to change notification settings - Fork 7
feat(NumpyModel): __eq__
and dump/load support for nested NumpyModels
#27
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
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.
Thank you for the PR, and welcome as one of the first contributors to the project! 🚀
This looks like a good first draft, please look at my comments.
Don't hesitate to mark your PR as a draft unless you are absolutely certain your code is ready for a final review before being merged.
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.
Please move to test_np_model.py
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.
Same as the other test file:
- Move to correct file
- Use
unittest
import numpy as np | ||
|
||
|
||
class TestEQ: |
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.
Can we also test __hash__
? Provided frozen=True
; still not sure if numpy arrays can be hashed.
for k, v in self.model_dump(exclude_unset=True).items(): | ||
if isinstance(v, np.ndarray): | ||
@staticmethod | ||
def _dump_numpy_split_dict(model: "NumpyModel") -> tuple[dict[str, np.ndarray], dict[str, Any]]: |
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.
This function is complicated enough for it to warrant a doc string.
if isinstance(v, np.ndarray): | ||
@staticmethod | ||
def _dump_numpy_split_dict(model: "NumpyModel") -> tuple[dict[str, np.ndarray], dict[str, Any]]: | ||
ndarray_field_to_array = dict[str, np.ndarray]() |
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.
Don't parametrize the dict, but add type hints to the variable itself, that way we stay close to PEP8:
ndarray_field_to_array = dict[str, np.ndarray]() | |
ndarray_field_to_array: dict[str, np.ndarray] = {} |
Apply this to the others please
for sub_k_other, sub_v_other in submodel_other_field_to_value.items(): | ||
other_field_to_value[f"{k}/{sub_k_other}"] = sub_v_other | ||
elif isinstance(v, np.ndarray): | ||
ndarray_field_to_array[k] = v |
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.
Same.
from pathlib import Path | ||
|
||
|
||
class TestDump: |
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.
Please inherit from unittest.IsolatedTestCase
c.dump(path, "multilevel_model_test") | ||
|
||
new_c = C.load(path, "multilevel_model_test") | ||
assert new_c == c No newline at end of file |
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.
Use self.AssertEquals
other_ndarray_field_to_array, other_other_field_to_value = other._dump_numpy_split_dict() | ||
|
||
self_ndarray_field_to_array, self_other_field_to_value = self._dump_numpy_split_dict(self) | ||
other_ndarray_field_to_array, other_other_field_to_value = other._dump_numpy_split_dict(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.
Whitespace on next line as before, please.
|
||
|
||
def extract_dirs_to_dict(dir_dict: dict[str, Any]) -> dict[str, Any]: | ||
""" Extract from a flat dict of {a/b/c : value} to {a: {b: {c: value}}}""" |
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.
You need to explain your nested dump and load strategy in the code somewhere so that newcomers can understand how different components work together to provide the respective utility to load and dump. Use doc strings, and not comments.
You also need to include a description for each argument.
__eq__
and dump/load
__eq__
and dump/load__eq__
and dump/load support for nested NumpyModels
@alonks1234 will you finalize your PR? |
Abandoned by author. |
Adds support for equality comparison and dump/load of nested NumpyModels.
The implementation is a recursive conversion of dicts of fields containing other dicts to a single "flat" dict matching fields to paths, which correspond nicely with file paths.
Fixes:
#25, #26