+ using correct interface now
[qpalma.git] / python / penalty_lookup_new.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import math
5
6 def penalty_lookup_new(penalty_struct, value):
7
8 limits = penalty_struct.limits
9 penalties = penalty_struct.penalties
10
11 if penalty_struct.transform == 'log':
12 value = math.log(value)
13
14 elif penalty_struct.transform == 'log(+3)':
15 value = math.log(value+3)
16
17 elif penalty_struct.transform == 'log(+1)':
18 value = math.log(value+1)
19
20 elif penalty_struct.transform == '' or\
21 penalty_struct.transform == '(+3)':
22 value += 3
23
24 elif penalty_struct.transform == 'mod3':
25 value = value%3
26
27 else:
28 assert False,'unknown penalty transform'
29
30 count = len([elem for elem in limits if elem <= value])
31
32 if count == 0:
33 pen = penalties[0]
34 elif count == length(limits):
35 pen=penalties[-1]
36 else:
37 pen = (penalties[count+1]*(value-limits[count]) + penalties[count]\
38 *(limits[count+1]-value)) / (limits[count+1]-limits[count])