/
container.py
287 lines (211 loc) · 7.91 KB
/
container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import logging
import numpy as np
from ..decorators import _manage_log_level_via_verbosity
from ..docstring import _docstring_substitution_definitions
from ..functions import atol, rtol
logger = logging.getLogger(__name__)
class Container:
"""Mixin class for storing object components.
.. versionadded:: (cfdm) 1.7.0
"""
def __repr__(self):
"""Called by the `repr` built-in function.
x.__repr__() <==> repr(x)
.. versionadded:: (cfdm) 1.7.0
"""
return f"<{self.__class__.__name__}: {self}>"
def __str__(self):
"""Called by the `str` built-in function.
x.__str__() <==> str(x)
.. versionadded:: (cfdm) 1.7.0
"""
out = sorted(self._components)
return ", ".join(out)
def __docstring_substitutions__(self):
"""Define docstring substitutions for the class hierarchy.
The defined substitutions apply to this class along with all
of its subclasses.
These are in addtion to, and take precendence over, docstring
substitutions defined by the base classes of this class.
See `_docstring_substitutions` for details.
.. versionaddedd:: (cfdm) 1.8.7.0
:Returns:
`dict`
The docstring substitutions that have been applied.
"""
return _docstring_substitution_definitions
def __docstring_package_depth__(self):
"""Return the package depth for {{package}} substitutions.
See `_docstring_package_depth` for details.
"""
return 0
@property
def _atol(self):
"""Internal alias for `{{package}}.atol`.
An alias is necessary to avoid a name clash with the keyword
argument of identical name (`atol`) in calling functions.
"""
return atol().value
@property
def _rtol(self):
"""Internal alias for `{{package}}.rtol`.
An alias is necessary to avoid a name clash with the keyword
argument of identical name (`rtol`) in calling functions.
"""
return rtol().value
def _equals(
self,
x,
y,
rtol=None,
atol=None,
ignore_data_type=False,
basic=False,
**kwargs,
):
"""Whether two objects are the same.
Equality either uses one or other of the objects `!equals`
methods, or casts them as numpy arrays and carried aout
numerically tolerant equality checks.
.. versionadded:: (cfdm) 1.7.0
:Parameters:
{{atol: number, optional}}
{{rtol: number, optional}}
"""
if basic:
# x and y can be compared with the basic == operator
return bool(x == y)
if rtol is None:
rtol = self._rtol
else:
rtol = float(rtol)
if atol is None:
atol = self._atol
else:
atol = float(atol)
kwargs["ignore_data_type"] = ignore_data_type
kwargs["rtol"] = rtol
kwargs["atol"] = atol
eq = getattr(x, "equals", None)
if callable(eq):
# --------------------------------------------------------
# x has a callable "equals" method
# --------------------------------------------------------
return eq(y, **kwargs)
eq = getattr(y, "equals", None)
if callable(eq):
# --------------------------------------------------------
# y has a callable "equals" method
# --------------------------------------------------------
return eq(x, **kwargs)
x = np.asanyarray(x)
y = np.asanyarray(y)
if x.shape != y.shape:
return False
# # ------------------------------------------------------------
# # Cast x and y as numpy arrays
# # ------------------------------------------------------------
# if not isinstance(x, np.ndarray):
# x = np.asanyarray(x)
#
# if not isinstance(y, np.ndarray):
# y = np.asanyarray(y)
# THIS IS WHERE SOME NUMPY FUTURE WARNINGS ARE COMING FROM
if not ignore_data_type and x.dtype != y.dtype:
if x.dtype.kind not in ("S", "U") and y.dtype.kind not in (
"S",
"U",
):
return False
x_is_masked = np.ma.isMA(x)
y_is_masked = np.ma.isMA(y)
if not (x_is_masked or y_is_masked):
try:
return bool(np.allclose(x, y, rtol=rtol, atol=atol))
except (IndexError, NotImplementedError, TypeError):
return bool(np.all(x == y))
else:
if x_is_masked and y_is_masked:
if (x.mask != y.mask).any():
return False
elif (x_is_masked and x.mask.any()) or (
y_is_masked and y.mask.any()
):
return False
try:
return bool(np.ma.allclose(x, y, rtol=rtol, atol=atol))
except (IndexError, NotImplementedError, TypeError):
out = np.ma.all(x == y)
if out is np.ma.masked:
return True
else:
return bool(out)
@_manage_log_level_via_verbosity
def _equals_preprocess(self, other, verbose=None, ignore_type=False):
"""Common preprocessing prior to testing of equality.
* If the LHS operand is (object identity) the RHS operand then
return True.
* If ignore_type=False and the LHS operand is not of exactly
the same type as the RHS operand then return False.
* If ignore_type=True and the LHS operand is not of exactly
the same type as the RHS operand then instantiate a new
instance based on the the RHS class and return it.
.. versionadded:: (cfdm) 1.7.0
"""
# Check for object identity
if self is other:
return True
# Check that each object is of compatible type
if ignore_type:
if not isinstance(other, self.__class__):
other = type(self)(source=other, copy=False)
elif not isinstance(other, self.__class__):
logger.info(
f"{self.__class__.__name__}: Incompatible type: {type(other)}"
)
return False
return other
def _iter(self, body, pre=None, post=None, short=False, **kwargs):
"""General purpose iterator.
.. versionadded:: (cfdm) 1.8.9.0
:Parameters:
body: iterable
An iterable to step through.
pre: sequence of iterable, optional
Iterables to step through before *body* is iterated.
post: sequence of iterable, optional
Iterables to step through after *body* has been
iterated.
short: `bool`, optional
If True then stop after the first element of either
*pre*, *body* or *post* is realised.
kwargs: optional
Ignored.
:Return:
generator
The elements of *pre*, *body* and *post*.
"""
if pre:
for it in pre:
for x in it:
yield x
if short:
return
for x in body:
yield x
if short:
return
if post:
for it in post:
for x in it:
yield x
if short:
return
def _package(self):
"""Return the name of the package in which this class resides.
:Returns:
`str`
The package name
"""
depth = self.__class__._docstring_package_depth(self.__class__)
return ".".join(self.__module__.split(".")[0 : depth + 1])