Skip to content

Commit

Permalink
OWNeighbors: data info displayed in the status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
aturanjanin committed Apr 2, 2020
1 parent bd2f7ec commit 6a959e3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
26 changes: 16 additions & 10 deletions Orange/widgets/data/owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from Orange.widgets.utils.signals import Input, Output
from Orange.widgets.widget import OWWidget, Msg
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.utils.state_summary import format_summary_details,\
format_multiple_summaries

METRICS = [
("Euclidean", distance.Euclidean),
Expand Down Expand Up @@ -88,14 +90,17 @@ def __init__(self):
def _set_input_summary(self):
n_data = len(self.data) if self.data else 0
n_refs = len(self.reference) if self.reference else 0

if n_data or n_refs:
details = \
f"{n_data if n_data else 'No'} data instance(s) on input\n" \
f"{n_refs if n_refs else 'No'} reference instance(s) on input "
self.info.set_input_summary(f"{n_data} | {n_refs} ", details)
else:
self.info.set_input_summary(self.info.NoInput)
summary, details, kwargs = self.info.NoInput, "", {}

if self.data or self.reference:
summary = f"{self.info.format_number(n_data)}, " \
f"{self.info.format_number(n_refs)}"
details = format_multiple_summaries([
("Data", self.data),
("Reference", self.reference)
])
kwargs = {"format": Qt.RichText}
self.info.set_input_summary(summary, details, **kwargs)

@Inputs.data
def set_data(self, data):
Expand All @@ -107,8 +112,8 @@ def set_ref(self, refs):

def handleNewSignals(self):
self.compute_distances()
self.unconditional_apply()
self._set_input_summary()
self.unconditional_apply()

def recompute(self):
self.compute_distances()
Expand Down Expand Up @@ -146,7 +151,8 @@ def apply(self):
self.info.set_output_summary(self.info.NoOutput)
else:
neighbors = self._data_with_similarity(indices)
self.info.set_output_summary(str(len(neighbors)))
summary, details = len(neighbors), format_summary_details(neighbors)
self.info.set_output_summary(summary, details)
self.Outputs.data.send(neighbors)

def _compute_indices(self):
Expand Down
69 changes: 38 additions & 31 deletions Orange/widgets/data/tests/test_owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from Orange.data import Table, Domain, ContinuousVariable
from Orange.widgets.data.owneighbors import OWNeighbors, METRICS
from Orange.widgets.tests.base import WidgetTest, ParameterMapping
from Orange.widgets.utils.state_summary import format_summary_details, \
format_multiple_summaries


class TestOWNeighbors(WidgetTest):
Expand Down Expand Up @@ -122,36 +124,43 @@ def test_missing_values(self):
widget.apply_button.button.click()
self.assertIsNotNone(self.get_output("Neighbors"))

def test_input_summary(self):
def test_summary(self):
"""Check if status bar is updated when data is received"""
widget = self.widget
input_sum = widget.info.set_input_summary = Mock()
data = Table("iris")

input_sum.reset_mock()
self.send_signal(widget.Inputs.reference, data[:5])
input_sum.assert_called_with("0 | 5 ", "No data instance(s) on input\n"\
"5 reference instance(s) on input ")

input_sum.reset_mock()
self.send_signal(widget.Inputs.data, data[:10])
input_sum.assert_called_with("10 | 5 ", "10 data instance(s) on input\n"\
"5 reference instance(s) on input ")

input_sum.reset_mock()
self.send_signals([(widget.Inputs.data, None), (widget.Inputs.reference, None)])
input_sum.assert_called_once()
self.assertEqual(input_sum.call_args[0][0].brief, "")

input_sum.reset_mock()
self.send_signal(widget.Inputs.data, data[:15])
input_sum.assert_called_with("15 | 0 ", "15 data instance(s) on input\n" \
"No reference instance(s) on input ")

input_sum.reset_mock()
self.send_signals([(widget.Inputs.data, data[:12]), (widget.Inputs.reference, data[-1:])])
input_sum.assert_called_with("12 | 1 ", "12 data instance(s) on input\n"\
"1 reference instance(s) on input ")
info = self.widget.info
data, reference = Table("iris"), Table("iris")[:5]
no_input, no_output = "No data on input", "No data on output"

self.send_signal(self.widget.Inputs.data, data)
data_list = [("Data", data), ("Reference", None)]
summary, details = "150, 0", format_multiple_summaries(data_list)
self.assertEqual(info._StateInfo__input_summary.brief, summary)
self.assertEqual(info._StateInfo__input_summary.details, details)
self.assertEqual(info._StateInfo__output_summary.brief, "")
self.assertEqual(info._StateInfo__output_summary.details, no_output)

self.send_signal(self.widget.Inputs.reference, reference)
data_list = [("Data", data), ("Reference", reference)]
summary, details = "150, 5", format_multiple_summaries(data_list)
self.assertEqual(info._StateInfo__input_summary.brief, summary)
self.assertEqual(info._StateInfo__input_summary.details, details)
output = self.get_output(self.widget.Outputs.data)
summary, details = f"{len(output)}", format_summary_details(output)
self.assertEqual(info._StateInfo__output_summary.brief, summary)
self.assertEqual(info._StateInfo__output_summary.details, details)

self.send_signal(self.widget.Inputs.data, None)
data_list = [("Data", None), ("Reference", reference)]
summary, details = "0, 5", format_multiple_summaries(data_list)
self.assertEqual(info._StateInfo__input_summary.brief, summary)
self.assertEqual(info._StateInfo__input_summary.details, details)
self.assertEqual(info._StateInfo__output_summary.brief, "")
self.assertEqual(info._StateInfo__output_summary.details, no_output)

self.send_signal(self.widget.Inputs.reference, None)
self.assertEqual(info._StateInfo__input_summary.brief, "")
self.assertEqual(info._StateInfo__input_summary.details, no_input)
self.assertEqual(info._StateInfo__output_summary.brief, "")
self.assertEqual(info._StateInfo__output_summary.details, no_output)

def test_compute_distances_apply_called(self):
"""Check compute distances and apply are called when receiving signal"""
Expand Down Expand Up @@ -329,7 +338,6 @@ def test_data_with_similarity(self):

def test_apply(self):
widget = self.widget
output_sum = widget.info.set_output_summary = Mock()
widget.auto_apply = True
data = Table("iris")
indices = np.array([5, 10, 15, 100])
Expand All @@ -342,7 +350,6 @@ def test_apply(self):
np.testing.assert_almost_equal(neigh.X, data.X[indices])
np.testing.assert_almost_equal(
neigh.metas.flatten(), widget.distances[indices])
output_sum.assert_called_with(str(len(neigh)))

def test_all_equal_ref(self):
widget = self.widget
Expand Down

0 comments on commit 6a959e3

Please sign in to comment.