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

Revision 10502:6b593a8cd5a0, 3.6 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>BR-kNN</name>
3<description>BR-kNN 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 OWBRkNN(OWWidget):
16    settingsList = ["name","k"]
17
18    def __init__(self, parent=None, signalManager = None, name='BR-kNN'):
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                       ]
26        self.outputs = [("Learner", orange.Learner),("BR-kNN Classifier", Orange.multilabel.BRkNNClassifier)]
27
28        # Settings
29        self.name = 'BR-kNN'
30        self.k = 1
31       
32        self.loadSettings()
33
34        self.data = None                    # input data set
35        self.preprocessor = None            # no preprocessing as default
36        self.set_learner()                  # this just sets the learner, no data
37                                            # has come to the input yet
38
39        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', \
40                 tooltip='Name to be used by other widgets to identify your learner/classifier.')
41
42        OWGUI.separator(self.controlArea)
43
44        wbN = OWGUI.widgetBox(self.controlArea, "Neighbours")
45        OWGUI.spin(wbN, self, "k", 1, 100, 1, None, "Number of neighbours", orientation="horizontal")
46       
47        OWGUI.separator(self.controlArea)
48
49        OWGUI.button(self.controlArea, self, "&Apply", callback=self.set_learner, disabled=0, default=True)
50       
51        OWGUI.rubber(self.controlArea)
52
53        self.resize(100,250)
54
55    def send_report(self):
56        self.reportSettings("Learning parameters",
57                            [("base_learner", self.baselearnerList[self.base_learner][0])])
58        self.reportData(self.data)
59           
60    def set_data(self,data): 
61        if data == None:
62            return
63
64        if not Orange.multilabel.is_multilabel(data):
65            self.warning(0, "Multi-label data with class values 0 and 1 is "
66                            "expected on the input.")
67            return
68        self.warning(0, None)
69       
70        self.data = data
71        self.set_learner()
72
73    def set_preprocessor(self, pp):
74        self.preprocessor = pp
75        self.set_learner()
76         
77    def set_learner(self):
78        self.learner = Orange.multilabel.BRkNNLearner(k = self.k)
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("BR-kNN 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=OWBRkNN()
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.