Calibrator API¶
Calibrator ¶
A Facade for running parameter calibration from a defined CalibrationProblem.
Source code in commol/api/calibrator.py
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 | |
Attributes¶
Functions¶
__init__ ¶
__init__(simulation: Simulation, problem: CalibrationProblem)
Initializes the calibration from a Simulation and CalibrationProblem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulation
|
Simulation
|
A fully initialized Simulation object with the model to calibrate. |
required |
problem
|
CalibrationProblem
|
A fully constructed and validated calibration problem definition. |
required |
Source code in commol/api/calibrator.py
run ¶
run() -> CalibrationResult
Runs the calibration optimization.
Returns:
| Type | Description |
|---|---|
CalibrationResult
|
Object containing the optimized parameter values, final loss, convergence status, and other optimization statistics. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Rust extension is not available. |
ValueError
|
If calibration problem setup is invalid. |
RuntimeError
|
If optimization fails. |
Source code in commol/api/calibrator.py
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 | |
Related Classes¶
CalibrationProblem¶
CalibrationProblem ¶
Bases: BaseModel
Defines a complete calibration problem.
This class encapsulates all the information needed to calibrate model parameters against observed data. It provides validation of the calibration setup but delegates the actual optimization to the Rust backend.
Attributes:
| Name | Type | Description |
|---|---|---|
observed_data |
list[ObservedDataPoint]
|
List of observed data points to fit against |
parameters |
list[CalibrationParameter]
|
List of parameters to calibrate with their bounds |
loss_config |
LossConfig
|
Configuration for the loss function |
optimization_config |
OptimizationConfig
|
Configuration for the optimization algorithm |
CalibrationResult¶
CalibrationResult ¶
Bases: BaseModel
Result of a calibration run.
This is a simple data class that holds the results returned from the Rust calibration function.
Attributes:
| Name | Type | Description |
|---|---|---|
best_parameters |
dict[str, float]
|
Dictionary mapping parameter IDs to their calibrated values |
parameter_names |
list[str]
|
Ordered list of parameter names |
best_parameters_list |
list[float]
|
Ordered list of parameter values (matches parameter_names order) |
final_loss |
float
|
Final loss value achieved |
iterations |
int
|
Number of iterations performed |
converged |
bool
|
Whether the optimization converged |
termination_reason |
str
|
Explanation of why optimization terminated |
CalibrationParameter¶
CalibrationParameter ¶
Bases: BaseModel
Defines a parameter to be calibrated with its bounds.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Parameter identifier (must match a model parameter ID) |
min_bound |
float
|
Minimum allowed value for this parameter |
max_bound |
float
|
Maximum allowed value for this parameter |
initial_guess |
float | None
|
Optional starting value for optimization (if None, midpoint is used) |
ObservedDataPoint¶
ObservedDataPoint ¶
Bases: BaseModel
Represents a single observed data point for calibration.
Attributes:
| Name | Type | Description |
|---|---|---|
step |
int
|
Time step of the observation |
compartment |
str
|
Name of the compartment being observed |
value |
float
|
Observed value |
weight |
float
|
Weight for this observation in the loss function (default: 1.0) |
LossConfig¶
LossConfig ¶
Bases: BaseModel
Configuration for the loss function used in calibration.
Attributes:
| Name | Type | Description |
|---|---|---|
function |
LossFunction
|
The loss function to use for measuring fit quality |
OptimizationConfig¶
OptimizationConfig ¶
Bases: BaseModel
Configuration for the optimization algorithm.
Attributes:
| Name | Type | Description |
|---|---|---|
algorithm |
Literal[nelder_mead, particle_swarm]
|
The optimization algorithm to use |
config |
NelderMeadConfig | ParticleSwarmConfig
|
Configuration for the selected algorithm |
NelderMeadConfig¶
NelderMeadConfig ¶
Bases: BaseModel
Configuration for the Nelder-Mead optimization algorithm.
The Nelder-Mead method is a simplex-based derivative-free optimization algorithm, suitable for problems where gradients are not available.
Attributes:
| Name | Type | Description |
|---|---|---|
max_iterations |
int
|
Maximum number of iterations (default: 1000) |
sd_tolerance |
float
|
Convergence tolerance for standard deviation (default: 1e-6) |
alpha |
float | None
|
Reflection coefficient (default: None, uses argmin's default) |
gamma |
float | None
|
Expansion coefficient (default: None, uses argmin's default) |
rho |
float | None
|
Contraction coefficient (default: None, uses argmin's default) |
sigma |
float | None
|
Shrink coefficient (default: None, uses argmin's default) |
verbose |
bool
|
Enable verbose output during optimization (default: False) |
header_interval |
int
|
Number of iterations between table header repeats in verbose output (default: 100) |
ParticleSwarmConfig¶
ParticleSwarmConfig ¶
Bases: BaseModel
Configuration for the Particle Swarm Optimization algorithm.
Particle Swarm Optimization (PSO) is a population-based metaheuristic inspired by social behavior of bird flocking or fish schooling.
Attributes:
| Name | Type | Description |
|---|---|---|
num_particles |
int
|
Number of particles in the swarm (default: 40) |
max_iterations |
int
|
Maximum number of iterations (default: 1000) |
target_cost |
float | None
|
Target cost for early stopping (optional) |
inertia_factor |
float | None
|
Inertia weight applied to velocity (default: None, uses argmin's default) |
cognitive_factor |
float | None
|
Attraction to personal best (default: None, uses argmin's default) |
social_factor |
float | None
|
Attraction to swarm best (default: None, uses argmin's default) |
verbose |
bool
|
Enable verbose output during optimization (default: False) |
header_interval |
int
|
Number of iterations between table header repeats in verbose output (default: 100) |
Enumerations¶
LossFunction¶
LossFunction ¶
Bases: str, Enum
Available loss functions for calibration.
Attributes:
| Name | Type | Description |
|---|---|---|
SSE |
str
|
Sum of Squared Errors |
RMSE |
str
|
Root Mean Squared Error |
MAE |
str
|
Mean Absolute Error |
WEIGHTED_SSE |
str
|
Weighted Sum of Squared Errors |
Attributes¶
OptimizationAlgorithm¶
OptimizationAlgorithm ¶
Bases: str, Enum
Available optimization algorithms.
Attributes:
| Name | Type | Description |
|---|---|---|
NELDER_MEAD |
str
|
Nelder-Mead simplex algorithm |
PARTICLE_SWARM |
str
|
Particle Swarm Optimization |