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

DataArray apply and Numpy array container #408

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jsmariegaard
Copy link
Member

New method apply similar to methods in pandas and xarray for applying a mathematical function to all values in DataArray.

    >>> da.apply(np.sin)
    >>> da.apply(np.clip, 0.0, 1.0)
    >>> da.apply(lambda x: x**2 if x > 0 else 0)

@ecomodeller
Copy link
Member

Would be even nicer if we could support this syntax;

da2 = np.sqrt(da)

I can't really figure out how this works, but somehow it calls the __array_ufunc__ function.
https://github.com/pydata/xarray/blob/3d3b236df3f8f5982760e68e064eb0db2aacc4a2/xarray/core/arithmetic.py#L43

@jsmariegaard
Copy link
Member Author

Would be even nicer if we could support this syntax;

da2 = np.sqrt(da)

I can't really figure out how this works, but somehow it calls the __array_ufunc__ function. https://github.com/pydata/xarray/blob/3d3b236df3f8f5982760e68e064eb0db2aacc4a2/xarray/core/arithmetic.py#L43

Great idea! A hacky first attempt is now checked in.

Note also this medium write up about someone attempting something similar: https://towardsdatascience.com/wrapping-numpys-arrays-971e015e14bb

@jsmariegaard
Copy link
Member Author

image

@ecomodeller
Copy link
Member

This seem like the proper reference
https://numpy.org/devdocs/user/basics.dispatch.html#writing-custom-array-containers

@jsmariegaard jsmariegaard marked this pull request as draft September 6, 2022 05:47
@jsmariegaard
Copy link
Member Author

This PR will have to wait some weeks until we have time to do a proper implementation of Numpy array container such that a mikeio.DataArray can be considered truly array-like

@jsmariegaard jsmariegaard changed the title DataArray apply DataArray apply and Numpy array container Sep 9, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants