forked from ESSS/pytest-regressions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndarrays_regression.py
336 lines (290 loc) · 13 KB
/
ndarrays_regression.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from pytest_regressions.common import perform_regression_check, import_error_message
class NDArraysRegressionFixture:
"""
NumPy NPZ regression fixture implementation used on ndarrays_regression fixture.
"""
THRESHOLD = 100
ROWFORMAT = "{:>15s} {:>20s} {:>20s} {:>20s}\n"
def __init__(self, datadir, original_datadir, request):
"""
:type datadir: Path
:type original_datadir: Path
:type request: FixtureRequest
"""
self._tolerances_dict = {}
self._default_tolerance = {}
self.request = request
self.datadir = datadir
self.original_datadir = original_datadir
self._force_regen = False
self._with_test_class_names = False
def _check_data_types(self, key, obtained_array, expected_array):
"""
Check if data type of obtained and expected arrays are the same. Fail if not.
Helper method used in _check_fn method.
"""
try:
import numpy as np
except ModuleNotFoundError:
raise ModuleNotFoundError(import_error_message("NumPy"))
__tracebackhide__ = True
if obtained_array.dtype != expected_array.dtype:
# Check if both data types are comparable as numbers (float, int, short, bytes, etc...)
if np.issubdtype(obtained_array.dtype, np.number) and np.issubdtype(
expected_array.dtype, np.number
):
return
# Check if both data types are comparable as strings
if np.issubdtype(obtained_array.dtype, str) and np.issubdtype(
expected_array.dtype, str
):
return
# In case they are not, assume they are not comparable
error_msg = (
"Data types are not the same.\n"
f"key: {key}\n"
f"Obtained: {obtained_array.dtype}\n"
f"Expected: {expected_array.dtype}\n"
)
raise AssertionError(error_msg)
def _check_data_shapes(self, key, obtained_array, expected_array):
"""
Check if obtained and expected arrays have the same size.
Helper method used in _check_fn method.
"""
__tracebackhide__ = True
if obtained_array.shape != expected_array.shape:
error_msg = (
"Shapes are not the same.\n"
f"Key: {key}\n"
f"Obtained: {obtained_array.shape}\n"
f"Expected: {expected_array.shape}\n"
)
raise AssertionError(error_msg)
def _check_fn(self, obtained_filename, expected_filename):
"""
Check if dict contents dumped to a file match the contents in expected file.
:param str obtained_filename:
:param str expected_filename:
"""
try:
import numpy as np
except ModuleNotFoundError:
raise ModuleNotFoundError(import_error_message("NumPy"))
__tracebackhide__ = True
# Turn result of np.load into a dictionary, such that the files are closed immediately.
obtained_data = dict(np.load(str(obtained_filename)))
expected_data = dict(np.load(str(expected_filename)))
# Check mismatches in the keys.
if set(obtained_data) != set(expected_data):
error_msg = (
"They keys in the obtained results differ from the expected results.\n"
)
error_msg += " Matching keys: "
error_msg += str(list(set(obtained_data) & set(expected_data)))
error_msg += "\n"
error_msg += " New in obtained: "
error_msg += str(list(set(obtained_data) - set(expected_data)))
error_msg += "\n"
error_msg += " Missing from obtained: "
error_msg += str(list(set(expected_data) - set(obtained_data)))
error_msg += "\n"
error_msg += "To update values, use --force-regen option.\n\n"
raise AssertionError(error_msg)
# Compare the contents of the arrays.
comparison_tables_dict = {}
for k, obtained_array in obtained_data.items():
expected_array = expected_data.get(k)
tolerance_args = self._tolerances_dict.get(k, self._default_tolerance)
self._check_data_types(k, obtained_array, expected_array)
self._check_data_shapes(k, obtained_array, expected_array)
if np.issubdtype(obtained_array.dtype, np.inexact):
not_close_mask = ~np.isclose(
obtained_array,
expected_array,
equal_nan=True,
**tolerance_args,
)
else:
not_close_mask = obtained_array != expected_array
if np.any(not_close_mask):
if not_close_mask.ndim == 0:
diff_ids = [()]
else:
diff_ids = np.array(np.nonzero(not_close_mask)).T
comparison_tables_dict[k] = (
expected_array.size,
expected_array.shape,
diff_ids,
obtained_array[not_close_mask],
expected_array[not_close_mask],
)
if len(comparison_tables_dict) > 0:
error_msg = "Values are not sufficiently close.\n"
error_msg += "To update values, use --force-regen option.\n\n"
for k, (
size,
shape,
diff_ids,
obtained_array,
expected_array,
) in comparison_tables_dict.items():
# Summary
error_msg += f"{k}:\n Shape: {shape}\n"
pct = 100 * len(diff_ids) / size
error_msg += (
f" Number of differences: {len(diff_ids)} / {size} ({pct:.1f}%)\n"
)
if np.issubdtype(obtained_array.dtype, np.number) and len(diff_ids) > 1:
error_msg += (
" Statistics are computed for differing elements only.\n"
)
abs_errors = abs(obtained_array - expected_array)
error_msg += " Stats for abs(obtained - expected):\n"
error_msg += f" Max: {abs_errors.max()}\n"
error_msg += f" Mean: {abs_errors.mean()}\n"
error_msg += f" Median: {np.median(abs_errors)}\n"
error_msg += (
f" Stats for abs(obtained - expected) / abs(expected):\n"
)
expected_nonzero = np.array(np.nonzero(expected_array)).T
rel_errors = abs(
(
obtained_array[expected_nonzero]
- expected_array[expected_nonzero]
)
/ expected_array[expected_nonzero]
)
if len(rel_errors) != len(abs_errors):
pct = 100 * len(rel_errors) / len(abs_errors)
error_msg += f" Number of (differing) non-zero expected results: {len(rel_errors)} / {len(abs_errors)} ({pct:.1f}%)\n"
error_msg += f" Relative errors are computed for the non-zero expected results.\n"
else:
rel_errors = abs(
(obtained_array - expected_array) / expected_array
)
error_msg += f" Max: {rel_errors.max()}\n"
error_msg += f" Mean: {rel_errors.mean()}\n"
error_msg += f" Median: {np.median(rel_errors)}\n"
# Details results
error_msg += " Individual errors:\n"
if len(diff_ids) > self.THRESHOLD:
error_msg += (
f" Only showing first {self.THRESHOLD} mismatches.\n"
)
diff_ids = diff_ids[: self.THRESHOLD]
obtained_array = obtained_array[: self.THRESHOLD]
expected_array = expected_array[: self.THRESHOLD]
error_msg += self.ROWFORMAT.format(
"Index",
"Obtained",
"Expected",
"Difference",
)
for diff_id, obtained, expected in zip(
diff_ids, obtained_array, expected_array
):
diff_id_str = ", ".join(str(i) for i in diff_id)
if len(diff_id) != 1:
diff_id_str = f"({diff_id_str})"
error_msg += self.ROWFORMAT.format(
diff_id_str,
str(obtained),
str(expected),
str(obtained - expected)
if isinstance(obtained, np.number)
else "",
)
error_msg += "\n"
raise AssertionError(error_msg)
def _dump_fn(self, data_object, filename):
"""
Dump dict contents to the given filename
:param Dict[str, np.ndarray] data_object:
:param str filename:
"""
try:
import numpy as np
except ModuleNotFoundError:
raise ModuleNotFoundError(import_error_message("NumPy"))
np.savez_compressed(str(filename), **data_object)
def check(
self,
data_dict,
basename=None,
fullpath=None,
tolerances=None,
default_tolerance=None,
):
"""
Checks a dictionary of NumPy ndarrays, containing only numeric data, against a previously recorded version, or generate a new file.
Example::
def test_some_data(ndarrays_regression):
points, values = some_function()
ndarrays_regression.check(
{
'points': points, # array with shape (100, 3)
'values': values, # array with shape (100,)
},
default_tolerance=dict(atol=1e-8, rtol=1e-8)
)
:param Dict[str, numpy.ndarray] data_dict: dictionary of NumPy ndarrays containing
data for regression check. The arrays can have any shape.
:param str basename: basename of the file to test/record. If not given the name
of the test is used.
:param str fullpath: complete path to use as a reference file. This option
will ignore embed_data completely, being useful if a reference file is located
in the session data dir for example.
:param dict tolerances: dict mapping keys from the data_dict to tolerance settings
for the given data. Example::
tolerances={'U': Tolerance(atol=1e-2)}
:param dict default_tolerance: dict mapping the default tolerance for the current
check call. Example::
default_tolerance=dict(atol=1e-7, rtol=1e-18).
If not provided, will use defaults from numpy's ``isclose`` function.
``basename`` and ``fullpath`` are exclusive.
"""
try:
import numpy as np
except ModuleNotFoundError:
raise ModuleNotFoundError(import_error_message("NumPy"))
import functools
__tracebackhide__ = True
assert isinstance(data_dict, dict), (
"Only dictionaries with NumPy arrays or array-like objects are "
"supported on ndarray_regression fixture.\n"
"Object with type '%s' was given. " % (str(type(data_dict)),)
)
for key, array in data_dict.items():
assert isinstance(
key, str
), "The dictionary keys must be strings. " "Found key with type '%s'" % (
str(type(key))
)
data_dict[key] = np.asarray(array)
for key, array in data_dict.items():
# Rejected: timedelta, datetime, objects, zero-terminated bytes and raw data
if array.dtype in ["m", "M", "O", "S", "a", "V"]:
raise TypeError(
"Only numeric data is supported on ndarrays_regression fixture.\n"
f"Array '{key}' with type '{array.dtype}' was given.\n"
)
if tolerances is None:
tolerances = {}
self._tolerances_dict = tolerances
if default_tolerance is None:
default_tolerance = {}
self._default_tolerance = default_tolerance
dump_fn = functools.partial(self._dump_fn, data_dict)
perform_regression_check(
datadir=self.datadir,
original_datadir=self.original_datadir,
request=self.request,
check_fn=self._check_fn,
dump_fn=dump_fn,
extension=".npz",
basename=basename,
fullpath=fullpath,
force_regen=self._force_regen,
with_test_class_names=self._with_test_class_names,
)