<commit_before>from PyQt4.QtGui import QApplication
import Orange.data
from Orange import classification
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting

class OWClassificationTree(widget.OWWidget):
    name = "Classification Tree"
    icon = "icons/ClassificationTree.svg"
    priority = 30
    inputs = [("Data", Orange.data.Table, "set_data")]
    outputs = [
#        ("Learner", classification.tree.ClassificationTreeLearner),
#        ("Classifier", classification.tree.ClassificationTreeClassifier)
    ]
    want_main_area = False

    model_name = Setting("Classification Tree")
    attribute_score = Setting(0)
    limit_min_leaf = Setting(True)
    min_leaf = Setting(2)
    limit_min_internal = Setting(True)
    min_internal = Setting(5)
    limit_depth = Setting(True)
    max_depth = Setting(100)
    
    scores = (("Entropy", "entropy"), ("Gini Index", "gini"))

    def __init__(self):
        super().__init__()

        self.data = None
        self.learner = None
        self.preprocessor = None
        self.classifier = None

        gui.lineEdit(self.controlArea, self, 'model_name', box='Name',
                     tooltip='The name will identify this model in other '
                             'widgets')

        gui.comboBox(self.controlArea, self, "attribute_score",
                     box='Feature selection',
                     items=[name for name, _ in self.scores])

        box = gui.widgetBox(self.controlArea, 'Pruning')
        gui.spin(box, self, "min_leaf", 1, 1000,
                 label="Min. instances in leaves ", checked="limit_min_leaf")
        gui.spin(box, self, "min_internal", 1, 1000,
                 label="Stop splitting nodes with less instances than ",
                 checked="limit_min_internal")
        gui.spin(box, self, "max_depth", 1, 1000,
                 label="Limit the depth to ", checked="limit_depth")

        self.btn_apply = gui.button(self.controlArea, self, "&Apply",
                                    callback=self.set_learner, disabled=0,
                                    default=True)

        gui.rubber(self.controlArea)
        self.resize(100, 100)

        self.set_learner()

    def sendReport(self):
        self.reportSettings(
            "Model parameters",
            [("Attribute selection", self.scores[self.attribute_score][0]),
             ("Pruning", ", ".join(s for s, c in (
                 ("%i instances in leaves" % self.min_leaf,
                  self.limit_min_leaf),
                 ("%i instance in internal node" % self.min_internal,
                  self.limit_min_internal),
                 ("maximum depth %i" % self.max_depth, self.limit_depth)) if c)
              or ": None")])
        self.reportData(self.data)

    def set_preprocessor(self, preprocessor):
        self.preprocessor = preprocessor
        self.set_learner()

    def set_learner(self):
        self.btn_apply.setFocus()
        self.learner = ...
        self.learner.name = self.name
        if self.preprocessor:
            self.learner = self.preprocessor.wrapLearner(self.learner)
        self.send("Learner", self.learner)

        self.error()
        if self.data is not None:
            try:
                self.classifier = self.learner(self.data)
                self.classifier.name = self.name
            except Exception as errValue:
                self.error(str(errValue))
                self.classifier = None
        else:
            self.classifier = None
        self.send("Classification Tree", self.classifier)

    def set_data(self, data):
        if self.data.class_var is None:
            self.error("Data has no target variable")
            data = None
        self.data = data
        self.set_learner()


if __name__ == "__main__":
    import sys
    a = QApplication(sys.argv)
    ow = OWClassificationTree()

#    d = orange.Table('iris')
#    ow.setData(d)

    ow.show()
    a.exec_()
    ow.saveSettings()
<commit_msg>Tree widgets constructs the learner/classifier and sends it<commit_after>from PyQt4.QtGui import QApplication
import Orange.data
from Orange.classification import tree
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting


class OWClassificationTree(widget.OWWidget):
    name = "Classification Tree"
    icon = "icons/ClassificationTree.svg"
    priority = 30
    inputs = [("Data", Orange.data.Table, "set_data")]
    outputs = [
        ("Learner", tree.ClassificationTreeLearner),
        ("Classifier", tree.ClassificationTreeClassifier)
    ]
    want_main_area = False

    model_name = Setting("Classification Tree")
    attribute_score = Setting(0)
    limit_min_leaf = Setting(True)
    min_leaf = Setting(2)
    limit_min_internal = Setting(True)
    min_internal = Setting(5)
    limit_depth = Setting(True)
    max_depth = Setting(100)
    
    scores = (("Entropy", "entropy"), ("Gini Index", "gini"))

    def __init__(self):
        super().__init__()

        self.data = None
        self.learner = None
        self.preprocessor = None
        self.classifier = None

        gui.lineEdit(self.controlArea, self, 'model_name', box='Name',
                     tooltip='The name will identify this model in other '
                             'widgets')

        gui.comboBox(self.controlArea, self, "attribute_score",
                     box='Feature selection',
                     items=[name for name, _ in self.scores])

        box = gui.widgetBox(self.controlArea, 'Pruning')
        gui.spin(box, self, "min_leaf", 1, 1000,
                 label="Min. instances in leaves ", checked="limit_min_leaf")
        gui.spin(box, self, "min_internal", 1, 1000,
                 label="Stop splitting nodes with less instances than ",
                 checked="limit_min_internal")
        gui.spin(box, self, "max_depth", 1, 1000,
                 label="Limit the depth to ", checked="limit_depth")

        self.btn_apply = gui.button(self.controlArea, self, "&Apply",
                                    callback=self.set_learner, disabled=0,
                                    default=True)

        gui.rubber(self.controlArea)
        self.resize(100, 100)

        self.set_learner()

    def sendReport(self):
        self.reportSettings(
            "Model parameters",
            [("Attribute selection", self.scores[self.attribute_score][0]),
             ("Pruning", ", ".join(s for s, c in (
                 ("%i instances in leaves" % self.min_leaf,
                  self.limit_min_leaf),
                 ("%i instance in internal node" % self.min_internal,
                  self.limit_min_internal),
                 ("maximum depth %i" % self.max_depth, self.limit_depth)) if c)
              or ": None")])
        self.reportData(self.data)

    def set_preprocessor(self, preprocessor):
        self.preprocessor = preprocessor
        self.set_learner()

    def set_learner(self):
        self.btn_apply.setFocus()
        self.learner = tree.ClassificationTreeLearner(
            criterion=self.scores[self.attribute_score][1],
            max_depth=self.max_depth,
            min_samples_split=self.min_internal, min_samples_leaf=self.min_leaf)
        self.learner.name = self.name
        if self.preprocessor:
            self.learner = self.preprocessor.wrapLearner(self.learner)
        self.send("Learner", self.learner)

        self.error()
        if self.data is not None:
            try:
                self.classifier = self.learner(self.data)
                self.classifier.name = self.name
            except Exception as errValue:
                self.error(str(errValue))
                self.classifier = None
        else:
            self.classifier = None
        self.send("Classification Tree", self.classifier)

    def set_data(self, data):
        if self.data.class_var is None:
            self.error("Data has no target variable")
            data = None
        self.data = data
        self.set_learner()


if __name__ == "__main__":
    import sys
    a = QApplication(sys.argv)
    ow = OWClassificationTree()

#    d = orange.Table('iris')
#    ow.setData(d)

    ow.show()
    a.exec_()
    ow.saveSettings()

{'fraction_non_alphanumeric': 0.10834446738734362, 'fraction_numerical': 0.007895056480019433, 'mean_word_length': 2.093163035311796, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3512834', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2288, 'n_words': 520}