-
Notifications
You must be signed in to change notification settings - Fork 3
/
dcd_shogun_factory.py
570 lines (362 loc) · 14.3 KB
/
dcd_shogun_factory.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/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) 2010-2011 Christian Widmer
# Copyright (C) 2010-2011 Max-Planck-Society
"""
module to create shogun data objects according to given parameters
"""
import shogun
from shogun.Classifier import SVMLight, LibLinear, L2R_LR, L2R_L2LOSS_SVC_DUAL, DomainAdaptationSVM, DomainAdaptationSVMLinear
from shogun.Kernel import WeightedDegreeStringKernel, LinearKernel, PolyKernel, GaussianKernel, CTaxonomy
from shogun.Kernel import CombinedKernel, WeightedDegreeRBFKernel
from shogun.Features import StringCharFeatures, RealFeatures, CombinedFeatures, StringWordFeatures
from shogun.Features import DNA, PROTEIN, Labels
from shogun.PreProc import SortWordString
from shogun.Kernel import WeightedDegreeStringKernel, CombinedKernel, WeightedCommWordStringKernel, WeightedDegreePositionStringKernel
from shogun.Features import StringCharFeatures, DNA, StringWordFeatures, CombinedFeatures
from shogun.Features import CombinedDotFeatures, HashedWDFeatures, HashedWDFeaturesTransposed, WDFeatures, ImplicitWeightedSpecFeatures, StringByteFeatures
from shogun.PreProc import SortWordString
import cPickle
import numpy
def set_svm_parameters(svm, param):
"""
set svm paramerers based on param object (same for all svms)
"""
lab = svm.get_labels()
# set cost
if param.flags["normalize_cost"]:
norm_c_pos = param.cost / float(len([l for l in lab.get_labels() if l==1]))
norm_c_neg = param.cost / float(len([l for l in lab.get_labels() if l==-1]))
svm.set_C(norm_c_neg, norm_c_pos)
else:
svm.set_C(param.cost, param.cost)
# set epsilon
if param.flags.has_key("epsilon"):
svm.set_epsilon(param.flags["epsilon"])
# show debugging output
if not param.flags.has_key("debug_off"):
svm.io.enable_progress()
svm.io.set_loglevel(shogun.Classifier.MSG_DEBUG)
# optimization settings
if param.flags.has_key("threads"):
svm.parallel.set_num_threads(param.flags["threads"])
# set bias
if param.flags.has_key("use_bias"):
svm.set_bias_enabled(param.flags["use_bias"])
return svm
def create_empty_kernel(param):
"""
kernel factory
@param param: parameter object
@type param: Parameter
@return subclass of shogun Kernel object
@rtype: Kernel
"""
kernel = None
if param.kernel == "WeightedDegreeStringKernel":
kernel = WeightedDegreeStringKernel(param.wdk_degree)
elif param.kernel == "LinearKernel":
kernel = LinearKernel()
elif param.kernel == "PolyKernel":
kernel = PolyKernel(10, 1, False)
elif param.kernel == "GaussianKernel":
kernel = GaussianKernel(10, param.sigma)
elif param.kernel == "WeightedDegreeRBFKernel":
size_cache = 50
nof_properties = 5 #20
sigma = param.transform
kernel = WeightedDegreeRBFKernel(size_cache, sigma, param.wdk_degree, nof_properties)
else:
raise Exception, "Unknown kernel type:" + param.kernel
if hasattr(param, "flags") and param.flags.has_key("cache_size"):
kernel.set_cache_size(param.flags["cache_size"])
if param.flags.has_key("debug"):
kernel.io.set_loglevel(shogun.Kernel.MSG_DEBUG)
return kernel
def create_kernel(examples, param):
"""
kernel factory
@param examples: list/array of examples
@type examples: list
@param param: parameter object
@type param: Parameter
@return subclass of shogun Kernel object
@rtype: Kernel
"""
# first create feature object of correct type
feat = create_features(examples, param)
kernel = None
if param.kernel == "WeightedDegreeStringKernel":
kernel = WeightedDegreeStringKernel(feat, feat, param.wdk_degree)
kernel.set_cache_size(200)
elif param.kernel == "LinearKernel":
kernel = LinearKernel(feat, feat)
elif param.kernel == "PolyKernel":
kernel = PolyKernel(feat, feat, 1, False)
elif param.kernel == "GaussianKernel":
kernel = GaussianKernel(feat, feat, param.sigma)
elif param.kernel == "WeightedDegreeRBFKernel":
size_cache = 200
nof_properties = 20
sigma = param.base_similarity
kernel = WeightedDegreeRBFKernel(feat, feat, sigma, param.wdk_degree, nof_properties, size_cache)
elif param.kernel == "Promoter":
kernel = create_promoter_kernel(examples, param.flags)
else:
raise Exception, "Unknown kernel type."
if hasattr(param, "flags") and param.flags.has_key("cache_size"):
kernel.set_cache_size(param.flags["cache_size"])
if param.flags.has_key("debug"):
kernel.io.set_loglevel(shogun.Kernel.MSG_DEBUG)
return kernel
def create_features(examples, param):
"""
factory for features
@param examples: list/array of examples
@type examples: list
@return subclass of shogun Features object
@rtype: Features
"""
assert(len(examples) > 0)
feat = None
#TODO: refactor
if param and param.flags.has_key("svm_type") and param.flags["svm_type"] == "liblineardual":
# create hashed promoter features
return create_hashed_promoter_features(examples, param.flags)
if param and param.kernel == "Promoter":
print "creating promoter features"
# create promoter features
return create_promoter_features(examples, param.flags)
#auto_detect string type
if type(examples[0]) == str:
# check what alphabet is used
longstr = ""
num_seqs = min(len(examples), 20)
for i in range(num_seqs):
longstr += examples[i]
if len(set([letter for letter in longstr]))>5:
feat = StringCharFeatures(PROTEIN)
if param and param.flags.has_key("debug"):
print "FEATURES: StringCharFeatures(PROTEIN)"
else:
feat = StringCharFeatures(DNA)
if param and param.flags.has_key("debug"):
print "FEATURES: StringCharFeatures(DNA)"
feat.set_features(examples)
else:
# assume real features
examples = numpy.array(examples, dtype=numpy.float64)
examples = numpy.transpose(examples)
feat = RealFeatures(examples)
if param and param.flags.has_key("debug"):
print "FEATURES: RealFeatures"
return feat
def create_labels(labels):
"""
create shogun labels
@param labels: list of labels
@type labels: list<float>
@return: labels in shogun format
@rtype: Labels
"""
lab = Labels(numpy.double(labels))
return lab
def get_spectrum_features(data, order=3, gap=0, reverse=True):
"""
create feature object used by spectrum kernel
"""
charfeat = StringCharFeatures(data, DNA)
feat = StringWordFeatures(charfeat.get_alphabet())
feat.obtain_from_char(charfeat, order-1, order, gap, reverse)
preproc = SortWordString()
preproc.init(feat)
feat.add_preprocessor(preproc)
feat.apply_preprocessor()
return feat
def get_wd_features(data, feat_type="dna"):
"""
create feature object for wdk
"""
if feat_type == "dna":
feat = StringCharFeatures(DNA)
elif feat_type == "protein":
feat = StringCharFeatures(PROTEIN)
else:
raise Exception("unknown feature type")
feat.set_features(data)
return feat
def create_combined_wd_features(instances, feat_type):
"""
creates a combined wd feature object
"""
num_features = len(instances[0])
# contruct combined features
feat = CombinedFeatures()
for idx in range(num_features):
# cut column idx
data = [instance[idx] for instance in instances]
seq_len = len(data[0])
for seq in data:
if len(seq) != seq_len:
print "warning, seq lengths differ", len(seq), seq_len, "in", idx, "num_feat", num_features
tmp_feat = get_wd_features(data, feat_type)
feat.append_feature_obj(tmp_feat)
return feat
def create_combined_spectrum_features(instances, feat_type):
"""
creates a combined spectrum feature object
"""
num_features = len(instances[0])
# contruct combined features
feat = CombinedFeatures()
for idx in range(num_features):
# cut column idx
data = [instance[idx] for instance in instances]
tmp_feat = get_spectrum_features(data, feat_type)
feat.append_features(tmp_feat)
return feat
def create_combined_wd_kernel(instances, param):
"""
creates a combined wd kernel object
"""
num_features = len(instances[0])
# contruct combined features
kernel = CombinedKernel()
for idx in range(num_features):
param.kernel = "WeightedDegreeStringKernel"
tmp_kernel = create_empty_kernel(param)
kernel.append_kernel(tmp_kernel)
combined_features = create_combined_wd_features(instances, feat_type="dna")
kernel.init(combined_features, combined_features)
return kernel
def create_empty_promoter_kernel(param):
"""
creates an uninitialized promoter kernel
@param param:
"""
# centered WDK/WDK-shift
if param["shifts"] == 0:
kernel_center = WeightedDegreeStringKernel(param["degree"])
else:
kernel_center = WeightedDegreePositionStringKernel(10, param["degree"])
shifts_vector = numpy.ones(param["center_offset"]*2, dtype=numpy.int32)*param["shifts"]
kernel_center.set_shifts(shifts_vector)
kernel_center.set_cache_size(param["kernel_cache"]/3)
# border spetrum kernels
size = param["kernel_cache"]/3
use_sign = False
kernel_left = WeightedCommWordStringKernel(size, use_sign)
kernel_right = WeightedCommWordStringKernel(size, use_sign)
# assemble combined kernel
kernel = CombinedKernel()
kernel.append_kernel(kernel_center)
kernel.append_kernel(kernel_left)
kernel.append_kernel(kernel_right)
return kernel
def create_promoter_kernel(examples, param):
"""
creates a promoter kernel
@param examples:
@param param:
"""
# create uninitialized kernel
kernel = create_empty_promoter_kernel(param)
# get features
feat = create_promoter_features(examples, param)
# init combined kernel
kernel.init(feat, feat)
return kernel
def create_promoter_features(data, param):
"""
creates promoter combined features
@param examples:
@param param:
"""
print "creating promoter features"
(center, left, right) = split_data_promoter(data, param["center_offset"], param["center_pos"])
# set up base features
feat_center = StringCharFeatures(DNA)
feat_center.set_features(center)
feat_left = get_spectrum_features(left)
feat_right = get_spectrum_features(right)
# construct combined features
feat = CombinedFeatures()
feat.append_feature_obj(feat_center)
feat.append_feature_obj(feat_left)
feat.append_feature_obj(feat_right)
return feat
def split_data_promoter(data, center_offset, center_pos):
'''
split promoter data in three parts
@param data:
'''
center = [seq[(center_pos - center_offset):(center_pos + center_offset)] for seq in data]
left = [seq[0:center_pos] for seq in data]
right = [seq[center_pos:] for seq in data]
#print left, center, right
return (center, left, right)
########################################################
# COFFIN stuff
########################################################
def create_hashed_promoter_features(data, param):
"""
creates a promoter feature object
"""
print "creating __hashed__ promoter features (for linear SVM)"
(center, left, right) = split_data_promoter(data, param["center_offset"], param["center_pos"])
# set up base features
feats_center = create_hashed_features_wdk(param, center)
feats_left = create_hashed_features_spectrum(param, left)
feats_right = create_hashed_features_spectrum(param, right)
# create combined features
feats = CombinedDotFeatures()
feats.append_feature_obj(feats_center)
feats.append_feature_obj(feats_left)
feats.append_feature_obj(feats_right)
return feats
def create_hashed_features_wdk(data, degree):
"""
creates hashed dot features for the wdk
"""
# fix parameters
start_degree = 0
hash_bits = 12
order = 1
gap = 0
reverse = True
dat = [str(xt) for xt in data]
# create raw features
feats_char = StringCharFeatures(dat, DNA)
feats_raw = StringByteFeatures(DNA)
feats_raw.obtain_from_char(feats_char, order-1, order, gap, reverse)
# finish up
feats = HashedWDFeaturesTransposed(feats_raw, start_degree, degree, degree, hash_bits)
#feats = HashedWDFeatures(feats_raw, start_degree, degree, degree, hash_bits)
#feats = WDFeatures(feats_raw, 1, 8)#, degree, hash_bits)
return feats
def create_hashed_features_spectrum(param, data):
"""
creates hashed dot features for the spectrum kernel
"""
# extract parameters
order = param["degree_spectrum"]
# fixed parameters
gap = 0
reverse = True
normalize = True
# create features
feats_char = StringCharFeatures(data, DNA)
feats_word = StringWordFeatures(feats_char.get_alphabet())
feats_word.obtain_from_char(feats_char, order-1, order, gap, reverse)
# create preproc
preproc = SortWordString()
preproc.init(feats_word)
feats_word.add_preproc(preproc)
feats_word.apply_preproc()
# finish
feats = ImplicitWeightedSpecFeatures(feats_word, normalize)
return feats