From 343d277cc274e4c9f28710ad87f274bbe1be2a05 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 27 Jun 2021 14:21:11 +0200 Subject: [PATCH 1/7] Update indexing.py --- xarray/core/indexing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index 1ace4db241d..9f5ef76f07a 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -367,7 +367,7 @@ def __getitem__(self, key): class LazilyIndexedArray(ExplicitlyIndexedNDArrayMixin): """Wrap an array to make basic and outer indexing lazy.""" - __slots__ = ("array", "key") + __slots__ = ("array", "key", "_cache") def __init__(self, array, key=None): """ @@ -404,7 +404,7 @@ def _updated_key(self, new_key): return BasicIndexer(full_key) return OuterIndexer(full_key) - @property + @pd.util.cache_readonly def shape(self): shape = [] for size, k in zip(self.array.shape, self.key.tuple): From ef7f1ef0c6ebc4d85345fcba7cdb69285eb4e3bc Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 28 Jul 2021 09:34:49 +0200 Subject: [PATCH 2/7] Cache shape in Variable --- xarray/core/variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index f228ef43e32..5c7942cd536 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -326,7 +326,7 @@ def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): def dtype(self): return self._data.dtype - @property + @pd.util.cache_readonly def shape(self): return self._data.shape From 59bd433e1db79fa61f6a622405ed57ebd49b334e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 28 Jul 2021 09:52:50 +0200 Subject: [PATCH 3/7] Update variable.py --- xarray/core/variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 5c7942cd536..dc69ba634e8 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -291,7 +291,7 @@ class Variable(AbstractArray, NdimSizeLenMixin, VariableArithmetic): they can use more complete metadata in context of coordinate labels. """ - __slots__ = ("_dims", "_data", "_attrs", "_encoding") + __slots__ = ("_dims", "_data", "_attrs", "_encoding", "_cache") def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): """ From c5f5db735f56e97aeff55ebc03b2f29ca20cb5a0 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 8 Jul 2022 20:43:43 +0200 Subject: [PATCH 4/7] Add read only cached_properties --- xarray/core/utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index a87beafaf19..ace8c0047df 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -977,3 +977,11 @@ def contains_only_dask_or_numpy(obj) -> bool: for var in obj.variables.values() ] ) + + +class cached_property(functools.cached_property): + """Read only version of functools.cached_property.""" + + def __set__(self, instance, val): + """Raise an error when attempting to set a cached property.""" + raise AttributeError("Can't set attribute") From c16f31a3f4bf74893ae4b8111a9e0553b25f3068 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 8 Jul 2022 20:44:11 +0200 Subject: [PATCH 5/7] add cached property to shape --- xarray/core/indexing.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index 660c7e0b8e6..3507c686e82 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -19,7 +19,7 @@ from .options import OPTIONS from .pycompat import dask_version, integer_types, is_duck_dask_array, sparse_array_type from .types import T_Xarray -from .utils import either_dict_or_kwargs, get_valid_numpy_dtype +from .utils import cached_property, either_dict_or_kwargs, get_valid_numpy_dtype if TYPE_CHECKING: from .indexes import Index @@ -465,7 +465,7 @@ def __getitem__(self, key): class LazilyIndexedArray(ExplicitlyIndexedNDArrayMixin): """Wrap an array to make basic and outer indexing lazy.""" - __slots__ = ("array", "key", "_cache") + __slots__ = ("array", "key", "__dict__") def __init__(self, array, key=None): """ @@ -502,7 +502,7 @@ def _updated_key(self, new_key): return BasicIndexer(full_key) return OuterIndexer(full_key) - @pd.util.cache_readonly + @cached_property def shape(self): shape = [] for size, k in zip(self.array.shape, self.key.tuple): @@ -545,7 +545,7 @@ def __repr__(self): class LazilyVectorizedIndexedArray(ExplicitlyIndexedNDArrayMixin): """Wrap an array to make vectorized indexing lazy.""" - __slots__ = ("array", "key") + __slots__ = ("array", "key", "__dict__") def __init__(self, array, key): """ @@ -561,7 +561,7 @@ def __init__(self, array, key): self.key = _arrayize_vectorized_indexer(key, array.shape) self.array = as_indexable(array) - @property + @cached_property def shape(self): return np.broadcast(*self.key.tuple).shape @@ -1360,7 +1360,7 @@ def transpose(self, order): class PandasIndexingAdapter(ExplicitlyIndexedNDArrayMixin): """Wrap a pandas.Index to preserve dtypes and handle explicit indexing.""" - __slots__ = ("array", "_dtype") + __slots__ = ("array", "_dtype", "__dict__") def __init__(self, array: pd.Index, dtype: DTypeLike = None): self.array = utils.safe_cast_to_index(array) @@ -1384,7 +1384,7 @@ def __array__(self, dtype: DTypeLike = None) -> np.ndarray: array = array.astype("object") return np.asarray(array.values, dtype=dtype) - @property + @cached_property def shape(self) -> tuple[int]: return (len(self.array),) @@ -1460,7 +1460,7 @@ class PandasMultiIndexingAdapter(PandasIndexingAdapter): """ - __slots__ = ("array", "_dtype", "level", "adapter") + __slots__ = ("array", "_dtype", "level", "adapter", "__dict__") def __init__( self, From 8ac33edd7a830a61004f282c8caf0ced6b2ab0cc Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 10 Jul 2022 10:02:05 +0200 Subject: [PATCH 6/7] use cached_property --- xarray/core/variable.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 5dcd5aacee6..bbc3eacf829 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -48,6 +48,7 @@ NdimSizeLenMixin, OrderedSet, _default, + cached_property, decode_numpy_dict_values, drop_dims_from_indexers, either_dict_or_kwargs, @@ -292,7 +293,7 @@ class Variable(AbstractArray, NdimSizeLenMixin, VariableArithmetic): they can use more complete metadata in context of coordinate labels. """ - __slots__ = ("_dims", "_data", "_attrs", "_encoding", "_cache") + __slots__ = ("_dims", "_data", "_attrs", "_encoding", "__dict__") def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): """ @@ -327,7 +328,7 @@ def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False): def dtype(self): return self._data.dtype - @pd.util.cache_readonly + @cached_property def shape(self): return self._data.shape From 3d117c320a143fd3d70b67ed2c707e1f9174cb6b Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 10 Jul 2022 16:34:28 +0200 Subject: [PATCH 7/7] Update indexing.py --- xarray/core/indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index 7d9b9c7c56a..07cfc86d687 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -1468,7 +1468,7 @@ class PandasMultiIndexingAdapter(PandasIndexingAdapter): """ - __slots__ = ("array", "_dtype", "level", "adapter", "__dict__") + __slots__ = ("array", "_dtype", "level", "adapter") def __init__( self,