File: test_spectral.py

package info (click to toggle)
umap-learn 0.5.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,284 kB
  • sloc: python: 9,863; sh: 87; makefile: 20
file content (51 lines) | stat: -rw-r--r-- 1,686 bytes parent folder | download
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
from umap.spectral import spectral_layout, tswspectral_layout

import numpy as np
import pytest
from scipy.version import full_version as scipy_full_version_
from warnings import catch_warnings


scipy_full_version = tuple(int(n) for n in scipy_full_version_.split("."))


@pytest.mark.skipif(
    scipy_full_version < (1, 10),
    reason="SciPy installing with Python 3.7 does not converge under same circumstances"
)
def test_tsw_spectral_init(iris):
    # create an arbitrary (dense) random affinity matrix
    seed = 42
    rng = np.random.default_rng(seed=seed)
    # matrix must be of sufficient size of lobpcg will refuse to work on it
    n = 20
    graph = rng.standard_normal(n * n).reshape((n, n)) ** 2
    graph = graph.T * graph

    spec = spectral_layout(None, graph, 2, random_state=seed ** 2)
    tsw_spec = tswspectral_layout(None, graph, 2, random_state=seed ** 2, tol=1e-8)

    # Make sure the two methods produce similar embeddings.
    rmsd = np.mean(np.sum((spec - tsw_spec) ** 2, axis=1))
    assert (
        rmsd < 1e-6
    ), "tsvd-warmed spectral init insufficiently close to standard spectral init"


@pytest.mark.skipif(
    scipy_full_version < (1, 10),
    reason="SciPy installing with Py 3.7 does not warn reliably on convergence failure"
)
def test_ensure_fallback_to_random_on_spectral_failure():
    dim = 1000
    k = 10
    assert k >= 10
    assert dim // 10 > k
    y = np.eye(dim, k=1)
    u = np.random.random((dim, dim // 10))
    graph = y + y.T + u @ u.T
    with pytest.warns(
        UserWarning,
        match="Spectral initialisation failed!"
    ):
        tswspectral_layout(u, graph, k, random_state=42, maxiter=2, method="lobpcg")