Skip to content

Commit

Permalink
Merge pull request #6595 from ZanMervic/threaded_apply_domain
Browse files Browse the repository at this point in the history
Apply Domain: computation in a thread
  • Loading branch information
markotoplak authored Oct 13, 2023
2 parents d07762d + b7d376b commit 0597b34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
48 changes: 36 additions & 12 deletions Orange/widgets/data/owtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@
from Orange.widgets.utils.sql import check_sql_input
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.widget import OWWidget, Input, Output, Msg
from Orange.widgets.utils.concurrent import TaskState, ConcurrentWidgetMixin


class OWTransform(OWWidget):
class TransformRunner:
@staticmethod
def run(
data: Table,
template_data: Table,
state: TaskState
) -> Optional[Table]:
if data is None or template_data is None:
return None

state.set_status("Transforming...")
transformed_data = data.transform(template_data.domain)
return transformed_data


class OWTransform(OWWidget, ConcurrentWidgetMixin):
name = "Apply Domain"
description = "Applies template domain on data table."
category = "Transform"
Expand All @@ -31,7 +47,8 @@ class Error(OWWidget.Error):
buttons_area_orientation = None

def __init__(self):
super().__init__()
OWWidget.__init__(self)
ConcurrentWidgetMixin.__init__(self)
self.data = None # type: Optional[Table]
self.template_data = None # type: Optional[Table]
self.transformed_info = describe_data(None) # type: OrderedDict
Expand Down Expand Up @@ -62,16 +79,8 @@ def handleNewSignals(self):

def apply(self):
self.clear_messages()
transformed_data = None
if self.data and self.template_data:
try:
transformed_data = self.data.transform(self.template_data.domain)
except Exception as ex: # pylint: disable=broad-except
self.Error.error(ex)

data = transformed_data
self.transformed_info = describe_data(data)
self.Outputs.transformed_data.send(data)
self.cancel()
self.start(TransformRunner.run, self.data, self.template_data)

def send_report(self):
if self.data:
Expand All @@ -81,6 +90,21 @@ def send_report(self):
if self.transformed_info:
self.report_items("Transformed data", self.transformed_info)

def on_partial_result(self, _):
pass

def on_done(self, result: Optional[Table]):
self.transformed_info = describe_data(result)
self.Outputs.transformed_data.send(result)

def on_exception(self, ex):
self.Error.error(ex)
self.Outputs.transformed_data.send(None)

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


if __name__ == "__main__": # pragma: no cover
from Orange.preprocess import Discretize
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/tests/test_owtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_error_transforming(self):
data.transform = Mock(side_effect=Exception())
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.template_data, self.disc_data)
self.assertTrue(self.widget.Error.error.is_shown())
output = self.get_output(self.widget.Outputs.transformed_data)
self.assertTrue(self.widget.Error.error.is_shown())
self.assertIsNone(output)
self.send_signal(self.widget.Inputs.data, None)
self.assertFalse(self.widget.Error.error.is_shown())
Expand Down

0 comments on commit 0597b34

Please sign in to comment.