Skip to content

Commit

Permalink
bump coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Feb 4, 2020
1 parent 5d1869d commit 96abfef
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphtools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def _build_kernel(self):
kernel = self.apply_anisotropy(kernel)
if (kernel - kernel.T).max() > 1e-5:
warnings.warn("K should be symmetric", RuntimeWarning)
if np.any(kernel.diagonal == 0):
if np.any(kernel.diagonal() == 0):
warnings.warn("K should have a non-zero diagonal", RuntimeWarning)
return kernel

Expand Down
5 changes: 5 additions & 0 deletions test/test_exact.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def test_precomputed_invalid():
)


def test_precomputed_nonzero_diagonal():
with assert_warns_message(RuntimeWarning, "K should have a non-zero diagonal"):
build_graph(np.zeros((10, 10)), precomputed="affinity", n_pca=None)


def test_duplicate_data():
with assert_warns_regex(
RuntimeWarning,
Expand Down
40 changes: 40 additions & 0 deletions test/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def test_knn_no_knn_no_bandwidth():
build_graph(data, graphtype="knn", knn=None, bandwidth=None, thresh=1e-4)


def test_knn_graph_invalid_symm():
with assert_raises_message(
ValueError,
"kernel_symm 'invalid' not recognized. Choose from '+', '*', 'mnn', or 'none'.",
):
build_graph(data, graphtype="knn", knn=5, thresh=1e-4, kernel_symm="invalid")


#####################################################
# Check kernel
#####################################################
Expand Down Expand Up @@ -152,6 +160,38 @@ def test_knn_graph():
)


def test_knn_graph_multiplication_symm():
k = 3
n_pca = 20
pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data)
data_nu = pca.transform(data)
pdx = squareform(pdist(data_nu, metric="euclidean"))
knn_dist = np.partition(pdx, k, axis=1)[:, :k]
epsilon = np.max(knn_dist, axis=1)
K = np.empty_like(pdx)
for i in range(len(pdx)):
K[i, pdx[i, :] <= epsilon[i]] = 1
K[i, pdx[i, :] > epsilon[i]] = 0

W = K * K.T
np.fill_diagonal(W, 0)
G = pygsp.graphs.Graph(W)
G2 = build_graph(
data,
n_pca=n_pca,
decay=None,
knn=k - 1,
random_state=42,
use_pygsp=True,
kernel_symm="*",
)
assert G.N == G2.N
np.testing.assert_equal(G.dw, G2.dw)
assert (G.W - G2.W).nnz == 0
assert (G2.W - G.W).sum() == 0
assert isinstance(G2, graphtools.graphs.kNNGraph)


def test_knn_graph_sparse():
k = 3
n_pca = 20
Expand Down
4 changes: 4 additions & 0 deletions test/test_mnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def test_sample_idx_unique():
build_graph(
data, graph_class=graphtools.graphs.MNNGraph, sample_idx=np.ones(len(data))
)
with assert_warns_message(
UserWarning, "Only one unique sample. Not using MNNGraph"
):
build_graph(data, sample_idx=np.ones(len(data)), graphtype="mnn")


def test_sample_idx_none():
Expand Down
5 changes: 5 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def test_nonzero_discrete_knngraph(thresh):
def test_nonzero_discrete_decay_graph(thresh):
G = graphtools.Graph(data, n_pca=10, knn=5, decay=15, thresh=thresh)
assert not graphtools.utils.nonzero_discrete(G.K, [0.5, 1])


def test_nonzero_discrete_constant():
assert graphtools.utils.nonzero_discrete(2, [1, 2])
assert not graphtools.utils.nonzero_discrete(2, [1, 3])

0 comments on commit 96abfef

Please sign in to comment.