-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add .imag
and .real
properties to NamedArray
#8365
Changes from all commits
d3edc96
1991fb3
01396c8
2193130
5358670
8d77be1
c2b12cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,16 +24,11 @@ | |
from xarray.namedarray._typing import ( | ||
_arrayfunction_or_api, | ||
_chunkedarray, | ||
_DType, | ||
_DType_co, | ||
_ScalarType_co, | ||
_ShapeType_co, | ||
) | ||
from xarray.namedarray.utils import ( | ||
_default, | ||
is_duck_dask_array, | ||
to_0d_object_array, | ||
) | ||
from xarray.namedarray.utils import _default, is_duck_dask_array, to_0d_object_array | ||
|
||
if TYPE_CHECKING: | ||
from numpy.typing import ArrayLike, NDArray | ||
|
@@ -46,6 +41,7 @@ | |
_Dim, | ||
_Dims, | ||
_DimsLike, | ||
_DType, | ||
_IntOrUnknown, | ||
_ScalarType, | ||
_Shape, | ||
|
@@ -516,6 +512,28 @@ def data(self, data: duckarray[Any, _DType_co]) -> None: | |
self._check_shape(data) | ||
self._data = data | ||
|
||
@property | ||
def imag(self) -> Self: | ||
""" | ||
The imaginary part of the array. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.imag | ||
""" | ||
return self._replace(data=self.data.imag) # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only use return self._new(data=self.data.imag) |
||
|
||
@property | ||
def real(self) -> Self: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea here. Follow the same idea but use _SupportsReal instead. |
||
""" | ||
The real part of the array. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.real | ||
""" | ||
return self._replace(data=self.data.real) # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea, dtype or shape has changed in this method so use |
||
|
||
def __dask_tokenize__(self) -> Hashable: | ||
# Use v.data, instead of v._data, in order to cope with the wrappers | ||
# around NetCDF and the like | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,7 @@ | |
import pytest | ||
|
||
from xarray.core.indexing import ExplicitlyIndexed | ||
from xarray.namedarray._typing import ( | ||
_arrayfunction_or_api, | ||
_DType_co, | ||
_ShapeType_co, | ||
) | ||
from xarray.namedarray._typing import _arrayfunction_or_api, _DType_co, _ShapeType_co | ||
from xarray.namedarray.core import NamedArray, from_array | ||
from xarray.namedarray.utils import _default | ||
|
||
|
@@ -171,6 +167,16 @@ def test_data(random_inputs: np.ndarray[Any, Any]) -> None: | |
named_array.data = np.random.random((3, 4)).astype(np.float64) | ||
|
||
|
||
def test_real_and_imag() -> None: | ||
named_array: NamedArray[Any, Any] | ||
named_array = NamedArray(["x"], np.arange(3) - 1j * np.arange(3)) | ||
expected_real = np.arange(3) | ||
assert np.array_equal(named_array.real.data, expected_real) | ||
|
||
expected_imag = -np.arange(3) | ||
assert np.array_equal(named_array.imag.data, expected_imag) | ||
Comment on lines
+170
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not testing dtypes here. They don't match.
The test should look something like this: def test_real_and_imag() -> None:
expected_real: np.ndarray[Any, np.dtype[np.float64]]
expected_real = np.arange(3, dtype=np.float64)
expected_imag: np.ndarray[Any, np.dtype[np.float64]]
expected_imag = -np.arange(3, dtype=np.float64)
arr: np.ndarray[Any, np.dtype[np.complex128]]
arr = expected_real + 1j * expected_imag
named_array: NamedArray[Any, np.dtype[np.complex128]]
named_array = NamedArray(["x"], arr)
actual_real: np.ndarray[Any, np.dtype[np.float64]] = named_array.real.data
assert np.array_equal(actual_real, expected_real)
assert actual_real.dtype == expected_real.dtype
actual_imag: np.ndarray[Any, np.dtype[np.float64]] = named_array.imag.data
assert np.array_equal(actual_imag, expected_imag)
assert actual_imag.dtype == expected_imag.dtype There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for the review... i'm going to address these in a follow up PR. |
||
|
||
|
||
# Additional tests as per your original class-based code | ||
@pytest.mark.parametrize( | ||
"data, dtype", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you use
imag
you're changing the dtype of the class, so it's not Self you're returning but rather a new NamedArray. Which won't match with self.This should similar to the function, like:
Here's how numpy does it:
https://github.com/numpy/numpy/blob/14bb214bca49b167abc375fa873466a811e62102/numpy/__init__.pyi#L1495-L1500
Variable will also need this style of typing.