source: orange/Orange/OrangeWidgets/Prototypes/OWBR.py @ 10502:6b593a8cd5a0

Revision 10502:6b593a8cd5a0, 3.7 KB checked in by Matija Polajnar <matija.polajnar@…>, 2 years ago (diff)

Make multi-label warning and errors more clear on what a 'multi-label dataset' is from our perspective; Miha warned me students had troubles with this.

Line 
1"""
2<name>Binary Relevance</name>
3<description>Binary Relevance Multi-label Learner</description>
4<icon>icons/Unknown.png</icon>
5<contact>Wencan Luo (wencanluo.cn(@at@)gmail.com)</contact>
6<priority>100</priority>
7"""
8from OWWidget import *
9import OWGUI
10from exceptions import Exception
11from orngWrap import PreprocessedLearner
12
13import Orange
14
15class OWBR(OWWidget):
16    settingsList = ["name"]
17
18    def __init__(self, parent=None, signalManager = None, name='Binary Relevance'):
19        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
20
21        self.callbackDeposit = []
22
23        self.inputs = [("Examples", ExampleTable, self.set_data), 
24                       ("Preprocess", PreprocessedLearner, self.set_preprocessor),
25                       ("Binary Classification", Orange.classification.Learner, self.set_base_learner)
26                       ]
27        self.outputs = [("Learner", orange.Learner),("Binary Relevance Classifier", Orange.multilabel.BinaryRelevanceClassifier)]
28
29        # Settings
30        self.name = 'Binary Relevance'
31        self.base_learner = Orange.core.BayesLearner;
32       
33        self.loadSettings()
34
35        self.data = None                    # input data set
36        self.preprocessor = None            # no preprocessing as default
37        self.set_learner()                   # this just sets the learner, no data
38                                            # has come to the input yet
39
40        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \
41                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
42
43        OWGUI.separator(self.controlArea)
44
45        OWGUI.button(self.controlArea, self, "&Apply", callback=self.set_learner, disabled=0, default=True)
46       
47        OWGUI.rubber(self.controlArea)
48
49        self.resize(100,250)
50
51    def send_report(self):
52        self.reportSettings("Learning parameters",
53                            [("base_learner", self.baselearnerList[self.base_learner][0])])
54        self.reportData(self.data)
55           
56    def set_data(self,data):
57        if data == None:
58            return
59
60        if not Orange.multilabel.is_multilabel(data):
61            self.warning(0, "Multi-label data with class values 0 and 1 is "
62                            "expected on the input.")
63            return
64        self.warning(0, None)
65
66        self.data = data
67        self.set_learner()
68
69    def set_preprocessor(self, pp):
70        self.preprocessor = pp
71        self.set_learner()
72       
73    def set_base_learner(self,base_learner):
74        self.base_learner = base_learner
75        self.set_learner()
76   
77    def set_learner(self):
78        self.learner = Orange.multilabel.BinaryRelevanceLearner(base_learner = self.base_learner)
79        if self.preprocessor:
80            self.learner = self.preprocessor.wrapLearner(self.learner)
81        self.learner.name = self.name
82
83        self.send("Learner", self.learner)
84
85        self.learn()
86
87    def learn(self):
88        self.classifier = None
89        if self.data and self.learner:
90            try:
91                self.classifier = self.learner(self.data)
92                self.classifier.name = self.name
93            except Exception, (errValue):
94                self.classifier = None
95                self.error(str(errValue))
96        self.send("Binary Relevance Classifier", self.classifier)
97
98##############################################################################
99# Test the widget.
100# Make sure that a sample data set (emotions.tab) is in the directory.
101
102if __name__=="__main__":
103    a=QApplication(sys.argv)
104    ow=OWBR()
105
106    dataset = Orange.data.Table('emotions.tab')
107    ow.set_data(dataset)
108
109    ow.show()
110    a.exec_()
111    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.