-
Notifications
You must be signed in to change notification settings - Fork 0
/
auxilliary_functions.py
239 lines (195 loc) · 8.82 KB
/
auxilliary_functions.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
'''
author: Michael A. Reiter
(c) ETH Zurich, Michael A. Reiter, 2022
This file is part of Dashing Growth Curves.
Dashing Growth Curves is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Dashing Growth Curves is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Dashing Growth Curves. If not, see <https://www.gnu.org/licenses/>.
'''
import dash_bootstrap_components as dbc
from dash import html
from string import ascii_uppercase
import numpy as np
import math_functions as mf
################################################
# graph configs
################################################
def graph_config(f_name):
config = {
'toImageButtonOptions': {
'format': 'svg', # one of png, svg, jpeg, webp
'filename': f_name,
'height': 500,
'width': 1000,
'scale': 0.5 # Multiply title/legend/axis/canvas sizes by this factor
},
'displaylogo': False,
'modeBarButtonsToRemove': ['pan', 'zoomin', 'zoomout', 'zoom', 'select2D', 'autoscale', 'lasso']
}
return config
################################################
# download buttons
################################################
def create_download_button(name):
button_out = dbc.Button(html.B(name),
color='secondary',
style={
'height':'60px',
'width': '100px'
}
)
return button_out
################################################
# link buttons
################################################
def create_link_button(name, target):
button_out = dbc.Button(html.B(name),
color='secondary',
target=target,
href=target,
style={
'height':'55px',
'width': '150px',
'display': 'flex',
'align-items': 'center',
'justify-content': 'center',
}
)
return button_out
################################################
# set sample locations
################################################
def set_sample_locations(df):
# generate sample locations to fit 12, 24, 96 or 384 well format
# if data wasn't collected from any of the type, just generate numerical IDs
sample_locations = []
if df.shape[0] == 384:
for x in ascii_uppercase[:16]:
for y in np.arange(1,25):
sample_locations.append('{}{}'.format(x, y))
elif df.shape[0] == 96:
for x in ascii_uppercase[:8]:
for y in np.arange(1,13):
sample_locations.append('{}{}'.format(x, y))
elif df.shape[0] == 24:
for x in ascii_uppercase[:4]:
for y in np.arange(1,7):
sample_locations.append('{}{}'.format(x, y))
elif df.shape[0] == 12:
for x in ascii_uppercase[:3]:
for y in np.arange(1,5):
sample_locations.append('{}{}'.format(x, y))
else:
for i in np.arange(1, df.shape[0] + 1):
sample_locations.append('{}'.format(i))
return sample_locations
################################################
# generate alert
################################################
def generate_alert(alert_message):
alert = dbc.Alert(alert_message,
duration = 10000,
color = 'danger',
)
return alert
################################################
# generate fitted curve
################################################
def generate_fitted_curve(t, fitting_algorithm, growth_data_sp, n_iter=1000):
t0 = growth_data_sp['t0']
t1 = growth_data_sp['t1']
t0_idx = growth_data_sp['t0_idx']
t1_idx = growth_data_sp['t1_idx']
mu = growth_data_sp['mumax']
N0 = growth_data_sp['N0']
l = growth_data_sp['t0']
A = growth_data_sp['A']
v = growth_data_sp['v']
# for tight fits, need to convert lag time value to lambda parameter
if fitting_algorithm == 'Logistic - tight':
l = l - 0.17 * A / mu
elif fitting_algorithm == 'Gompertz - tight':
l = l - 0.014 * A / mu
# create timepoints
if fitting_algorithm in ['Manual', 'Manual-like']:
t_fit_start = t[t0_idx]
t_fit_end = t[t1_idx]
elif fitting_algorithm == 'Easy Linear':
t_fit_start = t0
t_fit_end = t1
else:
t_fit_start = t[0]
t_fit_end = t[-1]
t_fit = np.linspace(t_fit_start, t_fit_end, n_iter)
# create fit values (i.e. associated y-values)
if fitting_algorithm in ['Manual', 'Manual-like', 'Easy Linear']:
y_fit = mf.exp_function(t_fit, N0, mu)
elif 'Gompertz' in fitting_algorithm:
y_gompertz_fit = mf.modified_gompertz(t_fit, N0, A, mu, l)
y_fit = np.exp(y_gompertz_fit)
elif 'Logistic' in fitting_algorithm:
y_logistic_fit = mf.modified_logistic(t_fit, N0, A, mu, l)
y_fit = np.exp(y_logistic_fit)
# elif fitting_algorithm == 'Richards':
# y_richards_fit = mf.modified_richards(t_fit, A, mu, l, v)
# y_fit = np.exp(y_richards_fit) * n0
# elif fitting_algorithm == 'Schnute':
# y_schnute_fit = mf.modified_schnute(t_fit, A, mu, l, v)
# y_fit = np.exp(y_schnute_fit) * n0
return t_fit, y_fit
def generate_start_end_logphase_indicator_lines(fitting_algorithm, growth_data_sp, sample_trace_blanked, n_iter=1000):
t0 = growth_data_sp['t0']
t0_std = growth_data_sp['t0_std']
t1 = growth_data_sp['t1']
t1_std = growth_data_sp['t1_std']
A = growth_data_sp['A']
mu = growth_data_sp['mumax']
l = growth_data_sp['t0']
N0 = growth_data_sp['N0']
v = growth_data_sp['v']
# for tight fits, need to convert lag time value to lambda parameter
if fitting_algorithm == 'Logistic - tight':
l = l - 0.17 * A / mu
elif fitting_algorithm == 'Gompertz - tight':
l = l - 0.014 * A / mu
x_0 = np.ones(n_iter) * t0
x_1 = np.ones(n_iter) * t1
if fitting_algorithm in ['Manual', 'Manual-like']:
t0_idx = growth_data_sp['t0_idx']
y0 = sample_trace_blanked.iloc[t0_idx]
t1_idx = growth_data_sp['t1_idx']
y1 = sample_trace_blanked.iloc[t1_idx]
hovertext_start = ['start log-phase: {0:.2f} h'.format(t0) for x in range(n_iter)]
hovertext_end = ['end log-phase: {0:.2f} h'.format(t1) for x in range(n_iter)]
elif fitting_algorithm == 'Easy Linear':
y0 = mf.exp_function(t0, N0, mu)
y1 = mf.exp_function(t1, N0, mu)
hovertext_start = ['start log-phase: {0:.2f} h'.format(t0) for x in range(n_iter)]
hovertext_end = ['end log-phase: {0:.2f} h'.format(t1) for x in range(n_iter)]
else:
if 'Gompertz' in fitting_algorithm:
y0 = np.exp(mf.modified_gompertz(t0, N0, A, mu, l))
y1 = np.exp(mf.modified_gompertz(t1, N0, A, mu, l))
elif 'Logistic' in fitting_algorithm:
y0 = np.exp(mf.modified_logistic(t0, N0, A, mu, l))
y1 = np.exp(mf.modified_logistic(t1, N0, A, mu, l))
# elif fitting_algorithm == 'Richards':
# y0 = np.exp(mf.modified_richards(t0, N0, A, mu, l, v))
# y1 = np.exp(mf.modified_richards(t1, N0, A, mu, l, v))
# elif fitting_algorithm == 'Schnute':
# y0 = np.exp(mf.modified_schnute(t0, N0, A, mu, l, v))
# y1 = np.exp(mf.modified_schnute(t1, N0, A, mu, l, v))
hovertext_start = ['start log-phase: {0:.2f} ± {1:.2f} h'.format(t0, t0_std) for x in range(n_iter)]
hovertext_end = ['end log-phase: {0:.2f} h ± {1:.2f}'.format(t1, t1_std) for x in range(n_iter)]
y_0 = np.linspace(0, y0, n_iter)
y_1 = np.linspace(0, y1, n_iter)
return x_0, y_0, x_1, y_1, hovertext_start, hovertext_end
################################################
# format nan for store
################################################
# np.nan value cannot be stored in serialized dict (get turned to None value), need to convert them to 'NaN' (a string)
def format_value_for_store(x):
if ~np.isnan(x):
return x
else:
return 'NaN'