-
Notifications
You must be signed in to change notification settings - Fork 0
/
egfr.py
273 lines (239 loc) · 8.55 KB
/
egfr.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
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
def transform_gaussmf(x, param, val):
point = param[0]
y = fuzz.gaussmf(x, param[0], param[1])
idx = np.searchsorted(x, point, 'left')
if(idx < x.size - 1):
if(x[idx] == x[idx + 1]):
idx += 1
if(val == 0):
for i in xrange(idx):
y[i] = 0
else:
for i in xrange(idx + 1, x.size):
y[i] = 0
return y
def calculate_egfr_mfs(time, egf, egfr, egf_index, t):
size = egf.size
#egfr = np.linspace(0, egf[egf_index], size)
time_low = fuzz.zmf(time, 0, time[egf.size*t - t] - time[egf_index*t] + time[t] )
time_high = fuzz.smf(time, 0, time[egf.size*t - t] - time[egf_index*t] + time[t] )
egfr_low = fuzz.gaussmf(egfr, 0, egf[egf_index]/20)
egfr_high = transform_gaussmf(egfr, (egf[egf_index], egf[egf_index]/20), 1)
return ((time_low, time_high, egfr_low, egfr_high))
def calculate_raf_mfs( egfr, akt, raf):
egfr_low = fuzz.zmf(egfr, 0, 0.95)
egfr_high = fuzz.smf(egfr, 0.1, 0.9)
akt_low = fuzz.zmf(akt, 0, 0.95)
akt_high = fuzz.smf(akt, 0.1, 0.9)
raf_low = fuzz.gaussmf(raf, 0, egfr[egfr.size - 1]/20)
raf_high = fuzz.gaussmf(raf, egfr[egfr.size - 1], egfr[egfr.size - 1]/20)
return ((egfr_low, egfr_high), (akt_low, akt_high), (raf_low, raf_high))
def calculate_erk_mfs(raf, erk):
raf_low = fuzz.zmf(raf, 0, 0.95)
raf_high = fuzz.smf(erk, 0.1, 0.9)
erk_low = fuzz.gaussmf(erk, 0, raf[raf.size - 1]/20)
erk_high = fuzz.gaussmf(erk, raf[raf.size - 1], raf[raf.size - 1]/20)
return (raf_low, raf_high, erk_low, erk_high)
def calculate_pi3k_mfs(egfr, erk, pi3k):
egfr_low = fuzz.zmf(egfr, 0, 0.95)
egfr_high = fuzz.smf(egfr, 0.1, 0.9)
erk_low = fuzz.zmf(erk, 0, 0.9)
erk_high = fuzz.smf(erk, 0, 0.9)
pi3k_low = fuzz.gaussmf(pi3k, 0, egfr[egfr.size - 1]/20)
pi3k_high = fuzz.gaussmf(pi3k, egfr[egfr.size - 1], egfr[egfr.size - 1]/20)
return ((egfr_low, egfr_high), (erk_low, erk_high), (pi3k_low, pi3k_high))
def calculate_akt_mfs(pi3k, akt):
pi3k_low = fuzz.zmf(pi3k, 0, 0.95)
pi3k_high = fuzz.smf(pi3k, 0, 0.9)
akt_low = fuzz.gaussmf(akt, 0, akt[akt.size -1]/20)
akt_high = fuzz.gaussmf(akt, akt[akt.size -1], akt[akt.size -1]/20)
return ((pi3k_low, pi3k_high), (akt_low, akt_high))
def calculate_egfr(time_mfs, egfr_mfs, egfr, time_index):
a1 = time_mfs[0][time_index]
c1 = np.fmin(a1, egfr_mfs[0])
a2 = time_mfs[1][time_index]
c2 = np.fmin(a2, egfr_mfs[1])
c_com = np.fmax(c1, c2)
try:
egfr_val = fuzz.defuzz(egfr, c_com, 'centroid')
except AssertionError as e:
egfr_val = 0
return egfr_val
def calculate_raf(egfr_mfs, akt_mfs, raf_mfs, raf, egfr_index, akt_index):
a1 = egfr_mfs[1][egfr_index]
c1 = np.fmin(a1, raf_mfs[1])
a2 = akt_mfs[1][akt_index]
c2 = np.fmin(a2, raf_mfs[1])
c_com = np.fmax(c1, c2)
a3_1 = egfr_mfs[0][egfr_index]
a3_2 = akt_mfs[0][akt_index]
a3 = min(a3_1, a3_2)
c3 = np.fmin(a3, raf_mfs[0])
c_com = np.fmax(c_com, c3)
try:
raf_val = fuzz.defuzz(raf, c_com, 'centroid')
except AssertionError as e:
raf_val = 0
return raf_val
def calculate_erk(raf_mfs, erk_mfs, erk, raf_index):
a1 = raf_mfs[0][raf_index]
c1 = np.fmin(a1, erk_mfs[0])
a2 = raf_mfs[1][raf_index]
c2 = np.fmin(a2, erk_mfs[1])
c_com = np.fmax(c1, c2)
try:
erk_val = fuzz.defuzz(erk, c_com, 'centroid')
except AssertionError as e:
erk_val = 0
return erk_val
def calculate_pi3k(egfr_mfs, erk_mfs, pi3k_mfs, pi3k, egfr_index, erk_index):
a1 = egfr_mfs[0][egfr_index]
c1 = np.fmin(a1, pi3k_mfs[0])
a2 = erk_mfs[1][erk_index]
c2 = np.fmin(a2, pi3k_mfs[0])
c_com = np.fmax(c1, c2)
a3_1 = egfr_mfs[1][egfr_index]
a3_2 = erk_mfs[0][erk_index]
a3 = min(a3_1, a3_2)
c3 = np.fmin(a3, pi3k_mfs[1])
c_com = np.fmax(c_com, c3)
try:
pi3k_val = fuzz.defuzz(pi3k, c_com, 'centroid')
except AssertionError as e:
pi3k_val = 0
return pi3k_val
def calaculate_akt(pi3k_mfs, akt_mfs, akt, pi3k_index):
a1 = pi3k_mfs[0][pi3k_index]
c1 = np.fmin(a1, akt_mfs[0])
a2 = pi3k_mfs[1][pi3k_index]
c2 = np.fmin(a2, akt_mfs[1])
c_com = np.fmax(c1, c2)
try:
akt_val = fuzz.defuzz(akt, c_com, 'centroid')
except AssertionError as e:
akt_val = 0
return akt_val
def rules(present_values, initial_values, time_indexes, time_length):
y = np.copy(present_values)
egf_index = np.searchsorted(initial_values[0], present_values[0], 'left')
egfr_index = np.searchsorted(initial_values[2], present_values[2], 'left')
raf_index = np.searchsorted(initial_values[3], present_values[3], 'left')
erk_index = np.searchsorted(initial_values[4], present_values[4], 'left')
pi3k_index = np.searchsorted(initial_values[5], present_values[5], 'left')
akt_index = np.searchsorted(initial_values[6], present_values[6], 'left')
if(egf_index > 0):
egfr_mfs = calculate_egfr_mfs(initial_values[-1], initial_values[0], initial_values[2], egf_index, time_length[0])
raf_mfs = calculate_raf_mfs(initial_values[2], initial_values[6], initial_values[3])
pi3k_mfs = calculate_pi3k_mfs(initial_values[2], initial_values[4], initial_values[5])
erk_mfs = calculate_erk_mfs(initial_values[3], initial_values[4])
akt_mfs = calculate_akt_mfs(initial_values[5], initial_values[6])
if(present_values[0] == 0):
y[2] = 0
else:
y[2] = calculate_egfr((egfr_mfs[0], egfr_mfs[1]), (egfr_mfs[2], egfr_mfs[3]), initial_values[2], time_indexes[0])
if(present_values[2] == 0):
y[3] = 0
y[5] = 0
else:
y[3] = calculate_raf((raf_mfs[0][0], raf_mfs[0][1]), (raf_mfs[1][0], raf_mfs[1][1]), (raf_mfs[2][0], raf_mfs[2][1]), initial_values[3], egfr_index, akt_index)
y[5] = calculate_pi3k((pi3k_mfs[0][0], pi3k_mfs[0][1]), (pi3k_mfs[1][0], pi3k_mfs[1][1]), (pi3k_mfs[2][0], pi3k_mfs[2][1]), initial_values[5], egfr_index, erk_index)
if(present_values[3] == 0):
y[4] = 0
else:
y[4] = calculate_erk((erk_mfs[0], erk_mfs[1]), (erk_mfs[2], erk_mfs[3]), initial_values[4], raf_index)
if(present_values[5] == 0):
y[6] = 0
else:
y[6] = calaculate_akt((akt_mfs[0][0], akt_mfs[0][1]), (akt_mfs[1][0], akt_mfs[1][1]), initial_values[6], pi3k_index)
not_updated = []
for i in xrange(len(y)):
if y[i] == present_values[i]:
not_updated.append(i)
if 0 in not_updated:
time_indexes[0] += 1
else:
time_indexes[0] = 1
return y , time_indexes
def main():
y = np.array([0.9, 0, 0, 0, 0, 0, 0])
size = 100
time_length = [10, 1.0, 1.0]
time = np.linspace(0, time_length[0], size*time_length[0] + 1)
egf = np.linspace(0, 1, size + 1)
egfr = np.linspace(0, y[0], size + 1)
raf = egfr
pi3k = egfr
akt = egfr
erk = np.linspace(0, y[0], size + 1)
egfr_out = np.zeros(time.size)
time_indexes = [1, 1]
new_raf_out = np.zeros(time_length[0]*size + 1)
new_erk_out = np.zeros(time_length[0]*size + 1)
new_pi3k_out = np.zeros(time_length[0]*size + 1)
new_akt_out = np.zeros(time_length[0]*size + 1)
'''for i in xrange(1,size+1):
if i == 0:
for t in xrange(1, time.size + 1):
egfr_out[t] = 0
continue
egfr_mfs = calculate_egfr_mfs(time, egf, egfr, i, time_length)
#raf_mfs = calculate_raf_mfs(time_raf, egfr, raf, i, 1)
for t in xrange(1,time.size):
if t == 0:
egfr_out[t] = 0
raf_out[t] = 0
continue
egfr_out[t] = calculate_egfr((egfr_mfs[0], egfr_mfs[1]), (egfr_mfs[2], egfr_mfs[3]), egfr, t)
#raf_out[t] = calculate_egfr((raf_mfs[0], raf_mfs[1]), (raf_mfs[2], raf_mfs[3]), raf, t)
#time = time_raf
if i%10 == 0:
#pass
plt.plot(time, egfr_out)
plt.xlabel('Time')
plt.ylabel('egfr')
#time_length = 1
plt.axis([-0.01,time_length + .1,-0.01,1.1])
plt.show()'''
initial_vals = (egf, egf, egfr, raf, erk, pi3k, akt, time)
y.resize(1, y.size)
time_raf_delay = time_length[1]*size
time_erk_delay = time_length[2]*size
time_pi3k_delay = time_raf_delay
time_akt_delay = time_erk_delay
if((time_raf_delay%2 == 0 or (time_raf_delay + 1)%2 == 0) and time_erk_delay%2 == 0 or (time_erk_delay+1)%2 == 0):
for i in xrange(1, time.size):
temp, time_indexes = rules(y[i - 1], initial_vals, time_indexes, time_length)
y = np.vstack((y, temp))
new_raf_out[i] = y[i, 3]
new_erk_out[i] = y[i, 4]
new_pi3k_out[i] = y[i, 5]
new_akt_out[i] = y[i, 6]
if( i > time_raf_delay ):
y[i, 3] = new_raf_out[i - time_raf_delay]
else:
y[i, 3] = 0
if(i > time_pi3k_delay):
y[i ,5] = new_pi3k_out[i - time_pi3k_delay]
else:
y[i, 5] = 0
if( i > time_erk_delay):
y[i, 4] = new_erk_out[i - time_erk_delay]
else:
y[i, 4] = 0
if( i > time_akt_delay):
y[i, 6] = new_akt_out[i - time_akt_delay]
else:
y[i, 6] = 0
#print y[i]
plt.plot(time, y[:,2], time, y[:,5], time, y[:,4])
plt.xlabel('Time')
plt.ylabel('egfr')
plt.axis([-0.1,time_length[0] + 0.01, -0.01, 1.01])
plt.show()
else:
print "Not a valid time_raf_delay or time_erk_delay"
if(__name__ == '__main__'):
main()