library.py 11.8 KB
Newer Older
Janez K's avatar
Janez K committed
1
2
3
4
5
6
7
8
9
def cforange_split_dataset(input_dict):
    import orange
    output_dict = {}
    data = input_dict['dataset']
    selection = orange.MakeRandomIndices2(data,float(input_dict['p']))
    train_data = data.select(selection,0)
    test_data = data.select(selection,1)
    output_dict['train_data']=train_data
    output_dict['test_data']=test_data
Janez K's avatar
Janez K committed
10
    return output_dict
Janez K's avatar
Janez K committed
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

def cforange_score_estimation(input_dict):
    import orange
    import orngFSS
    data = input_dict['dataset']
    ma = orngFSS.attMeasure(data,orange.MeasureAttribute_relief(k=int(input_dict['k']), m=int(input_dict['m'])))
    output_string = ""
    output_dict = {}
    output_dict['results'] = ma
    return output_dict

def cforange_best_natts(input_dict):
    import orange
    import orngFSS
    data = input_dict['dataset']
    scores = input_dict['scores']
    n = int(input_dict['n'])
    new_dataset = orngFSS.selectBestNAtts(data,scores,n)
    output_dict={}
    output_dict['new_dataset'] = new_dataset
    return output_dict

def cforange_atts_above_thresh(input_dict):
    import orange
    import orngFSS
    data = input_dict['dataset']
    scores = input_dict['scores']
    thresh = float(input_dict['thresh'])
    new_dataset = orngFSS.selectAttsAboveThresh(data,scores,thresh)
    output_dict={}
    output_dict['new_dataset'] = new_dataset
    return output_dict

def cforange_filter_relieff(input_dict):
    import orange
    import orngFSS
    data = input_dict['dataset']
    measure = orange.MeasureAttribute_relief(k=int(input_dict['k']), m=int(input_dict['m']))
    margin = float(input_dict['margin'])
    new_dataset = orngFSS.filterRelieff(data,measure,margin)
    output_dict = {}
    output_dict['new_dataset'] = new_dataset
Janez K's avatar
Janez K committed
53
54
    return output_dict   

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def cforange_multiple_cross_validation(input_dict):
    import orange, orngTest, orngStat
    learners = input_dict['learners']
    data = input_dict['dataset']
    folds = int(input_dict['folds'])
    results = orngTest.crossValidation(learners, data, folds=folds)
    output_dict = {}
    output_dict['results']=results
    return output_dict

def cforange_proportion_test(input_dict):
    import orange, orngTest, orngStat
    learners = input_dict['learners']
    data = input_dict['dataset']
    learnProp = float(input_dict['learnProp'])
    times = int(input_dict['times'])
    results = orngTest.proportionTest(learners, data, learnProp, times=times)
    output_dict = {}
    output_dict['results']=results
    return output_dict  

def cforange_leave_one_out(input_dict):
Janez K's avatar
Janez K committed
77
78
79
    import orange, orngTest, orngStat
    learners = input_dict['learners']
    data = input_dict['dataset']
80
81
82
83
84
85
86
87
88
    results = orngTest.leaveOneOut(learners, data)
    output_dict = {}
    output_dict['results']=results
    return output_dict       

def cforange_cross_validation(input_dict):
    import orange, orngTest, orngStat
    learners = [input_dict['learner']]
    data = input_dict['dataset']
Janez K's avatar
Janez K committed
89
90
91
92
    folds = int(input_dict['folds'])
    results = orngTest.crossValidation(learners, data, folds=folds)
    output_dict = {}
    output_dict['results']=results
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
    return output_dict

def cforange_classification_accuracy(input_dict):
    import orngStat
    results = input_dict['results']
    CAs = orngStat.CA(results)
    if len(CAs)==1:
        CAs = CAs[0]
    output_dict = {}
    output_dict['ca']=CAs
    return output_dict

