source: orange/docs/extend-widgets/rst/OWDataSamplerB.py @ 11085:7f846578bf26

Revision 11085:7f846578bf26, 2.5 KB checked in by markotoplak, 15 months ago (diff)

Moved datasets from Orange/doc/datasets to Orange/datasets.

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