Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix AdaBoost widgets and add some tests #1474

Merged
merged 7 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions Orange/widgets/classify/owadaboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,43 @@ class OWAdaBoostClassification(OWBaseLearner):
learning_rate = Setting(1.)
algorithm = Setting(0)

DEFAULT_BASE_ESTIMATOR = TreeLearner()

def add_main_layout(self):
box = gui.widgetBox(self.controlArea, "Parameters")
self.base_estimator = TreeLearner()
self.base_label = gui.label(box, self, "Base estimator: " + self.base_estimator.name)

gui.spin(box, self, "n_estimators", 1, 100, label="Number of estimators:",
alignment=Qt.AlignRight, callback=self.settings_changed)
gui.doubleSpin(box, self, "learning_rate", 1e-5, 1.0, 1e-5,
label="Learning rate:", decimals=5, alignment=Qt.AlignRight,
controlWidth=90, callback=self.settings_changed)
self.base_estimator = self.DEFAULT_BASE_ESTIMATOR
self.base_label = gui.label(
box, self, "Base estimator: " + self.base_estimator.name)

self.n_estimators_spin = gui.spin(
box, self, "n_estimators", 1, 100, label="Number of estimators:",
alignment=Qt.AlignRight, callback=self.settings_changed)
self.learning_rate_spin = gui.doubleSpin(
box, self, "learning_rate", 1e-5, 1.0, 1e-5, label="Learning rate:",
decimals=5, alignment=Qt.AlignRight, controlWidth=90,
callback=self.settings_changed)
self.add_specific_parameters(box)

def add_specific_parameters(self, box):
gui.comboBox(box, self, "algorithm", label="Algorithm:",
orientation=Qt.Horizontal, items=self.losses,
callback=self.settings_changed)
self.algorithm_combo = gui.comboBox(
box, self, "algorithm", label="Algorithm:", items=self.losses,
orientation=Qt.Horizontal, callback=self.settings_changed)

def create_learner(self):
return self.LEARNER(
base_estimator=self.base_estimator,
n_estimators=self.n_estimators,
learning_rate=self.learning_rate,
preprocessors=self.preprocessors,
algorithm=self.losses[self.algorithm]
)

def set_base_learner(self, model):
self.base_estimator = model
if self.base_estimator:
self.base_label.setText("Base estimator: " + self.base_estimator.name)
self.apply_button.setDisabled(False)
else:
self.base_label.setText("No base estimator")
self.apply_button.setDisabled(True)
def set_base_learner(self, learner):
self.base_estimator = learner if learner \
else self.DEFAULT_BASE_ESTIMATOR
self.base_label.setText("Base estimator: " + self.base_estimator.name)
if self.auto_apply:
self.apply()