def cforange_classification_accuracy(input_dict):
    import orngStat
    results = input_dict['results']
    if input_dict['reportSE']=='true':
        reportSE = True
    else:
        reportSE = False
    CAs = orngStat.CA(results,reportSE=reportSE)
    if len(CAs)==1:
        CAs = CAs[0]
    output_dict = {}
    output_dict['ca']=CAs
    return output_dict 

def cforange_average_probability(input_dict):
    import orngStat
    results = input_dict['results']
    if input_dict['reportSE']=='true':
        reportSE = True
    else:
        reportSE = False
    APs = orngStat.AP(results,reportSE=reportSE)
    if len(APs)==1:
        APs = APs[0]
    output_dict = {}
    output_dict['ap']=APs
    return output_dict

def cforange_brier_score(input_dict):
    import orngStat
    results = input_dict['results']
    if input_dict['reportSE']=='true':
        reportSE = True
    else:
        reportSE = False
    BSs = orngStat.BrierScore(results,reportSE=reportSE)
    if len(BSs)==1:
        BSs = BSs[0]
    output_dict = {}
    output_dict['bs']=BSs
    return output_dict 

def cforange_information_score(input_dict):
    import orngStat
    results = input_dict['results']
    if input_dict['reportSE']=='true':
        reportSE = True
    else:
        reportSE = False
    ISs = orngStat.IS(results,apriori=None,reportSE=reportSE)
    if len(ISs)==1:
        ISs = ISs[0]
    output_dict = {}
    output_dict['is']=ISs
    return output_dict

def cforange_confusion_matrix(input_dict):
    import orngStat
    results = input_dict['results']
    classIndex = int(input_dict['classIndex'])
    if input_dict['cutoff']!='':
        cutoff = float(input_dict['cutoff'])
        cm = orngStat.confusionMatrices(results,classIndex=classIndex,cutoff=cutoff)
    else:
        cm = orngStat.confusionMatrices(results,classIndex=classIndex)
    if len(cm)==1:
        cm = cm[0]
    print cm
    output_dict = {}
    output_dict['cm']=cm
    return output_dict

def cforange_confusion_matrix_computations(input_dict):
    import orngStat
    cm = input_dict['cm']
    alpha = float(input_dict['alpha'])
    output_dict = {}
    output_dict['sens']=orngStat.sens(cm)
    output_dict['spec']=orngStat.spec(cm)
    output_dict['PPV']=orngStat.PPV(cm)
    output_dict['NPV']=orngStat.NPV(cm)
    output_dict['precision']=orngStat.precision(cm)
    output_dict['recall']=orngStat.recall(cm)
    output_dict['F1']=orngStat.F1(cm)
    output_dict['Falpha']=orngStat.Falpha(cm,alpha=alpha)
    output_dict['MCC']=orngStat.MCC(cm)
    return output_dict  

def cforange_auc(input_dict):
    import orngStat
    results = input_dict['results']
    method = int(input_dict['method'])
    auc = orngStat.AUC(results,method)
    output_dict = {}
    output_dict['AUC']=auc
Janez K's avatar
Janez K committed
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
    return output_dict  

def cforange_MSE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.MSE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['MSE']=errors
    return output_dict

def cforange_RMSE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.RMSE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['RMSE']=errors
    return output_dict    

def cforange_MAE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.MAE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['MAE']=errors
    return output_dict

def cforange_RSE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.RSE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['RSE']=errors
    return output_dict    

def cforange_RRSE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.RRSE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['RRSE']=errors
    return output_dict    

