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
|
# ===========================
# Testing (session) Fixture
# ==========================
import pytest
import numpy as np
from scipy import sparse
from sklearn.datasets import load_iris
from umap import UMAP, AlignedUMAP
# Globals, used for all the tests
SEED = 189212 # 0b101110001100011100
np.random.seed(SEED)
# 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 aligned_iris(iris):
slices = [iris.data[i : i + 50] for i in range(0, 125, 25)]
target = [iris.target[i : i + 50] for i in range(0, 125, 25)]
return slices, target
@pytest.fixture(scope="session")
def aligned_iris_relations():
return [{a: a + 25 for a in range(25)} for i in range(4)]
@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 iris_model_large(iris):
return UMAP(
n_neighbors=10,
min_dist=0.01,
random_state=42,
force_approximation_algorithm=True,
).fit(iris.data)
@pytest.fixture(scope="session")
def iris_subset_model(iris, iris_selection):
return UMAP(n_neighbors=10, min_dist=0.01, random_state=42).fit(
iris.data[iris_selection]
)
@pytest.fixture(scope="session")
def iris_subset_model_large(iris, iris_selection):
return UMAP(
n_neighbors=10,
min_dist=0.01,
random_state=42,
force_approximation_algorithm=True,
).fit(iris.data[iris_selection])
@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
)
@pytest.fixture(scope="session")
def aligned_iris_model(aligned_iris, aligned_iris_relations):
data, target = aligned_iris
model = AlignedUMAP()
model.fit(data, relations=aligned_iris_relations)
return model
# 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",
)
|