-
Notifications
You must be signed in to change notification settings - Fork 2
/
hmstss.py
353 lines (334 loc) · 14.9 KB
/
hmstss.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
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 11 13:50:27 2015
@author: Administrator
"""
from constants import (FIBER_HMSTSS_ID, FIBER_RCV, MS)
from simulation import SimFiber, stim_num
from fitlif import LifModel
import copy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pickle
import os
fiber_hmstss_use = FIBER_HMSTSS_ID
fiber_id = fiber_hmstss_use
level_num = 10
class HmstssFiber(SimFiber):
def __init__(self, level):
self.factor = 'Hmstss'
self.control = 'Force'
self.level = level
self.get_dist()
self.load_traces()
self.load_trans_params()
self.get_predicted_fr()
def get_predicted_fr(self, trans_params=None):
"""
Returns the predicted fr based on the instance's stress/strain/sener.
1) If `trans_params is None`:
Perform calculation on the instance's current `self.trans_params`
and save data to `self.predicted_fr`;
2) Otherwise:
Sepcify `trans_params` and return the `predicted_fr` w/o
updating the instance's properties.
Note: do `copy.deepcopy()` before modifying the old `trans_params`!
"""
# Determine whether a static call or not
update_instance = False
if trans_params is None:
update_instance = True
trans_params = self.trans_params
# Calculate predicted fr
quantity = 'stress'
# Get the quantity_dict_list for input
quantity_dict_list = [{
'quantity_array': self.traces[i][quantity],
'max_index': self.traces[i]['max_index']}
for i in range(stim_num)]
# Calculate
lifModel = LifModel(**FIBER_RCV[fiber_id])
predicted_fr =\
lifModel.trans_param_to_predicted_fr(
quantity_dict_list, trans_params[fiber_id][quantity])
# Update instance if needed
if update_instance:
self.predicted_fr = predicted_fr
return predicted_fr
def load_fiber():
fname = './pickles/hmstss/hmstss.pkl'
already_exist = os.path.isfile(fname)
if already_exist:
with open(fname, 'rb') as f:
hmstssFiberList = pickle.load(f)
else:
hmstssFiberList = []
for level in range(level_num):
hmstssFiberList.append(HmstssFiber(level))
with open(fname, 'wb') as f:
pickle.dump(hmstssFiberList, f)
return hmstssFiberList
if __name__ == '__main__':
hmstssFiberList = load_fiber()
# %% Some local constants
thickness_array = np.linspace(125, 433, level_num)
median_dict = {'resting': 3, 'active': 6}
resting_grouping_list = [[8, 5, 3, 1], [11, 7, 2], [6, 4, 2], [13, 5]]
active_grouping_list_list = [
[[9, 8, 5, 2, 2], [11, 6, 5, 2, 1, 1], [10, 8, 4, 3, 1]],
[[13, 9, 6, 2], [14, 8, 6, 2], [15, 8, 3, 2, 2]],
[[7, 5, 3, 2, 2], [8, 6, 4, 1], [7, 5, 4, 3]],
[[15, 8, 3, 2], [16, 9, 3], [14, 7, 4, 3]]]
grouping_table = np.empty((len(resting_grouping_list),
len(active_grouping_list_list[0]) + 1),
dtype='object')
for i in range(grouping_table.shape[0]):
grouping_table[i, 0] = resting_grouping_list[i]
for j in range(grouping_table.shape[1] - 1):
grouping_table[i, j + 1] = active_grouping_list_list[i][j]
grouping_df = pd.DataFrame(grouping_table,
index=['Fiber #%d' % (i + 1) for i in range(4)],
columns=['Resting'] + ['Active'] * 3)
grouping_df.to_csv('./csvs/grouping.csv')
def validate_grouping():
"""
Validate number of MC counts.
"""
for row in grouping_df.iterrows():
for group in row[1]:
print(sum(group))
base_grouping = resting_grouping_list[0]
stimuli = hmstssFiberList[0].static_force_exp
# %% Function definitions
def get_response_from_hc_grouping(
grouping, skinlevel, base_grouping=base_grouping):
quantity = 'stress'
fiber_id = fiber_hmstss_use
fname = './pickles/hmstss/hmstss%d_%d%d.pkl' % (
skinlevel, base_grouping[0], grouping[0])
already_exist = os.path.isfile(fname)
if already_exist:
with open(fname, 'rb') as f:
response = pickle.load(f)
else:
hmstssFiber = hmstssFiberList[skinlevel]
trans_params_fit = hmstssFiber.trans_params
dr = grouping[0] / base_grouping[0]
trans_params = copy.deepcopy(trans_params_fit)
trans_params[fiber_id][quantity][0] *= dr
trans_params[fiber_id][quantity][1] *= dr
if dr == 1:
response = hmstssFiber.predicted_fr.T[1]
else:
response = hmstssFiber.get_predicted_fr(trans_params).T[1]
# Deal with data saving
with open(fname, 'wb') as f:
pickle.dump(response, f)
return response
def get_sts(response, st=False):
if st is False:
start_idx = 0
else:
start_idx = response.nonzero()[0][0]
sts = np.polyfit(stimuli[start_idx:], response[start_idx:], 1)[0]
return sts
def get_sts_change(base_response, response):
return get_sts(response) - get_sts(base_response)
def get_percent(base_response, response, overall=False):
if overall: # Calculate overall change
percent = (response.sum() / base_response.sum() - 1)
else:
percent = (response[-1] / base_response[-1]) - 1
return percent
def plot_phase(grouping, skinlevel, axes,
fiber_id=fiber_hmstss_use, **kwargs):
response = get_response_from_hc_grouping(grouping, skinlevel)
axes.plot(stimuli, response, **kwargs)
return axes
# %% Table for first computational experiment, change skin thickness
t42 = np.empty((level_num, len(resting_grouping_list)))
t43 = np.empty((level_num, len(resting_grouping_list)))
for grouping_id, base_grouping in enumerate(resting_grouping_list):
base_response = get_response_from_hc_grouping(base_grouping, 0)
for skinlevel in range(level_num):
response = get_response_from_hc_grouping(base_grouping, skinlevel)
t42[skinlevel, grouping_id] = get_percent(base_response, response)
t43[skinlevel, grouping_id] = get_sts(response)
# Get column and row labels
columns = [str(grouping) for grouping in resting_grouping_list]
rows = ['%d μm' % thickness for thickness in thickness_array]
t42_df = pd.DataFrame(t42, columns=columns, index=rows)
t43_df = pd.DataFrame(t43, columns=columns, index=rows)
t42_df.to_csv('./csvs/t42.csv')
t43_df.to_csv('./csvs/t43.csv')
# %% Figure for first computational experiment, change skin thickness
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
for grouping_id, base_grouping in enumerate(resting_grouping_list):
plot_phase(base_grouping, 0, axs.ravel()[grouping_id],
ls='-', marker='s', color='k', ms=MS,
label='%d μm' % thickness_array[0])
plot_phase(base_grouping, -1, axs.ravel()[grouping_id],
ls='-', marker='o', color='k', ms=MS,
label='%d μm' % thickness_array[-1])
axs.ravel()[grouping_id].set_title(
'Organ = %s' % str(base_grouping))
axs[0, 0].legend(loc=2)
for axes in axs[-1]:
axes.set_xlabel('Force (mN)')
for axes in axs.T[0]:
axes.set_ylabel('Static firing (Hz)')
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.tight_layout()
fig.savefig('./plots/lesniak_f41.png')
plt.close(fig)
# %% Table for third computational experiment, both skin and neuron changes
t46 = np.empty((len(resting_grouping_list), 2))
t47 = np.empty((len(resting_grouping_list), 2))
columns = ['Skin constant', 'Skin changes']
rows = []
for grouping_id, base_grouping in enumerate(resting_grouping_list):
active_grouping = active_grouping_list_list[grouping_id][0]
base_response = get_response_from_hc_grouping(
base_grouping, median_dict['resting'])
response_skin_constant = get_response_from_hc_grouping(
active_grouping, median_dict['resting'])
response_skin_changes = get_response_from_hc_grouping(
active_grouping, median_dict['active'])
t46[grouping_id, 0] = get_percent(
base_response, response_skin_constant)
t46[grouping_id, 1] = get_percent(
base_response, response_skin_changes)
t47[grouping_id, 0] = get_sts_change(
base_response, response_skin_constant)
t47[grouping_id, 1] = get_sts_change(
base_response, response_skin_changes)
rows.append('%s -> %s' % (str(base_grouping), str(active_grouping)))
t46_df = pd.DataFrame(t46, columns=columns, index=rows)
t47_df = pd.DataFrame(t47, columns=columns, index=rows)
t46_df.to_csv('./csvs/t46.csv')
t47_df.to_csv('./csvs/t47.csv')
# %% Plot for thrid computational experiment
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
for grouping_id, base_grouping in enumerate(resting_grouping_list):
active_grouping = active_grouping_list_list[grouping_id][0]
plot_phase(
base_grouping, median_dict['resting'], axs.ravel()[grouping_id],
ls='-', marker='s', color='k', ms=MS, label='Rest organ')
plot_phase(
active_grouping, median_dict['resting'], axs.ravel()[grouping_id],
ls='-', marker='o', color='k', ms=MS,
label='Active organ, skin constant')
plot_phase(
active_grouping, median_dict['active'], axs.ravel()[grouping_id],
ls='--', marker='^', color='k', ms=MS,
label='Active organ, skin changes')
axs.ravel()[grouping_id].set_title(
'Resting = %s, active = %s' % (base_grouping, active_grouping),
fontsize=6)
for axes in axs[-1]:
axes.set_xlabel('Force (mN)')
for axes in axs.T[0]:
axes.set_ylabel('Static firing (Hz)')
axs[0, 0].legend(loc=2, fontsize=6)
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.tight_layout()
fig.savefig('./plots/lesniak_f43.png')
plt.close(fig)
# %% Table for second computational experiment
mda = median_dict['active']
mdr = 0
skin_change_table = np.empty((len(resting_grouping_list) * len(
active_grouping_list_list[0]), 1))
t44 = np.empty((len(resting_grouping_list) * len(
active_grouping_list_list[0]), 2))
t45 = np.empty((len(resting_grouping_list) * len(
active_grouping_list_list[0]), 2))
active_organ_list = []
resting_organ_list = []
for i, base_grouping in enumerate(resting_grouping_list):
active_grouping_list = active_grouping_list_list[i]
for j, active_grouping in enumerate(active_grouping_list):
active_organ_list.append(active_grouping)
resting_organ_list.append(base_grouping)
base_response = get_response_from_hc_grouping(
base_grouping, mdr)
response_skin_constant = get_response_from_hc_grouping(
active_grouping, mdr)
def get_skin_number():
score = np.empty(level_num)
for i in range(level_num):
response_skin_changes = get_response_from_hc_grouping(
active_grouping, i)
score[i] = get_percent(base_response,
response_skin_changes)
skin_number = np.abs(score).argmin()
return skin_number
skin_change_table[3 * i + j] = get_skin_number()
response_skin_changes = get_response_from_hc_grouping(
active_grouping, skin_change_table[3 * i + j])
t44[3 * i + j, 0] = get_percent(
base_response, response_skin_constant)
t44[3 * i + j, 1] = get_percent(
base_response, response_skin_changes)
t45[3 * i + j, 0] = get_sts_change(
base_response, response_skin_constant)
t45[3 * i + j, 1] = get_sts_change(
base_response, response_skin_changes)
changed_to = thickness_array[skin_change_table.astype('int')].astype('int')
changed_to_um = ['%d μm' % thickness for thickness in changed_to]
columns = ['Resting organ', 'Active organ', 'Skin constant',
'Skin changes', 'Skin thickness changed to']
t44_df = pd.DataFrame(np.c_[resting_organ_list, active_organ_list,
t44, changed_to_um], columns=columns)
t45_df = pd.DataFrame(np.c_[resting_organ_list, active_organ_list,
t45, changed_to_um], columns=columns)
t44_df.to_csv('./csvs/t44.csv')
t45_df.to_csv('./csvs/t45.csv')
# %% Plot for second computational experiment
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
for grouping_id, base_grouping in enumerate(resting_grouping_list):
active_grouping = active_grouping_list_list[grouping_id][0]
plot_phase(
base_grouping, mdr, axs.ravel()[grouping_id],
ls='-', marker='s', color='k', ms=MS, label='Rest organ')
plot_phase(
active_grouping, mdr, axs.ravel()[grouping_id],
ls='-', marker='o', color='k', ms=MS,
label='Active organ, skin constant')
plot_phase(
active_grouping, skin_change_table[3 * grouping_id],
axs.ravel()[grouping_id],
ls='--', marker='^', color='k', ms=MS,
label='Active organ, skin changes')
axs.ravel()[grouping_id].set_title(
'Resting = %s, active = %s' % (base_grouping, active_grouping),
fontsize=6)
for axes in axs[-1]:
axes.set_xlabel('Force (mN)')
for axes in axs.T[0]:
axes.set_ylabel('Static firing (Hz)')
axs[0, 0].legend(loc=2, fontsize=6)
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.tight_layout()
fig.savefig('./plots/lesniak_f42.png')
plt.close(fig)
# %% Increase heminode for the slide
fig, axs = plt.subplots()
mdr = median_dict['resting']
g = [[11, 7, 2], [15, 10, 5], [13, 9, 4, 2, 2]]
for i in range(len(g)):
plot_phase(g[i], mdr, axs, ls=['-', '--', ':'][i],
marker=['s', '^', 'o'][i], color='k',
ms=MS, label=str(g[i]))
axs.legend(loc=2)
axs.set_xlabel('Force (mN)')
axs.set_ylabel('Static firing (Hz)')
fig.savefig('./plots/hmstss_heminode.png')
plt.close(fig)