Source

orange-reliability / _reliability / widgets / OWReliability.py

Full commit
Matija Polajnar 55e4bdc 



Aleš Erjavec f64b10f 
Matija Polajnar 55e4bdc 

























































































































































































































































































































































































































  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
<name>Reliability</name>
<contact>Ales Erjavec (ales.erjavec(@at@)fri.uni-lj.si)</contact>
<priority>310</priority>
<icon>icons/Reliability.svg</icon>
"""

from __future__ import absolute_import

import Orange
import _reliability as reliability
from Orange.evaluation import testing
#from Orange.utils import progress_bar_milestones
from functools import partial
 
from OWWidget import *
import OWGUI

class OWReliability(OWWidget):
    settingsList = ["variance_checked", "bias_checked", "bagged_variance",
        "local_cv", "local_model_pred_error", "bagging_variance_cn", 
        "mahalanobis_distance", "var_e", "bias_e", "bagged_m", "local_cv_k",
        "local_pe_k", "bagged_cn_m", "bagged_cn_k", "mahalanobis_k",
        "include_error", "include_class", "include_input_features",
        "auto_commit"]
    
    def __init__(self, parent=None, signalManager=None, title="Reliability"):
        OWWidget.__init__(self, parent, signalManager, title, wantMainArea=False)
        
        self.inputs = [("Learner", Orange.core.Learner, self.set_learner),
                       ("Training Data", Orange.data.Table, self.set_train_data),
                       ("Test Data", Orange.data.Table, self.set_test_data)]
        
        self.outputs = [("Reliability Scores", Orange.data.Table)]
        
        self.variance_checked = False
        self.bias_checked = False
        self.bagged_variance = False
        self.local_cv = False
        self.local_model_pred_error = False
        self.bagging_variance_cn = False
        self.mahalanobis_distance = True
        
        self.var_e = "0.01, 0.1, 0.5, 1.0, 2.0"
        self.bias_e =  "0.01, 0.1, 0.5, 1.0, 2.0"
        self.bagged_m = 10
        self.local_cv_k = 2
        self.local_pe_k = 5
        self.bagged_cn_m = 5
        self.bagged_cn_k = 1
        self.mahalanobis_k = 3
        
        self.include_error = True
        self.include_class = True
        self.include_input_features = False
        self.auto_commit = False
        
        # (selected attr name, getter function, count of returned estimators, indices of estimator results to use)
        self.estimators = \
            [("variance_checked", self.get_SAVar, 3, [0]),
             ("bias_checked", self.get_SABias, 3, [1, 2]),
             ("bagged_variance", self.get_BAGV, 1, [0]),
             ("local_cv", self.get_LCV, 1, [0]),
             ("local_model_pred_error", self.get_CNK, 2, [0, 1]),
             ("bagging_variance_cn", self.get_BVCK, 4, [0]),
             ("mahalanobis_distance", self.get_Mahalanobis, 1, [0])]
        
        #####
        # GUI
        #####
        self.loadSettings()
        
        box = OWGUI.widgetBox(self.controlArea, "Info", addSpace=True)
        self.info_box = OWGUI.widgetLabel(box, "\n\n")
        
        rbox = OWGUI.widgetBox(self.controlArea, "Methods", addSpace=True)
        def method_box(parent, name, value):
            box = OWGUI.widgetBox(rbox, name, flat=False)
            box.setCheckable(True)
            box.setChecked(bool(getattr(self, value)))
            self.connect(box, SIGNAL("toggled(bool)"),
                         lambda on: (setattr(self, value, on),
                                     self.method_selection_changed(value)))
            return box
            
        e_validator = QRegExpValidator(QRegExp(r"\s*(-?[0-9]+(\.[0-9]*)\s*,\s*)+"), self)
        variance_box = method_box(rbox, "Sensitivity analysis (variance)",
                                  "variance_checked")
        OWGUI.lineEdit(variance_box, self, "var_e", "Sensitivities:", 
                       tooltip="List of possible e values (comma separated) for SAvar reliability estimates.", 
                       callback=partial(self.method_param_changed, 0),
                       validator=e_validator)
        
        bias_box = method_box(rbox, "Sensitivity analysis (bias)",
                                    "bias_checked")
        OWGUI.lineEdit(bias_box, self, "bias_e", "Sensitivities:", 
                       tooltip="List of possible e values (comma separated) for SAbias reliability estimates.", 
                       callback=partial(self.method_param_changed, 1),
                       validator=e_validator)
        
        bagged_box = method_box(rbox, "Variance of bagged models",
                                "bagged_variance")
        
        OWGUI.spin(bagged_box, self, "bagged_m", 2, 100, step=1,
                   label="Models:",
                   tooltip="Number of bagged models to be used with BAGV estimate.",
                   callback=partial(self.method_param_changed, 2),
                   keyboardTracking=False)
        
        local_cv_box = method_box(rbox, "Local cross validation",
                                  "local_cv")
        
        OWGUI.spin(local_cv_box, self, "local_cv_k", 2, 20, step=1,
                   label="Nearest neighbors:",
                   tooltip="Number of nearest neighbors used in LCV estimate.",
                   callback=partial(self.method_param_changed, 3),
                   keyboardTracking=False)
        
        local_pe = method_box(rbox, "Local modeling of prediction error",
                              "local_model_pred_error")
        
        OWGUI.spin(local_pe, self, "local_pe_k", 1, 20, step=1,
                   label="Nearest neighbors:",
                   tooltip="Number of nearest neighbors used in CNK estimate.",
                   callback=partial(self.method_param_changed, 4),
                   keyboardTracking=False)
        
        bagging_cnn = method_box(rbox, "Bagging variance c-neighbors",
                                 "bagging_variance_cn")
        
        OWGUI.spin(bagging_cnn, self, "bagged_cn_m", 2, 100, step=1,
                   label="Models:",
                   tooltip="Number of bagged models to be used with BVCK estimate.",
                   callback=partial(self.method_param_changed, 5),
                   keyboardTracking=False)
        
        OWGUI.spin(bagging_cnn, self, "bagged_cn_k", 1, 20, step=1,
                   label="Nearest neighbors:",
                   tooltip="Number of nearest neighbors used in BVCK estimate.",
                   callback=partial(self.method_param_changed, 5),
                   keyboardTracking=False)
        
        mahalanobis_box = method_box(rbox, "Mahalanobis distance",
                                     "mahalanobis_distance")
        OWGUI.spin(mahalanobis_box, self, "mahalanobis_k", 1, 20, step=1,
                   label="Nearest neighbors:",
                   tooltip="Number of nearest neighbors used in BVCK estimate.",
                   callback=partial(self.method_param_changed, 6),
                   keyboardTracking=False)
        
        box = OWGUI.widgetBox(self.controlArea, "Output")
        
        OWGUI.checkBox(box, self, "include_error", "Include prediction error",
                       tooltip="Include prediction error in the output",
                       callback=self.commit_if)
        
        OWGUI.checkBox(box, self, "include_class", "Include original class and prediction",
                       tooltip="Include original class and prediction in the output.",
                       callback=self.commit_if)
        
        OWGUI.checkBox(box, self, "include_input_features", "Include input features",
                       tooltip="Include features from the input data set.",
                       callback=self.commit_if)
        
        cb = OWGUI.checkBox(box, self, "auto_commit", "Commit on any change",
                            callback=self.commit_if)
        
        self.commit_button = b = OWGUI.button(box, self, "Commit",
                                              callback=self.commit,
                                              autoDefault=True)
        
        OWGUI.setStopper(self, b, cb, "output_changed", callback=self.commit)
        
        self.commit_button.setEnabled(any([getattr(self, selected) \
                                for selected, _, _, _ in  self.estimators]))
        
        self.learner = None
        self.train_data = None
        self.test_data = None
        self.output_changed = False
        self.train_data_has_no_class = False
        self.train_data_has_discrete_class = False
        self.invalidate_results()
        
    def set_train_data(self, data=None):
        self.error()
        self.train_data_has_no_class = False
        self.train_data_has_discrete_class = False
        
        if data is not None:
            if not self.isDataWithClass(data, Orange.core.VarTypes.Continuous):
                if not data.domain.class_var:
                    self.train_data_has_no_class = True
                elif not isinstance(data.domain.class_var,
                                    Orange.feature.Continuous):
                    self.train_data_has_discrete_class = True
                    
                data = None
        
        self.train_data = data
        self.invalidate_results() 
        
    def set_test_data(self, data=None):
        self.test_data = data
        self.invalidate_results()
        
    def set_learner(self, learner=None):
        self.learner = learner
        self.invalidate_results()
        
    def handleNewSignals(self):
        name = "No learner on input"
        train = "No train data on input"
        test = "No test data on input"
        
        if self.learner:
            name = "Learner: " + (getattr(self.learner, "name") or type(self.learner).__name__)
            
        if self.train_data is not None:
            train = "Train Data: %i features, %i instances" % \
                (len(self.train_data.domain), len(self.train_data))
        elif self.train_data_has_no_class:
            train = "Train Data has no class variable"
        elif self.train_data_has_discrete_class:
            train = "Train Data doesn't have a continuous class"
            
        if self.test_data is not None:
            test = "Test Data: %i features, %i instances" % \
                (len(self.test_data.domain), len(self.test_data))
        elif self.train_data:
            test = "Test data: using training data"
        
        self.info_box.setText("\n".join([name, train, test]))
        
        if self.learner and self._test_data() is not None:
            self.commit_if()
        
    def invalidate_results(self, which=None):
        if which is None:
            self.results = [None for f in self.estimators]
#            print "Invalidating all"
        else:
            for i in which:
                self.results[i] = None
#            print "Invalidating", which
        
    def run(self):
        plan = []
        estimate_index = 0
        for i, (selected, method, count, offsets) in enumerate(self.estimators):
            if self.results[i] is None and getattr(self, selected):
                plan.append((i, method, [estimate_index + offset for offset in offsets]))
                estimate_index += count
                
        estimators = [method() for _, method, _ in plan]
        
        if not estimators:
            return
            
        pb = OWGUI.ProgressBar(self, len(self._test_data()))
        estimates = self.run_estimation(estimators, pb.advance)
        pb.finish()
        
        self.predictions = [v for v, _ in estimates]
        estimates = [prob.reliability_estimate for _, prob in estimates]
        
        for i, (index, method, estimate_indices) in enumerate(plan):
            self.results[index] = [[e[est_index] for e in estimates] \
                                   for est_index in estimate_indices]
        
    def _test_data(self):
        if self.test_data is not None:
            return self.test_data
        else:
            return self.train_data
    
    def get_estimates(self, estimator, advance=None):
        test = self._test_data()
        res = []
        for i, inst in enumerate(test):
            value, prob = estimator(inst, result_type=Orange.core.GetBoth)
            res.append((value, prob))
            if advance:
                advance()
        return res
                
    def run_estimation(self, estimators, advance=None):
        rel = reliability.Learner(self.learner, estimators=estimators)
        estimator = rel(self.train_data)
        return self.get_estimates(estimator, advance) 
    
    def get_SAVar(self):
        return reliability.SensitivityAnalysis(e=eval(self.var_e))
    
    def get_SABias(self):
        return reliability.SensitivityAnalysis(e=eval(self.bias_e))
    
    def get_BAGV(self):
        return reliability.BaggingVariance(m=self.bagged_m)
    
    def get_LCV(self):
        return reliability.LocalCrossValidation(k=self.local_cv_k)
    
    def get_CNK(self):
        return reliability.CNeighbours(k=self.local_pe_k)
    
    def get_BVCK(self):
        bagv = reliability.BaggingVariance(m=self.bagged_cn_m)
        cnk = reliability.CNeighbours(k=self.bagged_cn_k)
        return reliability.BaggingVarianceCNeighbours(bagv, cnk)
    
    def get_Mahalanobis(self):
        return reliability.Mahalanobis(k=self.mahalanobis_k)
    
    def method_selection_changed(self, method=None):
        self.commit_button.setEnabled(any([getattr(self, selected) \
                                for selected, _, _, _ in  self.estimators]))
        self.commit_if()
    
    def method_param_changed(self, method=None):
        if method is not None:
            self.invalidate_results([method])
        self.commit_if()
        
    def commit_if(self):
        if self.auto_commit:
            self.commit()
        else:
            self.output_changed = True
            
    def commit(self):
        from Orange import feature as variable
        name_mapper = {"Mahalanobis absolute": "Mahalanobis"}
        all_predictions = []
        all_estimates = []
        score_vars = []
        features = []
        table = None
        if self.learner and self.train_data is not None \
                and self._test_data() is not None:
            self.run()
            
            scores = []
            if self.include_class and not self.include_input_features:
                original_class = self._test_data().domain.class_var
                features.append(original_class)
                
            if self.include_class:
                prediction_var = variable.Continuous("Prediction")
                features.append(prediction_var)
                
            if self.include_error:
                error_var = variable.Continuous("Error")
                abs_error_var = variable.Continuous("Abs. Error")
                features.append(error_var)
                features.append(abs_error_var)
                
            for estimates, (selected, method, _, _) in zip(self.results, self.estimators):
                if estimates is not None and getattr(self, selected):
                    for estimate in estimates:
                        name = estimate[0].method_name
                        name = name_mapper.get(name, name)
                        var = variable.Continuous(name)
                        features.append(var)
                        score_vars.append(var)
                        all_estimates.append(estimate)
                    
            if self.include_input_features:
                dom = self._test_data().domain
                attributes = list(dom.attributes) + features
                domain = Orange.data.Domain(attributes, dom.class_var)
                domain.add_metas(dom.get_metas())
                
                data = Orange.data.Table(domain, self._test_data())
            else:
                domain = Orange.data.Domain(features, None)
                data = Orange.data.Table(domain, [[None] * len(features) for _ in self._test_data()])
            
            if self.include_class:
                for d, inst, pred in zip(data, self._test_data(), self.predictions):
                    if not self.include_input_features:
                        d[features[0]] = float(inst.get_class())
                    d[prediction_var] = float(pred)
            
            if self.include_error:
                for d, inst, pred in zip(data, self._test_data(), self.predictions):
                    error = float(pred) - float(inst.get_class())
                    d[error_var] = error
                    d[abs_error_var] = abs(error)
                    
            for estimations, var in zip(all_estimates, score_vars):
                for d, e in zip(data, estimations):
                    d[var] = e.estimate
            
            table = data
            
        self.send("Reliability Scores", table)
        self.output_changed = False
        
        
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = OWReliability()
    data = Orange.data.Table("housing")
    indices = Orange.core.MakeRandomIndices2(p0=20)(data)
    data = data.select(indices, 0)
    
    learner = Orange.regression.tree.TreeLearner()
    w.set_learner(learner)
    w.set_train_data(data)
    w.handleNewSignals()
    w.show()
    app.exec_()