Skip to content

Commit 0f53884

Browse files
committed
itemmodels.AbstractSortTableModel: Fix mapFromSourceRows
1 parent 9225e8d commit 0f53884

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-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: 30 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,34 @@ 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+
# self.model is not appropriate here since it is its own inverse, so the
140+
# test wouldn't check whether the method works in the right direction
141+
model = PyTableModel([[1, 4],
142+
[2, 2],
143+
[3, 3]])
144+
model.sort(1, Qt.AscendingOrder)
145+
self.assertSequenceEqual(model.mapToSourceRows(...).tolist(), [1, 2, 0])
146+
self.assertEqual(model.mapToSourceRows(1).tolist(), 2)
147+
self.assertSequenceEqual(model.mapToSourceRows([1, 2]).tolist(), [2, 0])
148+
self.assertSequenceEqual(model.mapToSourceRows([]), [])
149+
self.assertSequenceEqual(
150+
model.mapToSourceRows(np.array([], dtype=int)).tolist(), [])
151+
152+
def test_mapFromSourceRows(self):
153+
# self.model is not appropriate here since it is its own inverse, so the
154+
# test wouldn't check whether the method works in the right direction
155+
model = PyTableModel([[1, 4],
156+
[2, 2],
157+
[3, 3]])
158+
model.sort(1, Qt.AscendingOrder)
159+
self.assertSequenceEqual(model.mapFromSourceRows(...).tolist(), [2, 0, 1])
160+
self.assertEqual(model.mapFromSourceRows(1).tolist(), 0)
161+
self.assertSequenceEqual(model.mapFromSourceRows([1, 2]).tolist(), [0, 1])
162+
self.assertSequenceEqual(model.mapFromSourceRows([]), [])
163+
self.assertSequenceEqual(
164+
model.mapFromSourceRows(np.array([], dtype=int)).tolist(), [])
165+
136166

137167
class TestPyListModel(TestCase):
138168
@classmethod

0 commit comments

Comments
 (0)