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

Revision 10502:6b593a8cd5a0, 3.8 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>Label Powerset</name>
3<description>LabelPowerset 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 OWLP(OWWidget):
16    settingsList = ["name"]
17
18    def __init__(self, parent=None, signalManager = None, name='Label Powerset'):
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),("LabelPowerset Classifier", Orange.multilabel.LabelPowersetClassifier)]
28
29        # Settings
30        self.name = 'Label Powerset'
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.LabelPowersetLearner(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                #for i in range(10):
94                #    c,p = self.classifier(self.data[i],Orange.classification.Classifier.GetBoth)
95                #    print c,p
96            except Exception, (errValue):
97                self.classifier = None
98                self.error(str(errValue))
99        self.send("LabelPowerset Classifier", self.classifier)
100
101##############################################################################
102# Test the widget.
103# Make sure that a sample data set (emotions.tab) is in the directory.
104
105if __name__=="__main__":
106    a=QApplication(sys.argv)
107    ow=OWLP()
108
109    dataset = Orange.data.Table('emotions.tab')
110    ow.set_data(dataset)
111
112    ow.show()
113    a.exec_()
114    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.