-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
512 lines (392 loc) · 12 KB
/
app.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 14 Nov 2020
Dash app for cobweb plot
@author: tbury
"""
import os
import numpy as np
import pandas as pd
import base64
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
from app_functions import generate_cobweb_trajectory_sigmoid, make_cobweb_fig_sigmoid,\
make_restitution_fig_sigmoid, make_apd_sequence
#-------------
# Launch the dash app
#---------------
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# Get mathjax for latex
external_scripts = ['https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML']
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets,
external_scripts = external_scripts,
)
print('Launching dash')
server = app.server
#-----------------
# Generate figures
#-------------------
# Default model parameter values from Ravi
# y0=120, a=96.9, b=0.0104
# apdmax=216.9 (y0+a), alpha=96.9, tau=96 (1/b)
# Default model parameter values from Guevara et al. (1984)
# apdmax=207, alpha=136, tau=78
# # Default model parameters
# apdmax = 216.9 # y0+a
# alpha = 96.9 # a
# tau = 96 # 1/b
# Default model parameters (sigmoid function, from Ravi)
a = 201.3
b = 46.6
x0 = -13.5
theta = 0
ts = 300
# Defualt simulation parameters
apd0 = 150
nmax = 40
# Generate cobweb trajectory
# apd_traj = generate_cobweb_trajectory(nmax, apd0, apdmax, alpha, tau, theta, ts)
apd_traj = generate_cobweb_trajectory_sigmoid(nmax, apd0, a, b, x0, theta, ts)
# Make figures
# fig_cobweb = make_cobweb_fig(apdmax, alpha, tau, theta, ts, apd_traj)
# fig_restitution = make_restitution_fig(apdmax, alpha, tau)
# fig_apd_sequence = make_apd_sequence(apd_traj)
# Make figures (sigmoid)
fig_cobweb = make_cobweb_fig_sigmoid(a, b, x0, theta, ts, apd_traj)
fig_restitution = make_restitution_fig_sigmoid(a, b, x0)
fig_apd_sequence = make_apd_sequence(apd_traj)
#--------------------
# App layout
#–-------------------
# Font sizes
size_slider_text = '15px'
size_title = '30px'
# Parameter bounds
apd0_min = 0
apd0_max = 250
# apd0_step = 1
# apd0_marks = {x:str(x) for x in np.arange(apd0_min,apd0_max,100)}
nmax_min = 0
nmax_max = 100
# nmax_step = 20
# nmax_marks = {float(x):str(round(x,2)) for x in np.arange(nmax_min,nmax_max,20)}
# apdmax_min = 100
# apdmax_max = 300
# alpha_min = 0
# alpha_max = 200
# tau_min = 10
# tau_max = 190
a_min = 100
a_max = 300
b_min = 1
b_max = 100
x0_min = -50
x0_max = 50
theta_min = -20
theta_max = 20
ts_min = 100
ts_max = 500
# ts_step = 100
# ts_marks = {float(x):str(round(x,2)) for x in np.arange(ts_min,ts_max,100)}
# # PDF image of text
# image_filename = 'diff_eqn.png' # replace with your own image
# encoded_image = base64.b64encode(open(image_filename, 'rb').read())
model_text_2_line1 = \
'''
x_{t+1} = (3/2)x_t for 0 <= x_t < 2/3
'''
model_text_2_line2 = \
'''
x_{t+1} = (3/2)x_t - 1 for 2/3 <= x_t < 1
'''
app.layout = html.Div([
# Title section
html.Div(
html.H1('Restitution analysis',
style={'textAlign':'center',
'fontSize':size_title,
'color':'black',
'padding-top':'10px',
'padding-left':'30px',
}
)
),
# style={'width':'25%',
# 'height':'800px',
# 'fontSize':'10px',
# 'padding-left':'3%',
# 'padding-right':'2%',
# 'padding-bottom':'0px',
# 'padding-top':'40px',
# 'vertical-align': 'middle',
# 'display':'inline-block'
# }
# Left half of app
html.Div([
# html.H1('Restitution analysis',
# style={'textAlign':'left',
# 'fontSize':size_title,
# 'color':'black'}
# ),
# Slider for apd0
html.Label('APD_0 = {} ms'.format(apd0),
id='apd0_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='apd0_slider',
min=apd0_min,
max=apd0_max,
# step=apd0_step,
# marks=apd0_marks,
value=apd0,
),
# Slider for nmax
html.Label('Nmax = {}'.format(nmax),
id='nmax_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='nmax_slider',
min=nmax_min,
max=nmax_max,
# step=nmax_step,
# marks=nmax_marks,
value=nmax
),
# # Slider for apdmax
# html.Label('APD_max = {} ms'.format(apdmax),
# id='apdmax_slider_text',
# style={'fontSize':size_slider_text}),
# dcc.Slider(id='apdmax_slider',
# min=apdmax_min,
# max=apdmax_max,
# # step=y0_step,
# # marks=y0_marks,
# value=apdmax
# ),
# # Slider for alpha
# html.Label('alpha = {} ms'.format(alpha),
# id='alpha_slider_text',
# style={'fontSize':size_slider_text}),
# dcc.Slider(id='alpha_slider',
# min=alpha_min,
# max=alpha_max,
# value=alpha
# ),
# # Slider for tau
# html.Label('tau = {} ms'.format(tau),
# id='tau_slider_text',
# style={'fontSize':size_slider_text}),
# dcc.Slider(id='tau_slider',
# min=tau_min,
# max=tau_max,
# value=tau,
# ),
# Slider for a
html.Label('a = {} ms'.format(a),
id='a_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='a_slider',
min=a_min,
max=a_max,
# step=y0_step,
# marks=y0_marks,
value=a
),
# Slider for b
html.Label('b = {} ms'.format(b),
id='b_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='b_slider',
min=b_min,
max=b_max,
value=b
),
# Slider for x0
html.Label('x0 = {} ms'.format(x0),
id='x0_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='x0_slider',
min=x0_min,
max=x0_max,
value=x0,
),
# Slider for theta
html.Label('theta = {} ms'.format(theta),
id='theta_slider_text',
style={'fontSize':size_slider_text}),
dcc.Slider(id='theta_slider',
min=theta_min,
max=theta_max,
# step=theta_step,
# marks=theta_marks,
value=theta,
),
# Slider for ts
html.Label(r'$t_s = {} ms$'.format(ts),
id='ts_slider_text',
style={'fontSize':size_slider_text},
),
# dcc.Markdown(r'$ E=mc^2 $', mathjax=True),
dcc.Slider(id='ts_slider',
min=ts_min,
max=ts_max,
# step=1,
# marks=ts_marks,
value=ts
),],
style={'width':'25%',
'height':'470px',
'fontSize':'10px',
'padding-left':'3%',
'padding-right':'2%',
'padding-bottom':'0px',
'padding-top':'40px',
'vertical-align': 'middle',
'display':'inline-block'
}
),
# Restitution plot
html.Div(
[dcc.Graph(id='fig_restitution',
mathjax=True,
figure = fig_restitution,
# config={'displayModeBar': False},
),
],
style={'width':'30%',
'height':'470px',
'fontSize':'15px',
'padding-left':'0%',
'padding-right':'0%',
'vertical-align': 'middle',
'display':'inline-block'},
),
# Cobweb plot
html.Div(
[dcc.Graph(id='fig_cobweb',
mathjax=True,
figure = fig_cobweb,
# config={'displayModeBar': False},
),
],
style={'width':'30%',
'height':'470px',
'fontSize':'15px',
'padding-left':'0%',
'padding-right':'0%',
'vertical-align': 'middle',
'display':'inline-block'},
),
# APD sequence
html.Div(
[dcc.Graph(id='fig_apd_sequence',
mathjax=True,
figure = fig_apd_sequence,
# config={'displayModeBar': False},
),
],
style={'width':'90%',
'height':'320px',
'fontSize':'15px',
'padding-left':'5%',
'padding-right':'5%',
'vertical-align': 'middle',
'display':'inline-block'},
),
# Footer
html.Footer(
[
'Source code ',
html.A('here',
href='https://github.com/ThomasMBury/restitution-cobweb/',
target="_blank",
),
],
style={'fontSize':'15px',
'width':'100%',
# 'horizontal-align':'middle',
'textAlign':'center',
},
),
])
# #–-------------------
# # Callback functions
# #–--------------------
# Update text for sliders
@app.callback(
[
Output('apd0_slider_text','children'),
Output('nmax_slider_text','children'),
Output('a_slider_text','children'),
Output('b_slider_text','children'),
Output('x0_slider_text','children'),
Output('theta_slider_text','children'),
Output('ts_slider_text','children'),
],
[
Input('apd0_slider','value'),
Input('nmax_slider','value'),
# Input('apdmax_slider','value'),
# Input('alpha_slider','value'),
# Input('tau_slider','value'),
Input('a_slider','value'),
Input('b_slider','value'),
Input('x0_slider','value'),
Input('theta_slider','value'),
Input('ts_slider','value'),
]
)
def update_slider_text(apd0,nmax,a,b,x0,theta,ts):
# Slider text update
text_apd0 = 'APD_0 = {} ms'.format(apd0)
text_nmax = '# iterations = {}'.format(nmax)
# text_apdmax = 'APD_max = {} ms'.format(apdmax)
# text_alpha = 'alpha = {} ms'.format(alpha)
# text_tau = 'tau = {} ms'.format(tau)
text_a = 'a = {} ms'.format(a)
text_b = 'b = {} ms'.format(b)
text_x0 = 'x0 = {} ms'.format(x0)
text_theta = 'theta = {} ms'.format(theta)
text_ts = 'ts = {} ms'.format(ts)
return text_apd0,text_nmax,text_a,text_b,text_x0,text_theta,text_ts
# Update figures
@app.callback(
Output('fig_restitution','figure'),
Output('fig_cobweb','figure'),
Output('fig_apd_sequence','figure'),
[
Input('apd0_slider','value'),
Input('nmax_slider','value'),
# Input('apdmax_slider','value'),
# Input('alpha_slider','value'),
# Input('tau_slider','value'),
Input('a_slider','value'),
Input('b_slider','value'),
Input('x0_slider','value'),
Input('theta_slider','value'),
Input('ts_slider','value'),
],
)
def update_figs(apd0, nmax, a, b, x0, theta, ts):
# Generate cobweb trajectory
# apd_traj = generate_cobweb_trajectory(nmax, apd0, apdmax, alpha, tau, theta, ts)
apd_traj = generate_cobweb_trajectory_sigmoid(nmax, apd0, a, b, x0, theta, ts)
# # Make figures
# fig_cobweb = make_cobweb_fig(apdmax, alpha, tau, theta, ts, apd_traj)
# fig_restitution = make_restitution_fig(apdmax, alpha, tau)
# fig_apd_sequence = make_apd_sequence(apd_traj)
# Make figures (sigmoid)
fig_cobweb = make_cobweb_fig_sigmoid(a, b, x0, theta, ts, apd_traj)
fig_restitution = make_restitution_fig_sigmoid(a, b, x0)
fig_apd_sequence = make_apd_sequence(apd_traj)
return fig_restitution, fig_cobweb, fig_apd_sequence
#-----------------
# Add the server clause
#–-----------------
if __name__ == '__main__':
app.run_server(
debug=True,
host='127.0.0.1',
)