Spaces:
Sleeping
Sleeping
File size: 9,171 Bytes
9435413 81e15fe 9435413 9160077 81e15fe 9435413 9160077 9435413 81e15fe 9160077 81e15fe 9435413 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 7b70c21 81e15fe 54ab346 81e15fe e161839 81e15fe 56eae50 81e15fe 17bb12d 56eae50 81e15fe | 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 | from dash import Dash, Input, Output, State
import dash_bootstrap_components as dbc
import pandas as pd
from data.functions import *
from transportEqs import *
import numpy as np
from Kps_function.read_Kps_model import *
import os
from figure import build_figure
from main_layout import build_main_layout
from instructions import build_instructions_layout
from rst_info import build_rst_layout
from dash import html, dcc
app = Dash(
__name__,
assets_folder=os.path.join(os.path.dirname(__file__), "assets"),
suppress_callback_exceptions=True,
#external_stylesheets=[dbc.themes.BOOTSTRAP],
)
# --------------------------------------------------------------------
# Layout
# --------------------------------------------------------------------
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
html.Div(id="page-content") # <-- this MUST exist here
])
app.validation_layout = html.Div([
app.layout,
build_main_layout(),
build_instructions_layout(),
build_rst_layout(),
])
@app.callback(
Output("page-content", "children"),
Input("url", "pathname")
)
def route(pathname):
if pathname == "/instructions":
return build_instructions_layout()
elif pathname == "/rst":
return build_rst_layout()
else:
return build_main_layout()
# --------------------------------------------------------------------
# Main callback
# --------------------------------------------------------------------
@app.callback(
Output("c1_solvent", "disabled"),
Output("c1_volume", "disabled"),
Output("c1_swelling", "disabled"),
Output("c1_temp", "disabled"),
Output("c1_iterations", "disabled"),
Input("condition1_mode", "value"),
State("c1_solvent", "value"),
)
def toggle_condition1_inputs(mode, current_value):
disable = (mode != "in_vitro")
return disable, disable, disable, disable, disable
@app.callback(
Output("c2_solvent", "disabled"),
Output("c2_volume", "disabled"),
Output("c2_swelling", "disabled"),
Output("c2_temp", "disabled"),
Output("c2_iterations", "disabled"),
Input("condition2_mode", "value"),
State("c2_solvent", "value"),
)
def toggle_condition2_inputs(mode, current_value):
disable = (mode != "in_vitro")
return disable, disable, disable, disable, disable
@app.callback(
Output("mm0_chart", "figure"),
Input("calculate", "n_clicks"),
State("matrix", "value"),
State("tg", "value"),
State("crystal", "value"),
State("density", "value"),
State("polymer_volume", "value"),
State("surface_area", "value"),
# Condition 1
State("condition1_mode", "value"),
State("c1_solvent", "value"),
State("c1_volume", "value"),
State("c1_swelling", "value"),
State("c1_temp", "value"),
State("c1_time", "value"),
State("c1_iterations", "value"),
# Condition 2
State("condition2_mode", "value"),
State("c2_solvent", "value"),
State("c2_volume", "value"),
State("c2_swelling", "value"),
State("c2_temp", "value"),
State("c2_time", "value"),
State("c2_iterations", "value"),
State("samples", "value"),
prevent_initial_call=True,
)
def update_chart(n_clicks,
matrix, tg, crystal, density, polymer_volume, surface_area,
c1_mode, c1_solvent, c1_volume, c1_swelling,
c1_temp, c1_time, c1_iter,
c2_mode, c2_solvent, c2_volume,
c2_swelling, c2_temp, c2_time, c2_iter,samples):
# TODO: replace with logic that uses the form inputs
# For now, simply return the same figure when "Calculate" is clicked.
df = pd.DataFrame()
df['CASRN'] = soluteData['CASRN'].to_numpy()
N = int(samples)
pindex = np.where(polymers == matrix)[0][0]
CHRIS_category = categories[pindex]
Polymer_Density = float(density)
Polymer_Tg = float(tg) + 273.15
Polymer_X = float(crystal)/100.
L = float(polymer_volume)/float(surface_area)
# Condition 1
ExtractionTime = float(c1_time) * 3600.
if c1_mode == 'in_vivo_conservative':
medians, lowers, uppers = ConservativeMonteCarlo(soluteData, CHRIS_category, L, ExtractionTime, N)
elif c1_mode == 'in_vivo_tissue':
medians, lowers, uppers = TissueMonteCarlo(soluteData, CHRIS_category, L, ExtractionTime, N)
else:
Solvent_Name = c1_solvent
Solvent_MW = Solvent_MWs[Solvent_Name]
Solvent_Density = Solvent_Densities[Solvent_Name]
Solvent_PI = Solvent_PIs[Solvent_Name]
ExtractionT = float(c1_temp) + 273.15
Ms_M0 = float(c1_swelling)
Swell = 1.+(Ms_M0-1.)*Polymer_Density/Solvent_Density
w = (Ms_M0 - 1.)/(Ms_M0-Polymer_X)
Iterations = float(c1_iter)
Lbath = float(c1_volume) / float(surface_area)
medians, lowers, uppers = ExtractMonteCarlo(soluteData, Solvent_PI, w, ExtractionT, Polymer_Tg, Solvent_Name, Solvent_MW,
ExtractionTime, Swell, Iterations, CHRIS_category, L, Lbath, N)
df['Condition 1'] = medians
df['Cond1_err_plus'] = uppers-medians
df['Cond1_err_minus'] = medians-lowers
# Condition 2
ExtractionTime = float(c2_time) * 3600.
if c2_mode == 'in_vivo_conservative':
medians, lowers, uppers = ConservativeMonteCarlo(soluteData, CHRIS_category, L, ExtractionTime, N)
elif c2_mode == 'in_vivo_tissue':
medians, lowers, uppers = TissueMonteCarlo(soluteData, CHRIS_category, L, ExtractionTime, N)
else:
Solvent_Name = c2_solvent
Solvent_MW = Solvent_MWs[Solvent_Name]
Solvent_Density = Solvent_Densities[Solvent_Name]
Solvent_PI = Solvent_PIs[Solvent_Name]
ExtractionT = float(c2_temp) + 273.15
Ms_M0 = float(c2_swelling)
Swell = 1. + (Ms_M0 - 1.) * Polymer_Density / Solvent_Density
w = (Ms_M0 - 1.) / (Ms_M0 - Polymer_X)
Iterations = float(c2_iter)
Lbath = float(c2_volume) / float(surface_area)
medians, lowers, uppers = ExtractMonteCarlo(soluteData, Solvent_PI, w, ExtractionT, Polymer_Tg, Solvent_Name, Solvent_MW,
ExtractionTime, Swell, Iterations, CHRIS_category, L, Lbath, N)
df['Condition 2'] = medians
df['Cond2_err_plus'] = uppers - medians
df['Cond2_err_minus'] = medians - lowers
fig = build_figure(df)
return fig
def ConservativeMonteCarlo(soluteData, CHRIS_category, L, time, N):
nSolutes = len(soluteData)
medians = zeros(nSolutes)
lowers = zeros(nSolutes)
uppers = zeros(nSolutes)
for iSolute in range(nSolutes):
Solute_MW = soluteData['MW_new'].iloc[iSolute]
D_CHRIS = get_D_CHRIS(Solute_MW, CHRIS_category, N=N)
tau = D_CHRIS * time / L ** 2. # fixed 1 day
mass = Conservative(tau)
medians[iSolute] = percentile(mass, 50)
lowers[iSolute] = percentile(mass, 25)
uppers[iSolute] = percentile(mass, 75)
return medians, lowers, uppers
def TissueMonteCarlo(soluteData, CHRIS_category, L, time, N):
nSolutes = len(soluteData)
medians = zeros(nSolutes)
lowers = zeros(nSolutes)
uppers = zeros(nSolutes)
for iSolute in range(nSolutes):
Solute_MW = soluteData['MW_new'].iloc[iSolute]
Solute_logP = soluteData['LogP_new'].iloc[iSolute]
D_CHRIS = get_D_CHRIS(Solute_MW, CHRIS_category, N=N)
tau = D_CHRIS * time / L ** 2.
Dt = get_Dt(N)
Kpt = get_Kpt(Solute_logP, N)
beta = (1. / Kpt) * sqrt(Dt / D_CHRIS)
mass = SolubilityLimited(beta, tau)
medians[iSolute] = percentile(mass, 50)
lowers[iSolute] = percentile(mass, 25)
uppers[iSolute] = percentile(mass, 75)
return medians, lowers, uppers
def ExtractMonteCarlo(soluteData, Solvent_PI, w, ExtractionT, Polymer_Tg, Solvent_Name, Solvent_MW,
ExtractionTime, Swell, Iterations, CHRIS_category, L, Lbath, N):
nSolutes = len(soluteData)
medians = zeros(nSolutes)
lowers = zeros(nSolutes)
uppers = zeros(nSolutes)
for iSolute in range(nSolutes):
Solute_MW = soluteData['MW_new'].iloc[iSolute]
Solute_logP = soluteData['LogP_new'].iloc[iSolute]
Solute_Vabc = soluteData['Vabc'].iloc[iSolute]
D_Extract = get_D_Extract(w, ExtractionT, Polymer_Tg, Solvent_Name, Solvent_MW, Solute_MW, Solute_Vabc,
CHRIS_category, N=N)
tau = D_Extract * ExtractionTime / L ** 2.
ToKPS = array([[Solute_logP, Solvent_PI]])
model = QuantileGridFromCoeffs(export_dir='Kps_function/Kps_model')
Kps = 10**model.sample(ToKPS, n_samples=N)[0]
alpha = Lbath / L / Kps
massExtraction = zeros(N)
for i in range(N):
massExtraction[i] = Extraction(tau[i], alpha[i], Kps[i], Swell, Iterations)
medians[iSolute] = percentile(massExtraction, 50)
lowers[iSolute] = percentile(massExtraction, 25)
uppers[iSolute] = percentile(massExtraction, 75)
return medians, lowers, uppers
if __name__ == "__main__":
app.run(debug=True)
|