Skip to content

Commit

Permalink
Merge pull request #5986 from PrimozGodec/neighbours-suppport-corpus
Browse files Browse the repository at this point in the history
[FIX] Neighbours - support Table derived output types
  • Loading branch information
noahnovsak authored May 27, 2022
2 parents fd15025 + 819c859 commit 6aaad1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 10 additions & 11 deletions Orange/widgets/data/owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,16 @@ def _compute_indices(self):
return np.argpartition(dist, up_to - 1)[:up_to]

def _data_with_similarity(self, indices):
data = self.data
varname = get_unique_names(data.domain, "distance")
metas = data.domain.metas + (ContinuousVariable(varname), )
domain = Domain(data.domain.attributes, data.domain.class_vars, metas)
data_metas = self.distances[indices].reshape((-1, 1))
if data.domain.metas:
data_metas = np.hstack((data.metas[indices], data_metas))
neighbors = Table(domain, data.X[indices], data.Y[indices], data_metas)
neighbors.ids = data.ids[indices]
neighbors.attributes = self.data.attributes
return neighbors
domain = self.data.domain
dist_var = ContinuousVariable(get_unique_names(domain, "distance"))
metas = domain.metas + (dist_var, )
domain = Domain(domain.attributes, domain.class_vars, metas)
neighbours = self.data.from_table(domain, self.data, row_indices=indices)
distances = self.distances[indices]
with neighbours.unlocked(neighbours.metas):
if distances.size > 0:
neighbours.get_column_view(dist_var)[0][:] = distances
return neighbours


if __name__ == "__main__": # pragma: no cover
Expand Down
10 changes: 10 additions & 0 deletions Orange/widgets/data/tests/test_owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ def test_n_neighbours_spin_max(self):
self.send_signal(w.Inputs.data, None)
self.assertEqual(sb.maximum(), default)

def test_inherited_table(self):
# pylint: disable=abstract-method
class Table2(Table):
pass

data = Table2(self.iris)
self.send_signal(self.widget.Inputs.data, data)
self.send_signal(self.widget.Inputs.reference, data[0:1])
self.assertIsInstance(self.get_output(self.widget.Outputs.data), Table2)


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

0 comments on commit 6aaad1c

Please sign in to comment.