File: conftest.py

package info (click to toggle)
umap-learn 0.4.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,668 kB
  • sloc: python: 7,504; sh: 77; makefile: 17
file content (184 lines) | stat: -rw-r--r-- 4,585 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
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
# ===========================
#  Testing (session) Fixture
# ==========================

import pytest
import numpy as np
from scipy import sparse
from sklearn.datasets import load_iris
from umap import UMAP

# Globals, used for all the tests
np.random.seed(42)


# Spatial and Binary Data
# -----------------------
@pytest.fixture(scope="session")
def spatial_data():
    # - Spatial Data
    spatial_data = np.random.randn(10, 20)
    # Add some all zero data for corner case test
    return np.vstack([spatial_data, np.zeros((2, 20))])


@pytest.fixture(scope="session")
def binary_data():
    binary_data = np.random.choice(a=[False, True], size=(10, 20), p=[0.66, 1 - 0.66])
    # Add some all zero data for corner case test
    binary_data = np.vstack([binary_data, np.zeros((2, 20), dtype="bool")])
    return binary_data


# Sparse Spatial and Binary Data
# ------------------------------
@pytest.fixture(scope="session")
def sparse_spatial_data(spatial_data, binary_data):
    return sparse.csr_matrix(spatial_data * binary_data)


@pytest.fixture(scope="session")
def sparse_binary_data(binary_data):
    return sparse.csr_matrix(binary_data)


# Nearest Neighbour Data
# -----------------------
@pytest.fixture(scope="session")
def nn_data():
    nn_data = np.random.uniform(0, 1, size=(1000, 5))
    nn_data = np.vstack(
        [nn_data, np.zeros((2, 5))]
    )  # Add some all zero data for corner case test
    return nn_data


@pytest.fixture(scope="session")
def binary_nn_data():
    binary_nn_data = np.random.choice(
        a=[False, True], size=(1000, 5), p=[0.66, 1 - 0.66]
    )
    binary_nn_data = np.vstack(
        [binary_nn_data, np.zeros((2, 5), dtype="bool")]
    )  # Add some all zero data for corner case test
    return binary_nn_data


@pytest.fixture(scope="session")
def sparse_nn_data():
    return sparse.random(1000, 50, density=0.5, format="csr")


# Data With Repetitions
# ---------------------


@pytest.fixture(scope="session")
def repetition_dense():
    # Dense data for testing small n
    return np.array(
        [
            [5, 6, 7, 8],
            [5, 6, 7, 8],
            [5, 6, 7, 8],
            [5, 6, 7, 8],
            [5, 6, 7, 8],
            [5, 6, 7, 8],
            [1, 1, 1, 1],
            [1, 2, 3, 4],
            [1, 1, 2, 1],
        ]
    )


@pytest.fixture(scope="session")
def spatial_repeats(spatial_data):
    # spatial data repeats
    spatial_repeats = np.vstack(
        [np.repeat(spatial_data[0:2], [2, 0], axis=0), spatial_data, np.zeros((2, 20))]
    )
    # Add some all zero data for corner case test.  Make the first three rows identical
    # binary Data Repeat
    return spatial_repeats


@pytest.fixture(scope="session")
def binary_repeats(binary_data):
    binary_repeats = np.vstack(
        [
            np.repeat(binary_data[0:2], [2, 0], axis=0),
            binary_data,
            np.zeros((2, 20), dtype="bool"),
        ]
    )
    # Add some all zero data for corner case test.  Make the first three rows identical
    return binary_repeats


@pytest.fixture(scope="session")
def sparse_spatial_data_repeats(spatial_repeats, binary_repeats):
    return sparse.csr_matrix(spatial_repeats * binary_repeats)


@pytest.fixture(scope="session")
def sparse_binary_data_repeats(binary_repeats):
    return sparse.csr_matrix(binary_repeats)


@pytest.fixture(scope="session")
def sparse_test_data(nn_data, binary_nn_data):
    return sparse.csr_matrix(nn_data * binary_nn_data)


@pytest.fixture(scope="session")
def iris():
    return load_iris()


@pytest.fixture(scope="session")
def iris_selection():
    return np.random.choice([True, False], 150, replace=True, p=[0.75, 0.25])


@pytest.fixture(scope="session")
def iris_model(iris):
    return UMAP(n_neighbors=10, min_dist=0.01, random_state=42).fit(iris.data)


@pytest.fixture(scope="session")
def supervised_iris_model(iris):
    return UMAP(n_neighbors=10, min_dist=0.01, n_epochs=200, random_state=42).fit(
        iris.data, iris.target
    )


# UMAP Distance Metrics
# ---------------------
@pytest.fixture(scope="session")
def spatial_distances():
    return (
        "euclidean",
        "manhattan",
        "chebyshev",
        "minkowski",
        "hamming",
        "canberra",
        "braycurtis",
        "cosine",
        "correlation",
    )


@pytest.fixture(scope="session")
def binary_distances():
    return (
        "jaccard",
        "matching",
        "dice",
        "kulsinski",
        "rogerstanimoto",
        "russellrao",
        "sokalmichener",
        "sokalsneath",
        "yule",
    )