Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select: problem-specific minimize method for SaCeSS #1339

Merged
merged 32 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c69c80d
let users supply calibration results
dilpath Feb 1, 2024
456a30a
Merge branch 'develop' into select_use_old_calibrations
dilpath Mar 26, 2024
5f62604
minimize method maker for sacess
Mar 26, 2024
d51432a
doc
Mar 26, 2024
bdb9ba6
user-supplied constructor args
Mar 26, 2024
98a3da0
doc how to specify e.g. `max_walltime_s`
Mar 26, 2024
927a28b
allow saving of sacess histories
Mar 26, 2024
37f3d8d
Merge branch 'develop' into select_use_old_calibrations
Doresic Mar 27, 2024
1c66dc0
Merge remote-tracking branch 'origin/select_use_old_calibrations' int…
Doresic Mar 27, 2024
5683f9d
handle calibrated model via petab select
dilpath Mar 27, 2024
af86b25
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Mar 27, 2024
7fe5782
custom petab select branch
dilpath Mar 27, 2024
bfa2021
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Mar 27, 2024
3b51af9
functionality moved to petab-select
dilpath Mar 28, 2024
1c69991
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Apr 20, 2024
3859aef
Merge branch 'develop' into select_use_old_calibrations
Doresic Aug 15, 2024
aa6f481
change Iterable import
Doresic Aug 15, 2024
e6b5ead
Merge remote-tracking branch 'origin/select_use_old_calibrations' int…
Doresic Aug 15, 2024
01802e2
update method for next petab_select version
dilpath Oct 7, 2024
f221448
update for petab select
dilpath Oct 7, 2024
bdfd18e
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Nov 6, 2024
ffc46cb
Merge branch 'develop' into select_use_old_calibrations
dilpath Nov 6, 2024
88b01e5
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Nov 6, 2024
62a0f27
Merge branch 'develop' into select_use_old_calibrations
dilpath Nov 11, 2024
0fdfae0
update for next petab-select version
dilpath Nov 11, 2024
7488194
Merge branch 'select_use_old_calibrations' into select_sacess_minimiz…
dilpath Nov 11, 2024
6ea2b45
fix tmpdir
dilpath Nov 12, 2024
b24f4e8
handle no user tmpdir
dilpath Nov 12, 2024
f56d744
test SacessMinimizeMethod partially
dilpath Nov 12, 2024
d86d5d8
include fides dependency in select tests
dilpath Nov 12, 2024
65176dd
Update pypesto/select/misc.py
dilpath Nov 12, 2024
2de945e
Merge branch 'develop' into select_sacess_minimize_method
dilpath Nov 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pypesto/select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from . import postprocessors
from .misc import model_to_pypesto_problem
from .misc import SacessMinimizeMethod, model_to_pypesto_problem
from .problem import Problem

try:
Expand Down
20 changes: 18 additions & 2 deletions pypesto/select/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def __init__(
# TODO deprecated
model_to_pypesto_problem_method: Callable[[Any], Problem] = None,
model_problem_options: dict = None,
previously_calibrated_models=None,
):
"""Arguments are used in every `__call__`, unless overridden."""
self.petab_select_problem = petab_select_problem
Expand All @@ -256,6 +257,10 @@ def __init__(
self.select_first_improvement = select_first_improvement
self.startpoint_latest_mle = startpoint_latest_mle

self.previously_calibrated_models = {}
if previously_calibrated_models is not None:
self.previously_calibrated_models = previously_calibrated_models

self.logger = MethodLogger()

# TODO deprecated
Expand Down Expand Up @@ -384,8 +389,19 @@ def __call__(
# `self.select_first_improvement`)
newly_calibrated_models = {}
for candidate_model in candidate_space.models:
# autoruns calibration
self.new_model_problem(model=candidate_model)
# If the user has previously calibrated this model,
# skip calibration.
if candidate_model.get_hash() in self.previously_calibrated_models:
_model_problem = self.new_model_problem(
model=candidate_model,
autorun=False,
)
_model_problem.set_result_from_model(
self.previously_calibrated_models[candidate_model.get_hash()]
)
else:
self.new_model_problem(model=candidate_model)

newly_calibrated_models[
candidate_model.get_hash()
] = candidate_model
Expand Down
52 changes: 52 additions & 0 deletions pypesto/select/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from petab_select.constants import PETAB_PROBLEM

from ..objective import Objective
from ..optimize.ess import (
SacessOptimizer,
get_default_ess_options,
)
from ..petab import PetabImporter
from ..problem import Problem

Expand Down Expand Up @@ -163,3 +167,51 @@ def correct_x_guesses(
corrected_x_guess.append(corrected_value)
corrected_x_guesses.append(corrected_x_guess)
return corrected_x_guesses


class SacessMinimizeMethod:
"""Create a minimize method for SaCeSS that adapts to each problem.

When a pyPESTO SaCeSS optimizer is created, it takes the problem
dimension as input. Hence, an optimizer needs to be constructed for
each problem. Objects of this class act like a minimize method for model
selection, but a new problem-specific SaCeSS optimizer will be created
every time a model is minimized.

Class attributes correspond to pyPESTO's SaCeSS optimizer, and are
dilpath marked this conversation as resolved.
Show resolved Hide resolved
documented there.
"""

def __init__(
self,
num_workers: int,
local_optimizer,
max_walltime_s: int,
dweindl marked this conversation as resolved.
Show resolved Hide resolved
):
"""Construct a minimize-like object."""
self.num_workers = num_workers
self.local_optimizer = local_optimizer
self.max_walltime_s = max_walltime_s

def __call__(self, problem: Problem, **minimize_options):
"""Create then run a problem-specific sacess optimizer."""
# create optimizer
ess_init_args = get_default_ess_options(
num_workers=self.num_workers,
dim=problem.dim,
)
for x in ess_init_args:
x["local_optimizer"] = self.local_optimizer
ess = SacessOptimizer(
max_walltime_s=self.max_walltime_s,
sacess_loglevel=logging.DEBUG,
ess_loglevel=logging.WARNING,
dilpath marked this conversation as resolved.
Show resolved Hide resolved
ess_init_args=ess_init_args,
)

# optimize
result = ess.minimize(
problem=problem,
**minimize_options,
)
return result
6 changes: 6 additions & 0 deletions pypesto/select/model_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ def minimize(self) -> Result:
**self.minimize_options,
)

def set_result_from_model(self, model):
self.model.criteria = model.criteria
self.model.estimated_parameters = model.estimated_parameters
if self.postprocessor is not None:
self.postprocessor(self)

def set_result(self, result: Result):
"""Postprocess a result.

Expand Down
Loading