From b3b9e4693569c54f88000503de3935551be2ea84 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 29 Apr 2021 09:49:37 +0100 Subject: [PATCH 1/7] Fix exception when display_expand_data=False for file-backed array. --- xarray/core/formatting.py | 2 +- xarray/tests/test_formatting.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 31a1b777f43..cf8b0cdd803 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -510,7 +510,7 @@ def array_repr(arr): if _get_boolean_with_default("display_expand_data", default=True): data_repr = short_data_repr(arr) else: - data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"]) + data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"]) summary = [ "".format(type(arr).__name__, name_str, dim_summary(arr)), diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 1e2a968debe..71bef1bc182 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -8,6 +8,8 @@ import xarray as xr from xarray.core import formatting +from . import requires_netCDF4 + class TestFormatting: def test_get_indexer_at_least_n_items(self): @@ -472,6 +474,25 @@ def test_large_array_repr_length(): assert len(result) < 50 +@requires_netCDF4 +def test_repr_file_collapsed(tmp_path): + arr = xr.DataArray(np.random.randn(100), dims="test") + arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4") + + with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options( + display_expand_data=False + ): + actual = formatting.array_repr(arr) + expected = dedent( + """\ + + ... + Dimensions without coordinates: test""" + ) + + assert actual == expected + + @pytest.mark.parametrize( "display_max_rows, n_vars, n_attr", [(50, 40, 30), (35, 40, 30), (11, 40, 30), (1, 40, 30)], From e7060d9bf6e58bc5b0c38a193f4f8cd8c3e7034c Mon Sep 17 00:00:00 2001 From: Tom White Date: Tue, 4 May 2021 09:15:50 +0100 Subject: [PATCH 2/7] Update xarray/core/formatting.py Co-authored-by: keewis --- xarray/core/formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index cf8b0cdd803..4f538dd36d6 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -507,7 +507,7 @@ def array_repr(arr): else: name_str = "" - if _get_boolean_with_default("display_expand_data", default=True): + if _get_boolean_with_default("display_expand_data", default=True) or isinstance(arr.data, MemoryCachedArray): data_repr = short_data_repr(arr) else: data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"]) From 13b3c87d5ae3331fc8be1ef74c5e69a6cf3e974c Mon Sep 17 00:00:00 2001 From: Tom White Date: Tue, 4 May 2021 09:15:57 +0100 Subject: [PATCH 3/7] Update xarray/tests/test_formatting.py Co-authored-by: keewis --- xarray/tests/test_formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 71bef1bc182..79bd003aa12 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -476,7 +476,7 @@ def test_large_array_repr_length(): @requires_netCDF4 def test_repr_file_collapsed(tmp_path): - arr = xr.DataArray(np.random.randn(100), dims="test") + arr = xr.DataArray(np.arange(100), dims="test") arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4") with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options( From 4a79edfde08d250fdeb89132ad179a54e6cc724b Mon Sep 17 00:00:00 2001 From: Tom White Date: Tue, 4 May 2021 09:19:30 +0100 Subject: [PATCH 4/7] Fix import and test_repr_file_collapsed --- xarray/core/formatting.py | 5 ++++- xarray/tests/test_formatting.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 4f538dd36d6..e639a83500e 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -11,6 +11,7 @@ from pandas.errors import OutOfBoundsDatetime from .duck_array_ops import array_equiv +from .indexing import MemoryCachedArray from .options import OPTIONS, _get_boolean_with_default from .pycompat import dask_array_type, sparse_array_type from .utils import is_duck_array @@ -507,7 +508,9 @@ def array_repr(arr): else: name_str = "" - if _get_boolean_with_default("display_expand_data", default=True) or isinstance(arr.data, MemoryCachedArray): + if _get_boolean_with_default("display_expand_data", default=True) or isinstance( + arr.data, MemoryCachedArray + ): data_repr = short_data_repr(arr) else: data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"]) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 79bd003aa12..13be46224e3 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -486,7 +486,7 @@ def test_repr_file_collapsed(tmp_path): expected = dedent( """\ - ... + 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 86 87 88 89 90 91 92 93 94 95 96 97 98 99 Dimensions without coordinates: test""" ) From e00fad55ca69c2d35571243bc59415409dfd867c Mon Sep 17 00:00:00 2001 From: keewis Date: Tue, 4 May 2021 21:11:01 +0200 Subject: [PATCH 5/7] Update xarray/core/formatting.py --- xarray/core/formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index e639a83500e..c3e7187abed 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -509,7 +509,7 @@ def array_repr(arr): name_str = "" if _get_boolean_with_default("display_expand_data", default=True) or isinstance( - arr.data, MemoryCachedArray + arr.variable._data, MemoryCachedArray ): data_repr = short_data_repr(arr) else: From 44e4a4286e3cc7e79226d16ee432ca8ae868c04c Mon Sep 17 00:00:00 2001 From: Tom White Date: Wed, 5 May 2021 10:06:34 +0100 Subject: [PATCH 6/7] Test larger array in test_repr_file_collapsed --- xarray/tests/test_formatting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 13be46224e3..41f0f7d63bb 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -476,7 +476,7 @@ def test_large_array_repr_length(): @requires_netCDF4 def test_repr_file_collapsed(tmp_path): - arr = xr.DataArray(np.arange(100), dims="test") + arr = xr.DataArray(np.arange(100000), dims="test") arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4") with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options( @@ -485,8 +485,8 @@ def test_repr_file_collapsed(tmp_path): actual = formatting.array_repr(arr) expected = dedent( """\ - - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 86 87 88 89 90 91 92 93 94 95 96 97 98 99 + + [100000 values with dtype=int64] Dimensions without coordinates: test""" ) From 969749cd55c5aff4923921cbfa49a19f41e9ce1b Mon Sep 17 00:00:00 2001 From: Tom White Date: Wed, 5 May 2021 11:10:26 +0100 Subject: [PATCH 7/7] Change test_repr_file_collapsed to work on all platforms --- xarray/tests/test_formatting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 41f0f7d63bb..e823a67f2db 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -476,7 +476,7 @@ def test_large_array_repr_length(): @requires_netCDF4 def test_repr_file_collapsed(tmp_path): - arr = xr.DataArray(np.arange(100000), dims="test") + arr = xr.DataArray(np.arange(300), dims="test") arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4") with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options( @@ -485,8 +485,8 @@ def test_repr_file_collapsed(tmp_path): actual = formatting.array_repr(arr) expected = dedent( """\ - - [100000 values with dtype=int64] + + array([ 0, 1, 2, ..., 297, 298, 299]) Dimensions without coordinates: test""" )