Skip to content

Commit

Permalink
Add/forward startpoint_kwargs in PetabImporter.create_problem (#1135
Browse files Browse the repository at this point in the history
)

Simplifies combining PEtab startpoint sampling with checked sampling (i.e. resample in case of non-finite fvals/gradients), e.g.:

```python
problem = importer.create_problem(
    startpoint_kwargs=dict(check_fval=True, check_grad=True)
)
```

Closes #1119
  • Loading branch information
dweindl authored Oct 23, 2023
1 parent 883138a commit 74c526e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
30 changes: 25 additions & 5 deletions doc/example/petab_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@
"\n",
"Model outputs: ['pSTAT5A_rel', 'pSTAT5B_rel', 'rSTAT5A_rel'] \n",
"\n",
"Model states: ['STAT5A', 'STAT5B', 'pApB', 'pApA', 'pBpB', 'nucpApA', 'nucpApB', 'nucpBpB'] \n",
"\n"
"Model states: ['STAT5A', 'STAT5B', 'pApB', 'pApA', 'pBpB', 'nucpApA', 'nucpApB', 'nucpBpB'] \n"
]
}
],
Expand Down Expand Up @@ -407,6 +406,30 @@
")"
]
},
{
"cell_type": "markdown",
"source": [
"### Dealing with function evaluations at the initial point\n",
"\n",
"It is quite common in real applications, that the objective function is evaluable at every point in parameter space. Therefore, some local optimizations may fail directly at their initial point. Such results are usually not very informative and would be discarded. To directly discard such initial points, we can select a startpoint method that will resample starting points if the objective function (`check_fval`) or its gradient (`check_grad`) are non-finite:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"problem = importer.create_problem(\n",
" startpoint_kwargs=dict(check_fval=True, check_grad=True)\n",
")"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -517,9 +540,6 @@
"cell_type": "code",
"execution_count": 15,
"metadata": {
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
Expand Down
9 changes: 8 additions & 1 deletion pypesto/petab/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ def create_problem(
objective: AmiciObjective = None,
x_guesses: Optional[Iterable[float]] = None,
problem_kwargs: Dict[str, Any] = None,
startpoint_kwargs: Dict[str, Any] = None,
**kwargs,
) -> Problem:
"""Create a :class:`pypesto.Problem`.
Expand All @@ -688,6 +689,9 @@ def create_problem(
optimization.
problem_kwargs:
Passed to the `pypesto.Problem` constructor.
startpoint_kwargs:
Keyword arguments forwarded to
:meth:`PetabImporter.create_startpoint_method`.
**kwargs:
Additional key word arguments passed on to the objective,
if not provided.
Expand Down Expand Up @@ -741,6 +745,9 @@ def create_problem(
if problem_kwargs is None:
problem_kwargs = {}

if startpoint_kwargs is None:
startpoint_kwargs = {}

prior = self.create_prior()

if prior is not None:
Expand All @@ -762,7 +769,7 @@ def create_problem(
x_scales=x_scales,
x_priors_defs=prior,
startpoint_method=self.create_startpoint_method(
x_ids=np.delete(x_ids, x_fixed_indices)
x_ids=np.delete(x_ids, x_fixed_indices), **startpoint_kwargs
),
**problem_kwargs,
)
Expand Down

0 comments on commit 74c526e

Please sign in to comment.