-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathratefit.py
277 lines (232 loc) · 8.84 KB
/
ratefit.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
import IMP
import IMP.isd
import math
import numpy
import random
import time, sys
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
barLength = 10 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
class ChemicalEquations(object):
'''
initial integrator to get the experimental data points
'''
def __init__(self,initial_conc_reagent,ks,initial_conc_reactants):
self.ks=ks
self.reagent=initial_conc_reagent
self.reactants=initial_conc_reactants.copy()
def do_integrate(self,time,nintegration):
dt=time/nintegration
for i in numpy.linspace(0,time,nintegration):
dreagent=-sum(self.ks)*self.reagent*dt
dreactants=[]
for n,k in enumerate(self.ks):
dreactants.append(k*self.reagent*dt)
self.reagent=self.reagent+dreagent
for n,k in enumerate(self.ks):
self.reactants[n]=self.reactants[n]+dreactants[n]
#print(self.reagent,self.reactants)
return self.reactants
def setupnuisance(m, initialvalue, minvalue, maxvalue, isoptimized=True):
'''
function to setup the free baysian parameters
'''
nuisance = IMP.isd.Scale.setup_particle(IMP.Particle(m), initialvalue)
if minvalue:
nuisance.set_lower(minvalue)
if maxvalue:
nuisance.set_upper(maxvalue)
nuisance.set_is_optimized(nuisance.get_nuisance_key(), isoptimized)
return nuisance
class TruncatedNormal(object):
'''
class that encodes the pdf of truncated normal distribution
'''
def __init__(self,a,b,mean):
self.a=a
self.b=b
self.mean=mean
def get_normal_pdf(self,sigma,x):
return 1.0/math.sqrt(2.0*math.pi)*math.exp(-0.5*((x-self.mean)/sigma)**2)
def phi(self,x):
return 0.5*(1.0+math.erf(x/math.sqrt(2.0)))
def get_normalization(self,sigma):
return sigma*(self.phi((self.b-self.mean)/sigma)-self.phi((self.a-self.mean)/sigma))
def get_pdf(self,sigma,x):
return self.get_normal_pdf(sigma,x)/self.get_normalization(sigma)
def get_log_pdf(self,sigma,x):
a=0.5*((x-self.mean)/sigma)**2
b=math.log(sigma)
c=math.log(self.phi((self.b-self.mean)/sigma)-self.phi((self.a-self.mean)/sigma))
return a+b+c
def test_pdf(self):
mean=0.1
sigma=0.1
a=0.0
b=1.0
npoints=10000
cumu=0.0
for n,x in enumerate(numpy.linspace(a,b,npoints)):
pdf=self.get_pdf(sigma,x)
if n>0: cumu+=pdf*(b-a)/npoints
#print(x,pdf,cumu)
class MarginalTruncatedNormal(object):
'''
Numerical marginalization of a Truncated Normal likelihood
'''
def __init__(self,TruncatedNormal,sigma,nintegration_points=200,nvalues=200):
self.tn=TruncatedNormal
a=self.tn.a
b=self.tn.b
smin=sigma.get_lower()
smax=sigma.get_upper()
sincr=(smax-smin)/nintegration_points
self.table={}
for x in numpy.linspace(a,b,nvalues):
acc = 0.
for s in numpy.linspace(smin,smax,nintegration_points):
acc+=self.tn.get_pdf(s,x)/s*sincr
self.table[x]=acc
def get_pdf(self,x):
return self.table.get(x, self.table[min(self.table.keys(), key=lambda k: abs(k - x))])
class ChemicalRateRestraint(IMP.Restraint):
import math
def __init__(self,m,kparticles,particleindex,sigma,initial_conc_reagent,final_conc_reactant,time):
'''
input
'''
IMP.Restraint.__init__(self, m, "ChemicalRateRestraint %1%")
self.kparticles=kparticles
self.particleindex=particleindex
self.initial_conc_reagent=initial_conc_reagent
self.final_conc_reactant=final_conc_reactant
self.sigma=sigma
self.time=time
self.particle_list=self.kparticles+[self.sigma]
self.tn=TruncatedNormal(0,self.initial_conc_reagent,self.final_conc_reactant)
def unprotected_evaluate(self,da):
ksum=sum([k.get_scale() for k in self.kparticles])
ki=self.kparticles[self.particleindex].get_scale()
forward_model=ki/ksum*self.initial_conc_reagent*(1.0-math.exp(-self.time*ksum))
#prob=self.tn.get_log_pdf(self.sigma.get_scale(),forward_model)
return self.tn.get_log_pdf(self.sigma.get_scale(),forward_model)
def do_get_inputs(self):
return [p.get_particle() for p in self.particle_list]
class VoidRestraint(IMP.Restraint):
def __init__(self,m):
'''
input
'''
IMP.Restraint.__init__(self, m, "VoidRestraint %1%")
def unprotected_evaluate(self,da):
return 0.0
def do_get_inputs(self):
return []
class MarginalChemicalRateRestraint(IMP.Restraint):
import math
def __init__(self,m,kparticles,particleindex,sigma,initial_conc_reagent,final_conc_reactant,time):
'''
input
'''
IMP.Restraint.__init__(self, m, "MarginalChemicalRateRestraint %1%")
self.kparticles=kparticles
self.particleindex=particleindex
self.initial_conc_reagent=initial_conc_reagent
self.final_conc_reactant=final_conc_reactant
self.sigma=sigma
self.time=time
self.particle_list=self.kparticles+[self.sigma]
self.tn=TruncatedNormal(0,self.initial_conc_reagent,self.final_conc_reactant)
self.mtn = MarginalTruncatedNormal(self.tn,self.sigma)
print("Done")
def unprotected_evaluate(self,da):
ksum=sum([k.get_scale() for k in self.kparticles])
ki=self.kparticles[self.particleindex].get_scale()
forward_model=ki/ksum*self.initial_conc_reagent*(1.0-math.exp(-self.time*ksum))
prob=self.mtn.get_pdf(forward_model)
return -self.math.log(prob)
def do_get_inputs(self):
return [p.get_particle() for p in self.particle_list]
class ConcentrationSumPrior(IMP.Restraint):
import math
def __init__(self, m, kparticles, tolerance, initial_conc_reagent, time):
IMP.Restraint.__init__(self, m, "ConcentrationSumPrior %1%")
self.kparticles=kparticles
self.initial_conc_reagent=initial_conc_reagent
self.tolerance=tolerance
self.time=time
self.particle_list=self.kparticles
def unprotected_evaluate(self,da):
ksum=sum([k.get_scale() for k in self.kparticles])
conc_sum=0.0
for kp in self.kparticles:
ki=kp.get_scale()
conc_sum+=ki/ksum*self.initial_conc_reagent*(1.0-math.exp(-self.time*ksum))
invtol2=1.0/self.tolerance/self.tolerance
prob=1.0/self.tolerance*math.exp(-0.5*invtol2*(conc_sum-self.initial_conc_reagent)**2)
return -self.math.log(prob)
def do_get_inputs(self):
return [p.get_particle() for p in self.particle_list]
def randomize_reacts(react_dict,delta=0.1,sparseness=0.3):
randomized={}
for c in react_dict:
tmp=[]
for r in react_dict[c]:
if random.random()<sparseness:
r=None
else:
r=r+random.uniform(-delta*r,delta*r)
tmp.append(r)
randomized[c]=tmp
return randomized
def test_rates(ks,i,initial_conc_reagent,time):
ki=ks[i]
ksum=sum(ks)
return ki / ksum * initial_conc_reagent * (1.0 - math.exp(-time * ksum))
def iterative_optimization(rates,sigma,scoring_function):
ks=rates
sf=scoring_function
bestscore=sf.evaluate(False)
npoints=200
nparticles=len(ks)+1
incr=0
for i,k in enumerate(ks):
bestkv=k.get_scale()
for kv in numpy.logspace(-3.0, +3.0, npoints, endpoint=True):
update_progress(float(incr)/npoints/nparticles)
incr+=1
k.set_scale(kv)
score=sf.evaluate(False)
if score<bestscore:
bestscore=score
bestkv=kv
k.set_scale(bestkv)
bestsigma=sigma.get_scale()
for sv in numpy.logspace(-3.0, +1.0, 200, endpoint=True):
update_progress(float(incr) / npoints / nparticles)
incr += 1
sigma.set_scale(sv)
score = sf.evaluate(False)
if score < bestscore:
bestscore = score
bestsigma = sv
sigma.set_scale(bestsigma)