Skip to content

Commit

Permalink
Merge pull request #3547 from PrimozGodec/fix-neighbour-chrashes
Browse files Browse the repository at this point in the history
[FIX] Neighbours: Show error when data and reference have different domain.
  • Loading branch information
janezd authored Jan 24, 2019
2 parents 42ea260 + 8603572 commit 89d3cc9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Orange/widgets/data/owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Warning(OWWidget.Warning):
all_data_as_reference = \
Msg("Every data instance is same as some reference")

class Error(OWWidget.Error):
diff_domains = Msg("Data and reference have different domains.")

n_neighbors = Setting(10)
distance_index = Setting(0)
exclude_reference = Setting(True)
Expand Down Expand Up @@ -107,10 +110,16 @@ def recompute(self):
self.apply()

def compute_distances(self):
self.Error.diff_domains.clear()
if self.data is None or len(self.data) == 0 \
or self.reference is None or len(self.reference) == 0:
self.distances = None
return
if self.reference.domain != self.data.domain:
self.Error.diff_domains()
self.distances = None
return

distance = METRICS[self.distance_index][1]
n_ref = len(self.reference)
all_data = Table.concatenate([self.reference, self.data], 0)
Expand Down
55 changes: 54 additions & 1 deletion Orange/widgets/data/tests/test_owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from Orange.data import Table, Domain
from Orange.data import Table, Domain, ContinuousVariable
from Orange.widgets.data.owneighbors import OWNeighbors, METRICS
from Orange.widgets.tests.base import WidgetTest, ParameterMapping

Expand Down Expand Up @@ -343,6 +343,59 @@ def test_all_equal_ref(self):
self.assertFalse(widget.Warning.all_data_as_reference.is_shown())
self.assertIsNotNone(self.get_output(widget.Outputs.data))

def test_different_domains(self):
"""
Test weather widget show error when data and a reference have different
domains.
"""
w = self.widget

domain = Domain([ContinuousVariable("a")])
domain_ref = Domain([ContinuousVariable("b")])
data = Table(domain, np.random.rand(2, len(domain)))
reference = Table(
domain_ref, np.random.rand(1, len(domain_ref)))

# no error if one or both of the signals is missing
self.send_signal(w.Inputs.data, data)
self.assertFalse(w.Error.diff_domains.is_shown())

self.send_signal(w.Inputs.data, None)
self.assertFalse(w.Error.diff_domains.is_shown())

self.send_signal(w.Inputs.reference, data[:1])
self.assertFalse(w.Error.diff_domains.is_shown())

# same domain - no error
self.send_signal(w.Inputs.data, data)
self.send_signal(w.Inputs.reference, data[:1])
self.assertFalse(w.Error.diff_domains.is_shown())

# one attribute different attribute name
self.send_signal(w.Inputs.data, data)
self.send_signal(w.Inputs.reference, reference)
self.assertTrue(w.Error.diff_domains.is_shown())

# different number of attributes
domain_ref = Domain([ContinuousVariable("a"), ContinuousVariable("b")])
reference = Table(domain_ref, np.random.rand(1, len(domain_ref)))

# error disappears when data is set to None
self.send_signal(w.Inputs.data, data)
self.send_signal(w.Inputs.reference, reference)
self.assertTrue(w.Error.diff_domains.is_shown())

self.send_signal(w.Inputs.data, None)
self.assertFalse(w.Error.diff_domains.is_shown())

# error disappears when reference is set to None
self.send_signal(w.Inputs.data, data)
self.send_signal(w.Inputs.reference, reference)
self.assertTrue(w.Error.diff_domains.is_shown())

self.send_signal(w.Inputs.reference, None)
self.assertFalse(w.Error.diff_domains.is_shown())


if __name__ == "__main__":
unittest.main()

0 comments on commit 89d3cc9

Please sign in to comment.