petab_select.model
The Model class.
Functions
|
Compare two calibrated models by their criterion values. |
|
Generate a model from a PEtab Select list of model YAML file. |
|
Classes
|
A (possibly uncalibrated) model. |
- class petab_select.model.Model(petab_yaml, model_subspace_id=None, model_id=None, model_subspace_indices=None, predecessor_model_hash=None, parameters=None, estimated_parameters=None, criteria=None, petab_problem=None, model_hash=None)[source]
A (possibly uncalibrated) model.
NB: some of these attribute names correspond to constants defined in the constants.py file, to facilitate loading models from/saving models to disk (see the saved_attributes attribute).
- criteria
The criteria values of the calibrated model (e.g. AIC).
- hash_attributes
This attribute is currently not used. Attributes that will be used to calculate the hash of the
Model
instance. NB: this hash is used during pairwise comparison to determine whether any twoModel
instances are unique. The model instances are compared by their parameter estimation problems, as opposed to parameter estimation results, which may differ due to e.g. floating-point arithmetic.
- model_id
The model ID.
- petab_yaml
The path to the PEtab problem YAML file.
- parameters
Parameter values that will overwrite the PEtab problem definition, or change parameters to be estimated.
- estimated_parameters
Parameter estimates from a model calibration tool, for parameters that are specified as estimated in the PEtab problem or PEtab Select model YAML. These are untransformed values (i.e., not on log scale).
- saved_attributes
Attributes that will be saved to disk by the
Model.to_yaml()
method.
- __init__(petab_yaml, model_subspace_id=None, model_id=None, model_subspace_indices=None, predecessor_model_hash=None, parameters=None, estimated_parameters=None, criteria=None, petab_problem=None, model_hash=None)[source]
- compute_criterion(criterion, raise_on_failure=True)[source]
Compute a criterion value for the model.
The value will also be stored, which will overwrite any previously stored value for the criterion.
- Parameters:
criterion (
Criterion
) – The ID of the criterion (e.g.petab_select.constants.Criterion.AIC
).raise_on_failure (
bool
) – Whether to raise a ValueError if the criterion could not be computed. If False, None is returned.
- Return type:
- Returns:
The criterion value.
- static from_dict(model_dict, base_path=None, petab_problem=None)[source]
Generate a model from a dictionary of attributes.
- Parameters:
model_dict (
Dict
[str
,Any
]) – A dictionary of attributes. The keys are attribute names, the values are the corresponding attribute values for the model. Required attributes are the required arguments of theModel.__init__()
method.base_path (
Union
[str
,Path
]) – The path that any relative paths in the model are relative to (e.g. the path to the PEtab problem YAML fileModel.petab_yaml()
may be relative).petab_problem (
Problem
) – Optionally provide the PEtab problem, to avoid loading it multiple times. NB: This may cause issues if multiple models write to the same PEtab problem in memory.
- Return type:
- Returns:
A model instance, initialized with the provided attributes.
- get_criterion(criterion, compute=True, raise_on_failure=True)[source]
Get a criterion value for the model.
- Parameters:
criterion (
Criterion
) – The ID of the criterion (e.g.petab_select.constants.Criterion.AIC
).compute (
bool
) – Whether to try to compute the criterion value based on other model attributes. For example, if the'AIC'
criterion is requested, this can be computed from a predetermined model likelihood and its number of estimated parameters.raise_on_failure (
bool
) – Whether to raise a ValueError if the criterion could not be computed. If False, None is returned.
- Return type:
- Returns:
The criterion value, or None if it is not available. TODO check for previous use of this method before .get was used
- get_hash()[source]
Get the model hash.
Currently designed to only use pre-calibration information, such that if a model is calibrated twice and the two calibrated models differ in their parameter estimates, then they will still have the same hash.
This is not implemented as
__hash__
because Python automatically truncates values in a system-dependent manner, which reduces interoperability ( https://docs.python.org/3/reference/datamodel.html#object.__hash__ ).- Return type:
- Returns:
The hash.
- get_parameter_values(parameter_ids=None)[source]
Get parameter values.
Includes
ESTIMATE
for parameters that should be estimated.The ordering is by
parameter_ids
if supplied, elseself.petab_parameters
.
- property petab_parameter_ids_estimated: List[str]
Get the IDs of all estimated parameters.
- Returns:
The parameter IDs.
- property petab_parameter_ids_fixed: List[str]
Get the IDs of all fixed parameters.
- Returns:
The parameter IDs.
- property petab_parameters_singular: Dict[str, float | int | Literal['estimate']]
TODO deprecate and remove?
- to_dict(resolve_paths=True, paths_relative_to=None)[source]
Generate a dictionary from the attributes of a
Model
instance.- Parameters:
- Return type:
- Returns:
A dictionary of attributes. The keys are attribute names, the values are the corresponding attribute values for the model. Required attributes are the required arguments of the
Model.__init__()
method.
- to_petab(output_path=None, set_estimated_parameters=None)[source]
Generate a PEtab problem.
- Parameters:
output_path (
Union
[str
,Path
]) – The directory where PEtab files will be written to disk. If not specified, the PEtab files will not be written to disk.set_estimated_parameters (
Optional
[bool
]) – Whether to set the nominal value of estimated parameters to their estimates. If parameter estimates are available, this will default to True.
- Return type:
- Returns:
A 2-tuple. The first value is a PEtab problem that can be used with a PEtab-compatible tool for calibration of this model. If
output_path
is notNone
, the second value is the path to a PEtab YAML file that can be used to load the PEtab problem (the first value) into any PEtab-compatible tool.
- petab_select.model.default_compare(model0, model1, criterion, criterion_threshold=0)[source]
Compare two calibrated models by their criterion values.
It is assumed that the model
model0
provides a value for the criterioncriterion
, or is theVIRTUAL_INITIAL_MODEL
.- Parameters:
- Return type:
- Returns:
True` if ``model1
has a better criterion value thanmodel0
, elseFalse
.
- petab_select.model.models_from_yaml_list(model_list_yaml, petab_problem=None, allow_single_model=True)[source]
Generate a model from a PEtab Select list of model YAML file.
- Parameters:
model_list_yaml (
Union
[str
,Path
]) – The path to the PEtab Select list of model YAML file.petab_problem (
Problem
) – SeeModel.from_dict()
.allow_single_model (
bool
) – Given a YAML file that contains a single model directly (not in a 1-element list), ifTrue
then the single model will be read in, else aValueError
will be raised.
- Return type:
- Returns:
A list of model instances, initialized with the provided attributes.