Skip to content

Commit

Permalink
Lift Curve: Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Nov 6, 2020
1 parent 6709cee commit d0ef3cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
50 changes: 21 additions & 29 deletions Orange/widgets/evaluate/owliftcurve.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
"""
Lift Curve Widget
-----------------
"""
from enum import IntEnum
from typing import NamedTuple, Dict, Tuple

Expand All @@ -29,7 +24,7 @@


CurveData = NamedTuple(
"CurvePoints",
"CurveData",
[("contacted", np.ndarray), # classified as positive
("respondents", np.ndarray), # true positive rate
("thresholds", np.ndarray)]
Expand All @@ -42,7 +37,6 @@
[("points", CurveData),
("hull", CurveData)]
)
PointsAndHull.is_valid = property(lambda self: self.points.is_valid)


class CurveTypes(IntEnum):
Expand All @@ -55,16 +49,19 @@ class OWLiftCurve(widget.OWWidget):
"from the evaluation of classifiers."
icon = "icons/LiftCurve.svg"
priority = 1020
keywords = []
keywords = ["lift", "cumulative gain"]

class Inputs:
evaluation_results = Input("Evaluation Results", Orange.evaluation.Results)
evaluation_results = Input(
"Evaluation Results", Orange.evaluation.Results)

class Warning(widget.OWWidget.Warning):
undefined_curves = Msg("Some curves are undefined; check models and data")
undefined_curves = Msg(
"Some curves are undefined; check models and data")

class Error(widget.OWWidget.Error):
undefined_curves = Msg("No defined curves; check models and data")
undefined_curves = Msg(
"No defined curves; check models and data")

settingsHandler = EvaluationResultsContextHandler()
target_index = settings.ContextSetting(0)
Expand Down Expand Up @@ -136,7 +133,6 @@ def __init__(self):

@Inputs.evaluation_results
def set_results(self, results):
"""Set the input evaluation results."""
self.closeContext()
self.clear()
self.results = check_results_adequacy(results, self.Error)
Expand All @@ -147,7 +143,6 @@ def set_results(self, results):
self._setup_plot()

def clear(self):
"""Clear the widget state."""
self.plot.clear()
self.Warning.clear()
self.Error.clear()
Expand All @@ -158,22 +153,21 @@ def clear(self):
self._points_hull = {}

def _initialize(self, results):
N = len(results.predicted)

names = getattr(results, "learner_names", None)
if names is None:
names = ["#{}".format(i + 1) for i in range(N)]
n_models = len(results.predicted)

self.colors = colorpalettes.get_default_curve_colors(N)
self.classifier_names = getattr(results, "learner_names", None) \
or [f"#{i}" for i in range(n_models)]
self.selected_classifiers = list(range(n_models))

self.classifier_names = names
self.selected_classifiers = list(range(N))
for i in range(N):
self.colors = colorpalettes.get_default_curve_colors(n_models)
for i, color in enumerate(self.colors):
item = self.classifiers_list_box.item(i)
item.setIcon(colorpalettes.ColorIcon(self.colors[i]))
item.setIcon(colorpalettes.ColorIcon(color))

self.target_cb.addItems(results.data.domain.class_var.values)
self.target_index = 0
class_values = results.data.domain.class_var.values
self.target_cb.addItems(class_values)
if class_values:
self.target_index = 0

def _replot(self):
self.plot.clear()
Expand Down Expand Up @@ -229,10 +223,8 @@ def _plot(points, pen):
def _plot_default_line(self):
pen = QPen(QColor(20, 20, 20), 1, Qt.DashLine)
pen.setCosmetic(True)
if self.curve_type == CurveTypes.LiftCurve:
self.plot.plot([0, 1], [1, 1], pen=pen, antialias=True)
else:
self.plot.plot([0, 1], [0, 1], pen=pen, antialias=True)
y0 = 1 if self.curve_type == CurveTypes.LiftCurve else 0
self.plot.plot([0, 1], [y0, 1], pen=pen, antialias=True)

def _set_undefined_curves_err_warn(self, is_valid):
self.Error.undefined_curves.clear()
Expand Down
5 changes: 4 additions & 1 deletion doc/widgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@
"doc": "visual-programming/source/widgets/evaluate/liftcurve.md",
"icon": "../Orange/widgets/evaluate/icons/LiftCurve.svg",
"background": "#C3F3F3",
"keywords": []
"keywords": [
"lift",
"cumulative gain"
]
},
{
"text": "Calibration Plot",
Expand Down

0 comments on commit d0ef3cd

Please sign in to comment.