diff --git a/Orange/widgets/unsupervised/owlouvainclustering.py b/Orange/widgets/unsupervised/owlouvainclustering.py index 3e6f1cecf9c..a572e1f3845 100644 --- a/Orange/widgets/unsupervised/owlouvainclustering.py +++ b/Orange/widgets/unsupervised/owlouvainclustering.py @@ -7,6 +7,7 @@ from typing import Optional, Callable, Tuple, Any import numpy as np +import scipy.sparse as sp import networkx as nx from AnyQt.QtCore import ( @@ -44,6 +45,16 @@ METRICS = [("Euclidean", "l2"), ("Manhattan", "l1")] +def is_same_data(t1: Table, t2: Table) -> bool: + """Check whether the `X`s in two tables are the same.""" + # Sparse matrices can't be compared directly using np.array_equal otherwise + # a warning is triggered. + if t1.is_sparse() and t2.is_sparse(): + return t1.X.shape == t2.X.shape and (t1.X != t2.X).nnz == 0 + else: + return np.array_equal(t1.X, t2.X) + + class OWLouvainClustering(widget.OWWidget): name = "Louvain Clustering" description = "Detects communities in a network of nearest neighbors." @@ -407,8 +418,7 @@ def set_data(self, data): # Make sure to properly enable/disable slider based on `apply_pca` setting self.controls.pca_components.setEnabled(self.apply_pca) - # If X hasn't changed, there's no reason to recompute clusters - if prev_data and self.data and np.array_equal(self.data.X, prev_data.X): + if prev_data and self.data and is_same_data(prev_data, self.data): if self.auto_commit: self._send_data() return