Repository: biolab/orange3

Issue:
Louvian Clustering widget not saving custom k neighbours
In saved workflows k neighbours in the Louvian Clustering widget always reset to 30 upon reopening them.



Discussion:

Resolving patch:
diff --git a/Orange/widgets/unsupervised/owlouvainclustering.py b/Orange/widgets/unsupervised/owlouvainclustering.py
--- a/Orange/widgets/unsupervised/owlouvainclustering.py
+++ b/Orange/widgets/unsupervised/owlouvainclustering.py
@@ -431,10 +431,8 @@ def set_data(self, data):
         # Can't have more PCA components than the number of attributes
         n_attrs = len(data.domain.attributes)
         self.pca_components_slider.setMaximum(min(_MAX_PCA_COMPONENTS, n_attrs))
-        self.pca_components_slider.setValue(min(_DEFAULT_PCA_COMPONENTS, n_attrs))
         # Can't have more k neighbors than there are data points
         self.k_neighbors_spin.setMaximum(min(_MAX_K_NEIGBOURS, len(data) - 1))
-        self.k_neighbors_spin.setValue(min(_DEFAULT_K_NEIGHBORS, len(data) - 1))
 
         self.info_label.setText("Clustering not yet run.")
 


Test patch:
diff --git a/Orange/widgets/unsupervised/tests/test_owlouvain.py b/Orange/widgets/unsupervised/tests/test_owlouvain.py
--- a/Orange/widgets/unsupervised/tests/test_owlouvain.py
+++ b/Orange/widgets/unsupervised/tests/test_owlouvain.py
@@ -5,7 +5,7 @@
 from Orange.data import Table, Domain, ContinuousVariable
 from Orange.preprocess import Normalize
 from Orange.widgets.tests.base import WidgetTest
-from Orange.widgets.tests.utils import table_dense_sparse
+from Orange.widgets.tests.utils import table_dense_sparse, simulate
 from Orange.widgets.unsupervised.owlouvainclustering import OWLouvainClustering
 from sklearn.utils import check_random_state
 
@@ -234,3 +234,34 @@ def _compute_clustering(data):
         dense_result = _compute_clustering(dense_data)
         sparse_result = _compute_clustering(sparse_data)
         np.testing.assert_equal(dense_result.metas, sparse_result.metas)
+
+    def test_settings_correctly_restored(self):
+        """
+        This test checks if contextsettings are correctly restored after
+        dataset changed.
+        """
+        w = self.widget
+
+        self.send_signal(w.Inputs.data, self.iris)
+        w.controls.apply_pca.setChecked(False)
+        w.controls.pca_components.setValue(2)
+        simulate.combobox_activate_item(w.controls.metric_idx, "Manhattan")
+        w.controls.normalize.setChecked(False)
+        w.controls.k_neighbors.setValue(4)
+        w.controls.resolution.setValue(0.5)
+
+        self.send_signal(w.Inputs.data, Table("zoo"))
+        w.controls.apply_pca.setChecked(True)
+        w.controls.pca_components.setValue(3)
+        simulate.combobox_activate_item(w.controls.metric_idx, "Euclidean")
+        w.controls.normalize.setChecked(True)
+        w.controls.k_neighbors.setValue(5)
+        w.controls.resolution.setValue(1)
+
+        self.send_signal(w.Inputs.data, self.iris)
+        self.assertFalse(w.apply_pca)
+        self.assertEqual(2, w.pca_components)
+        self.assertEqual(1, w.metric_idx)
+        self.assertFalse(w.normalize)
+        self.assertEqual(4, w.k_neighbors)
+        self.assertEqual(0.5, w.resolution)