Skip to content

Commit f837920

Browse files
committed
tests: Add tests for implementations of _corrcoef2 and _spearmanr2
1 parent 3d10937 commit f837920

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Orange/distance/distance.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ def _corrcoef2(a, b, axis=0):
507507
numpy.corrcoef
508508
"""
509509
a, b = np.atleast_2d(a, b)
510+
if not (axis == 0 or axis == 1):
511+
raise ValueError("Invalid axis {} (only 0 or 1 accepted)".format(axis))
512+
510513
mean_a = np.mean(a, axis=axis, keepdims=True)
511514
mean_b = np.mean(b, axis=axis, keepdims=True)
512515
assert a.shape[axis] == b.shape[axis]
@@ -523,8 +526,6 @@ def _corrcoef2(a, b, axis=0):
523526
elif axis == 1:
524527
C = a.dot(b.T)
525528
assert C.shape == (n, m)
526-
else:
527-
raise ValueError()
528529

529530
ss_a = np.sum(a ** 2, axis=axis, keepdims=True)
530531
ss_b = np.sum(b ** 2, axis=axis, keepdims=True)

Orange/tests/test_distances.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
import numpy as np
88
import scipy
9+
import scipy.spatial
10+
import scipy.stats
911
from scipy.sparse import csr_matrix
1012

1113
from Orange.data import (Table, Domain, ContinuousVariable,
1214
DiscreteVariable, StringVariable, Instance)
1315
from Orange.distance import (Euclidean, SpearmanR, SpearmanRAbsolute,
1416
PearsonR, PearsonRAbsolute, Manhattan, Cosine,
1517
Jaccard, _preprocess, MahalanobisDistance)
18+
from Orange.distance.distance import _spearmanr2, _corrcoef2
1619
from Orange.misc import DistMatrix
1720
from Orange.tests import named_file, test_filename
1821
from Orange.util import OrangeDeprecationWarning
@@ -598,6 +601,30 @@ def test_spearmanr_distance_numpy(self):
598601
[0.3833333],
599602
[0.]]))
600603

604+
def test_spearmanr2(self):
605+
# Test that _spearnmanr2 returns the same result that stats.spearmanr
606+
# would
607+
n, m = tuple(np.random.randint(2, 5, size=2))
608+
mean = np.random.uniform(-1, 1, size=m)
609+
cov = np.random.uniform(0, 1./m, size=(m, m))
610+
cov = (cov + cov.T) / 2
611+
cov.flat[::m + 1] = 1.0
612+
X1 = np.random.multivariate_normal(mean, cov, size=n)
613+
X2 = np.random.multivariate_normal(mean, cov, size=n)
614+
expected = scipy.stats.spearmanr(X1, X2, axis=1)[0][:n, n:]
615+
np.testing.assert_almost_equal(
616+
_spearmanr2(X1, X2, axis=1),
617+
expected,
618+
decimal=9
619+
)
620+
621+
expected = scipy.stats.spearmanr(X1, X2, axis=0)[0][:m, m:]
622+
np.testing.assert_almost_equal(
623+
_spearmanr2(X1, X2, axis=0),
624+
expected,
625+
decimal=9,
626+
)
627+
601628

602629
# noinspection PyTypeChecker
603630
class TestSpearmanRAbsolute(TestCase):
@@ -752,6 +779,32 @@ def test_pearsonr_distance_numpy(self):
752779
[0.32783865],
753780
[0.]]))
754781

782+
def test_corrcoef2(self):
783+
# Test that _corrcoef2 returns the same result that np.corrcoef would
784+
n, m = tuple(np.random.randint(2, 5, size=2))
785+
mean = np.random.uniform(-1, 1, size=m)
786+
cov = np.random.uniform(0, 1./m, size=(m, m))
787+
cov = (cov + cov.T) / 2
788+
cov.flat[::m + 1] = 1.0
789+
X1 = np.random.multivariate_normal(mean, cov, size=n)
790+
X2 = np.random.multivariate_normal(mean, cov, size=n)
791+
expected = np.corrcoef(X1, X2, rowvar=True)[:n, n:]
792+
np.testing.assert_almost_equal(
793+
_corrcoef2(X1, X2, axis=1),
794+
expected,
795+
decimal=9
796+
)
797+
798+
expected = np.corrcoef(X1, X2, rowvar=False)[:m, m:]
799+
np.testing.assert_almost_equal(
800+
_corrcoef2(X1, X2, axis=0),
801+
expected,
802+
decimal=9,
803+
)
804+
805+
with self.assertRaises(ValueError):
806+
_corrcoef2(X1, X2, axis=10)
807+
755808

756809
# noinspection PyTypeChecker
757810
class TestPearsonRAbsolute(TestCase):

0 commit comments

Comments
 (0)