Skip to content

Commit 911f556

Browse files
committed
itemmodels.AbstractSortTableModel: Fix mapFromSourceRows
1 parent 9225e8d commit 911f556

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Orange/widgets/utils/itemmodels.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def mapToSourceRows(self, rows):
118118
119119
Parameters
120120
----------
121-
rows : int or list of int or numpy.ndarray or Ellipsis
121+
rows : int or list of int or numpy.ndarray of dtype=int or Ellipsis
122122
View (sorted) rows.
123123
124124
Returns
@@ -127,7 +127,10 @@ def mapToSourceRows(self, rows):
127127
Source rows matching input rows. If they are the same,
128128
simply input `rows` is returned.
129129
"""
130-
if self.__sortInd is not None:
130+
# self.__sortInd[rows] fails if `rows` is an empty list or array
131+
if self.__sortInd is not None \
132+
and (isinstance(rows, (int, type(Ellipsis)))
133+
or len(rows)):
131134
new_rows = self.__sortInd[rows]
132135
if rows is Ellipsis:
133136
new_rows.setflags(write=False)
@@ -139,7 +142,7 @@ def mapFromSourceRows(self, rows):
139142
140143
Parameters
141144
----------
142-
rows : int or list of int or numpy.ndarray or Ellipsis
145+
rows : int or list of int or numpy.ndarray of dtype=int or Ellipsis
143146
Source model rows.
144147
145148
Returns
@@ -148,7 +151,10 @@ def mapFromSourceRows(self, rows):
148151
ModelIndex (sorted) rows matching input source rows.
149152
If they are the same, simply input `rows` is returned.
150153
"""
151-
if self.__sortIndInv is not None:
154+
# self.__sortInd[rows] fails if `rows` is an empty list or array
155+
if self.__sortIndInv is not None \
156+
and (isinstance(rows, (int, type(Ellipsis)))
157+
or len(rows)):
152158
new_rows = self.__sortIndInv[rows]
153159
if rows is Ellipsis:
154160
new_rows.setflags(write=False)

Orange/widgets/utils/tests/test_itemmodels.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from unittest import TestCase
55

6+
import numpy as np
7+
68
from AnyQt.QtCore import Qt
79

810
from Orange.data import Domain, ContinuousVariable, DiscreteVariable
@@ -133,6 +135,30 @@ def test_sorting(self):
133135
self.model.sort(1, Qt.DescendingOrder)
134136
self.assertSequenceEqual(self.model.mapToSourceRows(...).tolist(), [0, 1])
135137

138+
def test_mapToSourceRows(self):
139+
model = PyTableModel([[1, 4],
140+
[2, 2],
141+
[3, 3]])
142+
model.sort(1, Qt.AscendingOrder)
143+
self.assertSequenceEqual(model.mapToSourceRows(...).tolist(), [1, 2, 0])
144+
self.assertEqual(model.mapToSourceRows(1).tolist(), 2)
145+
self.assertSequenceEqual(model.mapToSourceRows([1, 2]).tolist(), [2, 0])
146+
self.assertSequenceEqual(model.mapToSourceRows([]), [])
147+
self.assertSequenceEqual(
148+
model.mapToSourceRows(np.array([], dtype=int)).tolist(), [])
149+
150+
def test_mapFromSourceRows(self):
151+
model = PyTableModel([[1, 4],
152+
[2, 2],
153+
[3, 3]])
154+
model.sort(1, Qt.AscendingOrder)
155+
self.assertSequenceEqual(model.mapFromSourceRows(...).tolist(), [2, 0, 1])
156+
self.assertEqual(model.mapFromSourceRows(1).tolist(), 0)
157+
self.assertSequenceEqual(model.mapFromSourceRows([1, 2]).tolist(), [0, 1])
158+
self.assertSequenceEqual(model.mapFromSourceRows([]), [])
159+
self.assertSequenceEqual(
160+
model.mapFromSourceRows(np.array([], dtype=int)).tolist(), [])
161+
136162

137163
class TestPyListModel(TestCase):
138164
@classmethod

0 commit comments

Comments
 (0)