Fit Binding Model Parameters
Fit Binding Model Parameters¶
This example demonstrates how to estimate SMA binding parameters based on multiple gradient elution chromatograms.
It assumes familiarity with the CADETProcess process models, as introduced in the
Fit Column Transport Parameters example.
import os
import numpy as np
from CADETProcess.processModel import (
ComponentSystem,
FlowSheet,
Process,
Inlet,
Outlet,
LumpedRateModelWithPores,
)
from CADETProcess.processModel import StericMassAction
from CADETProcess.reference import ReferenceIO
from CADETProcess.comparison import Comparator
from CADETProcess.simulator import Cadet
from CADETProcess.optimization import OptimizationProblemProcess creation¶
To simplify the creation of multiple Process instances with the correct configurations,
the process creation was wrapped into a function below:
def create_process(cv_length=30):
# Component System
component_system = ComponentSystem()
component_system.add_component("Salt")
component_system.add_component("A")
Q_ml_min = 0.5 # ml/min
Q_m3_s = Q_ml_min / (60 * 1e6)
initial_salt_concentration = 50
final_salt_concentration = 500
# Unit Operations
inlet = Inlet(component_system, name="inlet")
inlet.flow_rate = Q_m3_s
column = create_column_model(
component_system, final_salt_concentration, initial_salt_concentration
)
column_volume = column.length * (column.diameter / 2) ** 2 * np.pi
outlet = Outlet(component_system, name="outlet")
flow_sheet = FlowSheet(component_system)
flow_sheet.add_unit(inlet)
flow_sheet.add_unit(column)
flow_sheet.add_unit(outlet)
flow_sheet.add_connection(inlet, column)
flow_sheet.add_connection(column, outlet)
# Process
process = Process(flow_sheet, f"{cv_length}")
load_duration = 9
t_gradient_start = 90.0
gradient_volume = cv_length * column_volume
gradient_duration = gradient_volume / inlet.flow_rate[0]
duration_post_gradient_wash = gradient_duration / 10 + 180
process.cycle_time = (
t_gradient_start + gradient_duration + duration_post_gradient_wash
)
t_gradient_end = t_gradient_start + gradient_duration
c_load = np.array([initial_salt_concentration, 1.0])
c_wash = np.array([initial_salt_concentration, 0.0])
c_elute = np.array([final_salt_concentration, 0.0])
gradient_slope = (c_elute - c_wash) / gradient_duration
c_gradient_poly = np.array(list(zip(c_wash, gradient_slope)))
c_post_gradient_wash = np.array([final_salt_concentration, 0.0])
process.add_event("load", "flow_sheet.inlet.c", c_load)
process.add_event("wash", "flow_sheet.inlet.c", c_wash, load_duration)
process.add_event(
"grad_start", "flow_sheet.inlet.c", c_gradient_poly, t_gradient_start
)
process.add_event(
"grad_end", "flow_sheet.inlet.c", c_post_gradient_wash, t_gradient_end
)
return process
def create_column_model(
component_system, final_salt_concentration, initial_salt_concentration
):
column = LumpedRateModelWithPores(component_system, name="column")
column.length = 0.02 # m
column.diameter = 0.008
column.particle_radius = 1.7e-5
column.axial_dispersion = 3.16e-7
column.bed_porosity = 0.3
column.particle_porosity = 0.5
column.film_diffusion = [1e-4, 1e-4]
binding_model = create_binding_model(component_system, final_salt_concentration)
column.binding_model = binding_model
column.c = [
initial_salt_concentration,
0,
]
column.cp = [initial_salt_concentration, 0]
column.q = [binding_model.capacity, 0]
return column
def create_binding_model(component_system, final_salt_concentration):
binding_model = StericMassAction(component_system, name="SMA")
binding_model.is_kinetic = True
binding_model.characteristic_charge = [0.0, 10]
binding_model.capacity = 400.0
binding_model.reference_solid_phase_conc = binding_model.capacity
binding_model.reference_liquid_phase_conc = final_salt_concentration
binding_model.steric_factor = [0.0, 10]
binding_model.adsorption_rate = [0.0, 10]
binding_model.desorption_rate = [0.0, 1e3]
return binding_modelCreating “experimental” data¶
To run the parameter estimation algorithm, we need experimental data. This function generates in-silico based “experimental” data for us to use.
def create_in_silico_experimental_data():
def save_csv(
results, directory=None, filename=None, units=None, noise_percentage=5
):
if not os.path.exists(directory):
os.makedirs(directory)
if units is None:
units = results.solution.keys()
for unit in units:
solution = results.solution[unit]["outlet"].solution
solution_without_salt = solution[:, 1:]
solution_without_salt = solution_without_salt + (
np.random.random(solution_without_salt.shape) - 0.5
) * noise_percentage / 100 * solution_without_salt.max(axis=0)
solution[:, 1:] = solution_without_salt
solution_times = results.solution[unit]["outlet"].time
full_solution = np.concatenate(
[np.atleast_2d(solution_times), solution.T]
).T
if filename is None:
filename = unit + "_output.csv"
file_path = os.path.join(directory, filename)
with open(file_path, "w") as file_handle:
np.savetxt(file_handle, full_solution, delimiter=",")
simulator = Cadet()
case_dir = "experimental_data"
os.makedirs(case_dir, exist_ok=True)
for cv in [5, 30, 120]:
process = create_process(cv_length=cv)
simulation_results = simulator.simulate(process)
save_csv(
simulation_results,
directory=case_dir,
filename=f"{cv}.csv",
units=["outlet"],
)Below are two convenience functions, written to simplifly loading references and creating comparators.
def load_reference(file_name, component_index=2):
data = np.loadtxt(file_name, delimiter=",")
time_experiment = data[:, 0]
c_experiment = data[:, component_index]
reference = ReferenceIO(
f"Peak {file_name}",
time_experiment,
c_experiment,
component_system=ComponentSystem(["A"]),
)
return reference
def create_comparator(reference):
comparator = Comparator(name=f"Comp {reference.name}")
comparator.add_reference(reference)
comparator.add_difference_metric(
"PeakPosition", reference, "outlet.outlet", components=["A"]
)
comparator.add_difference_metric(
"PeakHeight", reference, "outlet.outlet", components=["A"]
)
return comparatorif __name__ == "__main__":
create_in_silico_experimental_data()
optimization_problem = OptimizationProblem("SMA_binding_parameters")
simulator = Cadet()
optimization_problem.add_evaluator(simulator)
comparators = dict()
for cv in [5, 30, 120]:
process = create_process(cv)
reference = load_reference(f"experimental_data/{cv}.csv")
optimization_problem.add_evaluation_object(process)
comparator = create_comparator(reference)
# Here I stored the comparators for easy access from within the callback function.
# Note, that the dictionary keys need to be identical to the process names
# as defined in create_process() for this to work.
comparators[str(cv)] = comparator
optimization_problem.add_objective(
comparator,
name=f"Objective {comparator.name}",
evaluation_objects=[
process
], # limit this comparator to be applied to only this one process
n_objectives=comparator.n_metrics,
requires=[simulator],
)
optimization_problem.add_variable(
name="adsorption_rate",
parameter_path="flow_sheet.column.binding_model.adsorption_rate",
lb=1e-3,
ub=1e3,
transform="auto",
indices=[1], # modify only the protein (component index 1) parameter
)
optimization_problem.add_variable(
name="desorption_rate",
parameter_path="flow_sheet.column.binding_model.desorption_rate",
lb=1e-3,
ub=1e3,
transform="auto",
indices=[1], # modify only the protein (component index 1) parameter
)
optimization_problem.add_variable(
name="equilibrium_constant",
evaluation_objects=None,
lb=1e-4,
ub=1e3,
transform="auto",
indices=[1], # modify only the protein (component index 1) parameter
)
optimization_problem.add_variable(
name="kinetic_constant",
evaluation_objects=None,
lb=1e-4,
ub=1e3,
transform="auto",
indices=[1], # modify only the protein (component index 1) parameter
)
optimization_problem.add_variable(
name="characteristic_charge",
parameter_path="flow_sheet.column.binding_model.characteristic_charge",
lb=1,
ub=50,
transform="auto",
indices=[1], # modify only the protein (component index 1) parameter
)
optimization_problem.add_variable_dependency(
dependent_variable="desorption_rate",
independent_variables=[
"kinetic_constant",
],
transform=lambda k_kin: 1 / k_kin,
)
optimization_problem.add_variable_dependency(
dependent_variable="adsorption_rate",
independent_variables=["kinetic_constant", "equilibrium_constant"],
transform=lambda k_kin, k_eq: k_eq / k_kin,
)
def callback(simulation_results, individual, evaluation_object, callbacks_dir="./"):
comparator = comparators[evaluation_object.name]
comparator.plot_comparison(
simulation_results,
file_name=f"{callbacks_dir}/{individual.id}_{evaluation_object}_comparison.png",
)
optimization_problem.add_callback(callback, requires=[simulator])
print(optimization_problem.variable_names)
x0 = [1, 1, 1e-2, 1e-3, 10]
ind = optimization_problem.create_individual(x0)
optimization_problem.evaluate_callbacks(ind)['adsorption_rate', 'desorption_rate', 'equilibrium_constant', 'kinetic_constant', 'characteristic_charge']



