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

operation on complex number data #553

Closed
davidtrem opened this issue Aug 28, 2015 · 10 comments
Closed

operation on complex number data #553

davidtrem opened this issue Aug 28, 2015 · 10 comments

Comments

@davidtrem
Copy link
Contributor

Hello,

Would it be possible to add complex function operation on xray.DataArray ?

Function like np.sin, np.abs works properly on xray.DataArray containing complex numbers and
returns a DataArray (as expected).
However, np.real, np.imag, np.angle operation return an numpy.ndarray array instead of DataArray...

@davidtrem
Copy link
Contributor Author

A short example for convenience:

import xray
import numpy as np
arr = xray.DataArray(np.random.randn(2, 3) + 1j*np.random.randn(2, 3))
np.abs(arr)  # returns a xray DataArray
np.angle(arr)  # returns a numpy ndarray 

@davidtrem
Copy link
Contributor Author

I realized this is also an issue in Pandas. So I also reported here:
pandas-dev/pandas#10921

@shoyer
Copy link
Member

shoyer commented Aug 29, 2015

There's nothing we can do about np.angle, np.real and np.imag unfortunately -- these need to be updated on the numpy side to call __array_wrap__.

However, we could certainly add .real and .imag properties that access these computed values as DataArrays.

@davidtrem
Copy link
Contributor Author

That would be great to add .real and .imag .abs and .angle 👍

@rth
Copy link
Contributor

rth commented Oct 6, 2017

There is an open issue at numpy about this in numpy/numpy#6266

Also, for future reference, locally re-defining np.angle by removing the z = array(z) line from the official function appears to work well enough as a workaround, assuming the input is an xarray,

import numpy.core.numeric as _nx


def angle(z, deg=0):
    """Compute the angle of an xarray

    Parameters
    ----------
    z : array_like
        A complex number or sequence of complex numbers.
    deg : bool, optional
        Return angle in degrees if True, radians if False (default).

    Returns
    -------
    angle : ndarray or scalar
        The counterclockwise angle from the positive real axis on
        the complex plane, with dtype as numpy.float64.

    See: https://github.com/pydata/xarray/issues/553
         https://github.com/numpy/numpy/blob/v1.13.0/numpy/lib/function_base.py#L2072-L2115
    """
    if deg:
        fact = 180/pi
    else:
        fact = 1.0
    if (issubclass(z.dtype.type, _nx.complexfloating)):
        zimag = z.imag
        zreal = z.real
    else:
        zimag = 0
        zreal = z
    return np.arctan2(zimag, zreal)

@shoyer
Copy link
Member

shoyer commented Oct 6, 2017

You can also use xarray.ufuncs.angle() for this.

@rth
Copy link
Contributor

rth commented Oct 6, 2017

@shoyer Aww, great. Thanks for pointing this out.

@NotSqrt
Copy link
Contributor

NotSqrt commented Jun 4, 2018

@shoyer xarray.ufuncs.angle now triggers the DeprecationWarning added in #1962, and numpy/numpy#6266 is still open.

What is the recommended way to keep an DataArray when computing angles ?

Thanks !

@shoyer
Copy link
Member

shoyer commented Jun 4, 2018

The short answer is that we shouldn't remove xarray.ufuncs.angle until NumPy has a satisfactory overload mechanism for it. This might mean something like http://www.numpy.org/neps/nep-0018-array-function-protocol.html instead.

PendingDeprecationWarning is the weakest form of deprecation warning. Basically it means "We plan to deprecate it in the future, but haven't done so yet."

@NotSqrt
Copy link
Contributor

NotSqrt commented Jun 5, 2018

Thanks !

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

4 participants