git-svn-id: http://svn.tuebingen.mpg.de/ag-raetsch/projects/QPalma@8610 e1793c9e...
[qpalma.git] / qpalma / penalty_lookup_new.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import math
5 import pdb
6
7 def penalty_lookup_new(penalty_struct, value):
8
9 limits = penalty_struct.limits
10 penalties = penalty_struct.penalties
11
12 if penalty_struct.transform == 'log':
13 value = math.log(value)
14
15 elif penalty_struct.transform == 'log(+3)':
16 value = math.log(value+3)
17
18 elif penalty_struct.transform == 'log(+1)':
19 value = math.log(value+1)
20
21 elif penalty_struct.transform == '':
22 pass
23
24 elif penalty_struct.transform == '(+3)':
25 value += 3
26
27 elif penalty_struct.transform == 'mod3':
28 value = value%3
29
30 else:
31 assert False,'unknown penalty transform'
32
33 count = len([elem for elem in limits if elem <= value])
34
35 if count == 0:
36 pen = penalties[0]
37 elif count == len(limits):
38 pen=penalties[-1]
39 else:
40 pen = (penalties[count]*(value-limits[count-1]) + penalties[count-1]\
41 *(limits[count]-value)) / (limits[count]-limits[count-1])
42
43 return pen