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

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

Updated Widget development tutorial.

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