def get_learner_parameters(self):
return (("Base estimator", self.base_estimator),
Expand All @@ -69,6 +73,7 @@ def get_learner_parameters(self):
if __name__ == "__main__":
import sys
from PyQt4.QtGui import QApplication

a = QApplication(sys.argv)
ow = OWAdaBoostClassification()
ow.set_data(Table("iris"))
Expand Down
87 changes: 46 additions & 41 deletions Orange/widgets/classify/tests/test_owadaboostclassification.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
from PyQt4 import QtGui

from Orange.widgets.tests.base import WidgetTest
from Orange.classification import TreeLearner, KNNLearner
from Orange.widgets.classify.owadaboost import OWAdaBoostClassification
from Orange.widgets.tests.base import (WidgetTest, WidgetLearnerTestMixin,
GuiToParam)



class TestOWAdaBoostClassification(WidgetTest):

class TestOWAdaBoostClassification(WidgetTest, WidgetLearnerTestMixin):
def setUp(self):
self.widget = self.create_widget(OWAdaBoostClassification)
self.spinners = []
self.spinners.append(self.widget.findChildren(QtGui.QSpinBox)[0])
self.spinners.append(self.widget.findChildren(QtGui.QDoubleSpinBox)[0])
self.combobox_algorithm = self.widget.findChildren(QtGui.QComboBox)[0]

def test_visible_boxes(self):
""" Check if boxes are visible """
self.assertEqual(self.spinners[0].isHidden(), False)
self.assertEqual(self.spinners[1].isHidden(), False)
self.assertEqual(self.combobox_algorithm.isHidden(), False)

def test_parameters_on_output(self):
""" Check right paramaters on output """
self.widget.apply()
learner_params = self.widget.learner.params
self.assertEqual(learner_params.get("n_estimators"), self.spinners[0].value())
self.assertEqual(learner_params.get("learning_rate"), self.spinners[1].value())
self.assertEqual(learner_params.get('algorithm'), self.combobox_algorithm.currentText())


def test_output_algorithm(self):
""" Check if right learning algorithm is on output when we change algorithm """
for index, algorithmName in enumerate(self.widget.losses):
self.combobox_algorithm.setCurrentIndex(index)
self.combobox_algorithm.activated.emit(index)
self.assertEqual(self.combobox_algorithm.currentText(), algorithmName)
self.widget.apply()
self.assertEqual(self.widget.learner.params.get("algorithm").capitalize(),
self.combobox_algorithm.currentText().capitalize())

def test_learner_on_output(self):
""" Check if learner is on output after create widget and apply """
self.widget.apply()
self.assertNotEqual(self.widget.learner, None)
self.widget = self.create_widget(OWAdaBoostClassification,
stored_settings={"auto_apply": False})
self.init()

def combo_set_value(i, x):
x.activated.emit(i)
x.setCurrentIndex(i)

losses = self.widget.losses
nest_spin = self.widget.n_estimators_spin
nest_min_max = [nest_spin.minimum(), nest_spin.maximum()]
rate_spin = self.widget.learning_rate_spin
rate_min_max = [rate_spin.minimum(), rate_spin.maximum()]
self.gui_to_params = [
GuiToParam('algorithm', self.widget.algorithm_combo,
lambda x: x.currentText(),
combo_set_value, losses, list(range(len(losses)))),
GuiToParam('learning_rate', rate_spin, lambda x: x.value(),
lambda i, x: x.setValue(i), rate_min_max, rate_min_max),
GuiToParam('n_estimators', nest_spin, lambda x: x.value(),
lambda i, x: x.setValue(i), nest_min_max, nest_min_max)]

def test_input_learner(self):
"""Check if base learner properly changes with learner on the input"""
max_depth = 2
default_base_est = self.widget.base_estimator
self.assertIsInstance(default_base_est, TreeLearner)
self.assertIsNone(default_base_est.params.get("max_depth"))
self.send_signal("Learner", TreeLearner(max_depth=max_depth))
self.assertEqual(self.widget.base_estimator.params.get("max_depth"),
max_depth)
self.widget.apply_button.button.click()
output_base_est = self.get_output("Learner").params.get("base_estimator")
self.assertEqual(output_base_est.max_depth, max_depth)

def test_input_learner_disconnect(self):
"""Check base learner after disconnecting learner on the input"""
self.send_signal("Learner", KNNLearner())
self.assertIsInstance(self.widget.base_estimator, KNNLearner)
self.send_signal("Learner", None)
self.assertEqual(self.widget.base_estimator,
self.widget.DEFAULT_BASE_ESTIMATOR)
11 changes: 8 additions & 3 deletions Orange/widgets/regression/owadaboostregression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from PyQt4.QtCore import Qt

from Orange.regression.base_regression import LearnerRegression
from Orange.regression import TreeRegressionLearner
from Orange.data import Table
from Orange.ensembles import SklAdaBoostRegressionLearner
from Orange.widgets import gui
Expand All @@ -22,15 +23,18 @@ class OWAdaBoostRegression(owadaboost.OWAdaBoostClassification):
losses = ["Linear", "Square", "Exponential"]
loss = Setting(0)

DEFAULT_BASE_ESTIMATOR = TreeRegressionLearner()

def add_specific_parameters(self, box):
gui.comboBox(box, self, "loss", label="Loss:",
orientation=Qt.Horizontal, items=self.losses,
callback=self.settings_changed)
self.loss_combo = gui.comboBox(
box, self, "loss", label="Loss:", orientation=Qt.Horizontal,
items=self.losses, callback=self.settings_changed)

def create_learner(self):
return self.LEARNER(
base_estimator=self.base_estimator,
n_estimators=self.n_estimators,
learning_rate=self.learning_rate,
preprocessors=self.preprocessors,
loss=self.losses[self.loss].lower()
)
Expand All @@ -44,6 +48,7 @@ def get_learner_parameters(self):
if __name__ == "__main__":
import sys
from PyQt4.QtGui import QApplication

a = QApplication(sys.argv)
ow = OWAdaBoostRegression()
ow.set_data(Table("housing"))
Expand Down
52 changes: 52 additions & 0 deletions Orange/widgets/regression/tests/test_owadaboostregression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
from Orange.regression import TreeRegressionLearner, KNNRegressionLearner
from Orange.widgets.regression.owadaboostregression import OWAdaBoostRegression
from Orange.widgets.tests.base import (WidgetTest, WidgetLearnerTestMixin,
GuiToParam)


class TestOWAdaBoostRegression(WidgetTest, WidgetLearnerTestMixin):
def setUp(self):
self.widget = self.create_widget(OWAdaBoostRegression,
stored_settings={"auto_apply": False})
self.init()

def combo_set_value(i, x):
x.activated.emit(i)
x.setCurrentIndex(i)

losses = [loss.lower() for loss in self.widget.losses]
nest_spin = self.widget.n_estimators_spin
nest_min_max = [nest_spin.minimum(), nest_spin.maximum()]
rate_spin = self.widget.learning_rate_spin
rate_min_max = [rate_spin.minimum(), rate_spin.maximum()]
self.gui_to_params = [
GuiToParam('loss', self.widget.loss_combo,
lambda x: x.currentText().lower(),
combo_set_value, losses, list(range(len(losses)))),
GuiToParam('learning_rate', rate_spin, lambda x: x.value(),
lambda i, x: x.setValue(i), rate_min_max, rate_min_max),
GuiToParam('n_estimators', nest_spin, lambda x: x.value(),
lambda i, x: x.setValue(i), nest_min_max, nest_min_max)]

def test_input_learner(self):
"""Check if base learner properly changes with learner on the input"""
max_depth = 2
default_base_est = self.widget.base_estimator
self.assertIsInstance(default_base_est, TreeRegressionLearner)
self.assertIsNone(default_base_est.params.get("max_depth"))
self.send_signal("Learner", TreeRegressionLearner(max_depth=max_depth))
self.assertEqual(self.widget.base_estimator.params.get("max_depth"),
max_depth)
self.widget.apply_button.button.click()
output_base_est = self.get_output("Learner").params.get("base_estimator")
self.assertEqual(output_base_est.max_depth, max_depth)

def test_input_learner_disconnect(self):
"""Check base learner after disconnecting learner on the input"""
self.send_signal("Learner", KNNRegressionLearner())
self.assertIsInstance(self.widget.base_estimator, KNNRegressionLearner)
self.send_signal("Learner", None)
self.assertEqual(self.widget.base_estimator,
self.widget.DEFAULT_BASE_ESTIMATOR)
18 changes: 12 additions & 6 deletions Orange/widgets/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import unittest
from collections import namedtuple
from PyQt4 import QtGui

from PyQt4.QtGui import QApplication
import sip
Expand Down Expand Up @@ -259,18 +258,17 @@ def test_output_model(self):
def test_output_learner_name(self):
"""Check if learner's name properly changes"""
new_name = "Learner Name"
name_line_edit = self.widget.findChildren(QtGui.QLineEdit)[0]
self.widget.apply_button.button.click()
self.assertEqual(self.widget.learner.name, name_line_edit.text())
name_line_edit.setText(new_name)
self.assertEqual(self.widget.learner.name,
self.widget.name_line_edit.text())
self.widget.name_line_edit.setText(new_name)
self.widget.apply_button.button.click()
self.assertEqual(self.get_output("Learner").name, new_name)

def test_output_model_name(self):
"""Check if model's name properly changes"""
new_name = "Model Name"
name_line_edit = self.widget.findChildren(QtGui.QLineEdit)[0]
name_line_edit.setText(new_name)
self.widget.name_line_edit.setText(new_name)
self.send_signal("Data", self.data)
self.widget.apply_button.button.click()
self.assertEqual(self.get_output(self.model_name).name, new_name)
Expand All @@ -294,3 +292,11 @@ def test_parameters(self):
param = self.widget.learner.params.get(element.name)
self.assertEqual(param, element.get(element.gui_el))
self.assertEqual(param, val)
param = self.get_output("Learner").params.get(element.name)
self.assertEqual(param, val)
model = self.get_output(self.model_name)
if model is not None:
self.assertEqual(model.params.get(element.name), val)
else:
self.assertIn(self.widget.DATA_ERROR_ID,
self.widget.widgetState.get("Error"))
8 changes: 4 additions & 4 deletions Orange/widgets/utils/owlearnerwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ def add_main_layout(self):
pass

def add_learner_name_widget(self):
gui.lineEdit(self.controlArea, self, 'learner_name', box='Name',
tooltip='The name will identify this model in other widgets',
orientation=Qt.Horizontal,
callback=lambda: self.apply())
self.name_line_edit = gui.lineEdit(
self.controlArea, self, 'learner_name', box='Name',
tooltip='The name will identify this model in other widgets',
orientation=Qt.Horizontal, callback=lambda: self.apply())

def add_bottom_buttons(self):
box = gui.hBox(self.controlArea, True)
Expand Down