-
Notifications
You must be signed in to change notification settings - Fork 2
/
tutorial.py
234 lines (197 loc) · 7.87 KB
/
tutorial.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
from odynn import optim, utils
import pandas as pd
import seaborn as sns
import pylab as plt
import numpy as np
from odynn.models import cfg_model
from odynn import neuron as nr
from odynn import nsimul as ns
from sklearn.decomposition import PCA
def corr(df):
corr = df.corr()
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, cmap=cmap, center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5})
plt.show()
def scatt(df):
f, ax = plt.subplots(figsize=(6.5, 6.5))
sns.despine(f, left=True, bottom=True)
sns.scatterplot(x="loss", y="n__tau",
hue="rho_ca",
palette="autumn", linewidth=0,
data=df, ax=ax)
plt.show()
def violin(df):
# Use cubehelix to get a custom sequential palette
# pal = sns.cubehelix_palette(p, rot=-.5, dark=.3)
# Show each distribution with both violins and points
sns.violinplot(data=df, inner="points")
plt.show()
def get_df(dir):
dic = optim.get_vars(dir)
return pd.DataFrame.from_dict(dic)
def real_std(df):
df = df.copy()
mdps = [col for col in df.columns if 'mdp' in col or 'E' in col]
df = df.drop(columns=mdps)
variation = df.std() / df.mean()
d = {'Variation': abs(variation.values),
'Parameter': df.columns.values}
df2 = pd.DataFrame(d)
df2 = df2.sort_values(['Variation']).reset_index(drop=True)
mx = np.max(d['Variation'])
r = np.array([1., 0., 0.])
g = np.array([0., 1., 0.])
colors = [r * (1. - v / mx) + g * (v / mx) for v in df2['Variation']]
df2.plot.bar(x='Parameter', y='Variation', colors=colors, title='Relative standard deviation')
# ax = sns.barplot(x='Parameter', y='Variation', data=df2, palette=colors)
# plt.yscale('log')
plt.show()
def sigm():
def plot_sigm(pts, scale, col='k'):
plt.plot(pts, 1 / (1 + sp.exp((-30. - pts) / scale)), col, label='scale=%s'%scale)
import scipy as sp
pts = sp.arange(-12000, 20, 0.5)
# plot_sigm(pts, -1, col='#000000')
# plot_sigm(pts, -3, col='#440000')
# plot_sigm(pts, -10, col='#880000')
# plot_sigm(pts, -30, col='#bb0000')
# plot_sigm(pts, -100, col='#ff0000')
plot_sigm(pts, 1, col='#000000')
plot_sigm(pts, 3, col='#004400')
plot_sigm(pts, 10, col='#008800')
plot_sigm(pts, 30, col='#00bb00')
plot_sigm(pts, 1000, col='#00ff00')
plt.legend()
plt.title('Influence of $V_{scale}$ on the rate dynamics')
plt.show()
exit(0)
def table():
import re
neur = cfg_model.NEURON_MODEL
from odynn.models import celeg
dir = utils.set_dir('Integcomp_volt_mod3dt0.1-YES')
best = optim.get_best_result(dir)
for k, v in neur.default_params.items():
v = neur._constraints_dic.get(k, ['-inf', 'inf'])
u = ''
if 'tau' in k:
u = 'ms'
elif 'scale' in k or 'mdp' in k or 'E' in k:
u = 'mV'
elif 'g' in k:
u = 'mS/cm$^2$'
elif k == 'C_m':
u = '$\mu$F/cm$^2$'
else:
u = 'none'
tp = '%s &&& %s & %s&%s&%s&%s \\\\\n \\hline' % (k, v[0], v[1], u, cfg_model.NEURON_MODEL.default_params[k], best[k])
tp = re.sub('(.)__(.*) (&.*&.*&.*&.*&)', '\g<2>_\g<1> \g<3>', tp)
tp = tp.replace('inf', '$\\infty$')
tp = re.sub('scale_(.)', '$V_{scale}^\g<1>$', tp)
tp = re.sub('mdp_(.)', '$V_{mdp}^\g<1>$', tp)
tp = re.sub('tau_(.)', '$\\ tau^\g<1>$', tp)
tp = re.sub('E_(..?)', '$E_{\g<1>}$', tp)
tp = tp.replace('\\ tau', '\\tau')
tp = re.sub('g_([^ ]*) +', '$g_{\g<1>}$ ', tp)
tp = tp.replace('rho_ca', '$\\rho_{Ca}$')
tp = tp.replace('decay_ca', '$\\tau_{Ca}$')
tp = tp.replace('C_m', '$C_m$')
tp = tp.replace('alpha_h', '$\\alpha^h$')
tp = re.sub('(.*tau.*)&&&', '\g<1>&%s&%s&' % (celeg.MIN_TAU, celeg.MAX_TAU), tp)
tp = re.sub('(.*scale.*)&&&', '\g<1>&%s&%s&' % (celeg.MIN_SCALE, celeg.MAX_SCALE), tp)
print(tp)
exit(0)
def hhsimp_box(df):
utils.box(df, ['b', 'g', 'm', 'g', 'm'], ['C_m', 'g_L', 'g_K', 'E_L', 'E_K'])
plt.title('Membrane')
utils.save_show(True, True, 'boxmemb', dpi=300)
plt.subplot(3, 1, 1)
utils.box(df, ['m', '#610395'], ['a__mdp', 'b__mdp'])
plt.title('Midpoint')
plt.subplot(3, 1, 2)
utils.box(df, ['m', '#610395'], ['a__scale', 'b__scale'])
plt.title('Scale')
plt.subplot(3, 1, 3)
utils.box(df, ['m', '#610395'], ['a__tau', 'b__tau'])
plt.yscale('log')
plt.title('Time constant')
plt.tight_layout()
utils.save_show(True, True, 'boxrates', dpi=300)
def leak_box(df):
utils.box(df, ['b', 'g', 'Gold'], ['C_m', 'g_L', 'E_L'])
plt.title('Membrane')
utils.save_show(True, True, 'box1', dpi=300)
if __name__ == '__main__':
from odynn.nsimul import simul
import scipy as sp
t = sp.arange(0., 1200., 0.1)
i = 20. * ((t>400) & (t<800))
simul(t=t, i_inj=i, show=True);exit()
dir = utils.set_dir('Tapwith_dt0.5')
dic = optim.get_vars(dir, loss=False)
# df = pd.DataFrame.from_dict(dic)
# df = df.dropna()
# dfdisp = (df - df.mean()) / df.std()
# plt.plot(dfdisp.transpose())
# utils.save_show(True, True, 'dispreal', dpi=300)
dd = optim.get_vars_all(dir, losses=True)
optim.plot_loss_rate(dd['loss'], dd['rates'], dd['loss_test'], 50, show=True)
from odynn import datas
dic = optim.get_vars(dir, loss=True)
train, test = optim.get_data(dir)
print(dic['loss'])
df = pd.DataFrame(dic['loss'], columns=['loss'])
# df = pd.DataFrame.from_dict(dic)#.head(4)
df = df.sort_values('loss').reset_index(drop=True)
# df = df.dropna()
sns.barplot(x=df.index, y='loss', data=df)
# df.plot.bar(y='loss')
utils.save_show(True, True, 'lossfin_virt', dpi=300);exit()
# df = df[df['loss'] <= np.min(df['loss'])]
# hhsimp_box(df)
# cfg_model.NEURON_MODEL.boxplot_vars(dic, show=True, save=True)
dic = df.to_dict('list')
# dic = collections.OrderedDict(sorted(dic.items(), key=lambda t: t[0]))
# obj = circuit.CircuitTf.create_random(n_neuron=9, syn_keys={(i,i+1):True for i in range(8)}, gap_keys={}, n_rand=50, dt=0.1)
p = optim.get_best_result(dir)
print(p)
# p = {k: v[0] for k,v in p.items()}
for i in range(train[1].shape[-1]):
ns.comp_pars_targ(p, cfg_model.NEURON_MODEL.default_params, dt=train[0][1] - train[0][0], i_inj=train[1][:,i], suffix='virtrain%s'%i, show=True, save=True)
for i in range(test[1].shape[-1]):
ns.comp_pars_targ(p, cfg_model.NEURON_MODEL.default_params, dt=test[0][1] - test[0][0], i_inj=test[1][:,i], suffix='virtest%s'%i, show=True, save=True)
n = optim.get_model(dir)
n.init_params = dic
X = n.calculate(train[1])
Xt = n.calculate(test[1])
for i in range(X.shape[2]):
n.plot_output(train[0], train[1][:,i], X[:,:,i], [train[-1][0][:,i], train[-1][-1][:,i]], save=True, suffix='virtend%s'%i)
# for i in range(X.shape[3]):
# plt.subplot(2, 1, 1)
# plt.plot(train[-1][-1], 'r', label='train data')
# plt.plot(X[:, -1,:, i])
# plt.legend()
# plt.subplot(2, 1, 2)
# plt.plot(test[-1][-1], 'r', label='test data')
# plt.plot(Xt[:, -1,:, i])
# plt.legend()
# utils.save_show(True,True,'best_result%s'%i, dpi=300)
# for i in range(9):
# dicn = {k: v[:,i] for k,v in dic.items()}
# hhmodel.CElegansNeuron.plot_vars(dicn, show=True, save=False)
# scatt(df)
# pca = PCA()
# pca.fit(df)
# for c in pca.components_:
# for i, name in enumerate(df):
# print(name, '%.2f'%c[i])
# plt.plot(pca.explained_variance_ratio_)
# plt.show()
# sns.FacetGrid(data=df, row='C_m')
# plt.show()
# violin(df)