Skip to content

Commit

Permalink
Test and Score: More more minor changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Jan 24, 2020
1 parent 66bef42 commit a27cce6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
40 changes: 32 additions & 8 deletions Orange/widgets/evaluate/owtestlearners.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,19 +487,32 @@ def _which_missing_data(self):
# - we don't gain much with it
# - it complicates the unit tests
def _update_scorers(self):
if self.data is None or self.data.domain.class_var is None:
self.scorers = []
self.controls.comparison_criterion.model().clear()
return
self.scorers = usable_scorers(self.data.domain.class_var)
self.controls.comparison_criterion.model()[:] = \
[scorer.long_name or scorer.name for scorer in self.scorers]
if self.data and self.data.domain.class_var:
new_scorers = usable_scorers(self.data.domain.class_var)
else:
new_scorers = []
# Don't unnecessarily reset the model because this would always reset
# comparison_criterion; we alse set it explicitly, though, for clarity
if new_scorers != self.scorers:
self.scorers = new_scorers
self.controls.comparison_criterion.model()[:] = \
[scorer.long_name or scorer.name for scorer in self.scorers]
self.comparison_criterion = 0
if self.__pending_comparison_criterion is not None:
# Check for the unlikely case that some scorers have been removed
# from modules
if self.__pending_comparison_criterion < len(self.scorers):
self.comparison_criterion = self.__pending_comparison_criterion
self.__pending_comparison_criterion = None
self._update_compbox_title()

def _update_compbox_title(self):
criterion = self.comparison_criterion
if criterion < len(self.scorers):
scorer = self.scorers[criterion]()
self.compbox.setTitle(f"Model Comparison by {scorer.name}")
else:
self.compbox.setTitle(f"Model Comparison")

@Inputs.preprocessor
def set_preprocessor(self, preproc):
Expand Down Expand Up @@ -680,7 +693,7 @@ def _set_comparison_headers(self, names):

def _scores_by_folds(self, slots):
scorer = self.scorers[self.comparison_criterion]()
self.compbox.setTitle(f"Model comparison by {scorer.name}")
self._update_compbox_title()
if scorer.is_binary:
if self.class_selection != self.TARGET_AVERAGE:
class_var = self.data.domain.class_var
Expand Down Expand Up @@ -713,6 +726,9 @@ def _fill_table(self, names, scores):
if self.use_rope and self.rope:
p0, rope, p1 = baycomp.two_on_single(
row_scores, col_scores, self.rope)
if np.isnan(p0) or np.isnan(rope) or np.isnan(p1):
self._set_cells_na(table, row, col)
continue
self._set_cell(table, row, col,
f"{p0:.3f}<br/><small>{rope:.3f}</small>",
f"p({row_name} > {col_name}) = {p0:.3f}\n"
Expand All @@ -723,13 +739,21 @@ def _fill_table(self, names, scores):
f"p({col_name} = {row_name}) = {rope:.3f}")
else:
p0, p1 = baycomp.two_on_single(row_scores, col_scores)
if np.isnan(p0) or np.isnan(p1):
self._set_cells_na(table, row, col)
continue
self._set_cell(table, row, col,
f"{p0:.3f}",
f"p({row_name} > {col_name}) = {p0:.3f}")
self._set_cell(table, col, row,
f"{p1:.3f}",
f"p({col_name} > {row_name}) = {p1:.3f}")

@classmethod
def _set_cells_na(cls, table, row, col):
cls._set_cell(table, row, col, "NA", "comparison cannot be computed")
cls._set_cell(table, col, row, "NA", "comparison cannot be computed")

@staticmethod
def _set_cell(table, row, col, label, tooltip):
item = QLabel(label)
Expand Down
19 changes: 19 additions & 0 deletions Orange/widgets/evaluate/tests/test_owtestlearners.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,25 @@ def two_on_single(res1, res2, rope=0):
self.assertIn(f"{probs(row, col, w.rope)[0]:.3f}", text)
self.assertIn(f"{probs(row, col, w.rope)[1]:.3f}", text)

def test_nan_on_comparison(self):
w = self.widget
w.use_rope = True
self._set_three_majorities()
scores = [object(), object(), object()]
slots = w._successful_slots()

def two_on_single(_1, _2, rope=0):
if rope:
return np.nan, np.nan, np.nan
else:
return np.nan, np.nan

with patch("baycomp.two_on_single", new=two_on_single):
for w.rope in (0, 0.1):
w._fill_table(slots, scores)
label = w.comparison_table.cellWidget(1, 0)
self.assertEqual(label.text(), "NA")


class TestHelpers(unittest.TestCase):
def test_results_one_vs_rest(self):
Expand Down

0 comments on commit a27cce6

Please sign in to comment.