From acc2add8a091b0e758304518ae68d6614a6b1821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavlin=20Poli=C4=8Dar?= Date: Fri, 15 Feb 2019 10:21:04 +0100 Subject: [PATCH] OwLouvain: Properly compare new data with old without warnings --- Orange/widgets/unsupervised/owlouvainclustering.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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