Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:

- name: Run tests
run: |
nose2 -vvv
pytest

- name: Coveralls
env:
Expand Down
17 changes: 17 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[pytest]
testpaths = test
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
-v
--strict-markers
--tb=short
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
filterwarnings =
error
ignore::PendingDeprecationWarning:.*matrix subclass.*
ignore::DeprecationWarning:.*SafeConfigParser.*
ignore::DeprecationWarning:.*collections.*
ignore::DeprecationWarning:.*check_pickle.*
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ exclude =
profile = black
force_single_line = true
force_alphabetical_sort = true

[coverage]
always-on = True
coverage = graphtools
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
]

test_requires = [
"nose",
"nose2",
"pytest",
"pytest-cov",
"pandas",
"coverage",
"coveralls",
Expand Down Expand Up @@ -64,7 +64,6 @@
"fast": fast_requires,
"all": all_requires,
},
test_suite="nose2.collector.collector",
long_description=readme,
url="https://github.com/KrishnaswamyLab/graphtools",
download_url="https://github.com/KrishnaswamyLab/graphtools/archive/v{}.tar.gz".format(
Expand Down
20 changes: 15 additions & 5 deletions test/load_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from nose.tools import assert_raises_regex
from nose.tools import assert_warns_regex
from scipy.spatial.distance import cdist
from scipy.spatial.distance import pdist
from scipy.spatial.distance import squareform
Expand All @@ -8,23 +6,35 @@
from sklearn.decomposition import TruncatedSVD

import graphtools
import nose2
import numpy as np
import pandas as pd
import pygsp
import pytest
import re
import scipy.sparse as sp
import warnings


def assert_warns_message(expected_warning, expected_message, *args, **kwargs):
expected_regex = re.escape(expected_message)
return assert_warns_regex(expected_warning, expected_regex, *args, **kwargs)
if args:
# If function arguments are provided, call the function within the context
with pytest.warns(expected_warning, match=expected_regex):
return args[0](*args[1:], **kwargs)
else:
# Return the context manager to be used with 'with' statement
return pytest.warns(expected_warning, match=expected_regex)


def assert_raises_message(expected_warning, expected_message, *args, **kwargs):
expected_regex = re.escape(expected_message)
return assert_raises_regex(expected_warning, expected_regex, *args, **kwargs)
if args:
# If function arguments are provided, call the function within the context
with pytest.raises(expected_warning, match=expected_regex):
return args[0](*args[1:], **kwargs)
else:
# Return the context manager to be used with 'with' statement
return pytest.raises(expected_warning, match=expected_regex)


def reset_warnings():
Expand Down
11 changes: 5 additions & 6 deletions test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from load_tests import build_graph
from load_tests import data
from load_tests import graphtools
from load_tests import nose2
from load_tests import np
from load_tests import pd
from load_tests import pdist
from load_tests import sp
from load_tests import squareform
from nose.tools import assert_raises_regex

import numbers
import pytest
import warnings

try:
Expand Down Expand Up @@ -99,9 +98,9 @@ def test_negative_rank_threshold():


def test_True_n_pca_large_threshold():
with assert_raises_regex(
with pytest.raises(
ValueError,
r"Supplied threshold ([0-9\.]*) was greater than maximum singular value ([0-9\.]*) for the data matrix",
match=r"Supplied threshold ([0-9\.]*) was greater than maximum singular value ([0-9\.]*) for the data matrix",
):
build_graph(data, n_pca=True, rank_threshold=np.linalg.norm(data) ** 2)

Expand Down Expand Up @@ -388,10 +387,10 @@ def test_inverse_transform_sparse_svd():

# Flexible regex pattern that works across Python versions
sparse_error_pattern = r"(Sparse data|A sparse matrix) was passed, but dense data is required\. Use (?:'.*?'|X\.toarray\(\)) to convert to a dense numpy array\."
with assert_raises_regex(TypeError, sparse_error_pattern):
with pytest.raises(TypeError, match=sparse_error_pattern):
G.inverse_transform(sp.csr_matrix(G.data)[:, 0])

with assert_raises_regex(TypeError, sparse_error_pattern):
with pytest.raises(TypeError, match=sparse_error_pattern):
G.inverse_transform(sp.csr_matrix(G.data)[:, :15])

with assert_raises_message(
Expand Down
2 changes: 1 addition & 1 deletion test/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_estimator():
assert E.graph is None


@parameterized(
@parameterized.expand(
[
("precomputed", 1 - np.eye(10), "distance"),
("precomputed", np.eye(10), "affinity"),
Expand Down
12 changes: 6 additions & 6 deletions test/test_exact.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from load_tests import build_graph
from load_tests import data
from load_tests import graphtools
from load_tests import nose2
from load_tests import np
from load_tests import PCA
from load_tests import pdist
from load_tests import pygsp
from load_tests import sp
from load_tests import squareform
from load_tests import TruncatedSVD
from nose.tools import assert_warns_regex

import pytest
from scipy.sparse.csgraph import shortest_path

#####################################################
Expand Down Expand Up @@ -98,17 +98,17 @@ def test_precomputed_nonzero_diagonal():


def test_duplicate_data():
with assert_warns_regex(
with pytest.warns(
RuntimeWarning,
r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
match=r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
):
build_graph(np.vstack([data, data[:10]]), n_pca=20, decay=10, thresh=0)


def test_many_duplicate_data():
with assert_warns_regex(
with pytest.warns(
RuntimeWarning,
"Detected zero distance between ([0-9]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
match=r"Detected zero distance between ([0-9and,\s]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
):
build_graph(np.vstack([data, data]), n_pca=20, decay=10, thresh=0)

Expand Down
18 changes: 10 additions & 8 deletions test/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from load_tests import pygsp
from load_tests import sp
from load_tests import TruncatedSVD
from nose.tools import assert_raises_regex
from nose.tools import assert_warns_regex

import pytest
from scipy.sparse.csgraph import shortest_path

from scipy.spatial.distance import pdist
from scipy.spatial.distance import squareform

Expand Down Expand Up @@ -50,17 +51,17 @@ def test_build_knn_with_sample_idx():


def test_duplicate_data():
with assert_warns_regex(
with pytest.warns(
RuntimeWarning,
r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
match=r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.",
):
build_graph(np.vstack([data, data[:9]]), n_pca=None, decay=10, thresh=1e-4)


def test_duplicate_data_many():
with assert_warns_regex(
with pytest.warns(
RuntimeWarning,
"Detected zero distance between ([0-9]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
match=r"Detected zero distance between ([0-9and,\s]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.",
):
build_graph(np.vstack([data, data[:21]]), n_pca=None, decay=10, thresh=1e-4)

Expand Down Expand Up @@ -305,8 +306,9 @@ def test_knnmax():
)
assert isinstance(G2, graphtools.graphs.kNNGraph)
assert G.N == G2.N
assert np.all(G.dw == G2.dw)
assert (G.W - G2.W).nnz == 0
np.testing.assert_allclose(G.dw, G2.dw)
# Use allclose for sparse matrices to handle floating-point precision
np.testing.assert_allclose(G.W.toarray(), G2.W.toarray(), rtol=1e-7, atol=1e-10)
finally:
gg.NUMBA_AVAILABLE = original_numba

Expand Down
1 change: 0 additions & 1 deletion test/test_landmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from load_tests import digits
from load_tests import generate_swiss_roll
from load_tests import graphtools
from load_tests import nose2
from load_tests import np

import pygsp
Expand Down
6 changes: 3 additions & 3 deletions test/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np


@parameterized(
@parameterized.expand(
[
(np.array,),
(sparse.csr_matrix,),
Expand All @@ -26,13 +26,13 @@ def test_nonzero_discrete(matrix_class):
assert not graphtools.matrix.nonzero_discrete(X, [1, 3])


@parameterized([(0,), (1e-4,)])
@parameterized.expand([(0,), (1e-4,)])
def test_nonzero_discrete_knngraph(thresh):
G = graphtools.Graph(data, n_pca=10, knn=5, decay=None, thresh=thresh)
assert graphtools.matrix.nonzero_discrete(G.K, [0.5, 1])


@parameterized([(0,), (1e-4,)])
@parameterized.expand([(0,), (1e-4,)])
def test_nonzero_discrete_decay_graph(thresh):
G = graphtools.Graph(data, n_pca=10, knn=5, decay=15, thresh=thresh)
assert not graphtools.matrix.nonzero_discrete(G.K, [0.5, 1])
Expand Down
1 change: 0 additions & 1 deletion test/test_mnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from load_tests import digits
from load_tests import generate_swiss_roll
from load_tests import graphtools
from load_tests import nose2
from load_tests import np
from load_tests import pd
from load_tests import pygsp
Expand Down
1 change: 0 additions & 1 deletion test/test_random_landmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from load_tests import digits
from load_tests import generate_swiss_roll
from load_tests import graphtools
from load_tests import nose2
from load_tests import np

import pygsp
Expand Down
6 changes: 0 additions & 6 deletions unittest.cfg

This file was deleted.

Loading