source: orange/docs/extend-widgets/rst/OWDataSamplerB.py @ 11881:99bec0d8a70d

Revision 11881:99bec0d8a70d, 2.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 5 weeks ago (diff)

More fixes to widget development manual code snippets.

Line 
1"""
2<name>Data Sampler (B)</name>
3<description>Randomly selects a subset of instances from the data set</description>
4<icon>icons/DataSamplerB.svg</icon>
5<priority>20</priority>
6"""
7import Orange
8from OWWidget import *
9import OWGUI
10
11# [start-snippet-1]
12class OWDataSamplerB(OWWidget):
13    settingsList = ['proportion', 'commitOnChange']
14# [end-snippet-1]
15    def __init__(self, parent=None, signalManager=None):
16        OWWidget.__init__(self, parent, signalManager)
17
18        self.inputs = [("Data", Orange.data.Table, self.set_data)]
19        self.outputs = [("Sampled Data", Orange.data.Table)]
20# [start-snippet-2]
21        self.proportion = 50
22        self.commitOnChange = 0
23        self.loadSettings()
24# [end-snippet-2]
25        # GUI
26# [start-snippet-3]
27        box = OWGUI.widgetBox(self.controlArea, "Info")
28        self.infoa = OWGUI.widgetLabel(box, 'No data on input yet, waiting to get something.')
29        self.infob = OWGUI.widgetLabel(box, '')
30
31        OWGUI.separator(self.controlArea)
32        self.optionsBox = OWGUI.widgetBox(self.controlArea, "Options")
33        OWGUI.spin(self.optionsBox, self, 'proportion', min=10, max=90, step=10,
34                   label='Sample Size [%]:', callback=[self.selection, self.checkCommit])
35        OWGUI.checkBox(self.optionsBox, self, 'commitOnChange', 'Commit data on selection change')
36        OWGUI.button(self.optionsBox, self, "Commit", callback=self.commit)
37        self.optionsBox.setDisabled(1)
38# [end-snippet-3]
39        self.resize(100,50)
40
41# [start-snippet-4]
42    def set_data(self, dataset):
43        if dataset is not None:
44            self.dataset = dataset
45            self.infoa.setText('%d instances in input data set' % len(dataset))
46            self.optionsBox.setDisabled(0)
47            self.selection()
48            self.commit()
49        else:
50            self.send("Sampled Data", None)
51            self.optionsBox.setDisabled(1)
52            self.infoa.setText('No data on input yet, waiting to get something.')
53            self.infob.setText('')
54
55    def selection(self):
56        indices = Orange.data.sample.SubsetIndices2(p0=self.proportion / 100.)
57        ind = indices(self.dataset)
58        self.sample = self.dataset.select(ind, 0)
59        self.infob.setText('%d sampled instances' % len(self.sample))
60
61    def commit(self):
62        self.send("Sampled Data", self.sample)
63
64    def checkCommit(self):
65        if self.commitOnChange:
66            self.commit()
67# [end-snippet-4]
68
69if __name__=="__main__":
70    appl = QApplication(sys.argv)
71    ow = OWDataSamplerB()
72    ow.show()
73    dataset = Orange.data.Table('iris.tab')
74    ow.set_data(dataset)
75    appl.exec_()
Note: See TracBrowser for help on using the repository browser.