Skip to content

Commit

Permalink
OwLouvain: Properly compare new data with old without warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlin-policar committed Feb 15, 2019
1 parent ee5d4f3 commit acc2add
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Orange/widgets/unsupervised/owlouvainclustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit acc2add

Please sign in to comment.