source: orange/Orange/OrangeWidgets/Prototypes/OWLP.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 3.8 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

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 is expected on the input.")
62            return
63        self.warning(0, None)
64       
65        self.data = data
66        self.set_learner()
67
68    def set_preprocessor(self, pp):
69        self.preprocessor = pp
70        self.set_learner()
71       
72    def set_base_learner(self,base_learner):
73        self.base_learner = base_learner
74        self.set_learner()
75   
76    def set_learner(self):
77        self.learner = Orange.multilabel.LabelPowersetLearner(base_learner = self.base_learner)
78        if self.preprocessor:
79            self.learner = self.preprocessor.wrapLearner(self.learner)
80        self.learner.name = self.name
81
82        self.send("Learner", self.learner)
83
84        self.learn()
85
86    def learn(self):
87        self.classifier = None
88        if self.data and self.learner:
89            try:
90                self.classifier = self.learner(self.data)
91                self.classifier.name = self.name
92                #for i in range(10):
93                #    c,p = self.classifier(self.data[i],Orange.classification.Classifier.GetBoth)
94                #    print c,p
95            except Exception, (errValue):
96                self.classifier = None
97                self.error(str(errValue))
98        self.send("LabelPowerset Classifier", self.classifier)
99
100##############################################################################
101# Test the widget.
102# Make sure that a sample data set (emotions.tab) is in the directory.
103
104if __name__=="__main__":
105    a=QApplication(sys.argv)
106    ow=OWLP()
107
108    dataset = Orange.data.Table('emotions.tab')
109    ow.set_data(dataset)
110
111    ow.show()
112    a.exec_()
113    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.