Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] owtable: output sorted data #4644

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions Orange/widgets/data/owtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ def _setup_table_view(self, view, data):
header = view.horizontalHeader()
header.setSectionsClickable(is_sortable(data))
header.setSortIndicatorShown(is_sortable(data))
header.sortIndicatorChanged.connect(self.update_selection)

view.setModel(datamodel)

Expand Down Expand Up @@ -856,7 +857,6 @@ def get_selection(view):
rows = numpy.array(rows, dtype=numpy.intp)
# map the rows through the applied sorting (if any)
rows = model.mapToSourceRows(rows)
rows.sort()
rows = rows.tolist()
return rows, cols

Expand Down Expand Up @@ -887,21 +887,6 @@ def commit(self):
rowsel, colsel = self.get_selection(view)
self.selected_rows, self.selected_cols = rowsel, colsel

def select(data, rows, domain):
"""
Select the data subset with specified rows and domain subsets.

If either rows or domain is None they mean select all.
"""
if rows is not None and domain is not None:
return data.from_table(domain, data, rows)
elif rows is not None:
return data.from_table(data.domain, rows)
elif domain is not None:
return data.from_table(domain, data)
else:
return data

domain = table.domain

if len(colsel) < len(domain) + len(domain.metas):
Expand All @@ -924,13 +909,11 @@ def select_vars(role):
metas = select_vars(TableModel.Meta)
domain = Orange.data.Domain(attrs, class_vars, metas)

# Avoid a copy if all/none rows are selected.
# Avoid a copy if none rows are selected.
if not rowsel:
selected_data = None
elif len(rowsel) == len(table):
selected_data = select(table, None, domain)
else:
selected_data = select(table, rowsel, domain)
selected_data = table.from_table(domain, table, rowsel)

self.Outputs.selected_data.send(selected_data)
self.Outputs.annotated_data.send(create_annotated_table(table, rowsel))
Expand Down Expand Up @@ -1097,21 +1080,6 @@ def is_sortable(table):
return False


def test_model():
app = QApplication([])
view = QTableView(
sortingEnabled=True
)
data = Orange.data.Table("lenses")
model = TableModel(data)

view.setModel(model)

view.show()
view.raise_()
return app.exec()


if __name__ == "__main__": # pragma: no cover
WidgetPreview(OWDataTable).run(
[(Table("iris"), "iris"),
Expand Down
26 changes: 26 additions & 0 deletions Orange/widgets/data/tests/test_owtable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import unittest


from unittest.mock import Mock, patch
from AnyQt.QtCore import Qt

from Orange.widgets.data.owtable import OWDataTable
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
Expand Down Expand Up @@ -75,6 +78,29 @@ def test_pending_selection(self):
output = self.get_output(widget.Outputs.selected_data)
self.assertEqual(5, len(output))

def test_sorting(self):
self.send_signal(self.widget.Inputs.data, self.data)
self.widget.selected_rows = [0, 1, 2, 3, 4]
self.widget.selected_cols = list(range(len(self.data.domain)))
self.widget.set_selection()

output = self.get_output(self.widget.Outputs.selected_data)
output, _ = output.get_column_view(0)
output_original = output.tolist()

self.widget.tabs.currentWidget().sortByColumn(1, Qt.AscendingOrder)

output = self.get_output(self.widget.Outputs.selected_data)
output, _ = output.get_column_view(0)
output_sorted = output.tolist()

# the two outputs should not be the same.
self.assertTrue(output_original != output_sorted)

# check if output after sorting is actually sorted.
self.assertTrue(sorted(output_original) == output_sorted)
self.assertTrue(sorted(output_sorted) == output_sorted)


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