-
Notifications
You must be signed in to change notification settings - Fork 2
/
rasim.py
293 lines (280 loc) · 11.8 KB
/
rasim.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 5 21:58:07 2015
@author: Administrator
"""
from constants import FIBER_TOT_NUM, DT, FIBER_MECH_ID, COLOR_LIST
from simulation import SimFiber, level_num, stim_num, control_list
from fitlnp import stress2response
import numpy as np
import matplotlib.pyplot as plt
import pickle
factor_list = ['RaThick', 'RaInd']
rathick_thick_array = np.loadtxt(
'X:/YuxiangWang/AbaqusFolder/YoshiModel/csvs/simprop.csv',
delimiter=',').T[0]
rathick_ginf_array = np.loadtxt(
'X:/YuxiangWang/AbaqusFolder/YoshiModel/csvs/rathickg.csv',
delimiter=',').T[-1]
raind_ginf_array = np.loadtxt(
'X:/YuxiangWang/AbaqusFolder/YoshiModel/csvs/raindg.csv',
delimiter=',').T[-1]
class RaSim(SimFiber):
def __init__(self, factor, level, control):
self.factor = factor
self.level = level
self.control = control
self.get_dist()
self.load_traces()
self.predict_lnp()
def predict_lnp(self):
# Load lnp parameters
lnp_params = []
for i in range(FIBER_TOT_NUM):
lnp_params.append(np.loadtxt('./csvs/lnp_params_%d.csv' % i,
delimiter=','))
# Calculate firing rate
self.lnp_response = []
self.lnp_spikes = []
for fiber_id in range(FIBER_TOT_NUM):
lnp_response_fiber = []
lnp_spikes_fiber = []
lnp_params_fiber = lnp_params[fiber_id]
for stim_id in range(stim_num):
response = stress2response(lnp_params_fiber[:2],
lnp_params_fiber[2:],
self.traces[stim_id]['stress'],
self.traces[stim_id]['time'])
spikes = np.random.poisson(response * DT)
# Merge two spikes into one
spikes[spikes >= 1] = 1
lnp_response_fiber.append(response)
lnp_spikes_fiber.append(spikes)
self.lnp_response.append(lnp_response_fiber)
self.lnp_spikes.append(lnp_spikes_fiber)
if __name__ == '__main__':
run_fiber = False
if run_fiber:
raSimList = [[[] for j in range(level_num)]
for i in range(len(factor_list))]
for i, factor in enumerate(factor_list):
for level in range(level_num):
j = level
for k, control in enumerate(control_list):
raSim = RaSim(factor, level, control)
raSimList[i][j].append(raSim)
print(factor + str(level) + control + ' is done.')
with open('./pickles/raSimList.pkl', 'wb') as f:
pickle.dump(raSimList, f)
else:
with open('./pickles/raSimList.pkl', 'rb') as f:
raSimList = pickle.load(f)
# %% Plot viscoelasticity change effect, displ control
fiber_id = FIBER_MECH_ID
fig, axs = plt.subplots(3, 2, figsize=(7, 6))
for level in range(level_num):
color = COLOR_LIST[level]
label_thick = r'%d $\mu$m, $G_\infty$ = %.3f' % (
rathick_thick_array[level], rathick_ginf_array[level])
label_ind = r'%d $\mu$m, $G_\infty$ = %.3f' % (
rathick_thick_array[2], raind_ginf_array[level])
axs[0, 0].plot(
raSimList[0][level][0].traces[3]['time'],
raSimList[0][level][0].traces[3]['displ'] * 1e3,
'-k')
axs[0, 1].plot(
raSimList[1][level][0].traces[3]['time'],
raSimList[1][level][0].traces[3]['displ'] * 1e3,
'-k')
axs[1, 0].plot(
raSimList[0][level][0].traces[3]['time'],
raSimList[0][level][0].lnp_response[fiber_id][3],
'-', color=color, label=label_thick)
axs[1, 1].plot(
raSimList[1][level][0].traces[3]['time'],
raSimList[1][level][0].lnp_response[fiber_id][3],
'-', color=color, label=label_ind)
axs[2, 0].plot(
raSimList[0][level][0].traces[3]['time'],
raSimList[0][level][0].lnp_response[fiber_id][3] /
raSimList[0][level][0].lnp_response[fiber_id][3].max(),
'-', color=color, label=label_thick)
axs[2, 1].plot(
raSimList[1][level][0].traces[3]['time'],
raSimList[1][level][0].lnp_response[fiber_id][3] /
raSimList[1][level][0].lnp_response[fiber_id][3].max(),
'-', color=color, label=label_ind)
for axes in axs[-1]:
axes.set_xlabel('Time (s)')
for axes in axs.ravel():
axes.set_xlim(-0.25, 5.5)
axs[0, 0].set_ylabel('Displacement stimuli (mm)')
axs[0, 1].set_ylabel('Displacement stimuli (mm)')
axs[1, 0].set_ylabel('Expected response (Hz)')
axs[1, 1].set_ylabel('Expected response (Hz)')
axs[2, 0].set_ylabel('Normalized expected response')
axs[2, 1].set_ylabel('Normalized expected response')
axs[1, 0].legend(loc=1)
axs[1, 1].legend(loc=1)
axs[1, 1].set_ylim(0, 80)
fig.tight_layout()
for axes_id, axes in enumerate(axs.ravel()):
axes.text(-.15, 1.1, chr(65 + axes_id), transform=axes.transAxes,
fontsize=12, fontweight='bold', va='top')
fig.savefig('./plots/relaxadapt/visco_displ.png')
plt.close(fig)
# %% Plot viscoelasticity change effect, force control
fiber_id = FIBER_MECH_ID
fig, axs = plt.subplots(3, 2, figsize=(7, 6))
for level in range(level_num):
color = COLOR_LIST[level]
label_thick = r'%d $\mu$m, $G_\infty$ = %.3f' % (
rathick_thick_array[level], rathick_ginf_array[level])
label_ind = r'%d $\mu$m, $G_\infty$ = %.3f' % (
rathick_thick_array[2], raind_ginf_array[level])
axs[0, 0].plot(
raSimList[0][level][1].traces[3]['time'],
raSimList[0][level][1].traces[3]['force'] * 1e3,
'-k')
axs[0, 1].plot(
raSimList[1][level][1].traces[3]['time'],
raSimList[1][level][1].traces[3]['force'] * 1e3,
'-k')
axs[1, 0].plot(
raSimList[0][level][1].traces[3]['time'],
raSimList[0][level][1].lnp_response[fiber_id][3],
'-', color=color, label=label_thick)
axs[1, 1].plot(
raSimList[1][level][1].traces[3]['time'],
raSimList[1][level][1].lnp_response[fiber_id][3],
'-', color=color, label=label_ind)
axs[2, 0].plot(
raSimList[0][level][1].traces[3]['time'],
raSimList[0][level][1].lnp_response[fiber_id][3] /
raSimList[0][level][1].lnp_response[fiber_id][3].max(),
'-', color=color, label=label_thick)
axs[2, 1].plot(
raSimList[1][level][1].traces[3]['time'],
raSimList[1][level][1].lnp_response[fiber_id][3] /
raSimList[1][level][1].lnp_response[fiber_id][3].max(),
'-', color=color, label=label_ind)
for axes in axs[-1]:
axes.set_xlabel('Time (s)')
for axes in axs.ravel():
axes.set_xlim(-0.25, 5.5)
axs[0, 0].set_ylabel('Force stimuli (mN)')
axs[0, 1].set_ylabel('Force stimuli (mN)')
axs[1, 0].set_ylabel('Expected response (Hz)')
axs[1, 1].set_ylabel('Expected response (Hz)')
axs[2, 0].set_ylabel('Normalized expected response')
axs[2, 1].set_ylabel('Normalized expected response')
axs[1, 0].legend(loc=1)
axs[1, 1].legend(loc=1)
axs[0, 0].set_ylim(0, 5)
axs[0, 1].set_ylim(0, 5)
axs[1, 0].set_ylim(0, 80)
axs[1, 1].set_ylim(0, 80)
fig.tight_layout()
for axes_id, axes in enumerate(axs.ravel()):
axes.text(-.15, 1.1, chr(65 + axes_id), transform=axes.transAxes,
fontsize=12, fontweight='bold', va='top')
fig.savefig('./plots/relaxadapt/visco_force.png')
plt.close(fig)
# %% Plot viscoelasticity change effect
fiber_id = FIBER_MECH_ID
fig, axs = plt.subplots(2, 2, figsize=(7, 5))
for level in range(level_num):
label_thick = r'''%d $\mu$m
$G_\infty$=%.3f''' % (
rathick_thick_array[level], rathick_ginf_array[level])
label_ind = r'''%d $\mu$m
$G_\infty$=%.3f''' % (
rathick_thick_array[2], raind_ginf_array[level])
yoffset = - 2 * level + 2 * level_num - 2
axs[0, 0].plot(
raSimList[0][level][0].traces[3]['time'],
raSimList[0][level][0].lnp_spikes[fiber_id][3] + yoffset,
'-k')
axs[0, 0].text(-.1, yoffset, label_thick, ha='right', va='bottom',
fontsize=6)
axs[1, 0].plot(
raSimList[0][level][0].traces[3]['time'],
raSimList[0][level][0].lnp_response[fiber_id][3],
'-k')
yoffset = 2 * level
axs[0, 1].plot(
raSimList[1][level][0].traces[3]['time'],
raSimList[1][level][0].lnp_spikes[fiber_id][3] + yoffset,
'-k')
axs[0, 1].text(-.1, yoffset, label_ind, ha='right', va='bottom',
fontsize=6)
axs[1, 1].plot(
raSimList[1][level][0].traces[3]['time'],
raSimList[1][level][0].lnp_response[fiber_id][3],
'-k')
for axes in axs[0]:
axes.set_ylim(-1, 2 * level_num)
axes.axis('off')
for axes in axs[1]:
axes.set_xlabel('Time (s)')
for axes in axs.ravel():
axes.set_xlim(-.25, 5.5)
axs[1, 0].set_ylabel('Expected firing rate (Hz)')
axs[0, 0].set_title(r'Change in $G_\infty$ due to thickness variance')
axs[0, 1].set_title(r'Change in $G_\infty$ due to individual differences')
fig.suptitle('5-second ramp-and-hold displacement stimuli', fontsize=12)
fig.tight_layout()
fig.subplots_adjust(top=.9)
fig.savefig('./plots/relaxadapt/show_effect.png')
plt.close(fig)
# %% Plot effect of chaning stim
fiber_id = FIBER_MECH_ID
fig, axs = plt.subplots(3, 2, figsize=(7, 6))
for stim in range(1, stim_num):
color = COLOR_LIST[stim]
label = r'%d $\mu$m thickness, $G_\infty$ = %.3f' % (
rathick_thick_array[2], rathick_ginf_array[2])
axs[0, 0].plot(
raSimList[0][2][0].traces[stim]['time'],
raSimList[0][2][0].traces[stim]['displ'] * 1e3,
'-k')
axs[0, 1].plot(
raSimList[0][2][1].traces[stim]['time'],
raSimList[0][2][1].traces[stim]['force'] * 1e3,
'-k')
axs[1, 0].plot(
raSimList[0][2][0].traces[stim]['time'],
raSimList[0][2][0].lnp_response[fiber_id][stim],
'-k')
axs[1, 1].plot(
raSimList[0][2][1].traces[stim]['time'],
raSimList[0][2][1].lnp_response[fiber_id][stim],
'-k')
axs[2, 0].plot(
raSimList[0][2][0].traces[stim]['time'],
raSimList[0][2][0].lnp_response[fiber_id][stim] /
raSimList[0][2][0].lnp_response[fiber_id][stim].max(),
'-k')
axs[2, 1].plot(
raSimList[0][2][1].traces[stim]['time'],
raSimList[0][2][1].lnp_response[fiber_id][stim] /
raSimList[0][2][1].lnp_response[fiber_id][stim].max(),
'-k')
for axes in axs[-1]:
axes.set_xlabel('Time (s)')
for axes in axs.ravel():
axes.set_xlim(-0.25, 5.5)
axs[0, 0].set_ylabel('Displacement stimuli (mm)')
axs[0, 1].set_ylabel('Force stimuli (mN)')
axs[1, 0].set_ylabel('Expected response (Hz)')
axs[1, 1].set_ylabel('Expected response (Hz)')
axs[2, 0].set_ylabel('Normalized expected response')
axs[2, 1].set_ylabel('Normalized expected response')
for axes_id, axes in enumerate(axs.ravel()):
axes.text(-.15, 1.11, chr(65 + axes_id), transform=axes.transAxes,
fontsize=12, fontweight='bold', va='top')
fig.suptitle(label, fontsize=12)
fig.tight_layout()
fig.subplots_adjust(top=.92)
fig.savefig('./plots/relaxadapt/stim.png')
plt.close(fig)