Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Review _assert_within_tol positional arguments #15087

Merged
merged 9 commits into from Dec 2, 2021
46 changes: 23 additions & 23 deletions scipy/spatial/tests/test_distance.py
Expand Up @@ -439,24 +439,24 @@ def _my_metric(x, y, arg, kwarg=1, kwarg2=2):
arg=1.1, kwarg2=3.3), 5.4)

def test_cdist_euclidean_random_unicode(self):
eps = 1e-07
eps = 1e-15
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist_no_const(X1, X2, 'euclidean')
Y2 = wcdist_no_const(X1, X2, 'test_euclidean')
_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)

@pytest.mark.parametrize("p", [1.0, 1.23, 2.0, 3.8, 4.6, np.inf])
def test_cdist_minkowski_random(self, p):
eps = 1e-07
eps = 1e-15
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist_no_const(X1, X2, 'minkowski', p=p)
Y2 = wcdist_no_const(X1, X2, 'test_minkowski', p=p)
_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)

def test_cdist_cosine_random(self):
eps = 1e-07
eps = 1e-14
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist(X1, X2, 'cosine')
Expand All @@ -467,7 +467,7 @@ def norms(X):

Y2 = 1 - np.dot((X1 / norms(X1)), (X2 / norms(X2)).T)

_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)

def test_cdist_mahalanobis(self):
# 1-dimensional observations
Expand Down Expand Up @@ -575,11 +575,11 @@ def test_cdist_dtype_equivalence(self):
else:
for new_type in test[1]:
y2 = cdist(new_type(X1), new_type(X2), metric=metric)
_assert_within_tol(y1, y2, eps, verbose > 2)
_assert_within_tol(y1, y2, rtol=eps, verbose_=verbose > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try tightening the eps in this test? It's still at 1e-07.


def test_cdist_out(self):
# Test that out parameter works properly
eps = 1e-07
eps = 1e-15
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
out_r, out_c = X1.shape[0], X2.shape[0]
Expand All @@ -592,7 +592,7 @@ def test_cdist_out(self):
Y1 = cdist(X1, X2, metric, **kwargs)
Y2 = cdist(X1, X2, metric, out=out1, **kwargs)
# test that output is numerically equivalent
_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)
# test that Y_test1 and out1 are the same object
assert_(Y2 is out1)
# test for incorrect shape
Expand All @@ -616,7 +616,7 @@ def test_cdist_out(self):
def test_striding(self):
# test that striding is handled correct with calls to
# _copy_array_if_base_present
eps = 1e-07
eps = 1e-15
X1 = eo['cdist-X1'][::2, ::2]
X2 = eo['cdist-X2'][::2, ::2]
X1_copy = X1.copy()
Expand All @@ -638,7 +638,7 @@ def test_striding(self):
Y1 = cdist(X1, X2, metric, **kwargs)
Y2 = cdist(X1_copy, X2_copy, metric, **kwargs)
# test that output is numerically equivalent
_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)

def test_cdist_refcount(self):
for metric in _METRICS_NAMES:
Expand Down Expand Up @@ -747,7 +747,7 @@ def test_pdist_euclidean_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-euclidean-iris']
Y_test1 = wpdist_no_const(X, 'euclidean')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


@pytest.mark.slow
def test_pdist_euclidean_iris_nonC(self):
Expand Down Expand Up @@ -846,7 +846,7 @@ def test_pdist_cosine_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-cosine-iris']
Y_test1 = wpdist(X, 'cosine')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and here...


@pytest.mark.slow
def test_pdist_cosine_iris_nonC(self):
Expand Down Expand Up @@ -900,7 +900,7 @@ def test_pdist_cityblock_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-cityblock-iris']
Y_test1 = wpdist_no_const(X, 'cityblock')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

@pytest.mark.slow
def test_pdist_cityblock_iris_nonC(self):
Expand Down Expand Up @@ -947,7 +947,7 @@ def test_pdist_correlation_iris_float32(self):
X = eo['iris']
Y_right = np.float32(eo['pdist-correlation-iris'])
Y_test1 = wpdist(X, 'correlation')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

@pytest.mark.slow
def test_pdist_correlation_iris_nonC(self):
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def test_pdist_minkowski_5_8_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-minkowski-5.8-iris']
Y_test1 = wpdist_no_const(X, 'minkowski', p=5.8)
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

@pytest.mark.slow
def test_pdist_minkowski_5_8_iris_nonC(self):
Expand Down Expand Up @@ -1178,7 +1178,7 @@ def test_pdist_jensenshannon_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-jensenshannon-iris']
Y_test1 = pdist(X, 'jensenshannon')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

def test_pdist_jensenshannon_iris_nonC(self):
eps = 5e-12
Expand All @@ -1204,7 +1204,7 @@ def test_pdist_chebyshev_random_float32(self):
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-chebyshev']
Y_test1 = pdist(X, 'chebyshev')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

def test_pdist_chebyshev_random_nonC(self):
eps = 1e-08
Expand All @@ -1225,7 +1225,7 @@ def test_pdist_chebyshev_iris_float32(self):
X = np.float32(eo['iris'])
Y_right = eo['pdist-chebyshev-iris']
Y_test1 = pdist(X, 'chebyshev')
_assert_within_tol(Y_test1, Y_right, eps, verbose > 2)
_assert_within_tol(Y_test1, Y_right, atol=eps, verbose_=verbose > 2)

def test_pdist_chebyshev_iris_nonC(self):
eps = 1e-15
Expand Down Expand Up @@ -1376,15 +1376,15 @@ def test_pdist_canberra_match(self):
eps = 1e-10
y1 = wpdist_no_const(D, "canberra")
y2 = wpdist_no_const(D, "test_canberra")
_assert_within_tol(y1, y2, eps, verbose > 2)
_assert_within_tol(y1, y2, atol=eps, verbose_=verbose > 2)

def test_pdist_canberra_ticket_711(self):
# Test pdist(X, 'canberra') to see if Canberra gives the right result
# as reported on gh-1238.
eps = 1e-8
pdist_y = wpdist_no_const(([3.3], [3.4]), "canberra")
right_y = 0.01492537
_assert_within_tol(pdist_y, right_y, eps, verbose > 2)
_assert_within_tol(pdist_y, right_y, atol=eps, verbose_=verbose > 2)

def test_pdist_custom_notdouble(self):
# tests that when using a custom metric the data type is not altered
Expand Down Expand Up @@ -1470,7 +1470,7 @@ def test_pdist_dtype_equivalence(self):
else:
for new_type in test[1]:
y2 = pdist(new_type(X1), metric=metric)
_assert_within_tol(y1, y2, eps, verbose > 2)
_assert_within_tol(y1, y2, rtol=eps, verbose_=verbose > 2)

def test_pdist_out(self):
# Test that out parameter works properly
Expand Down Expand Up @@ -1516,7 +1516,7 @@ def test_striding(self):
Y1 = pdist(X, metric, **kwargs)
Y2 = pdist(X_copy, metric, **kwargs)
# test that output is numerically equivalent
_assert_within_tol(Y1, Y2, eps, verbose > 2)
_assert_within_tol(Y1, Y2, rtol=eps, verbose_=verbose > 2)

class TestSomeDistanceFunctions:

Expand Down