Optimizer¶
if __name__ == "__main__":
from CADETProcess.optimization import U_NSGA3
optimizer = U_NSGA3()
optimizer.n_max_gen = 60
optimizer.pop_size = 64
optimizer.n_cores = -4Run Optimization¶
if __name__ == "__main__":
optimization_results = optimizer.optimize(optimization_problem, use_checkpoint=False)==========================================================
n_gen | n_eval | n_nds | eps | indicator
==========================================================
1 | 64 | 4 | - | -
2 | 128 | 3 | 0.2458727913 | ideal
3 | 192 | 2 | 7.430050E+01 | ideal
4 | 256 | 2 | 1.638312E+01 | ideal
5 | 320 | 5 | 0.6340525215 | ideal
6 | 384 | 4 | 2.7995863745 | ideal
7 | 448 | 5 | 0.7443694659 | ideal
8 | 512 | 3 | 0.4247887690 | ideal
9 | 576 | 3 | 0.3843428802 | ideal
10 | 640 | 4 | 0.0957394546 | ideal
11 | 704 | 1 | 0.0217139075 | ideal
12 | 768 | 2 | 1.0000000000 | ideal
13 | 832 | 2 | 0.000000E+00 | f
14 | 896 | 3 | 0.2725701431 | ideal
15 | 960 | 4 | 0.0504104568 | ideal
16 | 1024 | 6 | 0.0527201862 | ideal
17 | 1088 | 7 | 0.0714330609 | ideal
18 | 1152 | 5 | 0.0884222111 | ideal
19 | 1216 | 2 | 1.0000903844 | ideal
20 | 1280 | 3 | 1.0000000000 | ideal
21 | 1344 | 4 | 0.0192336387 | f
22 | 1408 | 5 | 0.6709755278 | ideal
23 | 1472 | 6 | 0.2500102707 | ideal
24 | 1536 | 7 | 0.0030354418 | nadir
25 | 1600 | 9 | 0.0918570415 | f
26 | 1664 | 9 | 0.000000E+00 | f
27 | 1728 | 10 | 0.1445561489 | ideal
28 | 1792 | 11 | 0.0551777928 | f
29 | 1856 | 12 | 0.2272831441 | ideal
30 | 1920 | 12 | 0.0232568034 | ideal
31 | 1984 | 14 | 0.2291734081 | ideal
32 | 2048 | 15 | 0.0211017082 | ideal
33 | 2112 | 14 | 0.0208340462 | ideal
34 | 2176 | 16 | 0.0223889952 | ideal
35 | 2240 | 14 | 0.0652203901 | ideal
36 | 2304 | 14 | 0.1539362265 | ideal
37 | 2368 | 15 | 0.0107544266 | ideal
38 | 2432 | 15 | 0.0632699526 | nadir
39 | 2496 | 14 | 0.0029308958 | ideal
40 | 2560 | 13 | 0.0662293451 | nadir
41 | 2624 | 11 | 0.1562516771 | ideal
42 | 2688 | 14 | 0.0265877040 | ideal
43 | 2752 | 12 | 0.1336083621 | ideal
44 | 2816 | 11 | 0.0112375769 | ideal
45 | 2880 | 13 | 0.0645179513 | ideal
46 | 2944 | 12 | 0.1163267668 | ideal
47 | 3008 | 14 | 0.0715672492 | ideal
48 | 3072 | 15 | 0.0359656845 | ideal
49 | 3136 | 15 | 0.0290834801 | ideal
50 | 3200 | 14 | 0.0843695595 | ideal
51 | 3264 | 15 | 0.0212798019 | ideal
52 | 3328 | 14 | 0.1578133269 | ideal
53 | 3392 | 15 | 0.1111127538 | ideal
54 | 3456 | 15 | 0.1527773684 | ideal
55 | 3520 | 18 | 0.0647269001 | ideal
56 | 3584 | 10 | 0.2661438129 | ideal
57 | 3648 | 8 | 0.0491141417 | ideal
58 | 3712 | 10 | 0.1972661490 | ideal
59 | 3776 | 14 | 0.0282580878 | ideal
60 | 3840 | 15 | 0.0121572147 | f
Optimization Progress and Results¶
The OptimizationResults which are returned contain information about the progress of the optimization.
For example, the attributes x and f contain the final value(s) of parameters and the objective function.
if __name__ == "__main__":
print(optimization_results.x)
print(optimization_results.f)[[7.40134434e+00 9.86732866e+02 7.50085924e-03 1.01344552e-03
1.04178208e+01]
[2.97091005e+00 7.29792764e+02 4.07089546e-03 1.37025201e-03
9.97912957e+00]
[8.73517131e+00 9.86283644e+02 8.85665231e-03 1.01390711e-03
1.01543815e+01]
[5.71285679e+00 7.96372673e+02 7.17359721e-03 1.25569351e-03
1.05845365e+01]
[8.34651888e+00 9.88080135e+02 8.44720846e-03 1.01206366e-03
1.02532446e+01]
[7.51967596e+00 9.98281396e+02 7.53262155e-03 1.00172156e-03
1.05647766e+01]
[6.79115861e+00 9.88651926e+02 6.86910978e-03 1.01147833e-03
1.04736174e+01]
[6.16646889e+00 9.11403268e+02 6.76590605e-03 1.09720914e-03
1.04327875e+01]
[5.64722129e+00 8.24202442e+02 6.85174054e-03 1.21329415e-03
1.04684073e+01]
[6.20075107e+00 9.87492760e+02 6.27928762e-03 1.01266565e-03
1.06135980e+01]
[5.63887914e+00 8.24202442e+02 6.84161906e-03 1.21329415e-03
1.04684073e+01]
[4.91274304e+00 7.28741328e+02 6.74140858e-03 1.37222902e-03
1.05003404e+01]
[6.70934914e+00 9.79218215e+02 6.85174054e-03 1.02122283e-03
1.04667574e+01]
[6.51278785e+00 9.81670271e+02 6.63439451e-03 1.01867198e-03
1.05977542e+01]
[6.82859473e+00 9.98147571e+02 6.84126770e-03 1.00185587e-03
1.06267587e+01]]
[[2.87252352e-03 3.08384801e-04 4.52488657e-04 1.55586689e-04
4.06281622e-03 3.67082058e-03]
[3.17666868e-02 9.14230477e-04 4.31857923e-02 1.76706471e-03
4.70940651e-02 1.06957941e-03]
[1.97486278e-03 5.21537676e-03 4.52488657e-04 4.55914690e-03
1.08342319e-03 7.02857896e-03]
[8.97665827e-04 6.16608811e-05 4.29861606e-03 2.03049776e-03
9.47967269e-03 7.46588275e-03]
[1.97486278e-03 3.53643677e-03 4.52488657e-04 3.09071598e-03
2.97940614e-03 6.08826366e-03]
[0.00000000e+00 8.17990160e-04 5.65604828e-03 5.59465903e-04
1.08338123e-02 4.11018826e-03]
[4.84735880e-03 1.50234061e-03 1.80995277e-03 2.31392390e-03
1.89598906e-03 1.76095516e-03]
[5.74499963e-03 7.12584025e-04 3.84613488e-03 1.19926014e-03
5.41711756e-04 2.87084335e-03]
[4.84735880e-03 2.96537840e-05 1.80995277e-03 7.92748815e-04
1.89598906e-03 5.42569158e-03]
[4.84735880e-03 3.72834410e-03 1.35746523e-03 4.48094123e-03
3.25025941e-03 3.59009676e-04]
[4.84735880e-03 3.64159839e-05 2.03619628e-03 7.63834459e-04
1.62513400e-03 5.39126578e-03]
[4.84735880e-03 6.45162015e-04 1.58370908e-03 2.91613674e-03
2.43769831e-03 8.19017828e-03]
[4.84735880e-03 1.36078692e-03 2.26243958e-03 2.19266458e-03
1.35427870e-03 1.86394173e-03]
[3.77017962e-03 2.89722824e-03 6.78732927e-04 3.19596446e-03
5.41706511e-03 1.59497584e-03]
[1.97486278e-03 2.74066712e-03 3.16741022e-03 2.86542346e-03
8.39633573e-03 2.06449741e-03]]
After optimization, several figures can be plotted to vizualize the results. For example, the convergence plot shows how the function value changes with the number of evaluations.
if __name__ == "__main__":
optimization_results.plot_convergence()The plot_objectives method shows the objective function values of all evaluated individuals. Here, lighter color represent later evaluations. Note that by default the values are plotted on a log scale if they span many orders of magnitude. To disable this, set autoscale=False.
if __name__ == "__main__":
optimization_results.plot_objectives()All figures are saved automatically in the working_directory.
Moreover, results are stored in a .csv file.
The
results_all.csvfile contains information about all evaluated individuals.The
results_last.csvfile contains information about the last generation of evaluated individuals.The
results_pareto.csvfile contains only the best individual(s).