From ed5a8834ddfb741ee66f32926dce24d1f52c6a9e Mon Sep 17 00:00:00 2001 From: Max Jones Date: Sat, 16 Jul 2022 15:58:42 -0500 Subject: [PATCH 1/7] Pull xarray's nbytes from nbytes attribute on arrays --- doc/api.rst | 1 - xarray/core/variable.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index 1036b476c83..c5dedf45370 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -282,7 +282,6 @@ ndarray attributes DataArray.shape DataArray.size DataArray.dtype - DataArray.nbytes DataArray.chunks diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 90edf652284..fba0d42e336 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -333,7 +333,7 @@ def shape(self): @property def nbytes(self): - return self.size * self.dtype.itemsize + return self.data.nbytes @property def _in_memory(self): From 07585ac299d6b7b2c5561ae33d6b527e18f968a1 Mon Sep 17 00:00:00 2001 From: Max Jones Date: Sat, 16 Jul 2022 21:54:08 -0500 Subject: [PATCH 2/7] Calculate nbytes if it doesn't exist --- xarray/core/variable.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index fba0d42e336..65d9b4fe8fc 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -333,7 +333,10 @@ def shape(self): @property def nbytes(self): - return self.data.nbytes + if hasattr(self.data, "nbytes"): + return self.data.nbytes + else: + return self.size * self.dtype.itemsize @property def _in_memory(self): From 9b7d55951a9d0632ad531f91c57419843d2d7b78 Mon Sep 17 00:00:00 2001 From: Max Jones Date: Wed, 20 Jul 2022 14:25:10 -0400 Subject: [PATCH 3/7] Add test --- xarray/tests/test_array_api.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xarray/tests/test_array_api.py b/xarray/tests/test_array_api.py index 8e378054c29..affa10dc26e 100644 --- a/xarray/tests/test_array_api.py +++ b/xarray/tests/test_array_api.py @@ -43,6 +43,12 @@ def test_indexing(arrays) -> None: assert_equal(actual, expected) +def test_properties(arrays) -> None: + np_arr, xp_arr = arrays + assert np_arr.nbytes == 48 + assert xp_arr.nbytes == 48 + + def test_reorganizing_operation(arrays) -> None: np_arr, xp_arr = arrays expected = np_arr.transpose() From d26d0e5ab3a9f81f67f898d4aa385fec9ee8d497 Mon Sep 17 00:00:00 2001 From: Max Jones Date: Wed, 20 Jul 2022 15:41:28 -0400 Subject: [PATCH 4/7] Add docstrings --- xarray/core/dataarray.py | 6 ++++++ xarray/core/dataset.py | 6 ++++++ xarray/core/variable.py | 3 +++ 3 files changed, 15 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 8ef05361193..4a841f0fbdc 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -646,6 +646,12 @@ def size(self) -> int: @property def nbytes(self) -> int: + """ + Total bytes consumed by the elements of this DataArray's data. + + If the backend data array does not include ``nbytes``, estimates + the bytes consumed based on the ``size`` and ``dtype``. + """ return self.variable.nbytes @property diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4849738f453..f0ea7e46066 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1378,6 +1378,12 @@ def __array__(self, dtype=None): @property def nbytes(self) -> int: + """ + Total bytes consumed by the data arrays of all variables in this dataset. + + If the backend array for any variable does not include ``nbytes``, estimates + the total bytes for that array based on the ``size`` and ``dtype``. + """ return sum(v.nbytes for v in self.variables.values()) @property diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 0b5e95b5fa7..8fe7559076d 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -335,6 +335,9 @@ def shape(self): @property def nbytes(self): + """ + Total bytes consumed by the elements of the data array. + """ if hasattr(self.data, "nbytes"): return self.data.nbytes else: From ddf199c3bf50e8109e1056a6fa04149be6a16752 Mon Sep 17 00:00:00 2001 From: Max Jones Date: Thu, 21 Jul 2022 13:02:05 -0400 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/core/variable.py | 2 +- xarray/tests/test_array_api.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 8fe7559076d..5827b90ad75 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -334,7 +334,7 @@ def shape(self): return self._data.shape @property - def nbytes(self): + def nbytes(self) -> int: """ Total bytes consumed by the elements of the data array. """ diff --git a/xarray/tests/test_array_api.py b/xarray/tests/test_array_api.py index affa10dc26e..649bf3eec2b 100644 --- a/xarray/tests/test_array_api.py +++ b/xarray/tests/test_array_api.py @@ -45,8 +45,8 @@ def test_indexing(arrays) -> None: def test_properties(arrays) -> None: np_arr, xp_arr = arrays - assert np_arr.nbytes == 48 - assert xp_arr.nbytes == 48 + assert np_arr.nbytes == np_arr.data.nbytes + assert xp_arr.nbytes == np_arr.data.nbytes def test_reorganizing_operation(arrays) -> None: From 98dab3be48d6a63685e28b10b3ebcc82f744de07 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 21 Jul 2022 20:02:48 -0600 Subject: [PATCH 6/7] Add sparse variable test --- xarray/tests/test_sparse.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/tests/test_sparse.py b/xarray/tests/test_sparse.py index 5501d38fc48..5395845d63a 100644 --- a/xarray/tests/test_sparse.py +++ b/xarray/tests/test_sparse.py @@ -274,6 +274,9 @@ def setUp(self): self.data = sparse.random((4, 6), random_state=0, density=0.5) self.var = xr.Variable(("x", "y"), self.data) + def test_nbytes(self): + assert self.var.nbytes == self.data.nbytes + def test_unary_op(self): assert_sparse_equal(-self.var.data, -self.data) assert_sparse_equal(abs(self.var).data, abs(self.data)) From bba99754799a6cd9dad76b04aba3277376a8dfb5 Mon Sep 17 00:00:00 2001 From: dcherian Date: Fri, 22 Jul 2022 10:50:05 -0600 Subject: [PATCH 7/7] Add whats-new note --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index efecc469106..67f697597cf 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -34,6 +34,8 @@ Deprecations Bug fixes ~~~~~~~~~ +- :py:attr:`DataArray.nbytes` now uses the ``nbytes`` property of the underlying array if available. + By `Max Jones `_. Documentation ~~~~~~~~~~~~~