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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# ===============================
# UMAP fit Parameters Validation
# ===============================
import warnings
import numpy as np
from sklearn.metrics import pairwise_distances
import pytest
import numba
from umap import UMAP
# verify that we can import this; potentially for later use
import umap.validation
warnings.filterwarnings("ignore", category=UserWarning)
def test_umap_negative_op(nn_data):
u = UMAP(set_op_mix_ratio=-1.0)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_too_large_op(nn_data):
u = UMAP(set_op_mix_ratio=1.5)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_too_large_min_dist(nn_data):
u = UMAP(min_dist=2.0)
# a RuntimeWarning about division by zero in a,b curve fitting is expected
# caught and ignored for this test
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_min_dist(nn_data):
u = UMAP(min_dist=-1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_n_components(nn_data):
u = UMAP(n_components=-1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_non_integer_n_components(nn_data):
u = UMAP(n_components=1.5)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_too_small_n_neighbours(nn_data):
u = UMAP(n_neighbors=0.5)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_n_neighbours(nn_data):
u = UMAP(n_neighbors=-1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_metric(nn_data):
u = UMAP(metric=45)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_learning_rate(nn_data):
u = UMAP(learning_rate=-1.5)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_repulsion(nn_data):
u = UMAP(repulsion_strength=-0.5)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_sample_rate(nn_data):
u = UMAP(negative_sample_rate=-1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_init(nn_data):
u = UMAP(init="foobar")
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_numeric_init(nn_data):
u = UMAP(init=42)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_matrix_init(nn_data):
u = UMAP(init=np.array([[0, 0, 0], [0, 0, 0]]))
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_n_epochs(nn_data):
u = UMAP(n_epochs=-2)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_negative_target_n_neighbours(nn_data):
u = UMAP(target_n_neighbors=1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_output_metric(nn_data):
u = UMAP(output_metric="foobar")
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(output_metric="precomputed")
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(output_metric="hamming")
with pytest.raises(ValueError):
u.fit(nn_data)
def test_haversine_on_highd(nn_data):
u = UMAP(metric="haversine")
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_haversine_embed_to_highd(nn_data):
u = UMAP(n_components=3, output_metric="haversine")
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_too_many_neighbors_warns(nn_data):
u = UMAP(a=1.2, b=1.75, n_neighbors=2000, n_epochs=11, init="random")
u.fit(
nn_data[
:100,
]
)
assert u._a == 1.2
assert u._b == 1.75
def test_densmap_lambda(nn_data):
u = UMAP(densmap=True, dens_lambda=-1.0)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_densmap_var_shift(nn_data):
u = UMAP(densmap=True, dens_var_shift=-1.0)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_densmap_frac(nn_data):
u = UMAP(densmap=True, dens_frac=-1.0)
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(densmap=True, dens_frac=2.0)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_unique_and_precomputed(nn_data):
u = UMAP(metric="precomputed", unique=True)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_densmap_bad_output_metric(nn_data):
u = UMAP(densmap=True, output_metric="haversine")
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_n_components(nn_data):
u = UMAP(n_components=2.3)
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(n_components="23")
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(n_components=np.float64(2.3))
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_metrics(nn_data):
u = UMAP(metric="foobar")
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(metric=2.75)
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(output_metric="foobar")
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(output_metric=2.75)
with pytest.raises(ValueError):
u.fit(nn_data)
# u = UMAP(target_metric="foobar")
# assert_raises(ValueError, u.fit, nn_data)
# u = UMAP(target_metric=2.75)
# assert_raises(ValueError, u.fit, nn_data)
def test_umap_bad_n_jobs(nn_data):
u = UMAP(n_jobs=-2)
with pytest.raises(ValueError):
u.fit(nn_data)
u = UMAP(n_jobs=0)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_custom_distance_w_grad(nn_data):
@numba.njit()
def dist1(x, y):
return np.sum(np.abs(x - y))
@numba.njit()
def dist2(x, y):
return np.sum(np.abs(x - y)), (x - y)
u = UMAP(metric=dist1, n_epochs=11)
with pytest.warns(UserWarning) as warnings:
u.fit(nn_data[:10])
assert len(warnings) >= 1
u = UMAP(metric=dist2, n_epochs=11)
with pytest.warns(UserWarning) as warnings:
u.fit(nn_data[:10])
assert len(warnings) <= 1
def test_umap_bad_output_metric_no_grad(nn_data):
@numba.njit()
def dist1(x, y):
return np.sum(np.abs(x - y))
u = UMAP(output_metric=dist1)
with pytest.raises(ValueError):
u.fit(nn_data)
def test_umap_bad_hellinger_data(nn_data):
u = UMAP(metric="hellinger")
with pytest.raises(ValueError):
u.fit(-nn_data)
def test_umap_update_bad_params(nn_data):
dmat = pairwise_distances(nn_data[:100])
u = UMAP(metric="precomputed", n_epochs=11)
u.fit(dmat)
with pytest.raises(ValueError):
u.update(dmat)
u = UMAP(n_epochs=11)
u.fit(nn_data[:100], y=np.repeat(np.arange(5), 20))
with pytest.raises(ValueError):
u.update(nn_data[100:200])
def test_umap_fit_data_and_targets_compliant():
# x and y are required to be the same length
u = UMAP()
x = np.random.uniform(0, 1, (256, 10))
y = np.random.randint(10, size=(257,))
with pytest.raises(ValueError):
u.fit(x, y)
u = UMAP()
x = np.random.uniform(0, 1, (256, 10))
y = np.random.randint(10, size=(255,))
with pytest.raises(ValueError):
u.fit(x, y)
u = UMAP()
x = np.random.uniform(0, 1, (256, 10))
with pytest.raises(ValueError):
u.fit(x, [])
def test_umap_fit_instance_returned():
# Test that fit returns a new UMAP instance
# Passing both data and targets
u = UMAP()
x = np.random.uniform(0, 1, (256, 10))
y = np.random.randint(10, size=(256,))
res = u.fit(x, y)
assert isinstance(res, UMAP)
# Passing only data
u = UMAP()
x = np.random.uniform(0, 1, (256, 10))
res = u.fit(x)
assert isinstance(res, UMAP)
def test_umap_inverse_transform_fails_expectedly(sparse_spatial_data, nn_data):
u = UMAP(n_epochs=11)
u.fit(sparse_spatial_data[:100])
with pytest.raises(ValueError):
u.inverse_transform(u.embedding_[:10])
u = UMAP(metric="dice", n_epochs=11)
u.fit(nn_data[:100])
with pytest.raises(ValueError):
u.inverse_transform(u.embedding_[:10])
|