Skip to content

Commit 467ec5c

Browse files
authored
Merge pull request #4258 from janezd/louvain-graph-output
[FIX] Louvain Clustering: Update graph output for compatibility with the new network add-on
2 parents 3b7a479 + b912a85 commit 467ec5c

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

Orange/widgets/unsupervised/owlouvainclustering.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Optional, Callable, Tuple, Any
88

99
import numpy as np
10+
import scipy.sparse as sp
1011
import networkx as nx
1112

1213
from AnyQt.QtCore import (
@@ -30,9 +31,9 @@
3031
from Orange.widgets.widget import Msg
3132

3233
try:
33-
from orangecontrib.network.network import Graph
34+
from orangecontrib.network.network import Network
3435
except ImportError:
35-
Graph = None
36+
Network = None
3637

3738

3839
_MAX_PCA_COMPONENTS = 50
@@ -57,13 +58,10 @@ class OWLouvainClustering(widget.OWWidget):
5758
class Inputs:
5859
data = Input("Data", Table, default=True)
5960

60-
if Graph is not None:
61-
class Outputs:
62-
annotated_data = Output(ANNOTATED_DATA_SIGNAL_NAME, Table, default=True)
63-
graph = Output("Network", Graph)
64-
else:
65-
class Outputs:
66-
annotated_data = Output(ANNOTATED_DATA_SIGNAL_NAME, Table, default=True)
61+
class Outputs:
62+
annotated_data = Output(ANNOTATED_DATA_SIGNAL_NAME, Table, default=True)
63+
if Network is not None:
64+
graph = Output("Network", Network)
6765

6866
apply_pca = ContextSetting(True)
6967
pca_components = ContextSetting(_DEFAULT_PCA_COMPONENTS)
@@ -391,9 +389,12 @@ def _send_data(self):
391389
new_table.get_column_view(cluster_var)[0][:] = new_partition
392390
self.Outputs.annotated_data.send(new_table)
393391

394-
if Graph is not None:
395-
graph = Graph(self.graph)
396-
graph.set_items(new_table)
392+
if Network is not None:
393+
n_edges = self.graph.number_of_edges()
394+
edges = sp.coo_matrix(
395+
(np.ones(n_edges), np.array(self.graph.edges()).T),
396+
shape=(n_edges, n_edges))
397+
graph = Network(new_table, edges)
397398
self.Outputs.graph.send(graph)
398399

399400
@Inputs.data
@@ -414,7 +415,7 @@ def set_data(self, data):
414415
self.cancel()
415416
# Clear the outputs
416417
self.Outputs.annotated_data.send(None)
417-
if Graph is not None:
418+
if Network is not None:
418419
self.Outputs.graph.send(None)
419420

420421
# Clear internal state

Orange/widgets/unsupervised/tests/test_owlouvain.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,18 @@ def test_settings_correctly_restored(self):
265265
self.assertFalse(w.normalize)
266266
self.assertEqual(4, w.k_neighbors)
267267
self.assertEqual(0.5, w.resolution)
268+
269+
def test_graph_output(self):
270+
w = self.widget
271+
272+
# This test executes only if network add-on is installed
273+
if not hasattr(w.Outputs, "graph"):
274+
return
275+
276+
self.send_signal(w.Inputs.data, self.iris)
277+
graph = self.get_output(w.Outputs.graph)
278+
self.assertEqual(len(graph.nodes), len(self.iris))
279+
280+
self.send_signal(w.Inputs.data, None)
281+
graph = self.get_output(w.Outputs.graph)
282+
self.assertIsNone(graph)

0 commit comments

Comments
 (0)