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

Nested array reprs #2

Open
hameerabbasi opened this issue Sep 23, 2021 · 1 comment
Open

Nested array reprs #2

hameerabbasi opened this issue Sep 23, 2021 · 1 comment

Comments

@hameerabbasi
Copy link

I thought about this, and calculating the repr is indeed a hard problem, but it becomes easy if we exclude distributed/chunked arrays.

What I propose is:

  • every meta array have a __array_meta__(): duck_array which returns the inner array (as one can see for distributed arrays, this could require materialising the array).
  • In addition, there should be an __array_attrs__(html: bool): Dict[str, Union[int, bool, float, np.generic, str]] which should be on ALL arrays that want to be included into meta-arrays. This method returns a dict containing the elements it would like displayed. One can then use an implementation like the following:
def __repr__(self):
    attrs = {}
    type_names = []
    current_arr = self
    while current_arr is not None:
        # This formulation guards against duplicate attrs
        attrs = dict(**attrs, **current_arr.__array_attrs__(false))
        class_names.append(type(current_arr).__name__)
        current_arr = getattr(current_array, "__array_meta__", lambda: None)()
    return "<{super_type_name}: {attrs_repr}>".format(
        super_type_name=".".join(type_names),
        attrs_repr=", ".join(f"{k}: {v!r}" for k, v in attrs.items())
    )
@hameerabbasi
Copy link
Author

For a chunked representation, one could also have a third slot, __attr_aggregate__(attr_name): Callable[[Iterable[Union[int, bool, float, np.generic, str]]], Union[int, bool, float, np.generic, str]] which could aggregate the given attribute. This way, Dask (or other distributed/chunked array libraries) could easily aggregate the attrs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant