-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase_method.py
425 lines (268 loc) · 11.1 KB
/
base_method.py
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
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python2.5
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Written (W) 2009-2011 Christian Widmer
# Copyright (C) 2009-2011 Max-Planck-Society
"""
Created on 09.03.2009
@author: Christian Widmer
@summary: Defines an extensible method scaffold.
Here, we define the Method interface.
Basically, the different algorithms are hooked into
the experimental framework by extending this class.
"""
import copy
import numpy
import helper
from helper import Options
from expenv import Assessment, MultiAssessment
class BaseMethod(object):
"""
abstract baseclass for
domain adaptation methods
here, the method-specific train and test procedures are
implemented.
When creating a new method, it should sub-class this class.
"""
def __init__(self, param):
self.param = param
self.predictor = None
self.additional_information = {}
def train(self, instances):
"""
train predictor
"""
print "parameters:"
print self.param
print self.param.flags
self.predictor = self._train(instances, self.param)
return self.predictor
def evaluate(self, instances):
"""
Evaluates trained method on evaluation data using the trained predictor
"""
#make sure that the predictor was trained already
if (self.predictor == None):
raise Exception("predictor not trained, yet")
#separate labels & examples
examples = [inst.example for inst in instances]
labels = [inst.label for inst in instances]
#perform assessment
assessment = self._predict_and_assess(self.predictor, examples, labels)
print assessment
return assessment
def _train(self, train_data, param):
"""
training procedure using training examples and labels
@param train_data: instances
@type train_data: e.g. list<intances>
@param param: param for the training procedure
@type param: Parameter
"""
print "called abstract method, please override"
return 1
def _predict_and_assess(self, predictor, examples, labels, task_id):
"""
Computes predictor outputs and computes Assessment.
@param predictor: trained predictor
@type predictor: obj
@param examples: evaluation examples
@type examples: list
@param labels: evaluation labels
@type labels: list
@param task_id: task id
@type task_id: int
"""
#compute output
out = self._predict(predictor, examples, task_id)
#compute assessment
assessement = self._assess(out, labels)
return assessement
def _assess(self, out, lab):
"""
assessment only
@param out: predictor output
@type svm_out: list<float>
@param lab: labels
@type lab: list<float>
"""
auROC = helper.calcroc(out, lab)[0]
auPRC = helper.calcprc(out, lab)[0]
assessment = Assessment(auROC=auROC, auPRC=auPRC, pred=None, lab=None)
if self.param.flags.has_key("save_output") and self.param.flags["save_output"] == True:
assessment.save_output_and_labels(out, lab)
print assessment
return assessment
def _predict(self, predictor, examples, task_id):
"""
make prediction on examples using trained predictor
@param predictor: all information needed to predict
@type predictor: obj
@param examples: list of examples
@type examples: list
@param task_id: task id
@type task_id: str
"""
print "abstract method _predict, please override"
out = []
return out
def clear(self):
"""
reset predictor
"""
del self.predictor
def save_predictor(self, file_name):
"""
saves predictor to file system for later use
@param file_name: file name to save predictor
@type file_name: str
"""
print "saving predictor to", file_name
print self.predictor
try:
helper.save(file_name, self.predictor, "gzip")
except Exception, detail:
print "error writing predictor"
print detail
#########################################################
# Multi Stuff
class MultiMethod(BaseMethod):
"""
Method baseclass to deal with more than one data source
"""
def evaluate(self, eval_data, target_task=-1):
"""
Evaluates trained method on evaluation data using the trained SVM
@param eval_data: evaluation set containing examples and labels
@type eval_data: dict<str, list<instances> >
@param target_task: if set to -1, we consider average otherwise specific task
@type target_task: int
"""
#make sure that the predictor was trained already
if (self.predictor == None):
raise Exception("predictor not trained, yet")
#assessment for each task
multi_assessment = MultiAssessment()
# we use generator to efficiently iterate through items
for (task_id, instances) in eval_data:
#print "eval split_set:", instances[0].dataset.organism
#separate labels & examples
examples = [inst.example for inst in instances]
labels = [inst.label for inst in instances]
#import pdb; pdb.set_trace()
#perform assessment
assessment = self._predict_and_assess(self.predictor[task_id], examples, labels, task_id)
#attach task_id
assessment.task_id = task_id
multi_assessment.addAssessment(assessment)
#set top-level assessment values
if target_task == -1:
multi_assessment.compute_mean()
elif target_task >= 0:
multi_assessment.set_from_assessment(target_task)
return multi_assessment
class PreparedMultitaskData(Options):
'''
Class to hold information for prepared multitask data.
The main point is that it extracts examples, labels, task_names and task_nums
and provides them as separate variables.
Also, it is important that this is the place, where we provide
the mapping from task_names to task_ids.
Furthermore, it allows the shuffling of a data set on creation.
Once, created the instance is frozen.
'''
def __init__(self, instance_set, shuffle=False):
'''
@param instance_set: Mulittask data structure
@type instance_set: dict<str, list<instances> >
@param shuffle: boolean to indicate whether to shuffle dataset
@type shuffle: bool
'''
# create temp containers
examples = []
labels = []
task_vector_names = []
task_vector_nums = []
self.__name_to_id = {}
self.__id_to_name = {}
# sort by task_name
task_names = list(instance_set.keys())
task_names.sort()
# extract training data
for (task_id, task_name) in enumerate(task_names):
instances = instance_set[task_name]
print "train task name:", task_name
#assert(instances[0].dataset.organism==task_name)
examples.extend([inst.example for inst in instances])
labels.extend([inst.label for inst in instances])
task_vector_names.extend([str(task_name)]*len(instances))
task_vector_nums.extend([task_id]*len(instances))
# add mapping information
self.__name_to_id[task_name] = task_id
self.__id_to_name[task_id] = task_name
self.num_examples = len(examples)
# shuffle dataset if option is turned on
if shuffle:
# determine permutation
idx = numpy.random.permutation(range(self.num_examples))
# apply permutation to relevant vectors
examples = numpy.array(examples)[idx].tolist()
labels = numpy.array(labels)[idx].tolist()
task_vector_names = numpy.array(task_vector_names)[idx].tolist()
task_vector_nums = numpy.array(task_vector_nums)[idx].tolist()
# save permutation
self.permutation = idx
# sanity checks
assert(isinstance(examples, list))
assert(isinstance(labels, list))
assert(isinstance(task_vector_names, list))
assert(isinstance(task_vector_nums, list))
assert(self.num_examples == len(labels))
assert(self.num_examples == len(task_vector_names))
assert(self.num_examples == len(task_vector_nums))
for i in xrange(self.num_examples):
assert(type(task_vector_names[i])==str)
assert(type(task_vector_nums[i])==int)
# make sure we have the same keys (potentially in a different order)
sym_diff_keys_a = set(self.__name_to_id.keys()).symmetric_difference(set(self.__id_to_name.values()))
assert len(sym_diff_keys_a)==0, "symmetric difference between keys non-empty: " + str(sym_diff_keys_a)
sym_diff_keys_b = set(self.__name_to_id.values()).symmetric_difference(set(self.__id_to_name.keys()))
assert len(sym_diff_keys_b)==0, "symmetric difference between keys non-empty: " + str(sym_diff_keys_b)
self.examples = examples
self.labels = labels
self.task_vector_names = task_vector_names
self.task_vector_nums = task_vector_nums
# disallow changes to this instance
self.freeze()
def get_task_names(self):
'''
get list of task names
'''
return copy.copy(self.__name_to_id.keys())
def get_task_ids(self):
'''
get list of task names
'''
return copy.copy(self.__id_to_name.keys())
def id_to_name(self, idx):
'''
map task id to task name
@param idx: id to map
@type idx: int
'''
return self.__id_to_name[idx]
def name_to_id(self, name):
'''
map task name to assigned id
@param name: name to mpa
@type name: str
'''
return self.__name_to_id[name]
def get_num_tasks(self):
'''
get number of tasks
'''
return len(self.__name_to_id.keys())