Skip to content

Commit

Permalink
PEtab parameter mapping: minor speedup
Browse files Browse the repository at this point in the history
Avoid repeated lookups. Saves just a few seconds for Froehlich_CellSystems2018, but better than nothing...
  • Loading branch information
dweindl committed Jan 30, 2025
1 parent 923b8f4 commit d0ce54b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 8 additions & 3 deletions python/sdist/amici/petab/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,21 @@ def _get_par(model_par, value, mapping):
# condition table overrides must have been handled already,
# e.g. by the PEtab parameter mapping, but parameters from
# InitialAssignments may still be present.
if mapping[value] == model_par:
if (mapped_value := mapping[value]) == model_par:
# prevent infinite recursion
raise
return _get_par(value, mapping[value], mapping)
if model_par in problem_parameters:
return _get_par(value, mapped_value, mapping)

try:
# user-provided
return problem_parameters[model_par]
except KeyError:
pass

# prevent nan-propagation in derivative
if np.isnan(value):
return 0.0

# constant value
return value

Expand Down
9 changes: 4 additions & 5 deletions python/sdist/amici/petab/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ def get_states_in_condition_table(
if petab_problem.model.type_id not in (MODEL_TYPE_SBML, MODEL_TYPE_PYSB):
raise NotImplementedError()

species_check_funs = {
species_check_fun = {
MODEL_TYPE_SBML: lambda x: _element_is_sbml_state(
petab_problem.sbml_model, x
),
MODEL_TYPE_PYSB: lambda x: _element_is_pysb_pattern(
petab_problem.model.model, x
),
}
}[petab_problem.model.type_id]

states = {
resolve_mapping(petab_problem.mapping_df, col): (None, None)
if condition is None
Expand All @@ -48,9 +49,7 @@ def get_states_in_condition_table(
else None,
)
for col in petab_problem.condition_df.columns
if species_check_funs[petab_problem.model.type_id](
resolve_mapping(petab_problem.mapping_df, col)
)
if species_check_fun(resolve_mapping(petab_problem.mapping_df, col))
}

if petab_problem.model.type_id == MODEL_TYPE_PYSB:
Expand Down

0 comments on commit d0ce54b

Please sign in to comment.