Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/ma/testutils.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 """Miscellaneous functions for testing masked arrays and subclasses | |
2 | |
3 :author: Pierre Gerard-Marchant | |
4 :contact: pierregm_at_uga_dot_edu | |
5 :version: $Id: testutils.py 3529 2007-11-13 08:01:14Z jarrod.millman $ | |
6 | |
7 """ | |
8 from __future__ import division, absolute_import, print_function | |
9 | |
10 __author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)" | |
11 __version__ = "1.0" | |
12 __revision__ = "$Revision: 3529 $" | |
13 __date__ = "$Date: 2007-11-13 10:01:14 +0200 (Tue, 13 Nov 2007) $" | |
14 | |
15 | |
16 import operator | |
17 | |
18 import numpy as np | |
19 from numpy import ndarray, float_ | |
20 import numpy.core.umath as umath | |
21 from numpy.testing import * | |
22 import numpy.testing.utils as utils | |
23 | |
24 from .core import mask_or, getmask, masked_array, nomask, masked, filled, \ | |
25 equal, less | |
26 | |
27 #------------------------------------------------------------------------------ | |
28 def approx (a, b, fill_value=True, rtol=1e-5, atol=1e-8): | |
29 """Returns true if all components of a and b are equal subject to given tolerances. | |
30 | |
31 If fill_value is True, masked values considered equal. Otherwise, masked values | |
32 are considered unequal. | |
33 The relative error rtol should be positive and << 1.0 | |
34 The absolute error atol comes into play for those elements of b that are very | |
35 small or zero; it says how small a must be also. | |
36 """ | |
37 m = mask_or(getmask(a), getmask(b)) | |
38 d1 = filled(a) | |
39 d2 = filled(b) | |
40 if d1.dtype.char == "O" or d2.dtype.char == "O": | |
41 return np.equal(d1, d2).ravel() | |
42 x = filled(masked_array(d1, copy=False, mask=m), fill_value).astype(float_) | |
43 y = filled(masked_array(d2, copy=False, mask=m), 1).astype(float_) | |
44 d = np.less_equal(umath.absolute(x - y), atol + rtol * umath.absolute(y)) | |
45 return d.ravel() | |
46 | |
47 | |
48 def almost(a, b, decimal=6, fill_value=True): | |
49 """Returns True if a and b are equal up to decimal places. | |
50 If fill_value is True, masked values considered equal. Otherwise, masked values | |
51 are considered unequal. | |
52 """ | |
53 m = mask_or(getmask(a), getmask(b)) | |
54 d1 = filled(a) | |
55 d2 = filled(b) | |
56 if d1.dtype.char == "O" or d2.dtype.char == "O": | |
57 return np.equal(d1, d2).ravel() | |
58 x = filled(masked_array(d1, copy=False, mask=m), fill_value).astype(float_) | |
59 y = filled(masked_array(d2, copy=False, mask=m), 1).astype(float_) | |
60 d = np.around(np.abs(x - y), decimal) <= 10.0 ** (-decimal) | |
61 return d.ravel() | |
62 | |
63 | |
64 #................................................ | |
65 def _assert_equal_on_sequences(actual, desired, err_msg=''): | |
66 "Asserts the equality of two non-array sequences." | |
67 assert_equal(len(actual), len(desired), err_msg) | |
68 for k in range(len(desired)): | |
69 assert_equal(actual[k], desired[k], 'item=%r\n%s' % (k, err_msg)) | |
70 return | |
71 | |
72 def assert_equal_records(a, b): | |
73 """Asserts that two records are equal. Pretty crude for now.""" | |
74 assert_equal(a.dtype, b.dtype) | |
75 for f in a.dtype.names: | |
76 (af, bf) = (operator.getitem(a, f), operator.getitem(b, f)) | |
77 if not (af is masked) and not (bf is masked): | |
78 assert_equal(operator.getitem(a, f), operator.getitem(b, f)) | |
79 return | |
80 | |
81 | |
82 def assert_equal(actual, desired, err_msg=''): | |
83 "Asserts that two items are equal." | |
84 # Case #1: dictionary ..... | |
85 if isinstance(desired, dict): | |
86 if not isinstance(actual, dict): | |
87 raise AssertionError(repr(type(actual))) | |
88 assert_equal(len(actual), len(desired), err_msg) | |
89 for k, i in desired.items(): | |
90 if not k in actual: | |
91 raise AssertionError("%s not in %s" % (k, actual)) | |
92 assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k, err_msg)) | |
93 return | |
94 # Case #2: lists ..... | |
95 if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)): | |
96 return _assert_equal_on_sequences(actual, desired, err_msg='') | |
97 if not (isinstance(actual, ndarray) or isinstance(desired, ndarray)): | |
98 msg = build_err_msg([actual, desired], err_msg,) | |
99 if not desired == actual: | |
100 raise AssertionError(msg) | |
101 return | |
102 # Case #4. arrays or equivalent | |
103 if ((actual is masked) and not (desired is masked)) or \ | |
104 ((desired is masked) and not (actual is masked)): | |
105 msg = build_err_msg([actual, desired], | |
106 err_msg, header='', names=('x', 'y')) | |
107 raise ValueError(msg) | |
108 actual = np.array(actual, copy=False, subok=True) | |
109 desired = np.array(desired, copy=False, subok=True) | |
110 (actual_dtype, desired_dtype) = (actual.dtype, desired.dtype) | |
111 if actual_dtype.char == "S" and desired_dtype.char == "S": | |
112 return _assert_equal_on_sequences(actual.tolist(), | |
113 desired.tolist(), | |
114 err_msg='') | |
115 # elif actual_dtype.char in "OV" and desired_dtype.char in "OV": | |
116 # if (actual_dtype != desired_dtype) and actual_dtype: | |
117 # msg = build_err_msg([actual_dtype, desired_dtype], | |
118 # err_msg, header='', names=('actual', 'desired')) | |
119 # raise ValueError(msg) | |
120 # return _assert_equal_on_sequences(actual.tolist(), | |
121 # desired.tolist(), | |
122 # err_msg='') | |
123 return assert_array_equal(actual, desired, err_msg) | |
124 | |
125 | |
126 def fail_if_equal(actual, desired, err_msg='',): | |
127 """Raises an assertion error if two items are equal. | |
128 """ | |
129 if isinstance(desired, dict): | |
130 if not isinstance(actual, dict): | |
131 raise AssertionError(repr(type(actual))) | |
132 fail_if_equal(len(actual), len(desired), err_msg) | |
133 for k, i in desired.items(): | |
134 if not k in actual: | |
135 raise AssertionError(repr(k)) | |
136 fail_if_equal(actual[k], desired[k], 'key=%r\n%s' % (k, err_msg)) | |
137 return | |
138 if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)): | |
139 fail_if_equal(len(actual), len(desired), err_msg) | |
140 for k in range(len(desired)): | |
141 fail_if_equal(actual[k], desired[k], 'item=%r\n%s' % (k, err_msg)) | |
142 return | |
143 if isinstance(actual, np.ndarray) or isinstance(desired, np.ndarray): | |
144 return fail_if_array_equal(actual, desired, err_msg) | |
145 msg = build_err_msg([actual, desired], err_msg) | |
146 if not desired != actual: | |
147 raise AssertionError(msg) | |
148 | |
149 assert_not_equal = fail_if_equal | |
150 | |
151 | |
152 def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True): | |
153 """Asserts that two items are almost equal. | |
154 The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal) | |
155 """ | |
156 if isinstance(actual, np.ndarray) or isinstance(desired, np.ndarray): | |
157 return assert_array_almost_equal(actual, desired, decimal=decimal, | |
158 err_msg=err_msg, verbose=verbose) | |
159 msg = build_err_msg([actual, desired], | |
160 err_msg=err_msg, verbose=verbose) | |
161 if not round(abs(desired - actual), decimal) == 0: | |
162 raise AssertionError(msg) | |
163 | |
164 | |
165 assert_close = assert_almost_equal | |
166 | |
167 | |
168 def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='', | |
169 fill_value=True): | |
170 """Asserts that a comparison relation between two masked arrays is satisfied | |
171 elementwise.""" | |
172 # Fill the data first | |
173 # xf = filled(x) | |
174 # yf = filled(y) | |
175 # Allocate a common mask and refill | |
176 m = mask_or(getmask(x), getmask(y)) | |
177 x = masked_array(x, copy=False, mask=m, keep_mask=False, subok=False) | |
178 y = masked_array(y, copy=False, mask=m, keep_mask=False, subok=False) | |
179 if ((x is masked) and not (y is masked)) or \ | |
180 ((y is masked) and not (x is masked)): | |
181 msg = build_err_msg([x, y], err_msg=err_msg, verbose=verbose, | |
182 header=header, names=('x', 'y')) | |
183 raise ValueError(msg) | |
184 # OK, now run the basic tests on filled versions | |
185 return utils.assert_array_compare(comparison, | |
186 x.filled(fill_value), | |
187 y.filled(fill_value), | |
188 err_msg=err_msg, | |
189 verbose=verbose, header=header) | |
190 | |
191 | |
192 def assert_array_equal(x, y, err_msg='', verbose=True): | |
193 """Checks the elementwise equality of two masked arrays.""" | |
194 assert_array_compare(operator.__eq__, x, y, | |
195 err_msg=err_msg, verbose=verbose, | |
196 header='Arrays are not equal') | |
197 | |
198 | |
199 def fail_if_array_equal(x, y, err_msg='', verbose=True): | |
200 "Raises an assertion error if two masked arrays are not equal (elementwise)." | |
201 def compare(x, y): | |
202 return (not np.alltrue(approx(x, y))) | |
203 assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose, | |
204 header='Arrays are not equal') | |
205 | |
206 | |
207 def assert_array_approx_equal(x, y, decimal=6, err_msg='', verbose=True): | |
208 """Checks the elementwise equality of two masked arrays, up to a given | |
209 number of decimals.""" | |
210 def compare(x, y): | |
211 "Returns the result of the loose comparison between x and y)." | |
212 return approx(x, y, rtol=10. ** -decimal) | |
213 assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose, | |
214 header='Arrays are not almost equal') | |
215 | |
216 | |
217 def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): | |
218 """Checks the elementwise equality of two masked arrays, up to a given | |
219 number of decimals.""" | |
220 def compare(x, y): | |
221 "Returns the result of the loose comparison between x and y)." | |
222 return almost(x, y, decimal) | |
223 assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose, | |
224 header='Arrays are not almost equal') | |
225 | |
226 | |
227 def assert_array_less(x, y, err_msg='', verbose=True): | |
228 "Checks that x is smaller than y elementwise." | |
229 assert_array_compare(operator.__lt__, x, y, | |
230 err_msg=err_msg, verbose=verbose, | |
231 header='Arrays are not less-ordered') | |
232 | |
233 | |
234 def assert_mask_equal(m1, m2, err_msg=''): | |
235 """Asserts the equality of two masks.""" | |
236 if m1 is nomask: | |
237 assert_(m2 is nomask) | |
238 if m2 is nomask: | |
239 assert_(m1 is nomask) | |
240 assert_array_equal(m1, m2, err_msg=err_msg) |