fit.objects¶
Definition of Objects used in FitProblems and optimization.
MappingKind
¶
Bases: StrEnum
How the data of the fit mappings of a FitMappingCollection is used.
training (default) : the mappings are fitted, i.e., their residuals enter
the cost of the optimization.
validation : the mappings are not fitted. They are simulated and
evaluated with the training data when a fit is reported, which shows how
the fitted parameters describe data they were not fitted on.
outlier : the mappings are not fitted, because the data is not usable,
e.g. a curve which contradicts the rest of the data. They are simulated and
evaluated like the validation data, so a report says how far the data a fit
dropped is from the model and the decision to drop it can be checked.
excluded : the mappings are not used at all, and for another reason than
an outlier: the model does not describe them, e.g. a study arm with a
coadministration the model has no interaction for. The data is fine, the
model is not the one for it, so it is not an outlier: an outlier is a
decision about the data and an exclusion is a decision about the model.
Excluded mappings are not resolved, so they have no metrics.
The kind is set on the FitMappingCollection, i.e., when the data of a fit is
selected, not on the fit mappings of a simulation experiment: a mapping
describes a curve, the kind describes what a fit does with it, and the same
curve is training data of one fit and validation data of another.
NoiseDistribution
¶
Bases: StrEnum
Distribution of the noise of a measurement.
These are the distributions of PEtab v2. The simulation is the median of
the distribution and the noise formula gives its scale: the standard
deviation of normal, the standard deviation of the logarithm of
log-normal and the scale b of laplace and, on the logarithm, of
log-laplace.
PriorDistribution
¶
Bases: StrEnum
Distribution of the prior of a parameter.
These are the priors of PEtab v2, with the parameters of PEtab, e.g. the
mean and the standard deviation of normal and the bounds of uniform.
Prior
dataclass
¶
The prior of a parameter.
The prior is a distribution of PEtab v2 over the value of the parameter in its unit, truncated at the bounds of the parameter, i.e. normalized over them. A parameter without a prior has the uniform prior over its bounds.
Attributes:
| Name | Type | Description |
|---|---|---|
distribution |
PriorDistribution
|
the distribution. |
parameters |
tuple[float, ...]
|
the parameters of the distribution, see
|
log_density
¶
Get the log density of the prior at a value.
The distributions are the ones of petab, which the PEtab test suite
is calculated with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
the value of the parameter. |
required |
lower
|
float
|
the lower bound of the parameter. |
required |
upper
|
float
|
the upper bound of the parameter. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The log density of the prior truncated at the bounds, |
float
|
outside of them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the parameters do not fit the distribution. |
NoiseParameter
dataclass
¶
A parameter of a noise formula which is not an entity of a model.
Attributes:
| Name | Type | Description |
|---|---|---|
pid |
str
|
id of the parameter. |
value |
float
|
nominal value, which the log-likelihood uses unless the
parameter set it is evaluated at has a value for |
estimate |
bool
|
whether the problem the noise model was read from estimates
the parameter. |
lower_bound |
float | None
|
lower bound of the estimation, |
upper_bound |
float | None
|
upper bound of the estimation, |
NoiseModel
dataclass
¶
NoiseModel(
formula,
distribution=NORMAL,
placeholders=(),
placeholder_values=(),
parameters=(),
observable=None,
)
The noise of the measurements of a fit mapping.
The noise model does not enter the cost of a fit, which is a weighted
least squares fit. It is what the log-likelihood of a problem is
calculated with, see sbmlsim.fit.petab_v2.likelihood.
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
str
|
the noise formula in the math of PEtab over the selections of
roadrunner, e.g. |
distribution |
NoiseDistribution
|
distribution of the noise. |
placeholders |
tuple[str, ...]
|
symbols of the formula which have a value per measurement. |
placeholder_values |
tuple[tuple[float | str, ...], ...]
|
for every measurement the values of the placeholders, in the order of the measurements of the mapping. A value is a number or a formula of the selections. |
parameters |
tuple[NoiseParameter, ...]
|
the parameters of the formula and of the placeholder values with their nominal value, for a symbol which the simulation does not select. |
observable |
str | None
|
symbol of the formula which stands for the simulation,
|
formula_model
cached
property
¶
Get the noise formula with its placeholders as a formula model.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the formula or a placeholder value is not valid math. |
symbols
property
¶
Get the symbols the noise reads besides placeholders and observable.
These are selections of the simulation or parameters, sorted.
select
¶
Get the noise model of a selection of the measurements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
whether a measurement is selected, one entry per measurement. |
required |
Returns:
| Type | Description |
|---|---|
NoiseModel
|
The noise model with the placeholder values of the selected |
NoiseModel
|
measurements. |
ObservableModel
dataclass
¶
An observable which is a formula of the selections of a simulation.
The formula is the math of PEtab over the selections of roadrunner, i.e.
[S] is the concentration of a species and S its amount, e.g.
scale * ([pSTAT] + [ppSTAT]), see sbmlsim.simulator.formula. A
placeholder is a symbol which has a value per measurement, e.g. an offset
which differs between the measurements of one observable; its value is a
number or a formula of the selections, typically a parameter of the model.
The fit evaluates the observable at the data, so the values of the
placeholders belong to the data points of the reference of the mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
str
|
the formula of the observable. |
placeholders |
tuple[str, ...]
|
symbols of the formula which have a value per measurement. |
placeholder_values |
tuple[tuple[float | str, ...], ...]
|
for every measurement the values of the placeholders, in the order of the reference data of the mapping. A value is a number or a formula of the selections. |
unit |
str
|
unit of the value of the formula, the reference data is converted into it. |
symbols
property
¶
Get the selections the observable reads, sorted.
These are the symbols of the formula which are not placeholders and the symbols of the placeholder values which are formulas.
select
¶
Get the observable of a selection of the measurements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
whether a measurement is selected, one entry per measurement. |
required |
Returns:
| Type | Description |
|---|---|
ObservableModel
|
The observable with the placeholder values of the selected |
ObservableModel
|
measurements. |
evaluate
¶
Evaluate the observable at the measurements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Mapping[str, ndarray]
|
the values of the |
required |
size
|
int
|
the number of measurements. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The value of the observable at every measurement. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the observable has placeholders and not the values
of |
FitMappingCollection
¶
FitMappingCollection(
experiment,
mappings=None,
sid=None,
weights=None,
use_mapping_weights=False,
fit_parameters=None,
exclude=False,
kind=TRAINING,
)
The fit mappings of a simulation experiment which a fit uses together.
A collection selects mappings of one SimulationExperiment, says what a fit
does with them (MappingKind) and how they are weighted. It is the unit a
fit is defined in and the unit a PEtab problem is built from: the mappings
of a collection which share a simulation are one experiment of PEtab, see
sbmlsim.fit.petab_v2.
Initialize simulation experiment used in a fitting.
The weights must be updated according to the mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment
|
type[SimulationExperiment]
|
simulation experiment class the mappings belong to. |
required |
mappings
|
list[str] | None
|
mappings to use from the experiment. |
None
|
sid
|
str | None
|
id of the collection, which names the experiments of a PEtab
problem. The name of the simulation experiment class and the
kind of its mappings by default, i.e. |
None
|
weights
|
float | list[float] | None
|
weight of the mappings, the larger the value the larger the weight. A single value is used for all mappings. |
None
|
use_mapping_weights
|
bool
|
use the weights of the mappings instead of |
False
|
fit_parameters
|
dict[str, list[FitParameter]] | None
|
LOCAL parameters only changed in this simulation experiment. |
None
|
exclude
|
bool
|
flag to exclude the experiment from the fitting. |
False
|
kind
|
MappingKind
|
what a fit does with these mappings, i.e., whether they are fitted, only evaluated or not used at all. |
TRAINING
|
Raises:
| Type | Description |
|---|---|
ValueError
|
for duplicate mappings or unsupported local fit parameters. |
weights
property
writable
¶
Weights of fit mappings, None if the weight of the mapping is used.
resolve_mappings
¶
Use all mappings of the experiment if no mappings were selected.
A FitMappingCollection without mappings uses all fit mappings of its simulation
experiment. The keys are only known once the experiment is instantiated,
so the mappings and their weights are resolved in the initialization of the
OptimizationProblem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping_keys
|
Iterable[str]
|
keys of all fit mappings of the simulation experiment. |
required |
reduce
staticmethod
¶
Combine the fit mappings of the FitMappingCollections of the same experiment.
Experiments are combined per simulation experiment and MappingKind,
so that the training and the validation data stay apart. The mappings
and their weights are concatenated, the inputs are not modified.
Raises:
| Type | Description |
|---|---|
ValueError
|
if experiments of the same class cannot be combined, i.e., they use different weighting or repeat a mapping. |
MappingMetaData
dataclass
¶
Metadata for mapping.
Applications derive their metadata from this class to describe the curve,
e.g., the tissue, the route, the dosing or the health of the group of a
study. The metadata describes the data, not what a fit does with it: how a
curve is used is the MappingKind of the FitMappingCollection which selects it.
The fields are keyword only, so that a subclass can add fields without a default.
FitMapping
¶
FitMapping(
experiment,
reference,
observable,
weight=None,
metadata=None,
noise=None,
observable_model=None,
)
Mapping of reference data to observable data.
In the optimization the difference between the reference data (ground truth) and the observable (predicted data) is minimized. The weight allows to weight the FitMapping.
Initialize FitMapping.
To use the weight in the fit mapping the use_mapping_weights flag
must be set on the FitMappingCollection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment
|
Any
|
simulation experiment of the mapping. |
required |
reference
|
FitData
|
reference data (mostly experimental data). |
required |
observable
|
FitData
|
observable in the model. |
required |
weight
|
float | None
|
weight of the fit mapping, the count of the reference data is used if no weight is given. |
None
|
metadata
|
MappingMetaData | None
|
metadata of the mapping. |
None
|
noise
|
NoiseModel | None
|
noise model of the measurements, which the log-likelihood
of the problem uses. Without one the noise is normal with the
standard deviation of the reference data, see
|
None
|
observable_model
|
ObservableModel | None
|
the observable as a formula of the selections
of the task of |
None
|
weight
property
¶
Return the defined weight or the count of the reference data.
Raises:
| Type | Description |
|---|---|
ValueError
|
if neither a weight nor a count is available. |
FitParameter
¶
FitParameter(
pid,
start_value=None,
lower_bound=-inf,
upper_bound=inf,
unit=None,
target=None,
mappings=None,
scale=None,
prior=None,
description=None,
)
Parameter adjusted in a parameter optimization.
The bounds define the box in which the parameter can be varied. The start value is the initial value in the parameter fitting for algorithms which use it.
Initialize FitParameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pid
|
str
|
id of the estimated parameter. It is the name in the parameter
vector, in the parameter sets and in the profiles; it is the id
of the entity of the model unless |
required |
start_value
|
float | None
|
initial value for the fitting. |
None
|
lower_bound
|
float
|
lower bound for the fitting. |
-inf
|
upper_bound
|
float
|
upper bound for the fitting. |
inf
|
unit
|
str | None
|
unit of the parameter, the model unit is assumed if not given.
It is a unit of pint or has the unit ids of the model of its
target, e.g. |
None
|
target
|
str | None
|
entity of the model the value is written to. |
None
|
mappings
|
Any
|
|
None
|
scale
|
ParameterScaleType | str | None
|
space the optimizer searches the parameter in, or its name.
|
None
|
prior
|
Prior | Mapping[str, Any] | None
|
the prior of the parameter or its dictionary, see |
None
|
description
|
str | None
|
description of the parameter for the reports of a
fit. It is documentation and does not take part in the
equality; |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if the bounds or the start value are inconsistent, if a value is not a number, if the scale is not a scale, or if the target is the prefix of an external target alone. |
is_external
property
¶
Check whether the parameter is not an entity of the model.
The target of such a parameter has the prefix EXTERNAL_PREFIX. The
fit writes no change for it, the derived changes of the problem read
its value.
is_versioned
property
¶
Check whether the parameter applies to a part of the data only.
to_json
¶
Serialize to JSON.
Serializes to file if path is provided, otherwise returns JSON string.
FitData
¶
FitData(
experiment,
xid,
yid,
xid_sd=None,
xid_se=None,
yid_sd=None,
yid_se=None,
count=None,
dataset=None,
task=None,
function=None,
sel=None,
)
Data used in a fit.
This is either data from a dataset, a simulation results from a task or functional data, i.e. calculated from other data.
Initialize FitData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment
|
Any
|
simulation experiment the data belongs to. |
required |
xid
|
str | None
|
index of the x data, |
required |
yid
|
str
|
index of the y data. |
required |
xid_sd
|
str | None
|
index of the standard deviation of the x data. |
None
|
xid_se
|
str | None
|
index of the standard error of the x data. |
None
|
yid_sd
|
str | None
|
index of the standard deviation of the y data. |
None
|
yid_se
|
str | None
|
index of the standard error of the y data. |
None
|
count
|
int | str | None
|
number of subjects, either an integer or a column of the dataset. |
None
|
dataset
|
str | None
|
id of the dataset the data comes from. |
None
|
task
|
str | None
|
id of the task the data comes from. |
None
|
function
|
str | None
|
id of the function the data is calculated with. |
None
|
sel
|
Mapping[str, Any] | None
|
labels of the dimensions of a task, or column values of the rows
of a dataset, for every data of the fit data, see |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
FitDataInitialized
dataclass
¶
Initialized FitData with actual data content.
The data is created from the simulation experiment, the values are quantities with the units of the data.
describe_array
¶
Describe an array of elements, e.g. of a network, in one line of text.
The console and the text reports show an array in place of its elements, because a network has hundreds of them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
what the array is, e.g. |
required |
elements
|
int
|
number of elements of the array, estimated or not. |
required |
members
|
Sequence[FitParameter]
|
the parameters of the fit which are elements of the array. A versioned element has one parameter per version. |
required |
values
|
Sequence[float | None]
|
the value of every member, e.g. its start value, |
required |
Returns:
| Type | Description |
|---|---|
str
|
The number of estimated elements, the minimum, the maximum and the |
str
|
norm of the values of the members and the bounds when the members |
str
|
agree on them. |