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

Implement __array__() method for DNDarrays #1153

Closed
ClaudiaComito opened this issue May 18, 2023 · 0 comments · Fixed by #1154
Closed

Implement __array__() method for DNDarrays #1153

ClaudiaComito opened this issue May 18, 2023 · 0 comments · Fixed by #1154

Comments

@ClaudiaComito
Copy link
Contributor

ClaudiaComito commented May 18, 2023

Related
#1031

Feature functionality
Provide a view of the process-local array (a slice of the global array) as a numpy ndarray.

This is very different from the dndarray.numpy() method that provides a copy of the global array (gathered on each MPI process) as a numpy ndarray.

Potential confusion will be addressed in the documentation.

Additional context
Required for interoperability with the numpy ecosystem including the xarray library. Missing __array__ method results in numpy not recognizing the DNDarray as an array object, e.g.:

>>> import heat as ht
>>> import numpy as np
>>> a = ht.arange(10)
>>> a
DNDarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=ht.int32, device=cpu:0, split=None)
>>> np.asarray(a)
array([DNDarray(0, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(1, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(2, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(3, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(4, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(5, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(6, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(7, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(8, dtype=ht.int32, device=cpu:0, split=None),
       DNDarray(9, dtype=ht.int32, device=cpu:0, split=None)],
      dtype=object)

whereas interoperability requires:

>>> np.asarray(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant