source: orange/docs/extend-widgets/rst/OWDataSamplerB.py @ 11593:6edc44eb9655

Revision 11593:6edc44eb9655, 2.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 10 months ago (diff)

Updated Widget development tutorial.

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