-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
178 lines (111 loc) · 4.02 KB
/
analysis.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
# -*- coding: utf-8 -*-
"""
CCR analysis extension module
Kiri Choi (c) 2018
"""
import tellurium as te
import roadrunner
import numpy as np
from scipy import signal
from sklearn import neighbors
import plotting as pt
import networkx as nx
def getWeights(dist):
"""
"""
def ensembleFluxControlCoefficient(model_col):
"""
"""
r = te.loada(model_col[0])
F = np.empty((len(model_col), r.getNumReactions(), r.getNumReactions()))
for i in range(len(model_col)):
r = te.loada(model_col[i])
r.steadyState()
F[i] = r.getScaledFluxControlCoefficientMatrix()
return F
def ensemblePredictionMetric(realModel, model_col, predictionMetric=['SS', 'R', 'T', 'F']):
"""
"""
realSS, realR, realT, realF = testModelAnalysis(realModel)
if 'SS' in predictionMetric:
SS = ensembleSteadyState(model_col)
elif 'R' in predictionMetric:
R = ensembleReactionRates(model_col)
elif 'T' in predictionMetric:
T = ensembleTimeCourse(model_col)
elif 'F' in predictionMetric:
F = ensembleFluxControlCoefficient(model_col)
def ensembleReactionRates(model_col):
"""
"""
r = te.loada(model_col[0])
J = np.empty((len(model_col), r.getNumReactions()))
for i in range(len(model_col)):
r = te.loada(model_col[i])
r.steadyState()
J[i] = r.getReactionRates()
return J
def ensembleSteadyState(model_col):
"""
"""
r = te.loada(model_col[0])
SS = np.empty((len(model_col), r.getNumFloatingSpecies()))
for i in range(len(model_col)):
r = te.loada(model_col[i])
r.steadyState()
SS[i] = r.getFloatingSpeciesConcentrations()
return SS
def ensembleTimeCourse(model_col):
"""
"""
r = te.loada(model_col[0])
T = np.empty((len(model_col), 100, r.getNumFloatingSpecies()))
for i in range(len(model_col)):
r = te.loada(model_col[i])
T[i] = r.simulate(0, 100, 100)[:,1:]
return T
def isConnected(rl):
"""
Check if a reaction list is equivalent to a connected graph.
:param rl: reaction list
"""
G = nx.Graph()
for i in range(len(rl)):
for j in range(len(rl[i][3])):
G.add_edges_from([(rl[i][3][j], str(i))])
for k in range(len(rl[i][4])):
G.add_edges_from([(str(i), rl[i][4][k])])
for l in range(len(rl[i][5])):
G.add_edges_from([(rl[i][5][l], str(i))])
for m in range(len(rl[i][6])):
G.add_edges_from([(rl[i][6][m], str(i))])
return nx.is_connected(G)
def selectWithCutoff(model_top, dist_top, cutoff=0.1):
"""
Model selection routine that returns a list of models with distances within
the defined percentage.
:param model_top: list of models sorted according to corresponding distances
:param dist_top: list of sorted distances
:param cutoff: percentage to cutoff
"""
coind = int(len(model_top)*cutoff)
pt.plot_distance_histogram(dist_top, cutoff_val=dist_top[coind])
return model_top[:coind], dist_top[:coind]
def selectWithKernalDensity(model_top, dist_top):
"""
Model selection rountine that returns a list of models based on the output
of kernal density estimation.
:param model_top: list of models sorted according to corresponding distances
:param dist_top: list of sorted distances
"""
dist_top_reshape = dist_top.reshape((len(dist_top),1))
kde_xarr = np.linspace(0, np.max(dist_top), int(np.max(dist_top)*10))[:, np.newaxis]
kde = neighbors.KernelDensity(kernel='gaussian', bandwidth=0.3).fit(dist_top_reshape)
log_dens = kde.score_samples(kde_xarr)
minInd = signal.argrelextrema(log_dens, np.less)
if len(minInd[0]) == 0:
minInd = [[len(model_top)]]
return minInd[0], log_dens, kde_xarr.flatten()
def testModelAnalysis(realModel):
"""
"""