def cforange_RAE(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.RAE(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['RAE']=errors
    return output_dict    

def cforange_R2(input_dict):
    import orngStat
    results = input_dict['results']
    errors = orngStat.R2(results)
    if len(errors)==1:
        errors = errors[0]
    output_dict = {}
    output_dict['R2']=errors
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    return output_dict        

def cforange_prepare_results(input_dict):
    output_dict = {}
    learners = input_dict['learners']
    newlist = []
    for i in range(0,len(learners)):
        newdict = {}
        newdict['name']=str(learners[i])
        newdict['fbeta']=0.5
        newdict['precision']=input_dict['precision'][i]
        newdict['recall']=input_dict['recall'][i]
        newdict['fscore']=input_dict['f'][i]
        newlist.append(newdict)
    output_dict['alp']=newlist
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
    return output_dict

def cforange_example_distance(input_dict):
    import orange
    import random
    import orngClustering
    import orngMisc
    inputdata = input_dict['dataset']
    metricsIndex = int(input_dict['distanceMetrics'])
    metrics = [
        ("Euclidean", orange.ExamplesDistanceConstructor_Euclidean),
        ("Pearson Correlation", orngClustering.ExamplesDistanceConstructor_PearsonR),
        ("Spearman Rank Correlation", orngClustering.ExamplesDistanceConstructor_SpearmanR),
        ("Manhattan", orange.ExamplesDistanceConstructor_Manhattan),
        ("Hamming", orange.ExamplesDistanceConstructor_Hamming),
        ("Relief", orange.ExamplesDistanceConstructor_Relief),
        ]

    normalize = input_dict['normalization']
    if normalize=='true':
        normalize = True
    else:
        normalize = False

    data = inputdata
    constructor = metrics[metricsIndex][1]()
    constructor.normalize = normalize
    dist = constructor(data)
    matrix = orange.SymMatrix(len(data))
    matrix.setattr('items', data)
    for i in range(len(data)):
        for j in range(i+1):
            matrix[i, j] = dist(data[i], data[j])
    output_dict = {}
    output_dict['dm']=matrix
    return output_dict

def cforange_attribute_distance(input_dict):
    import orange
    import orngInteract
    inputdata = input_dict['dataset']
    discretizedData = None
    classInteractions = int(input_dict['classInteractions'])
    atts = inputdata.domain.attributes
    if len(atts) < 2:
        return None
    matrix = orange.SymMatrix(len(atts))
    matrix.setattr('items', atts)
    if classInteractions < 3:
        if inputdata.domain.hasContinuousAttributes():
            if discretizedData is None:
                try:
                    discretizedData = orange.Preprocessor_discretize(inputdata, method=orange.EquiNDiscretization(numberOfIntervals=4))
                except orange.KernelException, ex:
                    return None
            data = discretizedData
        else:
            data = inputdata

        # This is ugly (no shit)
        if not data.domain.classVar:
            if classInteractions == 0:
                classedDomain = orange.Domain(data.domain.attributes, orange.EnumVariable("foo", values=["0", "1"]))
                data = orange.ExampleTable(classedDomain, data)
            else:
                return None

        im = orngInteract.InteractionMatrix(data, dependencies_too=1)
        off = 1
        if classInteractions == 0:
            diss,labels = im.exportChi2Matrix()
            off = 0
        elif classInteractions == 1:
            (diss,labels) = im.depExportDissimilarityMatrix(jaccard=1)  # 2-interactions
        else:
            (diss,labels) = im.exportDissimilarityMatrix(jaccard=1)  # 3-interactions

        for i in range(len(atts)-off):
            for j in range(i+1):
                matrix[i+off, j] = diss[i][j]

    else:
        if classInteractions == 3:
            for a1 in range(len(atts)):
                for a2 in range(a1):
                    matrix[a1, a2] = (1.0 - orange.PearsonCorrelation(a1, a2, inputdata, 0).r) / 2.0
        else:
            if len(inputdata) < 3:
                return None
            import numpy, statc
            m = inputdata.toNumpyMA("A")[0]
            averages = numpy.ma.average(m, axis=0)
            filleds = [list(numpy.ma.filled(m[:,i], averages[i])) for i in range(len(atts))]
            for a1, f1 in enumerate(filleds):
                for a2 in range(a1):
                    matrix[a1, a2] = (1.0 - statc.spearmanr(f1, filleds[a2])[0]) / 2.0
    output_dict = {}
    output_dict['dm']=matrix        
383
    return output_dict