source: orange/Orange/OrangeWidgets/Regression/OWPade.py @ 9671:a7b056375472

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

Moved orange to Orange (part 2)

Line 
1"""
2<name>Pade</name>
3<description>Computes local partial derivatives</description>
4<icon>icons/Pade.png</icon>
5<priority>3500</priority>
6"""
7
8import orange, orngPade
9from OWWidget import *
10import OWGUI
11
12class OWPade(OWWidget):
13
14    settingsList = ["output", "method", "derivativeAsMeta", "originalAsMeta", "savedDerivativeAsMeta", "differencesAsMeta", "enableThreshold", "threshold"]
15    contextHandlers = {"": PerfectDomainContextHandler("", ["outputAttr", ContextField("attributes", selected="dimensions")])}
16
17    methodNames = ["First Triangle", "Star Univariate Regression", "Tube Regression"]
18    methods = [orngPade.firstTriangle, orngPade.starUnivariateRegression, orngPade.tubedRegression]
19    outputTypes = ["Qualitative constraint", "Quantitative differences"]
20   
21    def __init__(self, parent = None, signalManager = None, name = "Pade"):
22        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0)  #initialize base class
23        self.inputs = [("Data", ExampleTable, self.onDataInput)]
24        self.outputs = [("Data", ExampleTable)]
25
26        self.attributes = []
27        self.dimensions = []
28        self.output = 0
29        self.outputAttr = 0
30        self.derivativeAsMeta = 0
31        self.savedDerivativeAsMeta = 0
32        self.correlationsAsMeta = 1
33        self.differencesAsMeta = 1
34        self.originalAsMeta = 1
35        self.enableThreshold = 0
36        self.threshold = 0.0
37        self.method = 2
38        self.useMQCNotation = False
39        #self.persistence = 40
40
41        self.nNeighbours = 30
42
43        self.loadSettings()
44
45        box = OWGUI.widgetBox(self.controlArea, "Attributes") #, addSpace = True)
46        lb = self.lb = OWGUI.listBox(box, self, "dimensions", "attributes", selectionMode=QListWidget.MultiSelection, callback=self.dimensionsChanged)
47        hbox = OWGUI.widgetBox(box, orientation=0)
48        OWGUI.button(hbox, self, "All", callback=self.onAllAttributes)
49        OWGUI.button(hbox, self, "None", callback=self.onNoAttributes)
50        lb.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding))
51        lb.setMinimumSize(200, 200)
52       
53        OWGUI.separator(self.controlArea)
54
55        box = OWGUI.widgetBox(self.controlArea, "Method") #, addSpace = True)
56        OWGUI.comboBox(box, self, "method", callback = self.methodChanged, items = self.methodNames)
57#        self.nNeighboursSpin = OWGUI.spin(box, self, "nNeighbours", 10, 200, 10, label = "Number of neighbours" + "  ", callback = self.methodChanged)
58        #self.persistenceSpin = OWGUI.spin(box, self, "persistence", 0, 100, 5, label = "Persistence (0-100)" + "  ", callback = self.methodChanged, controlWidth=50)
59
60        OWGUI.separator(box)
61        hbox = OWGUI.widgetBox(box, orientation=0)
62        threshCB = OWGUI.checkBox(hbox, self, "enableThreshold", "Ignore differences below ")
63#        OWGUI.rubber(hbox, orientation = 0)
64        ledit = OWGUI.lineEdit(hbox, self, "threshold", valueType=float, validator=QDoubleValidator(0, 1e30, 0, self), controlWidth=50)
65        threshCB.disables.append(ledit)
66        threshCB.makeConsistent()
67        OWGUI.checkBox(box, self, "useMQCNotation", label = "Use MQC notation")
68
69        OWGUI.separator(self.controlArea)
70       
71        box = OWGUI.radioButtonsInBox(self.controlArea, self, "output", self.outputTypes, box="Output class", callback=self.dimensionsChanged)
72        self.outputLB = OWGUI.comboBox(OWGUI.indentedBox(box, sep=OWGUI.checkButtonOffsetHint(box.buttons[-1])), self, "outputAttr", callback=self.outputDiffChanged)
73       
74        OWGUI.separator(self.controlArea)
75       
76        box = OWGUI.widgetBox(self.controlArea, "Output meta attributes") #, addSpace = True)
77        self.metaCB = OWGUI.checkBox(box, self, "derivativeAsMeta", label="Qualitative constraint")
78        OWGUI.checkBox(box, self, "differencesAsMeta", label="Derivatives of selected attributes")
79        OWGUI.checkBox(box, self, "correlationsAsMeta", label="Absolute values of derivatives")
80        OWGUI.checkBox(box, self, "originalAsMeta", label="Original class attribute")
81
82        self.applyButton = OWGUI.button(self.controlArea, self, "&Apply", callback=self.apply, disabled=True, default=True)
83       
84        self.contAttributes = []
85        self.dimensions = []
86        self.data = None
87
88        #self.persistenceSpin.setEnabled(self.methods[self.method] == orngPade.canceling)
89        #self.setFixedWidth(self.sizeHint().width())
90
91    def sendReport(self):
92        self.reportSettings("Learning parameters", 
93                            [("Class is derived by", ", ".join(self.attributes[i][0] for i in self.dimensions) or "none"),
94                             ("Method", self.methodNames[self.method]),
95                             ("Threshold", self.threshold if self.enableThreshold else "None"),
96                           ])
97        self.reportSettings("Output", 
98                            [("Label", self.outputTypes[self.output]),
99                             not self.output and ("Notation", ["Pade", "QUIN"][self.useMQCNotation]),
100                             ("Meta attributes", ", ".join(s for s, c in [("qualitative constraint", self.derivativeAsMeta),
101                                                                         ("derivative", self.differencesAsMeta),
102                                                                         ("absolute derivative", self.correlationsAsMeta),
103                                                                         ("original class", self.originalAsMeta)] if c) or "none")])
104        self.reportData(self.data)
105
106    def onAllAttributes(self):
107        self.dimensions = range(len(self.attributes))
108        self.dimensionsChanged()
109
110    def onNoAttributes(self):
111        self.dimensions = []
112        self.dimensionsChanged()
113
114    def outputDiffChanged(self):
115        if not self.output:
116            self.output = 1
117        self.dimensionsChanged()
118
119    def dimensionsChanged(self):
120        if self.output and self.dimensions:
121            if not self.metaCB.isEnabled():
122                self.derivativeAsMeta = self.savedDerivativeAsMeta
123                self.metaCB.setEnabled(True)
124        else:
125            if self.metaCB.isEnabled():
126                self.savedDerivativeAsMeta = self.derivativeAsMeta
127                self.derivativeAsMeta = 0
128                self.metaCB.setEnabled(False)
129
130        self.applyButton.setEnabled(bool(self.dimensions) or (self.output and bool(self.contAttributes)))
131
132    def methodChanged(self):
133        self.deltas = None
134        #self.persistenceSpin.setEnabled(self.methods[self.method] == orngPade.canceling)
135        #self.nNeighboursSpin.setEnabled(bool(self.method==3))
136
137
138    def onDataInput(self, data):
139        self.closeContext()
140        if data and self.isDataWithClass(data, orange.VarTypes.Continuous, checkMissing=True):
141            orngPade.makeBasicCache(data, self)
142
143            icons = OWGUI.getAttributeIcons()
144            self.outputLB.clear()
145            for attr in self.contAttributes:
146                self.outputLB.addItem(icons[attr.varType], attr.name)
147
148            self.dimensions = range(len(self.attributes))
149        else:
150            orngPade.makeEmptyCache(self)
151            self.dimensions = []
152
153        self.openContext("", data)
154        self.dimensionsChanged()
155
156
157    def apply(self):
158        data = self.data
159        if not data or not self.contAttributes:
160            self.send("Data", None)
161            return
162
163        if not self.deltas:
164            self.deltas = [[None] * len(self.contAttributes) for x in xrange(len(self.data))]
165        if not getattr(self, "errors", None):
166            self.errors = [[None] * len(self.contAttributes) for x in xrange(len(self.data))]
167
168        dimensionsToCompute = [d for d in self.dimensions if not self.deltas[0][d]]
169        if self.output and self.outputAttr not in self.dimensions and not self.deltas[0][self.outputAttr]:
170            dimensionsToCompute.append(self.outputAttr)
171        if dimensionsToCompute:
172            self.progressBarInit()
173            self.methods[self.method](self, dimensionsToCompute, self.progressBarSet)
174            self.progressBarFinished()
175
176        paded, derivativeID, metaIDs, classID, corrIDs = orngPade.createQTable(self, data, self.dimensions,
177                                                             not self.output and -1 or self.outputAttr,
178                                                             self.enableThreshold and abs(self.threshold),
179                                                             self.useMQCNotation, self.derivativeAsMeta, self.differencesAsMeta, False, self.originalAsMeta)
180        self.send("Data", paded)
181
182
183
184if __name__=="__main__":
185    import sys
186
187    a=QApplication(sys.argv)
188    ow=OWPade()
189    ow.show()
190    #ow.onDataInput(orange.ExampleTable(r"c:\D\ai\Orange\test\squin\xyz-t"))
191    #ow.onDataInput(orange.ExampleTable(r"C:\delo\PADE\JJ_testi\sinxsiny\sinxsiny_noise05.tab"))
192    a.exec_()
193
194    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.