Skip to content

Commit

Permalink
Run PCA in a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Aug 11, 2023
1 parent 2dda296 commit 5f63e6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
48 changes: 33 additions & 15 deletions Orange/widgets/unsupervised/owpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from Orange.preprocess import preprocess
from Orange.projection import PCA
from Orange.widgets import widget, gui, settings
from Orange.widgets.utils.concurrent import ConcurrentWidgetMixin
from Orange.widgets.utils.slidergraph import SliderGraph
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.widget import Input, Output
Expand All @@ -21,7 +22,7 @@
LINE_NAMES = ["component variance", "cumulative variance"]


class OWPCA(widget.OWWidget):
class OWPCA(widget.OWWidget, ConcurrentWidgetMixin):
name = "PCA"
description = "Principal component analysis with a scree-diagram."
icon = "icons/PCA.svg"
Expand Down Expand Up @@ -57,8 +58,9 @@ class Error(widget.OWWidget.Error):

def __init__(self):
super().__init__()
self.data = None
ConcurrentWidgetMixin.__init__(self)

self.data = None
self._pca = None
self._transformed = None
self._variance_ratio = None
Expand Down Expand Up @@ -113,6 +115,7 @@ def __init__(self):

@Inputs.data
def set_data(self, data):
self.cancel()
self.clear_messages()
self.clear()
self.information()
Expand Down Expand Up @@ -141,6 +144,7 @@ def set_data(self, data):
self.fit()

def fit(self):
self.cancel()
self.clear()
self.Warning.trivial_components.clear()
if self.data is None:
Expand All @@ -151,20 +155,34 @@ def fit(self):
projector = self._create_projector()

if not isinstance(data, SqlTable):
pca = projector(data)
variance_ratio = pca.explained_variance_ratio_
cumulative = numpy.cumsum(variance_ratio)

if numpy.isfinite(cumulative[-1]):
self.components_spin.setRange(0, len(cumulative))
self._pca = pca
self._variance_ratio = variance_ratio
self._cumulative = cumulative
self._setup_plot()
else:
self.Warning.trivial_components()
self.start(self._call_projector, data, projector)

@staticmethod
def _call_projector(data: Table, projector, _):
return projector(data)

def on_done(self, result):
pca = result
variance_ratio = pca.explained_variance_ratio_
cumulative = numpy.cumsum(variance_ratio)

if numpy.isfinite(cumulative[-1]):
self.components_spin.setRange(0, len(cumulative))
self._pca = pca
self._variance_ratio = variance_ratio
self._cumulative = cumulative
self._setup_plot()
else:
self.Warning.trivial_components()

self.commit.now()

def on_partial_result(self, result):
pass

self.commit.now()
def onDeleteWidget(self):
self.shutdown()
super().onDeleteWidget()

def clear(self):
self._pca = None
Expand Down
18 changes: 11 additions & 7 deletions Orange/widgets/unsupervised/tests/test_owpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_constant_data(self):
# Ignore the warning: the test checks whether the widget shows
# Warning.trivial_components when this happens
with np.errstate(invalid="ignore"):
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.data, data, wait=5000)
self.assertTrue(self.widget.Warning.trivial_components.is_shown())
self.assertIsNone(self.get_output(self.widget.Outputs.transformed_data))
self.assertIsNone(self.get_output(self.widget.Outputs.components))
Expand All @@ -55,12 +55,12 @@ def test_limit_components(self):
X = np.random.RandomState(0).rand(101, 101)
data = Table.from_numpy(None, X)
self.widget.ncomponents = 100
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.data, data, wait=5000)
tran = self.get_output(self.widget.Outputs.transformed_data)
self.assertEqual(len(tran.domain.attributes), 100)
self.widget.ncomponents = 101 # should not be accesible
with self.assertRaises(IndexError):
self.send_signal(self.widget.Inputs.data, data)
self.widget._setup_plot() # pylint: disable=protected-access

def test_migrate_settings_limits_components(self):
settings = dict(ncomponents=10)
Expand All @@ -83,9 +83,11 @@ def test_variance_shown(self):
self.send_signal(self.widget.Inputs.data, self.iris)
self.widget.maxp = 2
self.widget._setup_plot()
self.wait_until_finished()
var2 = self.widget.variance_covered
self.widget.ncomponents = 3
self.widget._update_selection_component_spin()
self.wait_until_finished()
var3 = self.widget.variance_covered
self.assertGreater(var3, var2)

Expand All @@ -97,10 +99,11 @@ def test_unique_domain_components(self):

def test_variance_attr(self):
self.widget.ncomponents = 2
self.send_signal(self.widget.Inputs.data, self.iris)
self.send_signal(self.widget.Inputs.data, self.iris, wait=5000)
self.wait_until_stop_blocking()
self.widget._variance_ratio = np.array([0.5, 0.25, 0.2, 0.05])
self.widget.commit.now()
self.wait_until_finished()

result = self.get_output(self.widget.Outputs.transformed_data)
pc1, pc2 = result.domain.attributes
Expand Down Expand Up @@ -162,7 +165,7 @@ def test_normalize_data(self, prepare_table):
self.widget.controls.normalize.setChecked(True)
self.assertTrue(self.widget.controls.normalize.isChecked())
with patch.object(preprocess.Normalize, "__call__", wraps=lambda x: x) as normalize:
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.data, data, wait=5000)
self.wait_until_stop_blocking()
self.assertTrue(self.widget.controls.normalize.isEnabled())
normalize.assert_called_once()
Expand All @@ -171,7 +174,7 @@ def test_normalize_data(self, prepare_table):
self.widget.controls.normalize.setChecked(False)
self.assertFalse(self.widget.controls.normalize.isChecked())
with patch.object(preprocess.Normalize, "__call__", wraps=lambda x: x) as normalize:
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.data, data, wait=5000)
self.wait_until_stop_blocking()
self.assertTrue(self.widget.controls.normalize.isEnabled())
normalize.assert_not_called()
Expand All @@ -184,13 +187,14 @@ def test_normalization_variance(self, prepare_table):
# Enable normalization
self.widget.controls.normalize.setChecked(True)
self.assertTrue(self.widget.normalize)
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.data, data, wait=5000)
self.wait_until_stop_blocking()
variance_normalized = self.widget.variance_covered

# Disable normalization
self.widget.controls.normalize.setChecked(False)
self.assertFalse(self.widget.normalize)
self.wait_until_finished()
self.wait_until_stop_blocking()
variance_unnormalized = self.widget.variance_covered

Expand Down

0 comments on commit 5f63e6f

Please sign in to comment.