Skip to content

Commit 19eec22

Browse files
OWFeatureStatistics: Fix crash on discrete variables with no values
1 parent 3ae100d commit 19eec22

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Orange/widgets/data/owfeaturestatistics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def data(self, index, role):
500500
if role == Qt.DisplayRole:
501501
if isinstance(attribute, DiscreteVariable):
502502
output = self._center[row]
503-
if not np.isnan(output):
503+
if not np.isnan(output) and len(attribute.values) > 0:
504504
output = attribute.str_val(self._center[row])
505505
elif isinstance(attribute, TimeVariable):
506506
output = attribute.str_val(self._center[row])
@@ -515,7 +515,7 @@ def data(self, index, role):
515515
elif column == self.Columns.MIN:
516516
if role == Qt.DisplayRole:
517517
if isinstance(attribute, DiscreteVariable):
518-
if attribute.ordered:
518+
if attribute.ordered and len(attribute.values) > 0:
519519
output = attribute.str_val(self._min[row])
520520
elif isinstance(attribute, TimeVariable):
521521
output = attribute.str_val(self._min[row])
@@ -524,7 +524,7 @@ def data(self, index, role):
524524
elif column == self.Columns.MAX:
525525
if role == Qt.DisplayRole:
526526
if isinstance(attribute, DiscreteVariable):
527-
if attribute.ordered:
527+
if attribute.ordered and len(attribute.values) > 0:
528528
output = attribute.str_val(self._max[row])
529529
elif isinstance(attribute, TimeVariable):
530530
output = attribute.str_val(self._max[row])

Orange/widgets/data/tests/test_owfeaturestatistics.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
continuous_same
4141
]
4242

43+
# Discrete variable with no values
44+
empty_discrete = VarDataPair(
45+
DiscreteVariable("empty discrete", values=[]),
46+
np.array([np.nan] * 5, dtype=float),
47+
)
48+
4349
# Unordered discrete variable variations
4450
rgb_full = VarDataPair(
4551
DiscreteVariable('rgb_full', values=['r', 'g', 'b']),
@@ -251,6 +257,13 @@ def test_on_data_with_continuous_values_all_the_same(self, prepare_table):
251257
self.send_signal('Data', prepare_table(data))
252258
self.run_through_variables()
253259

260+
@table_dense_sparse
261+
def test_on_data_with_discrete_with_no_values(self, prepare_table):
262+
data = make_table([empty_discrete, ints_same])
263+
self.send_signal(self.widget.Inputs.data, prepare_table(data))
264+
self.force_render_table()
265+
self.run_through_variables()
266+
254267

255268
def select_rows(rows: List[int], widget: OWFeatureStatistics):
256269
"""Since the widget sorts the rows, selecting rows isn't trivial."""

0 commit comments

Comments
 (0)