source: orange/Orange/OrangeWidgets/Data/OWContinuize.py @ 11096:cf7d2ae9d22b

Revision 11096:cf7d2ae9d22b, 8.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 19 months ago (diff)

Added new svg icons for the widgets/categories.

Line 
1"""
2<name>Continuize</name>
3<description>Turns discrete attributes into continuous and, optionally, normalizes the continuous values.</description>
4<icon>icons/Continuize.svg</icon>
5<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact>
6<priority>2110</priority>
7"""
8#
9# OWContinuize.py
10# Continuize Widget
11# Turns discrete attributes into continuous
12#
13from OWWidget import *
14from orngWrap import PreprocessedLearner
15import OWGUI
16
17class OWContinuize(OWWidget):
18    settingsList = ["multinomialTreatment", "classTreatment", "zeroBased", "continuousTreatment", "autosend"]
19    contextHandlers = {"": ClassValuesContextHandler("", ["targetValue"])}
20
21    multinomialTreats = (("Target or First value as base", orange.DomainContinuizer.LowestIsBase),
22                         ("Most frequent value as base", orange.DomainContinuizer.FrequentIsBase),
23                         ("One attribute per value", orange.DomainContinuizer.NValues),
24                         ("Ignore multinomial attributes", orange.DomainContinuizer.Ignore),
25                         ("Ignore all discrete attributes", orange.DomainContinuizer.IgnoreAllDiscrete),
26                         ("Treat as ordinal", orange.DomainContinuizer.AsOrdinal),
27                         ("Divide by number of values", orange.DomainContinuizer.AsNormalizedOrdinal))
28
29    continuousTreats = (("Leave them as they are", orange.DomainContinuizer.Leave),
30                        ("Normalize by span", orange.DomainContinuizer.NormalizeBySpan),
31                        ("Normalize by variance", orange.DomainContinuizer.NormalizeByVariance))
32
33    classTreats = (("Leave it as it is", orange.DomainContinuizer.Ignore),
34                   ("Treat as ordinal", orange.DomainContinuizer.AsOrdinal),
35                   ("Divide by number of values", orange.DomainContinuizer.AsNormalizedOrdinal),
36                   ("Specified target value", -1))
37   
38    valueRanges = ["from -1 to 1", "from 0 to 1"]
39
40    def __init__(self,parent=None, signalManager = None, name = "Continuizer"):
41        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0)
42
43        self.inputs = [("Data", ExampleTable, self.setData)]
44        self.outputs = [("Data", ExampleTable), ("Preprocessor", PreprocessedLearner)]
45
46        self.multinomialTreatment = 0
47        self.targetValue = 0
48        self.continuousTreatment = 0
49        self.classTreatment = 0
50        self.zeroBased = 1
51        self.autosend = 0
52        self.dataChanged = False
53        self.loadSettings()
54
55        bgMultiTreatment = OWGUI.widgetBox(self.controlArea, "Multinomial attributes")
56        OWGUI.radioButtonsInBox(bgMultiTreatment, self, "multinomialTreatment", btnLabels=[x[0] for x in self.multinomialTreats], callback=self.sendDataIf)
57
58        self.controlArea.layout().addSpacing(4)
59
60        bgMultiTreatment = OWGUI.widgetBox(self.controlArea, "Continuous attributes")
61        OWGUI.radioButtonsInBox(bgMultiTreatment, self, "continuousTreatment", btnLabels=[x[0] for x in self.continuousTreats], callback=self.sendDataIf)
62
63        self.controlArea.layout().addSpacing(4)
64
65        bgClassTreatment = OWGUI.widgetBox(self.controlArea, "Discrete class attribute")
66        self.ctreat = OWGUI.radioButtonsInBox(bgClassTreatment, self, "classTreatment", btnLabels=[x[0] for x in self.classTreats], callback=self.sendDataIf)
67#        hbox = OWGUI.widgetBox(bgClassTreatment, orientation = "horizontal")
68#        OWGUI.separator(hbox, 19, 4)
69        hbox = OWGUI.indentedBox(bgClassTreatment, sep=OWGUI.checkButtonOffsetHint(self.ctreat.buttons[-1]), orientation="horizontal")
70        self.cbTargetValue = OWGUI.comboBox(hbox, self, "targetValue", label="Target Value ", items=[], orientation="horizontal", callback=self.cbTargetSelected)
71        def setEnabled(*args):
72            self.cbTargetValue.setEnabled(self.classTreatment == 3)
73        self.connect(self.ctreat.group, SIGNAL("buttonClicked(int)"), setEnabled)
74        setEnabled() 
75
76        self.controlArea.layout().addSpacing(4)
77
78        zbbox = OWGUI.widgetBox(self.controlArea, "Value range")
79        OWGUI.radioButtonsInBox(zbbox, self, "zeroBased", btnLabels=self.valueRanges, callback=self.sendDataIf)
80
81        self.controlArea.layout().addSpacing(4)
82
83        snbox = OWGUI.widgetBox(self.controlArea, "Send data")
84        OWGUI.button(snbox, self, "Send data", callback=self.sendData, default=True)
85        OWGUI.checkBox(snbox, self, "autosend", "Send automatically", callback=self.enableAuto)
86        self.data = None
87        self.sendPreprocessor()
88        self.resize(150,300)
89        #self.adjustSize()
90
91    def cbTargetSelected(self):
92        self.classTreatment = 3
93        self.sendDataIf()
94
95    def setData(self,data):
96        self.closeContext()
97
98        if not data:
99            self.data = None
100            self.cbTargetValue.clear()
101            self.openContext("", self.data)
102            self.send("Data", None)
103        else:
104            if not self.data or data.domain.classVar != self.data.domain.classVar:
105                self.cbTargetValue.clear()
106                if data.domain.classVar and data.domain.classVar.varType == orange.VarTypes.Discrete:
107                    for v in data.domain.classVar.values:
108                        self.cbTargetValue.addItem(" "+v)
109                    self.ctreat.setDisabled(False)
110                    self.targetValue = 0
111                else:
112                    self.ctreat.setDisabled(True)
113            self.data = data
114            self.openContext("", self.data)
115            self.sendData()
116
117    def sendDataIf(self):
118        self.dataChanged = True
119        if self.autosend:
120            self.sendPreprocessor()
121            self.sendData()
122
123    def enableAuto(self):
124        if self.dataChanged:
125            self.sendPreprocessor()
126            self.sendData()
127
128    def constructContinuizer(self):
129        conzer = orange.DomainContinuizer()
130        conzer.zeroBased = self.zeroBased
131        conzer.continuousTreatment = self.continuousTreatment
132        conzer.multinomialTreatment = self.multinomialTreats[self.multinomialTreatment][1]
133        conzer.classTreatment = self.classTreats[self.classTreatment][1]
134        return conzer
135
136    def sendPreprocessor(self):
137        continuizer = self.constructContinuizer()
138        self.send("Preprocessor", PreprocessedLearner(
139            lambda data, weightId=0, tc=(self.targetValue if self.classTreatment else -1): \
140                orange.ExampleTable(continuizer(data, weightId, tc) if data.domain.classVar and self.data.domain.classVar.varType == orange.VarTypes.Discrete else \
141                                    continuizer(data, weightId), data)))
142               
143               
144    def sendData(self):
145        continuizer = self.constructContinuizer()
146        if self.data:
147            if self.data.domain.classVar and self.data.domain.classVar.varType == orange.VarTypes.Discrete:
148                domain = continuizer(self.data, 0, self.targetValue if self.classTreatment else -1)
149            else:
150                domain = continuizer(self.data, 0)
151            domain.addmetas(self.data.domain.getmetas())
152            self.send("Data", orange.ExampleTable(domain, self.data))
153        self.dataChanged = False
154       
155    def sendReport(self):
156        self.reportData(self.data, "Input data")
157        clstr = "None"
158        if self.data is not None:
159            classVar = self.data.domain.classVar
160            if self.classTreatment == 3 and classVar and classVar.varType == orange.VarTypes.Discrete and len(classVar.values) >= 2: 
161                clstr = "Dummy variable for target '%s'" % classVar.values[self.targetValue]
162            else:
163                clstr = self.classTreats[self.classTreatment][0]
164        self.reportSettings("Settings",
165                            [("Multinominal attributes", self.multinomialTreats[self.multinomialTreatment][0]),
166                             ("Continuous attributes", self.continuousTreats[self.continuousTreatment][0]),
167                             ("Class attribute", clstr),
168                             ("Value range", self.valueRanges[self.zeroBased])])
169
170if __name__ == "__main__":
171    a = QApplication(sys.argv)
172    ow = OWContinuize()
173    #data = orange.ExampleTable("d:\\ai\\orange\\test\\iris")
174#    data = orange.ExampleTable(r"E:\Development\Orange Datasets\UCI\iris.tab")
175    data = orange.ExampleTable("../../doc/datasets/iris.tab")
176    ow.setData(data)
177    ow.show()
178    a.exec_()
179    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.