|
1 | 1 | import unittest
|
2 | 2 | import numpy as np
|
| 3 | +import pytest |
| 4 | +from numpy.linalg import LinAlgError |
| 5 | +from scipy.stats import ortho_group |
3 | 6 | from sklearn.datasets import load_iris
|
4 |
| -from numpy.testing import assert_array_almost_equal |
| 7 | +from numpy.testing import assert_array_almost_equal, assert_allclose |
| 8 | +from sklearn.utils.testing import ignore_warnings |
5 | 9 |
|
6 | 10 | from metric_learn import (
|
7 | 11 | LMNN, NCA, LFDA, Covariance, MLKR,
|
8 | 12 | LSML_Supervised, ITML_Supervised, SDML_Supervised, RCA_Supervised)
|
| 13 | +from metric_learn._util import transformer_from_metric |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class TestTransformerMetricConversion(unittest.TestCase):
|
@@ -76,6 +81,105 @@ def test_mlkr(self):
|
76 | 81 | L = mlkr.transformer_
|
77 | 82 | assert_array_almost_equal(L.T.dot(L), mlkr.get_mahalanobis_matrix())
|
78 | 83 |
|
| 84 | + @ignore_warnings |
| 85 | + def test_transformer_from_metric_edge_cases(self): |
| 86 | + """Test that transformer_from_metric returns the right result in various |
| 87 | + edge cases""" |
| 88 | + rng = np.random.RandomState(42) |
| 89 | + |
| 90 | + # an orthonormal matrix useful for creating matrices with given |
| 91 | + # eigenvalues: |
| 92 | + P = ortho_group.rvs(7, random_state=rng) |
| 93 | + |
| 94 | + # matrix with all its coefficients very low (to check that the algorithm |
| 95 | + # does not consider it as a diagonal matrix)(non regression test for |
| 96 | + # https://github.com/metric-learn/metric-learn/issues/175) |
| 97 | + M = np.diag([1e-15, 2e-16, 3e-15, 4e-16, 5e-15, 6e-16, 7e-15]) |
| 98 | + M = P.dot(M).dot(P.T) |
| 99 | + L = transformer_from_metric(M) |
| 100 | + assert_allclose(L.T.dot(L), M) |
| 101 | + |
| 102 | + # diagonal matrix |
| 103 | + M = np.diag(np.abs(rng.randn(5))) |
| 104 | + L = transformer_from_metric(M) |
| 105 | + assert_allclose(L.T.dot(L), M) |
| 106 | + |
| 107 | + # low-rank matrix (with zeros) |
| 108 | + M = np.zeros((7, 7)) |
| 109 | + small_random = rng.randn(3, 3) |
| 110 | + M[:3, :3] = small_random.T.dot(small_random) |
| 111 | + L = transformer_from_metric(M) |
| 112 | + assert_allclose(L.T.dot(L), M) |
| 113 | + |
| 114 | + # low-rank matrix (without necessarily zeros) |
| 115 | + R = np.abs(rng.randn(7, 7)) |
| 116 | + M = R.dot(np.diag([1, 5, 3, 2, 0, 0, 0])).dot(R.T) |
| 117 | + L = transformer_from_metric(M) |
| 118 | + assert_allclose(L.T.dot(L), M) |
| 119 | + |
| 120 | + # matrix with a determinant still high but which should be considered as a |
| 121 | + # non-definite matrix (to check we don't test the definiteness with the |
| 122 | + # determinant which is a bad strategy) |
| 123 | + M = np.diag([1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e-20]) |
| 124 | + M = P.dot(M).dot(P.T) |
| 125 | + assert np.abs(np.linalg.det(M)) > 10 |
| 126 | + assert np.linalg.slogdet(M)[1] > 1 # (just to show that the computed |
| 127 | + # determinant is far from null) |
| 128 | + with pytest.raises(LinAlgError) as err_msg: |
| 129 | + np.linalg.cholesky(M) |
| 130 | + assert str(err_msg.value) == 'Matrix is not positive definite' |
| 131 | + # (just to show that this case is indeed considered by numpy as an |
| 132 | + # indefinite case) |
| 133 | + L = transformer_from_metric(M) |
| 134 | + assert_allclose(L.T.dot(L), M) |
| 135 | + |
| 136 | + # matrix with lots of small nonzeros that make a big zero when multiplied |
| 137 | + M = np.diag([1e-3, 1e-3, 1e-3, 1e-3, 1e-3, 1e-3, 1e-3]) |
| 138 | + L = transformer_from_metric(M) |
| 139 | + assert_allclose(L.T.dot(L), M) |
| 140 | + |
| 141 | + # full rank matrix |
| 142 | + M = rng.randn(10, 10) |
| 143 | + M = M.T.dot(M) |
| 144 | + assert np.linalg.matrix_rank(M) == 10 |
| 145 | + L = transformer_from_metric(M) |
| 146 | + assert_allclose(L.T.dot(L), M) |
| 147 | + |
| 148 | + def test_non_symmetric_matrix_raises(self): |
| 149 | + """Checks that if a non symmetric matrix is given to |
| 150 | + transformer_from_metric, an error is thrown""" |
| 151 | + rng = np.random.RandomState(42) |
| 152 | + M = rng.randn(10, 10) |
| 153 | + with pytest.raises(ValueError) as raised_error: |
| 154 | + transformer_from_metric(M) |
| 155 | + assert str(raised_error.value) == "The input metric should be symmetric." |
| 156 | + |
| 157 | + def test_non_psd_raises(self): |
| 158 | + """Checks that a non PSD matrix (i.e. with negative eigenvalues) will |
| 159 | + raise an error when passed to transformer_from_metric""" |
| 160 | + rng = np.random.RandomState(42) |
| 161 | + D = np.diag([1, 5, 3, 4.2, -4, -2, 1]) |
| 162 | + P = ortho_group.rvs(7, random_state=rng) |
| 163 | + M = P.dot(D).dot(P.T) |
| 164 | + msg = ("Matrix is not positive semidefinite (PSD).") |
| 165 | + with pytest.raises(ValueError) as raised_error: |
| 166 | + transformer_from_metric(M) |
| 167 | + assert str(raised_error.value) == msg |
| 168 | + with pytest.raises(ValueError) as raised_error: |
| 169 | + transformer_from_metric(D) |
| 170 | + assert str(raised_error.value) == msg |
| 171 | + |
| 172 | + def test_almost_psd_dont_raise(self): |
| 173 | + """Checks that if the metric is almost PSD (i.e. it has some negative |
| 174 | + eigenvalues very close to zero), then transformer_from_metric will still |
| 175 | + work""" |
| 176 | + rng = np.random.RandomState(42) |
| 177 | + D = np.diag([1, 5, 3, 4.2, -1e-20, -2e-20, -1e-20]) |
| 178 | + P = ortho_group.rvs(7, random_state=rng) |
| 179 | + M = P.dot(D).dot(P.T) |
| 180 | + L = transformer_from_metric(M) |
| 181 | + assert_allclose(L.T.dot(L), M) |
| 182 | + |
79 | 183 |
|
80 | 184 | if __name__ == '__main__':
|
81 | 185 | unittest.main()
|
0 commit comments