-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathtest_dr.py
More file actions
253 lines (180 loc) · 7.43 KB
/
Copy pathtest_dr.py
File metadata and controls
253 lines (180 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Tests for module dr on Dimensionality Reduction"""
# Author: Remi Flamary <remi.flamary@unice.fr>
# Minhui Huang <mhhuang@ucdavis.edu>
# Antoine Collas <antoine.collas@inria.fr>
#
# License: MIT License
import numpy as np
import ot
import pytest
try: # test if autograd and pymanopt are installed
import ot.dr
nogo = False
except ImportError:
nogo = True
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_fda():
n_samples = 90 # nb samples in source and target datasets
rng = np.random.RandomState(0)
# generate gaussian dataset
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng)
n_features_noise = 8
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise)))
p = 1
Pfda, projfda = ot.dr.fda(xs, ys, p)
projfda(xs)
np.testing.assert_allclose(np.sum(Pfda**2, 0), np.ones(p))
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_fda_recovers_discriminant_direction():
# classes are separated along the first feature only, all others are noise,
# so FDA must return a direction aligned with e_0
rng = np.random.RandomState(1)
n_features = 5
xs = np.concatenate(
[
rng.randn(60, n_features) * 0.3 + shift * np.eye(1, n_features, 0)
for shift in [-6.0, 0.0, 6.0]
]
)
ys = np.repeat([0, 1, 2], 60)
Pfda, _ = ot.dr.fda(xs, ys, p=1)
direction = Pfda[:, 0] / np.linalg.norm(Pfda[:, 0])
np.testing.assert_array_less(0.95, np.abs(direction[0]))
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_fda_projection_is_centered():
rng = np.random.RandomState(0)
xs, ys = ot.datasets.make_data_classif("gaussrot", 90, random_state=rng)
xs = xs + 10.0 # off-centered data makes an absent centering visible
_, projfda = ot.dr.fda(xs, ys, p=1)
np.testing.assert_allclose(projfda(xs).mean(axis=0), 0.0, atol=1e-10)
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_fda_wda_do_not_modify_input():
rng = np.random.RandomState(0)
xs, ys = ot.datasets.make_data_classif("gaussrot", 90, random_state=rng)
xs = xs + 10.0
xs_copy = xs.copy()
ot.dr.fda(xs, ys, p=1)
np.testing.assert_allclose(xs, xs_copy)
ot.dr.wda(xs, ys, p=1, reg=1.0, k=5, maxiter=5)
np.testing.assert_allclose(xs, xs_copy)
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_wda():
n_samples = 100 # nb samples in source and target datasets
rng = np.random.RandomState(0)
# generate gaussian dataset
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng)
n_features_noise = 8
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise)))
p = 2
Pwda, projwda = ot.dr.wda(xs, ys, p, maxiter=10)
projwda(xs)
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p))
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_wda_low_reg():
n_samples = 100 # nb samples in source and target datasets
rng = np.random.RandomState(0)
# generate gaussian dataset
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng)
n_features_noise = 8
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise)))
p = 2
Pwda, projwda = ot.dr.wda(
xs, ys, p, reg=0.01, maxiter=10, sinkhorn_method="sinkhorn_log"
)
projwda(xs)
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p))
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_wda_normalized():
n_samples = 100 # nb samples in source and target datasets
rng = np.random.RandomState(0)
# generate gaussian dataset
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng)
n_features_noise = 8
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise)))
p = 2
P0 = rng.randn(10, p)
P0 /= P0.sum(0, keepdims=True)
Pwda, projwda = ot.dr.wda(xs, ys, p, maxiter=10, P0=P0, normalize=True)
projwda(xs)
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p))
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_prw():
d = 100 # Dimension
n = 100 # Number samples
k = 3 # Subspace dimension
dim = 3
def fragmented_hypercube(n, d, dim, rng):
assert dim <= d
assert dim >= 1
assert dim == int(dim)
a = (1.0 / n) * np.ones(n)
b = (1.0 / n) * np.ones(n)
# First measure : uniform on the hypercube
X = rng.uniform(-1, 1, size=(n, d))
# Second measure : fragmentation
tmp_y = rng.uniform(-1, 1, size=(n, d))
Y = tmp_y + 2 * np.sign(tmp_y) * np.array(dim * [1] + (d - dim) * [0])
return a, b, X, Y
rng = np.random.RandomState(42)
a, b, X, Y = fragmented_hypercube(n, d, dim, rng)
tau = 0.002
reg = 0.2
pi, U = ot.dr.projection_robust_wasserstein(
X, Y, a, b, tau, reg=reg, k=k, maxiter=1000, verbose=1
)
U0 = rng.randn(d, k)
U0, _ = np.linalg.qr(U0)
pi, U = ot.dr.projection_robust_wasserstein(
X, Y, a, b, tau, U0=U0, reg=reg, k=k, maxiter=1000, verbose=1
)
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)")
def test_ewca():
d = 5
n_samples = 50
k = 3
rng = np.random.RandomState(0)
# generate gaussian dataset
A = rng.normal(size=(d, d))
Q, _ = np.linalg.qr(A)
D = rng.normal(size=d)
D = (D / np.linalg.norm(D)) ** 4
cov = Q @ np.diag(D) @ Q.T
X = rng.multivariate_normal(np.zeros(d), cov, size=n_samples)
X = X - X.mean(0, keepdims=True)
assert X.shape == (n_samples, d)
# compute first 3 components with BCD
pi, U = ot.dr.ewca(
X, reg=0.01, method="BCD", k=k, verbose=1, sinkhorn_method="sinkhorn_log"
)
assert pi.shape == (n_samples, n_samples)
assert (pi >= 0).all()
assert np.allclose(pi.sum(0), 1 / n_samples, atol=1e-3)
assert np.allclose(pi.sum(1), 1 / n_samples, atol=1e-3)
assert U.shape == (d, k)
assert np.allclose(U.T @ U, np.eye(k), atol=1e-3)
# test that U contains the principal components
U_first_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, :k]
_, cos, _ = np.linalg.svd(U.T @ U_first_eigvec, full_matrices=False)
assert np.allclose(cos, np.ones(k), atol=1e-3)
# compute first 3 components with MM
pi, U = ot.dr.ewca(
X, reg=0.01, method="MM", k=k, verbose=1, sinkhorn_method="sinkhorn_log"
)
assert pi.shape == (n_samples, n_samples)
assert (pi >= 0).all()
assert np.allclose(pi.sum(0), 1 / n_samples, atol=1e-3)
assert np.allclose(pi.sum(1), 1 / n_samples, atol=1e-3)
assert U.shape == (d, k)
assert np.allclose(U.T @ U, np.eye(k), atol=1e-3)
# test that U contains the principal components
U_first_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, :k]
_, cos, _ = np.linalg.svd(U.T @ U_first_eigvec, full_matrices=False)
assert np.allclose(cos, np.ones(k), atol=1e-3)
# compute last 3 components
pi, U = ot.dr.ewca(
X, reg=100000, method="MM", k=k, verbose=1, sinkhorn_method="sinkhorn_log"
)
# test that U contains the last principal components
U_last_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, -k:]
_, cos, _ = np.linalg.svd(U.T @ U_last_eigvec, full_matrices=False)
assert np.allclose(cos, np.ones(k), atol=1e-3)