ModelBuilder
- class openpkpd.api.model_builder.ModelBuilder[source]
Bases:
objectFluent builder API for constructing openpkpd models in pure Python.
Chain method calls to configure the model, then call .build() to produce a BuiltModel ready for estimation.
- data(path, id='ID', time='TIME', dv='DV', amt='AMT', evid='EVID', **kwargs)[source]
Specify the dataset file path and column mappings.
- Parameters:
- Return type:
- dataset(ds)[source]
Provide a pre-loaded NONMEMDataset directly.
- Return type:
- Parameters:
ds (NONMEMDataset)
- subroutines(advan=2, trans=2, **kwargs)[source]
Set ADVAN and TRANS subroutine numbers and optional subroutine kwargs.
- Return type:
- Parameters:
- error(code)[source]
Set the $ERROR code block (NM-TRAN syntax).
- Return type:
- Parameters:
code (str)
- des(code)[source]
Set the $DES code block for ODE models (NM-TRAN syntax).
- Return type:
- Parameters:
code (str)
- theta(specs, labels=None)[source]
Set THETA initial estimates and bounds.
- Return type:
- Parameters:
- Each element can be:
A float: initial value, unbounded
A 2-tuple (lower, init): lower-bounded
A 3-tuple (lower, init, upper): fully bounded
A ThetaSpec instance
Examples
.theta([1.5, (0, 0.1, 1), (0, 30)])
- estimation(method='FOCE', **kwargs)[source]
Set estimation method and options.
- Parameters:
- Return type:
- covariance(matrix='SR', **kwargs)[source]
Enable covariance step after estimation.
- Return type:
- Parameters:
- impute_covariates(columns, method='locf')[source]
Impute missing covariate values after loading the dataset.
Applied automatically during
build()before model assembly.- Parameters:
- Return type:
- Returns:
self (fluent interface).
- clone()[source]
Return a copy of this builder. Callable attributes are shared by reference.
- Return type:
- class openpkpd.api.model_builder.BuiltModel(population_model, params, estimation_kwargs=<factory>, do_covariance=False, covariance_kwargs=<factory>)[source]
Bases:
objectA fully assembled model ready to fit.
Returned by ModelBuilder.build(). Call .fit() to run estimation.
- Parameters:
- population_model: PopulationModel
- params: ParameterSet
- design(sampling_times=None)[source]
Return a
PFIMEnginewired to this model.The engine can then evaluate or optimise the population Fisher Information Matrix (FIM) for the current parameters and model.
- Parameters:
sampling_times (
list[float] |ndarray|None) – Default sampling times (hours). IfNone, a 24-hour hourly grid is used as a starting point forcompute_fim/optimize_design.- Return type:
PFIMEngine- Returns:
PFIMEngineready to call.compute_fim()or.optimize_design().
Example:
built = ModelBuilder()...build() result = built.fit() engine = built.design() fim = engine.compute_fim(np.linspace(0.5, 24, 8)) optimal = engine.optimize_design(n_samples=6, t_min=0, t_max=24) print(optimal.summary())
- simulate(n_replicates=1, seed=42, result=None)[source]
Simulate replicate datasets from the fitted model.
Convenience wrapper around SimulationEngine that optionally runs estimation first if no EstimationResult is provided.
- Parameters:
n_replicates (
int) – Number of simulated datasets to generate (default 1).seed (
int) – Random seed for reproducibility (default 42).result (
EstimationResult|None) – Pre-computed EstimationResult. If None, calls fit() first.
- Return type:
SimulationResult- Returns:
SimulationResult with simulated_df, seed, and n_replicates.
Example
model = ModelBuilder()…build() result = model.fit() sim = model.simulate(n_replicates=500, result=result) print(sim.simulated_df.head())