source: orange/docs/extend-widgets/rst/OWDataSamplerA.py @ 11880:b4ef9ac82685

Revision 11880:b4ef9ac82685, 1.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 5 weeks ago (diff)

Fixes to widget development manual code snippets.

Line 
1"""
2<name>Data Sampler</name>
3<description>Randomly selects a subset of instances from the data set</description>
4<icon>icons/DataSamplerA.svg</icon>
5<priority>10</priority>
6"""
7# [start-snippet-1]
8import Orange
9from OWWidget import *
10import OWGUI
11
12class OWDataSamplerA(OWWidget):
13
14    def __init__(self, parent=None, signalManager=None):
15        OWWidget.__init__(self, parent, signalManager)
16
17        self.inputs = [("Data", Orange.data.Table, self.set_data)]
18        self.outputs = [("Sampled Data", Orange.data.Table)]
19
20        # GUI
21        box = OWGUI.widgetBox(self.controlArea, "Info")
22        self.infoa = OWGUI.widgetLabel(box, 'No data on input yet, waiting to get something.')
23        self.infob = OWGUI.widgetLabel(box, '')
24        self.resize(100,50)
25# [end-snippet-1]
26
27# [start-snippet-2]
28    def set_data(self, dataset):
29        if dataset is not None:
30            self.infoa.setText('%d instances in input data set' % len(dataset))
31            indices = Orange.data.sample.SubsetIndices2(p0=0.1)
32            ind = indices(dataset)
33            sample = dataset.select(ind, 0)
34            self.infob.setText('%d sampled instances' % len(sample))
35            self.send("Sampled Data", sample)
36        else:
37            self.infoa.setText('No data on input yet, waiting to get something.')
38            self.infob.setText('')
39            self.send("Sampled Data", None)
40# [end-snippet-2]
41
42# [start-snippet-3]
43if __name__=="__main__":
44    appl = QApplication(sys.argv)
45    ow = OWDataSamplerA()
46    ow.show()
47    dataset = Orange.data.Table('iris.tab')
48    ow.set_data(dataset)
49    appl.exec_()
50# [end-snippet-3]
Note: See TracBrowser for help on using the repository browser.