+ fixed a bug in the C++ interface
[qpalma.git] / python / set_param_palma.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import math
5 import numpy.matlib
6 import QPalmaDP
7 import pdb
8
9 def linspace(a,b,n):
10 intervalLength = b-a
11 stepSize = 1.0*intervalLength / (n-1)
12
13 interval = [0]*n
14 interval[0] = a
15 interval[-1] = b
16 for i in range(1,n-1):
17 interval[i] = a+(i*stepSize)
18
19 return interval
20
21 def logspace(a,b,n):
22 interval = [0]*n
23 begin = 10.0**a
24 end = 10.0**b
25 intervalSize = 1.0*(b-a)/(n-1)
26 interval[0] = begin
27 interval[-1] = end
28
29 for i in range(1,n-1):
30 interval[i] = 10**(a+i*intervalSize)
31
32 return interval
33
34 def log10(x):
35 return math.log(x)/math.log(10)
36
37 class Plf: #means piecewise linear function
38
39 def __init_(self):
40 self.len = 0
41 self.limits = []
42 self.penalties = []
43 self.transform = ''
44 self.name = ''
45 self.max_len = 0
46 self.min_len = 0
47
48 def convert2SWIG(self):
49 ps = QPalmaDP.penalty_struct()
50
51 ps.len = len(self.limits)
52
53 ps.limits = QPalmaDP.createDoubleArrayFromList(self.limits)
54 ps.penalties = QPalmaDP.createDoubleArrayFromList(self.penalties)
55
56 ps.max_len = self.max_len
57 ps.min_len = self.min_len
58 ps.transform = 0
59 ps.name = self.name
60
61 return ps
62
63
64 def set_params_pa():
65 h = plf()
66 h.len = int(model.intron_len_bins)
67 h.limits = model.intron_len_limits
68 h.penalties = model.intron_len_penalties
69 h.name = 'h'
70 h.max_len = int(max_intron_len)
71 h.min_len = int(min_intron_len)
72 h.transform = model.intron_len_transform
73
74 d = plf()
75 d.len = int(model.donor_bins)
76 d.limits = model.donor_limits
77 d.penalties = model.donor_penalties
78 d.name = 'd'
79 d.max_len = 100
80 d.min_len = -100
81
82 a = plf()
83 a.len = int(model.acceptor_bins)
84 a.limits = model.acceptor_limits
85 a.penalties = model.acceptor_penalties
86 a.name = 'a'
87 a.max_len = 100
88 a.min_len = -100
89
90 mmatrix = model.substitution_matrix
91
92
93 def set_param_palma(param, train_with_intronlengthinformation,\
94 min_intron_len=None, max_intron_len=None, min_svm_score=None, max_svm_score=None):
95
96 print 'Setting parameters ...'
97
98 if min_intron_len == None:
99 if train_with_intronlengthinformation:
100 min_intron_len=20
101 max_intron_len=1000
102 else:
103 min_intron_len = 1
104 max_intron_len = 2
105
106 if min_intron_len != None and max_intron_len != None:
107 min_svm_score=-5
108 max_svm_score=5
109
110 h = Plf()
111 d = Plf()
112 a = Plf()
113
114 ####################
115 # Gapfunktion
116 ####################
117 h.limits = logspace(math.log(min_intron_len,10),math.log(max_intron_len,10),30)
118 h.penalties = param[0:30].flatten().tolist()[0]
119 #h.transform = '+1'
120 h.transform = ''
121 h.name = 'h'
122 h.max_len = 100000
123 h.min_len = 4
124 h.id = 1
125 h.use_svm = 0
126 h.next_id = 0
127
128
129 ####################
130 # Donorfunktion
131 ####################
132 d.limits = linspace(min_svm_score,max_svm_score,30)
133 d.penalties = param[30:60].flatten().tolist()[0]
134 #d.transform = '+1'
135 d.transform = ''
136 d.name = 'd'
137 d.max_len = 100
138 d.min_len = -100
139 d.id = 1
140 d.use_svm = 0
141 d.next_id = 0
142
143
144 ####################
145 # Acceptorfunktion
146 ####################
147 a.limits = linspace(min_svm_score,max_svm_score,30)
148 a.penalties = param[60:90].flatten().tolist()[0]
149 #a.transform = '+1'
150 a.transform = ''
151 a.name = 'a'
152 a.max_len = 100
153 a.min_len = -100
154 a.id = 1
155 a.use_svm = 0
156 a.next_id = 0
157
158
159 ####################
160 # Matchmatrix
161 ####################
162 mmatrix = numpy.matlib.mat(param[90:126])
163 mmatrix.reshape(6,6)
164
165 return h,d,a,mmatrix
166
167 if __name__ == '__main__':
168 #min_intron_len=20
169 #max_intron_len=1000
170 #print logspace(math.log(min_intron_len,10),math.log(max_intron_len,10),30)
171
172 min_svm_score=-5
173 max_svm_score=5
174 print linspace(min_svm_score,max_svm_score,30)