1
+ import math
2
+
1
3
import numpy as np
2
4
3
5
from pandas.core.dtypes.missing import isna, array_equivalent
@@ -38,13 +40,6 @@ cdef bint is_dictlike(obj):
38
40
return hasattr (obj, ' keys' ) and hasattr (obj, ' __getitem__' )
39
41
40
42
41
- cdef bint decimal_almost_equal(double desired, double actual, int decimal):
42
- # Code from
43
- # http://docs.scipy.org/doc/numpy/reference/generated
44
- # /numpy.testing.assert_almost_equal.html
45
- return abs (desired - actual) < (0.5 * 10.0 ** - decimal)
46
-
47
-
48
43
cpdef assert_dict_equal(a, b, bint compare_keys = True ):
49
44
assert is_dictlike(a) and is_dictlike(b), (
50
45
" Cannot compare dict objects, one or both is not dict-like"
@@ -63,7 +58,7 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):
63
58
64
59
65
60
cpdef assert_almost_equal(a, b,
66
- check_less_precise = False ,
61
+ rtol = 1.e-5 , atol = 1.e-8 ,
67
62
bint check_dtype = True ,
68
63
obj = None , lobj = None , robj = None , index_values = None ):
69
64
"""
@@ -73,31 +68,33 @@ cpdef assert_almost_equal(a, b,
73
68
----------
74
69
a : object
75
70
b : object
76
- check_less_precise : bool or int, default False
77
- Specify comparison precision.
78
- 5 digits (False) or 3 digits (True) after decimal points are
79
- compared. If an integer, then this will be the number of decimal
80
- points to compare
71
+ rtol : float, default 1e-5
72
+ Relative tolerance.
73
+
74
+ .. versionadded:: 1.1.0
75
+ atol : float, default 1e-8
76
+ Absolute tolerance.
77
+
78
+ .. versionadded:: 1.1.0
81
79
check_dtype: bool, default True
82
- check dtype if both a and b are np.ndarray
80
+ check dtype if both a and b are np.ndarray.
83
81
obj : str, default None
84
82
Specify object name being compared, internally used to show
85
- appropriate assertion message
83
+ appropriate assertion message.
86
84
lobj : str, default None
87
85
Specify left object name being compared, internally used to show
88
- appropriate assertion message
86
+ appropriate assertion message.
89
87
robj : str, default None
90
88
Specify right object name being compared, internally used to show
91
- appropriate assertion message
89
+ appropriate assertion message.
92
90
index_values : ndarray, default None
93
91
Specify shared index values of objects being compared, internally used
94
- to show appropriate assertion message
92
+ to show appropriate assertion message.
95
93
96
94
.. versionadded:: 1.1.0
97
95
98
96
"""
99
97
cdef:
100
- int decimal
101
98
double diff = 0.0
102
99
Py_ssize_t i, na, nb
103
100
double fa, fb
@@ -108,8 +105,6 @@ cpdef assert_almost_equal(a, b,
108
105
if robj is None :
109
106
robj = b
110
107
111
- assert isinstance (check_less_precise, (int , bool ))
112
-
113
108
if isinstance (a, dict ) or isinstance (b, dict ):
114
109
return assert_dict_equal(a, b)
115
110
@@ -167,8 +162,7 @@ cpdef assert_almost_equal(a, b,
167
162
168
163
for i in range (len (a)):
169
164
try :
170
- assert_almost_equal(a[i], b[i],
171
- check_less_precise = check_less_precise)
165
+ assert_almost_equal(a[i], b[i], rtol = rtol, atol = atol)
172
166
except AssertionError :
173
167
is_unequal = True
174
168
diff += 1
@@ -200,24 +194,11 @@ cpdef assert_almost_equal(a, b,
200
194
# inf comparison
201
195
return True
202
196
203
- if check_less_precise is True :
204
- decimal = 3
205
- elif check_less_precise is False :
206
- decimal = 5
207
- else :
208
- decimal = check_less_precise
209
-
210
197
fa, fb = a, b
211
198
212
- # case for zero
213
- if abs (fa) < 1e-5 :
214
- if not decimal_almost_equal(fa, fb, decimal):
215
- assert False , (f' (very low values) expected {fb:.5f} '
216
- f' but got {fa:.5f}, with decimal {decimal}' )
217
- else :
218
- if not decimal_almost_equal(1 , fb / fa, decimal):
219
- assert False , (f' expected {fb:.5f} but got {fa:.5f}, '
220
- f' with decimal {decimal}' )
199
+ if not math.isclose(fa, fb, rel_tol = rtol, abs_tol = atol):
200
+ assert False , (f" expected {fb:.5f} but got {fa:.5f}, "
201
+ f" with rtol={rtol}, atol={atol}" )
221
202
return True
222
203
223
204
raise AssertionError (f" {a} != {b}" )
0 commit comments