From 06a586a50b57d5f01dcd2a89a89fe69a48b06825 Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Fri, 21 Jul 2023 10:35:14 -0400 Subject: [PATCH 1/6] attempted fix for matrix type erroring --- graspologic/utils/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graspologic/utils/utils.py b/graspologic/utils/utils.py index 8578460cc..84e4a6dc4 100644 --- a/graspologic/utils/utils.py +++ b/graspologic/utils/utils.py @@ -275,11 +275,11 @@ def is_almost_symmetric( Raises ------ TypeError - If the provided graph is not a numpy.ndarray or scipy.sparse.spmatrix + If the provided graph is not a numpy.ndarray or scipy.sparse.csr_array """ if (x.ndim != 2) or (x.shape[0] != x.shape[1]): return False - if isinstance(x, (np.ndarray, scipy.sparse.spmatrix)): + if isinstance(x, (np.ndarray, csr_array)): return abs(x - x.T).max() <= atol else: raise TypeError("input a correct matrix type.") From 4d690c19de2f896b086ccbbbda6ddbb862dc808a Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Mon, 31 Jul 2023 11:58:22 -0400 Subject: [PATCH 2/6] fix spec of csr_array --- graspologic/embed/svd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graspologic/embed/svd.py b/graspologic/embed/svd.py index 244d67ddc..d02d6ec11 100644 --- a/graspologic/embed/svd.py +++ b/graspologic/embed/svd.py @@ -7,11 +7,11 @@ import scipy import scipy.sparse as sp import sklearn +from scipy.sparse import csr_array from scipy.stats import norm from typing_extensions import Literal from graspologic.types import List, Tuple -from graspologic.utils import is_almost_symmetric SvdAlgorithmType = Literal["full", "truncated", "randomized", "eigsh"] @@ -107,7 +107,7 @@ def select_dimension( pp.918-930. """ # Handle input data - if not isinstance(X, np.ndarray) and not sp.isspmatrix_csr(X): + if not isinstance(X, (np.ndarray, csr_array)): msg = "X must be a numpy array or scipy.sparse.csr_array, not {}.".format( type(X) ) From 4a446717f9d251539a1774badd02251eac36efb5 Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Mon, 31 Jul 2023 11:59:55 -0400 Subject: [PATCH 3/6] add a random seed --- tests/test_spectral_embed.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_spectral_embed.py b/tests/test_spectral_embed.py index 05b4023ee..0934598df 100644 --- a/tests/test_spectral_embed.py +++ b/tests/test_spectral_embed.py @@ -164,6 +164,7 @@ def test_directed_vertex_direction(self): def test_directed_correct_latent_positions(self): # setup ase = AdjacencySpectralEmbed(n_components=3) + np.random.seed(8888) P = np.array([[0.9, 0.1, 0.1], [0.3, 0.6, 0.1], [0.1, 0.5, 0.6]]) M, labels = sbm([200, 200, 200], P, directed=True, return_labels=True) From 56de20851c8b52b793df3ebbc994b063ff126fad Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Mon, 31 Jul 2023 12:11:11 -0400 Subject: [PATCH 4/6] remove isspmatrix --- graspologic/utils/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/graspologic/utils/utils.py b/graspologic/utils/utils.py index 84e4a6dc4..1e17efc85 100644 --- a/graspologic/utils/utils.py +++ b/graspologic/utils/utils.py @@ -12,7 +12,7 @@ import scipy.sparse from beartype import beartype from scipy.optimize import linear_sum_assignment -from scipy.sparse import csgraph, csr_array, diags, isspmatrix_csr +from scipy.sparse import csgraph, csr_array, diags from scipy.sparse.csgraph import connected_components from sklearn.metrics import confusion_matrix from sklearn.utils import check_array, check_consistent_length, column_or_1d @@ -43,7 +43,7 @@ def average_matrices( """ if isinstance(matrices[0], np.ndarray): return np.mean(matrices, axis=0) # type: ignore - elif isspmatrix_csr(matrices[0]): + elif isinstance(matrices[0], csr_array): return np.sum(matrices) / len(matrices) raise TypeError(f"Unexpected type {matrices}") @@ -836,7 +836,7 @@ def augment_diagonal( degrees = (in_degrees + out_degrees) / 2 diag = weight * degrees / divisor - graph += diags(diag) if isspmatrix_csr(graph) else np.diag(diag) + graph += diags(diag) if isinstance(graph, csr_array) else np.diag(diag) return graph From d2479632f4c511e6d92423eb1cc022755504ccbd Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Mon, 31 Jul 2023 12:18:29 -0400 Subject: [PATCH 5/6] fix a type check --- tests/partition/test_leiden.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/partition/test_leiden.py b/tests/partition/test_leiden.py index 8541b9de8..5bbd1e923 100644 --- a/tests/partition/test_leiden.py +++ b/tests/partition/test_leiden.py @@ -193,8 +193,8 @@ def test_correct_types(self): node_ids = partitions.keys() for node_id in node_ids: self.assertTrue( - isinstance(node_id, (np.int32, np.intc)), - f"{node_id} has {type(node_id)} should be an np.int32/np.intc", + isinstance(node_id, np.integer), # this is the preferred numpy typecheck + f"{node_id} has {type(node_id)} should be an int", ) def test_hierarchical(self): From 76335fb134f0fe5135030c8193f27edc8d052baa Mon Sep 17 00:00:00 2001 From: bdpedigo Date: Mon, 31 Jul 2023 12:18:54 -0400 Subject: [PATCH 6/6] black --- tests/partition/test_leiden.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/partition/test_leiden.py b/tests/partition/test_leiden.py index 5bbd1e923..4e2247b16 100644 --- a/tests/partition/test_leiden.py +++ b/tests/partition/test_leiden.py @@ -193,7 +193,9 @@ def test_correct_types(self): node_ids = partitions.keys() for node_id in node_ids: self.assertTrue( - isinstance(node_id, np.integer), # this is the preferred numpy typecheck + isinstance( + node_id, np.integer + ), # this is the preferred numpy typecheck f"{node_id} has {type(node_id)} should be an int", )