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

Implement new parameter setting config style #393

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading