Skip to content

Conversation

alonks1234
Copy link

@alonks1234 alonks1234 commented Jan 2, 2024

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

@caniko caniko self-requested a review January 3, 2024 10:12
@caniko caniko added enhancement New feature or request NumpyModel Elements related to pydantic-numpy NumpyModel labels Jan 3, 2024
Copy link
Owner

@caniko caniko left a 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.

Copy link
Owner

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

Copy link
Owner

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:
Copy link
Owner

@caniko caniko Jan 3, 2024

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]]:
Copy link
Owner

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]()
Copy link
Owner

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:

Suggested change
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
Copy link
Owner

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:
Copy link
Owner

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
Copy link
Owner

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)
Copy link
Owner

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}}}"""
Copy link
Owner

@caniko caniko Jan 3, 2024

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.

@caniko caniko marked this pull request as draft January 3, 2024 10:26
@caniko caniko changed the title feat: Recursive NumpyModels for __eq__ and dump/load feat(NumpyModel): Recursive NumpyModels for __eq__ and dump/load Jan 3, 2024
@caniko caniko changed the title feat(NumpyModel): Recursive NumpyModels for __eq__ and dump/load feat(NumpyModel): __eq__ and dump/load support for nested NumpyModels Jan 3, 2024
@caniko
Copy link
Owner

caniko commented Jan 15, 2024

@alonks1234 will you finalize your PR?

@caniko caniko closed this Jan 19, 2024
@caniko
Copy link
Owner

caniko commented Jan 19, 2024

Abandoned by author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request NumpyModel Elements related to pydantic-numpy NumpyModel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants