Skip to content

Commit

Permalink
Implement new parameter setting config style (facebookresearch#393)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookresearch#393

To support more granular parameter settings in the future, we need to change the config style to be able to separately handle settings for each parameter.

Currently the only setting for each parameter are the lower and upper bounds, but we also implement parameter types here (which currently does nothing).

This is technically not a breaking change. Even though we have changed all documentation to match the new style, it is possible to define parameter bounds the old way. This will throw a warning however as this causes all parameter-specific bounds to be ignored.

Old tests in test_config.py are also modified to match the new style unless specifically testing for these changes. Other tests not in test_config.py are left unchanged (this was the case for the previous major config change where experiment was a section).

Differential Revision: D63679916
  • Loading branch information
JasonKChow authored and facebook-github-bot committed Oct 1, 2024
1 parent 6383dc9 commit c2b49b9
Show file tree
Hide file tree
Showing 17 changed files with 1,792 additions and 1,543 deletions.
48 changes: 47 additions & 1 deletion aepsych/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

_T = TypeVar("_T")


class Config(configparser.ConfigParser):

# names in these packages can be referred to by string name
Expand Down Expand Up @@ -157,6 +156,26 @@ def update(
if config_str is not None:
self.read_string(config_str)

# Warn if ub/lb is defined in common section
if "ub" in self["common"] and "lb" in self["common"]:
warnings.warn(
"ub and lb have been defined in common section, ignoring parameter specific blocks, be very careful!"
)
elif "parnames" in self["common"]: # it's possible to pass no parnames
par_names = self.getlist("common", "parnames", element_type=str, fallback = [])
lb = [None] * len(par_names)
ub = [None] * len(par_names)
for i, par_name in enumerate(par_names):
# Validate the parameter-specific block
self._check_param_settings(par_name)

lb[i] = self[par_name]["lower_bound"]
ub[i] = self[par_name]["upper_bound"]

self["common"]["lb"] = f"[{', '.join(lb)}]"
self["common"]["ub"] = f"[{', '.join(ub)}]"


# Deprecation warning for "experiment" section
if "experiment" in self:
for i in self["experiment"]:
Expand Down Expand Up @@ -191,6 +210,33 @@ def _str_to_obj(self, v: str, fallback_type: _T = str, warn: bool = True) -> obj
warnings.warn(f'No known object "{v}"!')
return fallback_type(v)

def _check_param_settings(self, param_name: str) -> None:
"""Check parameter-specific blocks have the correct settings, raises a ValueError if not.
Args:
param_name (str): Parameter block to check.
"""
# Check if the config block exists at all
if param_name not in self:
raise ValueError(f"Parameter {param_name} is missing its own config block.")

param_block = self[param_name]

# Checking if param_type is set
if "par_type" not in param_block:
raise ValueError(f"Parameter {param_name} is missing the param_type setting.")

# Each parameter type has a different set of required settings
if param_block['par_type'] == "continuous":
# Check if bounds exist
if "lower_bound" not in param_block:
raise ValueError(f"Parameter {param_name} is missing the lower_bound setting.")
if "upper_bound" not in param_block:
raise ValueError(f"Parameter {param_name} is missing the upper_bound setting.")
else:
raise ValueError(f"Parameter {param_name} has an unsupported parameter type {param_block['par_type']}.")


def __repr__(self):
return f"Config at {hex(id(self))}: \n {str(self)}"

Expand Down
57 changes: 0 additions & 57 deletions configs/ax_beta_regression_example.ini

This file was deleted.

61 changes: 0 additions & 61 deletions configs/ax_example.ini

This file was deleted.

70 changes: 0 additions & 70 deletions configs/ax_ordinal_exploration_example.ini

This file was deleted.

43 changes: 0 additions & 43 deletions configs/multi_outcome_example.ini

This file was deleted.

13 changes: 11 additions & 2 deletions configs/nonmonotonic_optimization_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
## reused in multiple other classes
[common]
parnames = [par1, par2] # names of the parameters
lb = [0, 0] # lower bounds of the parameters, in the same order as above
ub = [1, 1] # upper bounds of parameter, in the same order as above
stimuli_per_trial = 1 # the number of stimuli shown in each trial; 1 for single, or 2 for pairwise experiments
outcome_types = [binary] # the type of response given by the participant; can be [binary] or [continuous] for single
strategy_names = [init_strat, opt_strat] # The strategies that will be used, corresponding to the named sections below

# Parameter settings, blocks based on parameter names in [common]
[par1]
par_type = continuous
lower_bound = 0 # lower bound
upper_bound = 1 # upper bound

[par2]
par_type = continuous
lower_bound = 0
upper_bound = 1

# Configuration for the initialization strategy, which we use to gather initial points
# before we start doing model-based acquisition
[init_strat]
Expand Down
12 changes: 10 additions & 2 deletions configs/ordinal_exploration_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
## reused in multiple other classes
[common]
parnames = [par1, par2] # names of the parameters
lb = [-1, -1] # lower bounds of the parameters, in the same order as above
ub = [1, 1] # upper bounds of parameter, in the same order as above
stimuli_per_trial = 1 # the number of stimuli shown in each trial; 1 for single, or 2 for pairwise experiments
outcome_types = [ordinal]
strategy_names = [init_strat] # The strategies that will be used, corresponding to the named sections below

[par1]
par_type = continuous
lower_bound = -1
upper_bound = 1

[par2]
par_type = continuous
lower_bound = -1
upper_bound = 1

# Configuration for the initialization strategy, which we use to gather initial points
# before we start doing model-based acquisition
[init_strat]
Expand Down
13 changes: 11 additions & 2 deletions configs/pairwise_al_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
## reused in multiple other classes
[common]
parnames = [par1, par2] # names of the parameters
lb = [0, 0] # lower bounds of the parameters, in the same order as above
ub = [1, 1] # upper bounds of parameter, in the same order as above
stimuli_per_trial = 2 # the number of stimuli shown in each trial; 1 for single, or 2 for pairwise experiments
outcome_types = [binary] # the type of response given by the participant; can only be [binary] for pairwise for now
strategy_names = [init_strat, opt_strat] # The strategies that will be used, corresponding to the named sections below

# Parameter settings, blocks based on parameter names in [common]
[par1]
par_type = continuous
lower_bound = 0 # lower bound
upper_bound = 1 # upper bound

[par2]
par_type = continuous
lower_bound = 0
upper_bound = 1

# Configuration for the initialization strategy, which we use to gather initial points
# before we start doing model-based acquisition
[init_strat]
Expand Down
13 changes: 11 additions & 2 deletions configs/pairwise_opt_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
## reused in multiple other classes
[common]
parnames = [par1, par2] # names of the parameters
lb = [0, 0] # lower bounds of the parameters, in the same order as above
ub = [1, 1] # upper bounds of parameter, in the same order as above
stimuli_per_trial = 2 # the number of stimuli shown in each trial; 1 for single, or 2 for pairwise experiments
outcome_types = [binary] # the type of response given by the participant; can only be [binary] for pairwise for now
strategy_names = [init_strat, opt_strat] # The strategies that will be used, corresponding to the named sections below

# Parameter settings, blocks based on parameter names in [common]
[par1]
par_type = continuous
lower_bound = 0 # lower bound
upper_bound = 1 # upper bound

[par2]
par_type = continuous
lower_bound = 0
upper_bound = 1

# Configuration for the initialization strategy, which we use to gather initial points
# before we start doing model-based acquisition
[init_strat]
Expand Down
Loading

0 comments on commit c2b49b9

Please sign in to comment.