Repository navigation
Expand file tree
/
Copy pathsolver.py
More file actions
461 lines (396 loc) · 17.5 KB
/
Copy pathsolver.py
File metadata and controls
461 lines (396 loc) · 17.5 KB
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
import math
import pickle
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import random as rand
from numpy import random
from dFlipFlop import DFlipFlop
from acdc import ACDC
from repressilator import Repressilator
from sklearn import decomposition
from deap import creator, base, tools, algorithms
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
import matplotlib.ticker as ticker
import os.path
'''
Regions consist of cloud of points and principal component that govern the direction of exploration
'''
class Region:
def __init__(self, points, model, label, depth=1):
self.points = np.array(points)
self.model = model
self.pca = PCA(n_components=self.model.nParams)
self.components = None
self.prevComponents = None
self.cluster = False
self.terminated = False
self.iter = 0
self.maxIter = 10
self.threshold = 0.001
self.label = label
self.maxVarScale = 4
self.minVarScale = 2
self.varScaleDt = (self.maxVarScale - self.minVarScale)/(float(self.maxIter))
self.varScale = self.maxVarScale
self.depth = depth
def updateVariance(self):
self.varScale = self.varScale - self.varScaleDt
def updateIter(self):
self.iter = self.iter + 1
self.updateVariance()
def fitPCA(self):
self.prevComponents = self.components
self.pca.fit(self.points)
self.components = self.pca.components_
def transform(self, points):
return self.pca.transform(points)
def inverse_transform(self, points):
return self.pca.inverse_transform(points)
def converged(self):
if self.components is None or self.prevComponents is None:
return False
return np.linalg.norm(self.components - self.prevComponents) < self.threshold
def explored(self):
return self.terminated or self.iter > self.maxIter or self.converged()
'''
The main class
'''
class Solver:
def __init__(self, model, populationSize=5000, NGEN = 10, nsamples = 1e5):
self.model = model
self.populationSize = populationSize
self.NGEN = NGEN
self.nsamples = int(nsamples)
self.indpb = 0.75
#GA operators
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Candidate", list, fitness=creator.FitnessMax)
self.toolbox = base.Toolbox()
self.toolbox.register("candidate", self.generateCandidate)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.candidate)
self.toolbox.register("mate", tools.cxTwoPoint)
self.toolbox.register("mutate", self.mutateCandidate, indpb=self.indpb, mult=0.5)
self.toolbox.register("select", tools.selTournament, tournsize=int(self.populationSize/10))
#estimate initial values with GA
def findNominalValues(self):
nominalVals = []
for evalMode in self.model.modes:
nominalValsMode = []
self.toolbox.register("evaluate", evalMode)
#initialize new random population
self.popu = self.toolbox.population(self.populationSize)
for gen in range(self.NGEN):
print(gen)
#generate offspprings with crossover and mutations
offspring = algorithms.varAnd(self.popu, self.toolbox, cxpb=0.5, mutpb=0.75)
#evaluate individuals
fits = self.toolbox.map(self.toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
if self.model.isViable(ind) and ind not in nominalValsMode:
nominalValsMode.append(ind)
ind.fitness.values = fit
#roulete wheel selection
self.popu = self.toolbox.select(offspring, k=len(self.popu))
print("Number of viable points: " + str(len(nominalValsMode)))
nominalVals.extend(nominalValsMode)
return nominalVals
#creates an array of random candidates
def generateCandidate(self):
candidate = []
for ind in range(self.model.nParams):
candidate.append(random.uniform(self.model.parameter_values[self.model.params[ind]]["min"], self.model.parameter_values[self.model.params[ind]]["max"]))
return creator.Candidate(candidate)
def checkOutAllBounds(self, candidate):
for idx, val in enumerate(candidate):
if self.checkOutOfBounds(candidate, idx):
return True
return False
def checkOutOfBounds(self, candidate, idx):
#if out of bounds return True
if candidate[idx] < self.model.parameter_values[self.model.params[idx]]["min"] or candidate[idx] > self.model.parameter_values[self.model.params[idx]]["max"]:
return True
return False
#returns a tuple of mutated candidate
def mutateCandidate(self, candidate, indpb, mult):
for idx, val in enumerate(candidate):
rnd = random.uniform(0, 1)
if rnd >= indpb:
rnd2 = random.uniform(1 - mult, 1 + mult)
candidate[idx] = val*rnd2
if candidate[idx] < self.model.parameter_values[self.model.params[idx]]["min"]:
candidate[idx] = self.model.parameter_values[self.model.params[idx]]["min"]
if candidate[idx] > self.model.parameter_values[self.model.params[idx]]["max"]:
candidate[idx] = self.model.parameter_values[self.model.params[idx]]["max"]
return candidate,
def getViablePoints(self, points):
viable = list()
i = 0
for point in points:
i += 1
if i % 1000 == 0:
print(i)
#check if point is viable
if self.model.isViable(point):
viable.append(point)
return viable
# gap statistic method
# returns the optimal number of clusters
def gapStatistic(self, region, number_ref = 10, max_clusters = 2, plot = False):
#sample size is equal to the number of samples in gaussian sampling
sample_size = self.nsamples
subjects = np.array(region.points)
gaps = []
deviations = []
references = []
clusters_range = range(1, max_clusters + 1)
transformed = region.transform(subjects)
#get min and max parameter values in pca space
minP = np.min(transformed, axis=0)
maxP = np.max(transformed, axis=0)
for gap_clusters in clusters_range:
print(gap_clusters)
reference_inertia = []
for index in range(number_ref):
#OBB ... orientated bounding box
#random sampling within the PCA bounding box
reference = minP + random.rand(sample_size, self.model.nParams)*(maxP - minP)
reference = region.inverse_transform(reference)
kmeanModel = KMeans(gap_clusters)
kmeanModel.fit(reference)
reference_inertia.append(kmeanModel.inertia_)
kmeanModel = KMeans(gap_clusters)
kmeanModel.fit(subjects)
log_ref_inertia = np.log(reference_inertia)
#calculate gap
gap = np.mean(log_ref_inertia) - np.log(kmeanModel.inertia_)
sk = math.sqrt(1 + 1.0/number_ref)*np.std(log_ref_inertia)
gaps.append(gap)
deviations.append(sk)
# Plot the gaps
if plot:
plt.clf()
ax = plt.gca()
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
lines = plt.errorbar(clusters_range, gaps, ecolor='dodgerblue', yerr=deviations, fmt='-', color='dodgerblue')
plt.setp(lines[0], linewidth=1.5)
plt.ylabel('Gaps')
plt.show()
#return optimal number of clusters
for k in range(0, max_clusters - 1):
if gaps[k] >= gaps[k + 1] - deviations[k + 1]:
print("Optimal number of clusters: " + str(k + 1))
return k + 1
print("Optimal number of clusters: " + str(max_clusters))
return max_clusters
#returns the viable volume for
def getViableVolume(self, viableRegions, sample_size = int(1e4)):
volume = 0
for region in viableRegions:
regPoints = region.points
region.fitPCA()
transformed = region.transform(regPoints)
minP = np.min(transformed, axis=0)
maxP = np.max(transformed, axis=0)
dP = maxP - minP
volB = np.prod(dP)
mcRef = minP + random.rand(sample_size, self.model.nParams)*dP
mcRef = region.inverse_transform(mcRef)
viaPoints = self.getViablePoints(mcRef)
count = np.ma.size(viaPoints, axis=0)
#volume for region
ratio = count/sample_size
volume = volume + ratio*volB
print("Bounding box volume " + str(volB))
print("Volume " + str(volume))
print("Total volume " + str(self.model.getTotalVolume()))
print("Volume ratio:" + str(volume/self.model.getTotalVolume()))
return volume
def setBoxColors(self, bp, nRegions, ax, colors = ["#0E74C8", "#15A357", "r", "k"]):
colorLen = len(colors)
for i in range(nRegions):
col = colors[i % colorLen]
plt.setp(bp['boxes'][i], color=col, linewidth=1.5)
plt.setp(bp['caps'][2*i], color=col, linewidth=1.5)
plt.setp(bp['caps'][2*i + 1], color=col, linewidth=1.5)
plt.setp(bp['whiskers'][2*i], color=col, linewidth=1.5)
plt.setp(bp['whiskers'][2*i + 1], color=col, linewidth=1.5)
plt.setp(bp['fliers'][i], color=col)
plt.setp(bp['medians'][i], color=col, linewidth=1.5)
def plotParameterVariances(self, viableSets, names=None, units=None):
#go through all parameters
params = self.model.params
figure = plt.figure()
nRows = math.ceil(len(params)/3)
for pcount, param in enumerate(params):
ax1 = plt.subplot(nRows, 3, pcount+1)
#if names == None:
# ax1.set_title(str(param) + str(pcount))
#else:
# ax1.set_title(names[pcount])
if units != None:
plt.ylabel(names[pcount] + " " + units[pcount])
allRegions = []
#go through all regions
numSets = len(viableSets)
allNames = []
allBoxes = []
for count, reg in enumerate(viableSets):
points = np.array(reg.points)
data = points[:,pcount]
allRegions.append(data)
allNames.append("Region " + str(count + 1))
bp = ax1.boxplot(allRegions, positions=list(range(1, numSets + 1)), widths = 0.4)
self.setBoxColors(bp, numSets, ax1)
allBoxes = bp['boxes']
#draw legend
figure.legend(allBoxes, allNames, 'lower right')
plt.show()
#Main method
def run(self, filename, maxDepth=0):
#filename is a file to which viable sets will be serialized
#estimate the inital viable set
viablePoints = self.findNominalValues()
if not viablePoints:
print("No viable points found!")
return
#dump viable points to file
pickle.dump(viablePoints, open(filename + "ViableSet_IterGA.p", "wb+"))
reg = Region(viablePoints, self.model, "0")
reg.fitPCA()
fpca = PCA(n_components=2)
fpca.fit(reg.points)
viableSets = list()
viableSets.append(reg)
converged = False
iter = 0
while not converged:
converged = True
iter += 1
print("Iteration: " + str(iter))
for set in viableSets:
set.updateIter()
#if set not already explored
if not set.explored():
setSize = len(set.points)
print("Label: " + set.label)
print("Iter: " + str(set.iter))
print("Variance scaling factor: " + str(set.varScale))
converged = False
#sample with 0 mean and scaled variance of prinicpal components
candidateSet = random.multivariate_normal([0]*self.model.nParams, np.diag(set.pca.explained_variance_)*set.varScale, self.nsamples)
candidateSet = set.inverse_transform(candidateSet)
#check if parameter values are not out of range
inBounds = list()
for cand in candidateSet:
if not self.checkOutAllBounds(cand):
inBounds.append(cand)
inBounds = np.array(inBounds)
candidateSet = inBounds
X = fpca.transform(set.points)
Y = fpca.transform(candidateSet)
fig = plt.figure(iter)
plt.clf()
plt.scatter(Y[:, 0], Y[:, 1], c="red", alpha=0.1, edgecolor='k', rasterized=True)
plt.scatter(X[:, 0], X[:, 1], c="cornflowerblue", alpha=0.8, edgecolor='k', rasterized=True)
plt.xlabel('PC 1')
plt.ylabel('PC 2')
plt.savefig(filename + "Set" + set.label + "Iter" + str(set.iter) + ".pdf")
#identify viable points
viablePoints = np.array(self.getViablePoints(candidateSet))
#if viable set is smaller than number of parameters do not accept it
print("Number of viable points: " + str(len(viablePoints)))
if len(viablePoints) <= setSize/10:
#cluster if not enough points obtained with sampling
print("Clustering, insufficient number of points")
set.terminated = True
set.cluster = True
else:
pickle.dump(candidateSet, open(filename + "_Region" + str(set.label) + "CandidateSet_Iter" + str(set.iter) + ".p", "wb+"))
pickle.dump(viablePoints, open(filename + "_Region" + str(set.label) + "ViableSet_Iter" + str(set.iter) + ".p", "wb+"))
set.points = viablePoints
set.fitPCA()
#if set not already terminated, terminate it and cluster
elif not set.terminated:
set.terminated = True
set.cluster = True
#clustering, check for new clusters
newViableSets = list()
for set in viableSets:
if set.cluster and (maxDepth == 0 or set.depth < maxDepth):
set.cluster = False
setLabel = set.label
setDepth = set.depth
#determine the optimal number of clusters
print("Clustering set" + set.label)
k = self.gapStatistic(set)
if k > 1:
#cluster and divide sets based on clustering
#update the list of sets
converged = False
labels = KMeans(n_clusters = k).fit_predict(set.points)
for i in range(k):
ind = np.where(labels == i)[0]
points = set.points[ind]
reg = Region(points, self.model, setLabel + str(i), depth=setDepth+1)
reg.fitPCA()
#append new region to new set
newViableSets.append(reg)
#end of clustering
viableSets.extend(newViableSets)
#end of while loop
if __name__ == '__main__':
parameter_values = { "transcription": {"min": 0.01, "max": 50},
"translation": {"min": 0.01, "max": 50},
"protein_production": {"min": 0.1, "max": 50},
"rna_degradation": {"min": 0.1, "max": 100},
"protein_degradation": {"min": 0.001, "max": 50},
"hill": {"min": 1, "max": 5},
"Kd": {"min": 0.01, "max": 250},
"protease_concentration": {"min": 10, "max":1000}
}
#flip flop
filename = os.path.join(".", "DFlipFlop", "00", "flipflop")
print(filename)
model = DFlipFlop(parameter_values, np.array(["protein_production", "protein_production", "protein_production", "protein_production", "protein_degradation", "protein_degradation", "Kd","hill","Kd","protease_concentration"]), np.array([0.0 , 0.0, 100.0, 0.0]), omega1 = 0, omega2 = 0)
solver = Solver(model)
solver.run(filename, maxDepth=2)
#flip flop
filename = os.path.join(".", "DFlipFlop", "01", "flipflop")
print(filename)
model = DFlipFlop(parameter_values, np.array(["protein_production", "protein_production", "protein_production", "protein_production", "protein_degradation", "protein_degradation", "Kd","hill"]), np.array([0.0 , 0.0, 100.0, 0.0]), omega1 = 0, omega2 = 1)
solver = Solver(model)
solver.run(filename, maxDepth=2)
#flip flop
filename = os.path.join(".", "DFlipFlop", "10", "flipflop")
print(filename)
model = DFlipFlop(parameter_values, np.array(["protein_production", "protein_production", "protein_production", "protein_production", "protein_degradation", "protein_degradation", "Kd","hill","Kd","protease_concentration"]), np.array([0.0 , 0.0, 100.0, 0.0]), omega1 = 1, omega2 = 0)
solver = Solver(model)
solver.run(filename, maxDepth=2)
#flip flop
filename = os.path.join(".", "DFlipFlop", "11", "flipflop")
print(filename)
model = DFlipFlop(parameter_values, np.array(["protein_production", "protein_production", "protein_production", "protein_production", "protein_degradation", "protein_degradation", "Kd","hill"]), np.array([0.0 , 0.0, 100.0, 0.0]), omega1 = 1, omega2 = 1)
solver = Solver(model)
solver.run(filename,maxDepth=2)
#repressilator cost one
filename = os.path.join(".", "repressilatorCostOne", "exper0", "repres")
print(filename)
model = Repressilator(parameter_values, np.array(["transcription", "transcription", "hill", "translation", "rna_degradation", "protein_degradation", "Kd"]), np.array([0, 0, 10, 150, 0, 0]), mode=0)
solver = Solver(model)
solver.run(filename)
#repressilator cost two
filename = os.path.join(".", "repressilatorCostTwo", "exper0", "repres")
print(filename)
model = Repressilator(parameter_values, np.array(["transcription", "transcription", "hill", "translation", "rna_degradation", "protein_degradation", "Kd"]), np.array([0, 0, 10, 150, 0, 0]), mode=1)
solver = Solver(model)
solver.run(filename)
#ac-dc model
filename = os.path.join(".", "ACDC", "exper0", "acdc")
print(filename)
model = ACDC(parameter_values, np.array(["transcription", "hill", "translation", "rna_degradation", "protein_degradation", "Kd", "Kd", "Kd", "Kd"]), np.array([0, 37, 0, 280, 0, 280]))
solver = Solver(model)
solver.run(filename, maxDepth=2)