fit¶
The fit settings, the metadata filters and the selection of the mappings.
The models of pkdb_models share their fit settings apart from the integrator
tolerances (default_fit_settings). Every mapping_collections.py checks the
type of the metadata of a mapping (mapping_metadata), reads the observable of
a mapping (observable_id) and selects the training data by the values of the
metadata (metadata_filter) or the observable (observable_filter).
select_mappings selects the data of a fit with the paths of the model object,
see sbmlsim.fit.helpers.FitMappings.select for the filters, outliers and
validation data.
FIT_ID
module-attribute
¶
The form of the id of a fit, <subset>_<date>_<time>__<hash>.
MetadataFilter
dataclass
¶
A filter of fit mappings by the values of their metadata.
A mapping passes when every allowed field has one of its values and no
excluded field has one of its values. Build it with metadata_filter.
Attributes:
| Name | Type | Description |
|---|---|---|
metadata_type |
type[MappingMetaData]
|
the metadata class, |
allowed |
tuple[tuple[str, frozenset[Hashable]], ...]
|
the fields with the values a mapping must have. |
excluded |
tuple[tuple[str, frozenset[Hashable]], ...]
|
the fields with the values a mapping must not have. |
ObservableFilter
dataclass
¶
A filter of fit mappings by their observable, see observable_id.
Attributes:
| Name | Type | Description |
|---|---|---|
observables |
frozenset[str]
|
the ids of the observables a mapping may have, the index
of the data ( |
NamedFilter
dataclass
¶
A filter of fit mappings by their key and study, see named.
Attributes:
| Name | Type | Description |
|---|---|---|
mappings |
tuple[tuple[str, frozenset[str]], ...]
|
the study ids with their mapping keys, sorted, so that the filter compares, pickles and describes the same for equal mappings. |
LeveledFitDefinition
dataclass
¶
Bases: FitDefinition
A fit definition whose problems load their models at an optimization level.
sbmlsim builds the problem of a definition without a level, so it takes the
level of the environment variable SBMLSIM_JIT_OPTIMIZATION of the process
which builds it. The problem resolves its level once and carries it into
every worker of a pool (OptimizationProblem.__getstate__), which loads the
models with it: the problems of this definition get the level of the model,
in the process and in every worker, whatever the environment of the
process and of a forkserver which started the workers.
Attributes:
| Name | Type | Description |
|---|---|---|
jit_optimization |
str | None
|
the level of LLJIT ( |
default_fit_settings
¶
Get the settings of the fits of a model.
Normalized residuals with a linear loss; the curves are weighted by the
weights of their mappings and by their number of points, the points by their
errors (a mapping without errors is weighted with a CV of 0.5); the
integrator takes variable steps; a data point at the time of a change after
the start is compared with the state before the change (data_at_change),
sbmlsim's default, written out so that a change of the default does not
change the fits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
absolute_tolerance
|
float | AbsoluteTolerance
|
the absolute tolerance of the integrator, one value or
an |
required |
relative_tolerance
|
float
|
the relative tolerance of the integrator. |
1e-06
|
Returns:
| Type | Description |
|---|---|
FitSettings
|
The settings, stored with the result of a fit and read back by its report. |
mapping_metadata
¶
mapping_metadata(
fit_mapping: FitMapping,
) -> PKPDMappingMetaData
mapping_metadata(
fit_mapping, metadata_type=PKPDMappingMetaData
)
Get the metadata of a fit mapping, checked against the type of the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_mapping
|
FitMapping
|
the fit mapping. |
required |
metadata_type
|
type[MappingMetaData]
|
the metadata class, |
PKPDMappingMetaData
|
Returns:
| Type | Description |
|---|---|
MappingMetaData
|
The metadata. |
Raises:
| Type | Description |
|---|---|
TypeError
|
if the mapping has no metadata of the type. |
observable_id
¶
Get the observable of a fit mapping, the index of its data.
The index of the y data of the observable (sbmlsim.data.Data.index): the
selection without the brackets of a concentration, Cve_hctz for
[Cve_hctz], Aurine_hctz for Aurine_hctz. sbmlsim 0.9 writes the
concentration into the id of the data (<task>__conc__Cve_hctz), so the
id is no longer read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_mapping
|
FitMapping
|
the fit mapping. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The id of the observable, e.g. |
check_sampling_sites
¶
check_sampling_sites(
fit_mappings, sites, metadata_type=PKPDMappingMetaData
)
Check the sampling site of the mappings against their observable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_mappings
|
FitMappings | Iterable[FitMapping]
|
the fit mappings, or the instantiated experiments of a
model ( |
required |
sites
|
Mapping[str, SamplingSite]
|
the sampling site of every observable by observable id, e.g.
|
required |
metadata_type
|
type[PKPDMappingMetaData]
|
the metadata class, |
PKPDMappingMetaData
|
Returns:
| Type | Description |
|---|---|
list[str]
|
One problem per mapping whose site differs from the site of its |
list[str]
|
observable or whose observable is not in |
metadata_filter
¶
metadata_filter(
metadata_type=PKPDMappingMetaData,
/,
*,
excluded=None,
**allowed,
)
Build a filter of fit mappings by the values of their metadata.
# not executed
filter_control = metadata_filter(
route={Route.PO, Route.IV},
coadministration={Coadministration.NONE},
excluded={"dosing": {Dosing.MULTIPLE}},
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_type
|
type[MappingMetaData]
|
the metadata class, |
PKPDMappingMetaData
|
excluded
|
Mapping[str, Collection[Hashable]] | None
|
the fields with the values a mapping must not have. |
None
|
**allowed
|
Collection[Hashable]
|
the fields with the values a mapping must have. |
{}
|
Returns:
| Type | Description |
|---|---|
MetadataFilter
|
The filter, called by the selection of the mappings of a fit. |
Raises:
| Type | Description |
|---|---|
TypeError
|
if the metadata class is no dataclass (or an instance of
one), has no field of a given name, or the values of a field are not
a collection (a single value, also a string or |
ValueError
|
for a filter without clauses, with an empty set of allowed values, which would select nothing, or with an empty set of excluded values, which would exclude nothing. |
observable_filter
¶
Build a filter of fit mappings by their observable, e.g. the PK data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observables
|
Collection[str]
|
the ids of the observables, the index of the data
( |
required |
Returns:
| Type | Description |
|---|---|
ObservableFilter
|
The filter. |
Raises:
| Type | Description |
|---|---|
TypeError
|
if the observables are a single id or not a collection. |
ValueError
|
for no observables, which would select nothing. |
named
¶
Check whether a fit mapping is one of the named mappings of its study.
The key alone is not enough: two studies can share a key, so it counts only for the study it is named with.
named_filter
¶
Build a filter of the fit mappings named with their study.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mappings
|
NamedMappings
|
the mapping keys by study id. |
required |
Returns:
| Type | Description |
|---|---|
NamedFilter
|
The filter. |
select_mappings
¶
Select the data of a fit of a model, see sbmlsim.fit.helpers.FitMappings.
The experiments are created with the paths of the model object; the counts of the selected mappings per experiment and kind are printed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model object. |
required |
experiment_classes
|
Iterable[type[SimulationExperiment]]
|
the experiments with the fit mappings. |
required |
filters
|
MappingFilter | Iterable[MappingFilter]
|
the filters of the training data. |
()
|
outliers
|
Iterable[str]
|
the keys of the training data which is not usable. |
()
|
validation
|
Iterable[str] | MappingFilter
|
the keys or a filter of the validation data. |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, list[FitMappingCollection]]
|
The fit mapping collections of all kinds by experiment id. |
fit_subset
¶
Get the fit problem a fit belongs to from its id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_id
|
str
|
the id of a fit, e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The subset, e.g. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the id does not have the form of the id of a fit, also if
it holds a path separator or |
fit_date
¶
Get the day a fit ran from its id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_id
|
str
|
the id of a fit, e.g. |
required |
Returns:
| Type | Description |
|---|---|
date
|
The date of the id, e.g. 2026-10-16. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the id does not have the form of the id of a fit, see
|
remainder_problems
¶
Check that the fitted fractions of a remainder cannot sum above 1.
A remainder is the initial assignment 1 - a - b - ... of a constant of
the model (the remainder REST of the product fractions of a metabolism
of pkpdsim.pbpk, f_<p>_<s>): it keeps the sum of the fractions at 1,
and is negative, a product formed at a negative rate, when the other
fractions sum above 1. run_fit refuses a fit whose bounds allow it
before it starts: the upper bounds of the fitted fractions of a remainder
and the values of the others (the fixed values of the adopted fits, else
those of the model) sum to at most 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sbml_path
|
Path
|
the model the fit simulates. |
required |
parameters
|
Iterable[FitParameter]
|
the parameters of the fit. |
required |
fixed_values
|
Mapping[str, float] | None
|
the values of parameters the fit keeps fixed, by id. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The problems, naming the remainder, the fitted parameters and their |
list[str]
|
upper bounds. |
at_level
¶
Get a fit definition whose problems load their models at a level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definition
|
FitDefinition
|
the fit definition of the model. |
required |
level
|
str | None
|
the optimization level of the model
( |
required |
Returns:
| Type | Description |
|---|---|
FitDefinition
|
The definition itself without a level, else a |
FitDefinition
|
with its fields. |
leveled_definitions
¶
Get the fit definitions of a model at its optimization level, see at_level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, FitDefinition]
|
The fit definitions by name. |
start_point
¶
Get the parameters a fit starts from (run_fit --start).
The start is a json file of the values by parameter id, in the units of
the fit parameters (FitParameter.unit), or the id of a fit: an adopted
fit of the model (PKPDModel.fits_dir) or else a finished fit in the
output directory of the fits, whose best parameters (parameters.json, in
the units of the model) are converted into the units of the fit
parameters. A fit of another problem gives the parameters which the
definition fits. A parameter which the start does not give keeps its own
start value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
definition
|
FitDefinition
|
the fit problem. |
required |
start
|
str
|
the json file or the fit id. |
required |
output_dir
|
Path
|
the directory of the finished fits. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
The start value of every parameter the start gives which the |
dict[str, float]
|
definition fits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the start is neither a file nor a fit, a value is not a number, a parameter of the file is not fitted, a value of a fit has no unit for a fit parameter with one, or a value is outside of the bounds of its parameter. |
with_start
¶
Get a fit problem whose parameters start from a point.
The parameters are copies, so the definition of the model keeps its start values (the definitions are objects of the module of the model).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definition
|
FitDefinition
|
the fit problem. |
required |
point
|
Mapping[str, float]
|
the start value by parameter id. |
required |
Returns:
| Type | Description |
|---|---|
FitDefinition
|
The fit problem with the start values of the point. |
fixed_values
¶
Get what a fit of a subset keeps fixed: the adopted fits of the other problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
subset
|
str
|
the fit problem, e.g. |
required |
definition
|
FitDefinition
|
its definition, whose parameters are fitted and not fixed. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The adopted fits of the other subsets ( |
dict[str, float]
|
values of their parameters in the units of the model, without the |
tuple[list[str], dict[str, float]]
|
parameters the definition fits. |
current_fixed_fits
¶
Get the adopted fits whose parameters a fit of a subset keeps fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
subset
|
str
|
the fit problem, e.g. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The adopted fits of the model of the other subsets, in the order of |
list[str]
|
|
recorded_fixed_fits
¶
Get the fixed adopted fits which the provenance of a fit records.
run_fit records them in the arguments of the provenance (FIXED_FITS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
FitRecords
|
the records of the fit ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
The ids of the fixed fits, none for a fit without a result, without a |
...
|
provenance or of an older pkpdsim, which did not record them. |
report_context
¶
Get what the report of a fit says about the model beyond the fit itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
subset
|
str
|
the fit problem, e.g. |
required |
definition
|
FitDefinition
|
the definition of the fit problem. |
required |
fixed_fits
|
Sequence[str]
|
the adopted fits whose parameters the fit kept fixed, the
current ones for a fit which runs ( |
required |
Returns:
| Type | Description |
|---|---|
ReportContext
|
The title of the model and the fit, the description of the fit and of |
ReportContext
|
its parameters, and a note on the fixed adopted fits, none without |
ReportContext
|
them. |
records_context
¶
Get the context of the report of a fit from its records.
It is the context of sbmlsim.fit.cli.report_cli and of
sbmlsim.fit.cli.report_from_records for a model: the fixed adopted fits
are those the provenance of the record names (recorded_fixed_fits), not
the adopted fits of the model now.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PKPDModel
|
the model. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[str, FitDefinition, FitRecords], ReportContext]
|
A function of the subset, its definition and the records of the fit |
Callable[[str, FitDefinition, FitRecords], ReportContext]
|
which returns the context of its report ( |