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

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

More fixes to widget development manual code snippets.

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.set_data)]
18# [start-snippet-1]
19        self.outputs = [("Sampled Data", Orange.data.Table),
20                        ("Other Data", Orange.data.Table)]
21# [end-snippet-1]
22        self.proportion = 50
23        self.commitOnChange = 0
24        self.loadSettings()
25
26        # GUI
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
39        self.resize(100,50)
40
41    def set_data(self, dataset):
42        if dataset is not None:
43            self.dataset = dataset
44            self.infoa.setText('%d instances in input data set' % len(dataset))
45            self.optionsBox.setDisabled(0)
46            self.selection()
47            self.commit()
48        else:
49            self.send("Sampled Data", None)
50            self.optionsBox.setDisabled(1)
51            self.infoa.setText('No data on input yet, waiting to get something.')
52            self.infob.setText('')
53
54    def selection(self):
55        indices = Orange.data.sample.SubsetIndices2(p0=self.proportion / 100.)
56        ind = indices(self.dataset)
57        self.sample = self.dataset.select(ind, 0)
58        self.otherdata = self.dataset.select(ind, 1)
59        self.infob.setText('%d sampled instances' % len(self.sample))
60
61    def commit(self):
62        self.send("Sampled Data", self.sample)
63        self.send("Other Data", self.otherdata)
64
65    def checkCommit(self):
66        if self.commitOnChange:
67            self.commit()
68
69
70if __name__=="__main__":
71    appl = QApplication(sys.argv)
72    ow = OWDataSamplerC()
73    ow.show()
74    dataset = Orange.data.Table('iris.tab')
75    ow.set_data(dataset)
76    appl.exec_()
Note: See TracBrowser for help on using the repository browser.