|
3 | 3 | import pytest
|
4 | 4 |
|
5 | 5 | import numpy as np
|
6 |
| -import scipy.sparse as sp |
| 6 | +import scipy as sp |
| 7 | +import scipy.sparse as sps |
| 8 | +import scipy.sparse.csgraph as spgraph |
7 | 9 | import scipy.sparse.linalg as splin
|
8 | 10 | from numpy.testing import assert_almost_equal, assert_equal
|
9 | 11 |
|
@@ -32,7 +34,7 @@ def storage():
|
32 | 34 |
|
33 | 35 | def test_finch_backend():
|
34 | 36 | np_eye = np.eye(5)
|
35 |
| - sp_arr = sp.csr_matrix(np_eye) |
| 37 | + sp_arr = sps.csr_matrix(np_eye) |
36 | 38 |
|
37 | 39 | with sparse.Backend(backend=sparse.BackendType.Finch):
|
38 | 40 | import finch
|
@@ -69,19 +71,152 @@ def test_asarray(backend, format, order):
|
69 | 71 |
|
70 | 72 |
|
71 | 73 | @pytest.mark.parametrize("format, order", [("csc", "F"), ("csr", "C"), ("coo", "F"), ("coo", "C")])
|
72 |
| -def test_scipy_sparse_dispatch(backend, format, order): |
| 74 | +def test_scipy_spsolve(backend, format, order): |
73 | 75 | x = np.eye(10, order=order) * 2
|
74 | 76 | y = np.ones((10, 1), order=order)
|
| 77 | + x_pydata = sparse.asarray(x, format=format) |
| 78 | + y_pydata = sparse.asarray(y, format="coo") |
75 | 79 |
|
76 |
| - x_sp = sparse.asarray(x, format=format) |
77 |
| - y_sp = sparse.asarray(y, format="coo") |
78 |
| - |
79 |
| - actual = splin.spsolve(x_sp, y_sp) |
| 80 | + actual = splin.spsolve(x_pydata, y_pydata) |
80 | 81 | expected = np.linalg.solve(x, y.ravel())
|
81 |
| - |
82 | 82 | assert_almost_equal(actual, expected)
|
83 | 83 |
|
84 |
| - actual = splin.inv(x_sp) |
85 |
| - expected = np.linalg.inv(x) |
86 | 84 |
|
| 85 | +@pytest.mark.parametrize("format, order", [("csc", "F"), ("csr", "C"), ("coo", "F"), ("coo", "C")]) |
| 86 | +def test_scipy_inv(backend, format, order): |
| 87 | + x = np.eye(10, order=order) * 2 |
| 88 | + x_pydata = sparse.asarray(x, format=format) |
| 89 | + |
| 90 | + actual = splin.inv(x_pydata) |
| 91 | + expected = np.linalg.inv(x) |
87 | 92 | assert_almost_equal(actual.todense(), expected)
|
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.skip(reason="https://github.com/scipy/scipy/pull/20759") |
| 96 | +@pytest.mark.parametrize("format, order", [("csc", "F"), ("csr", "C"), ("coo", "F"), ("coo", "C")]) |
| 97 | +def test_scipy_norm(backend, format, order): |
| 98 | + x = np.eye(10, order=order) * 2 |
| 99 | + x_pydata = sparse.asarray(x, format=format) |
| 100 | + |
| 101 | + actual = splin.norm(x_pydata) |
| 102 | + expected = sp.linalg.norm(x) |
| 103 | + assert_almost_equal(actual, expected) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.skip(reason="https://github.com/scipy/scipy/pull/20759") |
| 107 | +@pytest.mark.parametrize("format, order", [("csc", "F"), ("csr", "C"), ("coo", "F"), ("coo", "C")]) |
| 108 | +def test_scipy_lsqr(backend, format, order): |
| 109 | + x = np.eye(10, order=order) * 2 |
| 110 | + y = np.ones((10, 1), order=order) |
| 111 | + x_pydata = sparse.asarray(x, format=format) |
| 112 | + |
| 113 | + actual_x, _ = splin.lsqr(x_pydata, y)[:2] |
| 114 | + expected_x, _ = sp.linalg.lstsq(x, y)[:2] |
| 115 | + assert_almost_equal(actual_x, expected_x.ravel()) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.skip(reason="https://github.com/scipy/scipy/pull/20759") |
| 119 | +@pytest.mark.parametrize("format, order", [("csc", "F"), ("csr", "C"), ("coo", "F"), ("coo", "C")]) |
| 120 | +def test_scipy_eigs(backend, format, order): |
| 121 | + x = np.eye(10, order=order) * 2 |
| 122 | + x_pydata = sparse.asarray(x, format=format) |
| 123 | + x_sp = sps.coo_matrix(x) |
| 124 | + |
| 125 | + actual_vals, _ = splin.eigs(x_pydata, k=3) |
| 126 | + expected_vals, _ = splin.eigs(x_sp, k=3) |
| 127 | + assert_almost_equal(actual_vals, expected_vals) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + "matrix_fn, format, order", |
| 132 | + [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C"), (sps.coo_matrix, "coo", "F")], |
| 133 | +) |
| 134 | +def test_scipy_connected_components(backend, graph, matrix_fn, format, order): |
| 135 | + graph = matrix_fn(np.array(graph, order=order)) |
| 136 | + sp_graph = sparse.asarray(graph, format=format) |
| 137 | + |
| 138 | + actual_n_components, actual_labels = spgraph.connected_components(sp_graph) |
| 139 | + expected_n_components, expected_labels = spgraph.connected_components(graph) |
| 140 | + assert actual_n_components == expected_n_components |
| 141 | + assert_equal(actual_labels, expected_labels) |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.parametrize( |
| 145 | + "matrix_fn, format, order", |
| 146 | + [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C"), (sps.coo_matrix, "coo", "F")], |
| 147 | +) |
| 148 | +def test_scipy_laplacian(backend, graph, matrix_fn, format, order): |
| 149 | + graph = matrix_fn(np.array(graph, order=order)) |
| 150 | + sp_graph = sparse.asarray(graph, format=format) |
| 151 | + |
| 152 | + actual_lap = spgraph.laplacian(sp_graph) |
| 153 | + expected_lap = spgraph.laplacian(graph) |
| 154 | + assert_equal(actual_lap.todense(), expected_lap.toarray()) |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize("matrix_fn, format, order", [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C")]) |
| 158 | +def test_scipy_shortest_path(backend, graph, matrix_fn, format, order): |
| 159 | + graph = matrix_fn(np.array(graph, order=order)) |
| 160 | + sp_graph = sparse.asarray(graph, format=format) |
| 161 | + |
| 162 | + actual_dist_matrix, actual_predecessors = spgraph.shortest_path(sp_graph, return_predecessors=True) |
| 163 | + expected_dist_matrix, expected_predecessors = spgraph.shortest_path(graph, return_predecessors=True) |
| 164 | + assert_equal(actual_dist_matrix, expected_dist_matrix) |
| 165 | + assert_equal(actual_predecessors, expected_predecessors) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.parametrize( |
| 169 | + "matrix_fn, format, order", |
| 170 | + [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C"), (sps.coo_matrix, "coo", "F")], |
| 171 | +) |
| 172 | +def test_scipy_breadth_first_tree(backend, graph, matrix_fn, format, order): |
| 173 | + graph = matrix_fn(np.array(graph, order=order)) |
| 174 | + sp_graph = sparse.asarray(graph, format=format) |
| 175 | + |
| 176 | + actual_bft = spgraph.breadth_first_tree(sp_graph, 0, directed=False) |
| 177 | + expected_bft = spgraph.breadth_first_tree(graph, 0, directed=False) |
| 178 | + assert_equal(actual_bft.todense(), expected_bft.toarray()) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.parametrize( |
| 182 | + "matrix_fn, format, order", |
| 183 | + [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C"), (sps.coo_matrix, "coo", "F")], |
| 184 | +) |
| 185 | +def test_scipy_dijkstra(backend, graph, matrix_fn, format, order): |
| 186 | + graph = matrix_fn(np.array(graph, order=order)) |
| 187 | + sp_graph = sparse.asarray(graph, format=format) |
| 188 | + |
| 189 | + actual_dist_matrix = spgraph.dijkstra(sp_graph, directed=False) |
| 190 | + expected_dist_matrix = spgraph.dijkstra(graph, directed=False) |
| 191 | + assert_equal(actual_dist_matrix, expected_dist_matrix) |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.parametrize( |
| 195 | + "matrix_fn, format, order", |
| 196 | + [(sps.csc_matrix, "csc", "F"), (sps.csr_matrix, "csr", "C"), (sps.coo_matrix, "coo", "F")], |
| 197 | +) |
| 198 | +def test_scipy_minimum_spanning_tree(backend, graph, matrix_fn, format, order): |
| 199 | + graph = matrix_fn(np.array(graph, order=order)) |
| 200 | + sp_graph = sparse.asarray(graph, format=format) |
| 201 | + |
| 202 | + actual_span_tree = spgraph.minimum_spanning_tree(sp_graph) |
| 203 | + expected_span_tree = spgraph.minimum_spanning_tree(graph) |
| 204 | + assert_equal(actual_span_tree.todense(), expected_span_tree.toarray()) |
| 205 | + |
| 206 | + |
| 207 | +@pytest.mark.skip(reason="https://github.com/scikit-learn/scikit-learn/pull/29031") |
| 208 | +@pytest.mark.parametrize("matrix_fn, format, order", [(sps.csc_matrix, "csc", "F")]) |
| 209 | +def test_scikit_learn_dispatch(backend, graph, matrix_fn, format, order): |
| 210 | + from sklearn.cluster import KMeans |
| 211 | + |
| 212 | + graph = matrix_fn(np.array(graph, order=order)) |
| 213 | + |
| 214 | + sp_graph = sparse.asarray(graph, format=format) |
| 215 | + |
| 216 | + neigh = KMeans(n_clusters=2) |
| 217 | + actual_labels = neigh.fit_predict(sp_graph) |
| 218 | + |
| 219 | + neigh = KMeans(n_clusters=2) |
| 220 | + expected_labels = neigh.fit_predict(graph) |
| 221 | + |
| 222 | + assert_equal(actual_labels, expected_labels) |
0 commit comments