Skip to content

Commit

Permalink
MDS: Catch exceptions in MDS optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Feb 10, 2017
1 parent 3a83658 commit c97ef0e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Orange/widgets/unsupervised/owmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Error(OWWidget.Error):
no_attributes = Msg("Data has no attributes")
mismatching_dimensions = \
Msg("Data and distances dimensions do not match.")
out_of_memory = Msg("Out of memory")
optimization_error = Msg("Error during optimization\n{}")

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -621,6 +623,7 @@ def __next_step(self):
return

loop = self.__update_loop
self.Error.out_of_memory.clear()
try:
embedding, stress, progress = next(self.__update_loop)
assert self.__update_loop is loop
Expand All @@ -630,6 +633,14 @@ def __next_step(self):
self.__draw_similar_pairs = True
self._update_plot()
self.plot.autoRange(padding=0.1, items=[self._scatter_item])
except MemoryError:
self.Error.out_of_memory()
self.__set_update_loop(None)
self.__draw_similar_pairs = True
except Exception as exc:
self.Error.optimization_error(str(exc))
self.__set_update_loop(None)
self.__draw_similar_pairs = True
else:
self.progressBarSet(100.0 * progress, processEvents=None)
self.embedding = embedding
Expand Down
18 changes: 18 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owmds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import random
from unittest.mock import patch, Mock

import numpy as np

Expand Down Expand Up @@ -69,3 +70,20 @@ def test_nan_plot(self):
simulate.combobox_run_through_all(self.widget.cb_shape_value)
simulate.combobox_run_through_all(self.widget.cb_size_value)
simulate.combobox_run_through_all(self.widget.cb_label_value)

@patch("Orange.projection.MDS.__call__", Mock(side_effect=MemoryError))
def test_out_of_memory(self):
with patch("sys.excepthook", Mock()) as hook:
self.send_signal("Data", self.data)
self.process_events()
hook.assert_not_called()
self.assertTrue(self.widget.Error.out_of_memory.is_shown())

@patch("Orange.projection.MDS.__call__", Mock(side_effect=ValueError))
def test_other_error(self):
with patch("sys.excepthook", Mock()) as hook:
self.send_signal("Data", self.data)
self.process_events()
hook.assert_not_called()
self.assertTrue(self.widget.Error.optimization_error.is_shown())

0 comments on commit c97ef0e

Please sign in to comment.