-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearn_sample_t.py
345 lines (224 loc) · 8.67 KB
/
learn_sample_t.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
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
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib as mpl
mpl.use('tkagg')
import matplotlib.pyplot as plt
import os
import time
from load_data import load_data
theta = np.random.random() * 2 * np.pi
last_sample = 0
def gaussian(x, mu, sig):
return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
def concentration_func(x,y,t):
origin = np.array([4.5, 0.])
delx = origin[0] - x
dely = origin[1] - y
dist = np.sqrt(delx**2 + dely**2)
std = 4
return gaussian(dist, mu=0, sig=std)*100*0
def xdot(t, X, p):
global theta
global last_sample
global go_forward
plate_r = 3
AWC_v, AWC_f, AWC_s, AIB_v, AIA_v, AIY_v, x, y = X
AWC_f_a, AWC_f_b, AWC_s_gamma, tm, AWC_v0, AWC_gain, AIB_v0, AIA_v0, AIY_v0, speed, w_1, w_2, w_3, w_4, w_5, w_6, w_7, sampling_time = p
conc = concentration_func(x, y, t)
dAWC_f = AWC_f_a*conc - AWC_f_b*AWC_f
dAWC_s = AWC_s_gamma*(AWC_f - AWC_s)
AWC_i = AWC_f-AWC_s
dAWC_v = 1/tm *(-AWC_v + AWC_v0 + np.tanh(-AWC_gain*AWC_i)) # -ve in tanh because downstep in conc activates AWC
AIB_i = w_1*AWC_v + w_4*AIA_v
dAIB_v = 1/tm *(-AIB_v + AIB_v0 + np.tanh(AIB_i)) # removed gains as redundant with the weights
AIA_i = w_2 * AWC_v + w_5*AIB_v + w_6*AIY_v
dAIA_v = 1 / tm * (-AIA_v + AIA_v0 + np.tanh(AIA_i))
AIY_i = w_3 * AWC_v + w_7 * AIA_v
dAIY_v = 1 / tm * (-AIY_v + AIY_v0 + np.tanh(AIY_i))
#go_forward = (np.random.random()*2 - 1) < AIY_v
turn = (np.random.random() * 4 - 2) < (AIB_v - AIY_v)
if turn:
theta = np.random.random() * 2 * np.pi
dx = speed * np.cos(theta)
dy = speed * np.sin(theta)
# stop worms going off the plate by choosing another random direction, this stops them getting stuck on the edge
x, y = X[6], X[7]
while abs((x + dx) ** 2 + (y + dy) ** 2) ** 0.5 > plate_r:
theta = np.random.random() * 2 * np.pi
dx = speed * np.cos(theta)
dy = speed * np.sin(theta)
return dAWC_v, dAWC_f, dAWC_s, dAIB_v, dAIA_v, dAIY_v, dx, dy
def plot_sol(solution, save_path = None):
#plot neuron voltages
plt.figure()
plt.plot(solution[0, :], label = 'AWC')
plt.plot(solution[3, :], label = 'AIB')
plt.plot(solution[4, :], label = 'AIA')
plt.plot(solution[5, :], label = 'AIY')
plt.legend()
if save_path is not None:
plt.savefig(os.path.join(save_path, 'voltages.pdf'))
#plot sensory neuron components
plt.figure()
plt.plot(solution[1,:], label = 'AWC fast')
plt.plot(solution[2,:], label = 'AWC slow')
plt.legend()
if save_path is not None:
plt.savefig(os.path.join(save_path, 'sensory.pdf'))
# plot worm position
fig, ax = plt.subplots(figsize = [6.4,6.4])
# plate outline
circle = plt.Circle([0,0], plate_r, fill=False)
ax.add_patch(circle)
# scoring sectors
pos = [-origin, origin]
rad = [2.5, 3.5]
for p in pos:
for r in rad:
circle = plt.Circle(p, r, fill=False, color='gray')
ax.add_patch(circle)
ax.vlines(0, domain[0], domain[1], color='grey')
ax.plot(solution[6,:], solution[7,:])
ax.scatter(solution[6,0], solution[7,0], label = 'start')
ax.scatter(solution[6,-1], solution[7,-1], label = 'end')
plt.xlim(domain[0], domain[1])
plt.ylim(domain[0], domain[1])
plt.legend(loc = 'lower left')
if save_path is not None:
plt.savefig(os.path.join(save_path, 'worm.pdf'))
def plot_conc(domain):
plt.figure()
x = np.arange(domain[0], domain[1], 0.001)
y = np.arange(domain[0], domain[1], 0.001)
X, Y = np.meshgrid(x, y)
img = concentration_func(X,Y,0)
plt.imshow(img, extent=[domain[0], domain[1], domain[1], domain[0]])
plt.colorbar()
def score_worm(solution):
trajectory = solution[6:8, :]
x = trajectory[0, :]
y = trajectory[1, :]
delx = origin[0] - x
dely = origin[1] - y
origin_dist = np.sqrt(delx ** 2 + dely ** 2)
mirror_origin = -origin
delx = mirror_origin[0] - x
dely = mirror_origin[1] - y
mirror_origin_dist = np.sqrt(delx ** 2 + dely ** 2)
sectors = []
if np.any(mirror_origin_dist < 2.5):
sectors.append(-3)
if np.any(mirror_origin_dist < 3.5):
sectors.append(-2)
if np.any(origin_dist < 2.5):
sectors.append(3)
if np.any(origin_dist < 3.5):
sectors.append(2)
if np.any(x < 0):
sectors.append(-1)
if np.any(x > 0):
sectors.append(1)
return sectors
def run_experiment(params, n_worms):
global theta
global last_sample
sectors = []
dt = params[-1]
for i in range(n_worms):
theta = np.random.random() * 2 * np.pi
last_sample = 0
sol = forward_euler(y0, params, dt, t_span[-1])
sector = score_worm(sol)
sectors.append(sector)
return sectors
def get_fitnesses(population):
# values form the no cond no odor data
ms = np.mean(list(map(sum, no_cond_no_odour)))
ss = np.std(list(map(sum, no_cond_no_odour)))
mr = np.mean(list(map(lambda x: max(x) - min(x), no_cond_no_odour)))
sr = np.std(list(map(lambda x: max(x) - min(x), no_cond_no_odour)))
print('real', ms, ss, mr, sr)
fitnesses = []
parameters = [AWC_f_a, AWC_f_b, AWC_s_gamma, tm, AWC_v0, AWC_gain, AIB_v0, AIA_v0, AIY_v0,
speed, w_1, w_2, w_3, w_4, w_5, w_6, w_7]
all_sectors = []
for p in population:
params = parameters + list(p)
sectors= run_experiment(params, n_worms)
all_sectors.append(sectors)
mean_score = np.mean(list(map(sum, sectors)))
std_score = np.std(list(map(sum, sectors)))
mean_range = np.mean(list(map(lambda x: max(x) - min(x), sectors)))
std_range = np.std(list(map(lambda x: max(x) - min(x), sectors)))
fitness = - (abs(mean_score-ms) + abs(mean_range-mr) + abs(std_score-ss) + abs(std_range-sr))
print(p, mean_score, std_score, mean_range, std_range)
fitnesses.append(fitness)
print(p, fitness)
return fitnesses, all_sectors
def param_scan(start, stop, step, save_path = './working_dir/param_scan', plot=True):
os.makedirs(save_path, exist_ok = True)
population = np.arange(start, stop, step).reshape(-1, 1)
fitnesses, all_sectors = get_fitnesses(population)
if plot:
plt.plot(population, fitnesses)
np.save(save_path + '/params.npy',population)
np.save(save_path + '/fitnesses.npy',fitnesses)
np.save(save_path + '/sectors.npy', all_sectors)
if plot:
plt.show()
def forward_euler(y0, params, dt, tmax):
y = y0
all_ys = [y0]
for t in np.arange(0, tmax+dt, dt):
y = y + np.array(xdot(t, y, params))*dt
all_ys.append(y)
return np.array(all_ys).T
no_cond_no_odour, no_cond_odour, aversive_odour, sex_odour = load_data('./data/behaviourdatabysector_NT.csv')
n_gens = 100
pop_size = 100
n_worms = 100 # number of worms in each experiment
origin = np.array([4.5, 0.])
# starting params from gosh et al
tm = 0.5 #s
AIB_v0 = AIA_v0 = AIY_v0 = AWC_v0 = 0
AWC_gain = 2
AWC_f_a = 4 #1/s
AWC_f_b = 15 #1/s
AWC_s_gamma = 2 #1/s
speed = 0.11 #mm/s
domain = [-3,3]
plate_r = 3
w_2 = w_3 = w_4 = w_5 = -2 # -ve weights
w_1 = w_6 = w_7 = 2 # +ve weights
sample_time = 0.01 #s
parameters = [AWC_f_a, AWC_f_b, AWC_s_gamma, tm, AWC_v0, AWC_gain, AIB_v0, AIA_v0, AIY_v0,
speed, w_1, w_2, w_3, w_4, w_5, w_6, w_7, sample_time]
t_span = [0, 1200] #s
y0 = [0, 0, 0, 0, 0, 0, 0, 0]
#sol = solve_ivp(xdot, t_span, y0, t_eval = np.arange(t_span[-1]), args = (p,)).y
dt = 0.11
#sol = forward_euler(y0, parameters, dt, t_span[-1])
#plot_sol(sol)
#plt.show()
#sys.exit()
plt.violinplot(list(map(sum, no_cond_no_odour)))
plt.ylim(bottom=-6.1, top=6.1)
plt.figure()
plt.violinplot(list(map(lambda x: max(x) - min(x), no_cond_no_odour)))
print(len(no_cond_no_odour))
print(no_cond_no_odour)
print('score', np.mean(list(map(sum, no_cond_no_odour))), 'score std', np.std(list(map(sum, no_cond_no_odour))), 'range', np.mean(list(map(lambda x: max(x) - min(x), no_cond_no_odour))), 'range std', np.std(list(map(lambda x: max(x) - min(x), no_cond_no_odour))))
'''
no_cond_no_odour = run_experiment(parameters, 35)
print(no_cond_no_odour)
plt.figure()
plt.violinplot(list(map(sum, no_cond_no_odour)))
plt.ylim(bottom=-6.1, top=6.1)
plt.figure()
plt.violinplot(list(map(lambda x: max(x) - min(x), no_cond_no_odour)))
print(len(no_cond_no_odour))
print('score', np.mean(list(map(sum, no_cond_no_odour))), 'score std', np.std(list(map(sum, no_cond_no_odour))), 'range', np.mean(list(map(lambda x: max(x) - min(x), no_cond_no_odour))), 'range std', np.std(list(map(lambda x: max(x) - min(x), no_cond_no_odour))))
'''
#plt.show()
plt.close('all')
param_scan(0.06, 0.141, 0.01)