source: orange/Orange/doc/extend-widgets/OWAttributeSampler.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 2.7 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

Line 
1"""
2<name>Attribute Sampler</name>
3<description>Lets the user select a list of attributes and the class attribute</description>
4<icon>icons/AttributeSampler.png</icon>
5<priority>1020</priority>
6"""
7
8from OWWidget import *
9import OWGUI
10
11class OWAttributeSampler(OWWidget):
12    settingsList = []
13    contextHandlers = {"": DomainContextHandler("", [
14            ContextField("classAttribute", DomainContextHandler.Required),
15            ContextField("attributeList", DomainContextHandler.List + DomainContextHandler.SelectedRequired,
16                         selected="selectedAttributes")])}
17
18    def __init__(self, parent=None, signalManager=None):
19        OWWidget.__init__(self, parent, signalManager, 'AttributeSampler')
20
21        self.inputs = [("Examples", ExampleTable, self.dataset)]
22        self.outputs = [("Examples", ExampleTable)]
23
24        self.icons = self.createAttributeIconDict()
25
26        self.attributeList = []
27        self.selectedAttributes = []
28        self.classAttribute = None
29        self.loadSettings()
30
31        OWGUI.listBox(self.controlArea, self, "selectedAttributes", "attributeList", box="Selected attributes", selectionMode = QListWidget.ExtendedSelection)
32        OWGUI.separator(self.controlArea)
33        self.classAttrCombo = OWGUI.comboBox(self.controlArea, self, "classAttribute", box="Class attribute")
34        OWGUI.separator(self.controlArea)
35        OWGUI.button(self.controlArea, self, "Commit", callback = self.outputData)
36
37        self.resize(150,400)
38
39
40    def dataset(self, data):
41        self.closeContext()
42
43        self.classAttrCombo.clear()
44        if data:
45            self.attributeList = [(attr.name, attr.varType) for attr in data.domain]
46            self.selectedAttributes = []
47            for attrName, attrType in self.attributeList:
48                self.classAttrCombo.addItem(self.icons[attrType], attrName)
49            self.classAttribute = 0
50        else:
51            self.attributeList = []
52            self.selectedAttributes = []
53            self.classAttrCombo.addItem("")
54
55        self.openContext("", data)
56
57        self.data = data
58        self.outputData()
59
60
61    def outputData(self):
62        if not self.data:
63            self.send("Examples", None)
64        else:
65            newDomain = orange.Domain([self.data.domain[i] for i in self.selectedAttributes], self.data.domain[self.classAttribute])
66            newData = orange.ExampleTable(newDomain, self.data)
67            self.send("Examples", newData)
68
69
70##############################################################################
71# Test the widget, run from prompt
72
73if __name__=="__main__":
74    appl = QApplication(sys.argv)
75    ow = OWAttributeSampler()
76    ow.show()
77
78    data = orange.ExampleTable('../datasets/iris.tab')
79    ow.dataset(data)
80
81    appl.exec_()
Note: See TracBrowser for help on using the repository browser.