id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
75
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
228,200
SheffieldML/GPyOpt
GPyOpt/interface/driver.py
BODriver._get_acq_evaluator
def _get_acq_evaluator(self, acq): """ Imports the evaluator """ from ..core.evaluators import select_evaluator from copy import deepcopy eval_args = deepcopy(self.config['acquisition']['evaluator']) del eval_args['type'] return select_evaluator(self.config['acquisition']['evaluator']['type'])(acq, **eval_args)
python
def _get_acq_evaluator(self, acq): """ Imports the evaluator """ from ..core.evaluators import select_evaluator from copy import deepcopy eval_args = deepcopy(self.config['acquisition']['evaluator']) del eval_args['type'] return select_evaluator(self.config['acquisition']['evaluator']['type'])(acq, **eval_args)
[ "def", "_get_acq_evaluator", "(", "self", ",", "acq", ")", ":", "from", ".", ".", "core", ".", "evaluators", "import", "select_evaluator", "from", "copy", "import", "deepcopy", "eval_args", "=", "deepcopy", "(", "self", ".", "config", "[", "'acquisition'", "]", "[", "'evaluator'", "]", ")", "del", "eval_args", "[", "'type'", "]", "return", "select_evaluator", "(", "self", ".", "config", "[", "'acquisition'", "]", "[", "'evaluator'", "]", "[", "'type'", "]", ")", "(", "acq", ",", "*", "*", "eval_args", ")" ]
Imports the evaluator
[ "Imports", "the", "evaluator" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/driver.py#L73-L82
228,201
SheffieldML/GPyOpt
GPyOpt/interface/driver.py
BODriver._check_stop
def _check_stop(self, iters, elapsed_time, converged): """ Defines the stopping criterion. """ r_c = self.config['resources'] stop = False if converged==0: stop=True if r_c['maximum-iterations'] !='NA' and iters>= r_c['maximum-iterations']: stop = True if r_c['max-run-time'] != 'NA' and elapsed_time/60.>= r_c['max-run-time']: stop = True return stop
python
def _check_stop(self, iters, elapsed_time, converged): """ Defines the stopping criterion. """ r_c = self.config['resources'] stop = False if converged==0: stop=True if r_c['maximum-iterations'] !='NA' and iters>= r_c['maximum-iterations']: stop = True if r_c['max-run-time'] != 'NA' and elapsed_time/60.>= r_c['max-run-time']: stop = True return stop
[ "def", "_check_stop", "(", "self", ",", "iters", ",", "elapsed_time", ",", "converged", ")", ":", "r_c", "=", "self", ".", "config", "[", "'resources'", "]", "stop", "=", "False", "if", "converged", "==", "0", ":", "stop", "=", "True", "if", "r_c", "[", "'maximum-iterations'", "]", "!=", "'NA'", "and", "iters", ">=", "r_c", "[", "'maximum-iterations'", "]", ":", "stop", "=", "True", "if", "r_c", "[", "'max-run-time'", "]", "!=", "'NA'", "and", "elapsed_time", "/", "60.", ">=", "r_c", "[", "'max-run-time'", "]", ":", "stop", "=", "True", "return", "stop" ]
Defines the stopping criterion.
[ "Defines", "the", "stopping", "criterion", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/driver.py#L84-L98
228,202
SheffieldML/GPyOpt
GPyOpt/interface/driver.py
BODriver.run
def run(self): """ Runs the optimization using the previously loaded elements. """ space = self._get_space() obj_func = self._get_obj(space) model = self._get_model() acq = self._get_acquisition(model, space) acq_eval = self._get_acq_evaluator(acq) from ..experiment_design import initial_design X_init = initial_design(self.config['initialization']['type'], space, self.config['initialization']['num-eval']) from ..methods import ModularBayesianOptimization bo = ModularBayesianOptimization(model, space, obj_func, acq, acq_eval, X_init) bo.run_optimization(max_iter = self.config['resources']['maximum-iterations'], max_time = self.config['resources']['max-run-time'] if self.config['resources']['max-run-time']!="NA" else np.inf, eps = self.config['resources']['tolerance'], verbosity=True) return bo
python
def run(self): """ Runs the optimization using the previously loaded elements. """ space = self._get_space() obj_func = self._get_obj(space) model = self._get_model() acq = self._get_acquisition(model, space) acq_eval = self._get_acq_evaluator(acq) from ..experiment_design import initial_design X_init = initial_design(self.config['initialization']['type'], space, self.config['initialization']['num-eval']) from ..methods import ModularBayesianOptimization bo = ModularBayesianOptimization(model, space, obj_func, acq, acq_eval, X_init) bo.run_optimization(max_iter = self.config['resources']['maximum-iterations'], max_time = self.config['resources']['max-run-time'] if self.config['resources']['max-run-time']!="NA" else np.inf, eps = self.config['resources']['tolerance'], verbosity=True) return bo
[ "def", "run", "(", "self", ")", ":", "space", "=", "self", ".", "_get_space", "(", ")", "obj_func", "=", "self", ".", "_get_obj", "(", "space", ")", "model", "=", "self", ".", "_get_model", "(", ")", "acq", "=", "self", ".", "_get_acquisition", "(", "model", ",", "space", ")", "acq_eval", "=", "self", ".", "_get_acq_evaluator", "(", "acq", ")", "from", ".", ".", "experiment_design", "import", "initial_design", "X_init", "=", "initial_design", "(", "self", ".", "config", "[", "'initialization'", "]", "[", "'type'", "]", ",", "space", ",", "self", ".", "config", "[", "'initialization'", "]", "[", "'num-eval'", "]", ")", "from", ".", ".", "methods", "import", "ModularBayesianOptimization", "bo", "=", "ModularBayesianOptimization", "(", "model", ",", "space", ",", "obj_func", ",", "acq", ",", "acq_eval", ",", "X_init", ")", "bo", ".", "run_optimization", "(", "max_iter", "=", "self", ".", "config", "[", "'resources'", "]", "[", "'maximum-iterations'", "]", ",", "max_time", "=", "self", ".", "config", "[", "'resources'", "]", "[", "'max-run-time'", "]", "if", "self", ".", "config", "[", "'resources'", "]", "[", "'max-run-time'", "]", "!=", "\"NA\"", "else", "np", ".", "inf", ",", "eps", "=", "self", ".", "config", "[", "'resources'", "]", "[", "'tolerance'", "]", ",", "verbosity", "=", "True", ")", "return", "bo" ]
Runs the optimization using the previously loaded elements.
[ "Runs", "the", "optimization", "using", "the", "previously", "loaded", "elements", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/driver.py#L100-L119
228,203
SheffieldML/GPyOpt
GPyOpt/optimization/optimizer.py
choose_optimizer
def choose_optimizer(optimizer_name, bounds): """ Selects the type of local optimizer """ if optimizer_name == 'lbfgs': optimizer = OptLbfgs(bounds) elif optimizer_name == 'DIRECT': optimizer = OptDirect(bounds) elif optimizer_name == 'CMA': optimizer = OptCma(bounds) else: raise InvalidVariableNameError('Invalid optimizer selected.') return optimizer
python
def choose_optimizer(optimizer_name, bounds): """ Selects the type of local optimizer """ if optimizer_name == 'lbfgs': optimizer = OptLbfgs(bounds) elif optimizer_name == 'DIRECT': optimizer = OptDirect(bounds) elif optimizer_name == 'CMA': optimizer = OptCma(bounds) else: raise InvalidVariableNameError('Invalid optimizer selected.') return optimizer
[ "def", "choose_optimizer", "(", "optimizer_name", ",", "bounds", ")", ":", "if", "optimizer_name", "==", "'lbfgs'", ":", "optimizer", "=", "OptLbfgs", "(", "bounds", ")", "elif", "optimizer_name", "==", "'DIRECT'", ":", "optimizer", "=", "OptDirect", "(", "bounds", ")", "elif", "optimizer_name", "==", "'CMA'", ":", "optimizer", "=", "OptCma", "(", "bounds", ")", "else", ":", "raise", "InvalidVariableNameError", "(", "'Invalid optimizer selected.'", ")", "return", "optimizer" ]
Selects the type of local optimizer
[ "Selects", "the", "type", "of", "local", "optimizer" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/optimization/optimizer.py#L238-L253
228,204
SheffieldML/GPyOpt
GPyOpt/util/arguments_manager.py
ArgumentsManager.evaluator_creator
def evaluator_creator(self, evaluator_type, acquisition, batch_size, model_type, model, space, acquisition_optimizer): """ Acquisition chooser from the available options. Guide the optimization through sequential or parallel evalutions of the objective. """ acquisition_transformation = self.kwargs.get('acquisition_transformation','none') if batch_size == 1 or evaluator_type == 'sequential': return Sequential(acquisition) elif batch_size >1 and (evaluator_type == 'random' or evaluator_type is None): return RandomBatch(acquisition, batch_size) elif batch_size >1 and evaluator_type == 'thompson_sampling': return ThompsonBatch(acquisition, batch_size) elif evaluator_type == 'local_penalization': if model_type not in ['GP', 'sparseGP', 'GP_MCMC', 'warpedGP']: raise InvalidConfigError('local_penalization evaluator can only be used with GP models') if not isinstance(acquisition, AcquisitionLP): acquisition_lp = AcquisitionLP(model, space, acquisition_optimizer, acquisition, acquisition_transformation) return LocalPenalization(acquisition_lp, batch_size)
python
def evaluator_creator(self, evaluator_type, acquisition, batch_size, model_type, model, space, acquisition_optimizer): """ Acquisition chooser from the available options. Guide the optimization through sequential or parallel evalutions of the objective. """ acquisition_transformation = self.kwargs.get('acquisition_transformation','none') if batch_size == 1 or evaluator_type == 'sequential': return Sequential(acquisition) elif batch_size >1 and (evaluator_type == 'random' or evaluator_type is None): return RandomBatch(acquisition, batch_size) elif batch_size >1 and evaluator_type == 'thompson_sampling': return ThompsonBatch(acquisition, batch_size) elif evaluator_type == 'local_penalization': if model_type not in ['GP', 'sparseGP', 'GP_MCMC', 'warpedGP']: raise InvalidConfigError('local_penalization evaluator can only be used with GP models') if not isinstance(acquisition, AcquisitionLP): acquisition_lp = AcquisitionLP(model, space, acquisition_optimizer, acquisition, acquisition_transformation) return LocalPenalization(acquisition_lp, batch_size)
[ "def", "evaluator_creator", "(", "self", ",", "evaluator_type", ",", "acquisition", ",", "batch_size", ",", "model_type", ",", "model", ",", "space", ",", "acquisition_optimizer", ")", ":", "acquisition_transformation", "=", "self", ".", "kwargs", ".", "get", "(", "'acquisition_transformation'", ",", "'none'", ")", "if", "batch_size", "==", "1", "or", "evaluator_type", "==", "'sequential'", ":", "return", "Sequential", "(", "acquisition", ")", "elif", "batch_size", ">", "1", "and", "(", "evaluator_type", "==", "'random'", "or", "evaluator_type", "is", "None", ")", ":", "return", "RandomBatch", "(", "acquisition", ",", "batch_size", ")", "elif", "batch_size", ">", "1", "and", "evaluator_type", "==", "'thompson_sampling'", ":", "return", "ThompsonBatch", "(", "acquisition", ",", "batch_size", ")", "elif", "evaluator_type", "==", "'local_penalization'", ":", "if", "model_type", "not", "in", "[", "'GP'", ",", "'sparseGP'", ",", "'GP_MCMC'", ",", "'warpedGP'", "]", ":", "raise", "InvalidConfigError", "(", "'local_penalization evaluator can only be used with GP models'", ")", "if", "not", "isinstance", "(", "acquisition", ",", "AcquisitionLP", ")", ":", "acquisition_lp", "=", "AcquisitionLP", "(", "model", ",", "space", ",", "acquisition_optimizer", ",", "acquisition", ",", "acquisition_transformation", ")", "return", "LocalPenalization", "(", "acquisition_lp", ",", "batch_size", ")" ]
Acquisition chooser from the available options. Guide the optimization through sequential or parallel evalutions of the objective.
[ "Acquisition", "chooser", "from", "the", "available", "options", ".", "Guide", "the", "optimization", "through", "sequential", "or", "parallel", "evalutions", "of", "the", "objective", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/util/arguments_manager.py#L17-L38
228,205
SheffieldML/GPyOpt
GPyOpt/acquisitions/LCB.py
AcquisitionLCB._compute_acq
def _compute_acq(self, x): """ Computes the GP-Lower Confidence Bound """ m, s = self.model.predict(x) f_acqu = -m + self.exploration_weight * s return f_acqu
python
def _compute_acq(self, x): """ Computes the GP-Lower Confidence Bound """ m, s = self.model.predict(x) f_acqu = -m + self.exploration_weight * s return f_acqu
[ "def", "_compute_acq", "(", "self", ",", "x", ")", ":", "m", ",", "s", "=", "self", ".", "model", ".", "predict", "(", "x", ")", "f_acqu", "=", "-", "m", "+", "self", ".", "exploration_weight", "*", "s", "return", "f_acqu" ]
Computes the GP-Lower Confidence Bound
[ "Computes", "the", "GP", "-", "Lower", "Confidence", "Bound" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/LCB.py#L31-L37
228,206
SheffieldML/GPyOpt
GPyOpt/acquisitions/LCB.py
AcquisitionLCB._compute_acq_withGradients
def _compute_acq_withGradients(self, x): """ Computes the GP-Lower Confidence Bound and its derivative """ m, s, dmdx, dsdx = self.model.predict_withGradients(x) f_acqu = -m + self.exploration_weight * s df_acqu = -dmdx + self.exploration_weight * dsdx return f_acqu, df_acqu
python
def _compute_acq_withGradients(self, x): """ Computes the GP-Lower Confidence Bound and its derivative """ m, s, dmdx, dsdx = self.model.predict_withGradients(x) f_acqu = -m + self.exploration_weight * s df_acqu = -dmdx + self.exploration_weight * dsdx return f_acqu, df_acqu
[ "def", "_compute_acq_withGradients", "(", "self", ",", "x", ")", ":", "m", ",", "s", ",", "dmdx", ",", "dsdx", "=", "self", ".", "model", ".", "predict_withGradients", "(", "x", ")", "f_acqu", "=", "-", "m", "+", "self", ".", "exploration_weight", "*", "s", "df_acqu", "=", "-", "dmdx", "+", "self", ".", "exploration_weight", "*", "dsdx", "return", "f_acqu", ",", "df_acqu" ]
Computes the GP-Lower Confidence Bound and its derivative
[ "Computes", "the", "GP", "-", "Lower", "Confidence", "Bound", "and", "its", "derivative" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/LCB.py#L39-L46
228,207
SheffieldML/GPyOpt
GPyOpt/core/task/variables.py
create_variable
def create_variable(descriptor): """ Creates a variable from a dictionary descriptor """ if descriptor['type'] == 'continuous': return ContinuousVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) elif descriptor['type'] == 'bandit': return BanditVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', None)) # bandits variables cannot be repeated elif descriptor['type'] == 'discrete': return DiscreteVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) elif descriptor['type'] == 'categorical': return CategoricalVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) else: raise InvalidConfigError('Unknown variable type ' + descriptor['type'])
python
def create_variable(descriptor): """ Creates a variable from a dictionary descriptor """ if descriptor['type'] == 'continuous': return ContinuousVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) elif descriptor['type'] == 'bandit': return BanditVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', None)) # bandits variables cannot be repeated elif descriptor['type'] == 'discrete': return DiscreteVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) elif descriptor['type'] == 'categorical': return CategoricalVariable(descriptor['name'], descriptor['domain'], descriptor.get('dimensionality', 1)) else: raise InvalidConfigError('Unknown variable type ' + descriptor['type'])
[ "def", "create_variable", "(", "descriptor", ")", ":", "if", "descriptor", "[", "'type'", "]", "==", "'continuous'", ":", "return", "ContinuousVariable", "(", "descriptor", "[", "'name'", "]", ",", "descriptor", "[", "'domain'", "]", ",", "descriptor", ".", "get", "(", "'dimensionality'", ",", "1", ")", ")", "elif", "descriptor", "[", "'type'", "]", "==", "'bandit'", ":", "return", "BanditVariable", "(", "descriptor", "[", "'name'", "]", ",", "descriptor", "[", "'domain'", "]", ",", "descriptor", ".", "get", "(", "'dimensionality'", ",", "None", ")", ")", "# bandits variables cannot be repeated", "elif", "descriptor", "[", "'type'", "]", "==", "'discrete'", ":", "return", "DiscreteVariable", "(", "descriptor", "[", "'name'", "]", ",", "descriptor", "[", "'domain'", "]", ",", "descriptor", ".", "get", "(", "'dimensionality'", ",", "1", ")", ")", "elif", "descriptor", "[", "'type'", "]", "==", "'categorical'", ":", "return", "CategoricalVariable", "(", "descriptor", "[", "'name'", "]", ",", "descriptor", "[", "'domain'", "]", ",", "descriptor", ".", "get", "(", "'dimensionality'", ",", "1", ")", ")", "else", ":", "raise", "InvalidConfigError", "(", "'Unknown variable type '", "+", "descriptor", "[", "'type'", "]", ")" ]
Creates a variable from a dictionary descriptor
[ "Creates", "a", "variable", "from", "a", "dictionary", "descriptor" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/variables.py#L230-L243
228,208
SheffieldML/GPyOpt
GPyOpt/core/task/variables.py
Variable.expand
def expand(self): """ Builds a list of single dimensional variables representing current variable. Examples: For single dimensional variable, it is returned as is discrete of (0,2,4) -> discrete of (0,2,4) For multi dimensional variable, a list of variables is returned, each representing a single dimension continuous {0<=x<=1, 2<=y<=3} -> continuous {0<=x<=1}, continuous {2<=y<=3} """ expanded_variables = [] for i in range(self.dimensionality): one_d_variable = deepcopy(self) one_d_variable.dimensionality = 1 if self.dimensionality > 1: one_d_variable.name = '{}_{}'.format(self.name, i+1) else: one_d_variable.name = self.name one_d_variable.dimensionality_in_model = 1 expanded_variables.append(one_d_variable) return expanded_variables
python
def expand(self): """ Builds a list of single dimensional variables representing current variable. Examples: For single dimensional variable, it is returned as is discrete of (0,2,4) -> discrete of (0,2,4) For multi dimensional variable, a list of variables is returned, each representing a single dimension continuous {0<=x<=1, 2<=y<=3} -> continuous {0<=x<=1}, continuous {2<=y<=3} """ expanded_variables = [] for i in range(self.dimensionality): one_d_variable = deepcopy(self) one_d_variable.dimensionality = 1 if self.dimensionality > 1: one_d_variable.name = '{}_{}'.format(self.name, i+1) else: one_d_variable.name = self.name one_d_variable.dimensionality_in_model = 1 expanded_variables.append(one_d_variable) return expanded_variables
[ "def", "expand", "(", "self", ")", ":", "expanded_variables", "=", "[", "]", "for", "i", "in", "range", "(", "self", ".", "dimensionality", ")", ":", "one_d_variable", "=", "deepcopy", "(", "self", ")", "one_d_variable", ".", "dimensionality", "=", "1", "if", "self", ".", "dimensionality", ">", "1", ":", "one_d_variable", ".", "name", "=", "'{}_{}'", ".", "format", "(", "self", ".", "name", ",", "i", "+", "1", ")", "else", ":", "one_d_variable", ".", "name", "=", "self", ".", "name", "one_d_variable", ".", "dimensionality_in_model", "=", "1", "expanded_variables", ".", "append", "(", "one_d_variable", ")", "return", "expanded_variables" ]
Builds a list of single dimensional variables representing current variable. Examples: For single dimensional variable, it is returned as is discrete of (0,2,4) -> discrete of (0,2,4) For multi dimensional variable, a list of variables is returned, each representing a single dimension continuous {0<=x<=1, 2<=y<=3} -> continuous {0<=x<=1}, continuous {2<=y<=3}
[ "Builds", "a", "list", "of", "single", "dimensional", "variables", "representing", "current", "variable", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/variables.py#L16-L36
228,209
SheffieldML/GPyOpt
GPyOpt/core/task/variables.py
ContinuousVariable.round
def round(self, value_array): """ If value falls within bounds, just return it otherwise return min or max, whichever is closer to the value Assumes an 1d array with a single element as an input. """ min_value = self.domain[0] max_value = self.domain[1] rounded_value = value_array[0] if rounded_value < min_value: rounded_value = min_value elif rounded_value > max_value: rounded_value = max_value return [rounded_value]
python
def round(self, value_array): """ If value falls within bounds, just return it otherwise return min or max, whichever is closer to the value Assumes an 1d array with a single element as an input. """ min_value = self.domain[0] max_value = self.domain[1] rounded_value = value_array[0] if rounded_value < min_value: rounded_value = min_value elif rounded_value > max_value: rounded_value = max_value return [rounded_value]
[ "def", "round", "(", "self", ",", "value_array", ")", ":", "min_value", "=", "self", ".", "domain", "[", "0", "]", "max_value", "=", "self", ".", "domain", "[", "1", "]", "rounded_value", "=", "value_array", "[", "0", "]", "if", "rounded_value", "<", "min_value", ":", "rounded_value", "=", "min_value", "elif", "rounded_value", ">", "max_value", ":", "rounded_value", "=", "max_value", "return", "[", "rounded_value", "]" ]
If value falls within bounds, just return it otherwise return min or max, whichever is closer to the value Assumes an 1d array with a single element as an input.
[ "If", "value", "falls", "within", "bounds", "just", "return", "it", "otherwise", "return", "min", "or", "max", "whichever", "is", "closer", "to", "the", "value", "Assumes", "an", "1d", "array", "with", "a", "single", "element", "as", "an", "input", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/variables.py#L100-L116
228,210
SheffieldML/GPyOpt
GPyOpt/core/task/variables.py
BanditVariable.round
def round(self, value_array): """ Rounds a bandit variable by selecting the closest point in the domain Closest here is defined by euclidian distance Assumes an 1d array of the same length as the single variable value """ distances = np.linalg.norm(np.array(self.domain) - value_array, axis=1) idx = np.argmin(distances) return [self.domain[idx]]
python
def round(self, value_array): """ Rounds a bandit variable by selecting the closest point in the domain Closest here is defined by euclidian distance Assumes an 1d array of the same length as the single variable value """ distances = np.linalg.norm(np.array(self.domain) - value_array, axis=1) idx = np.argmin(distances) return [self.domain[idx]]
[ "def", "round", "(", "self", ",", "value_array", ")", ":", "distances", "=", "np", ".", "linalg", ".", "norm", "(", "np", ".", "array", "(", "self", ".", "domain", ")", "-", "value_array", ",", "axis", "=", "1", ")", "idx", "=", "np", ".", "argmin", "(", "distances", ")", "return", "[", "self", ".", "domain", "[", "idx", "]", "]" ]
Rounds a bandit variable by selecting the closest point in the domain Closest here is defined by euclidian distance Assumes an 1d array of the same length as the single variable value
[ "Rounds", "a", "bandit", "variable", "by", "selecting", "the", "closest", "point", "in", "the", "domain", "Closest", "here", "is", "defined", "by", "euclidian", "distance", "Assumes", "an", "1d", "array", "of", "the", "same", "length", "as", "the", "single", "variable", "value" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/variables.py#L151-L159
228,211
SheffieldML/GPyOpt
GPyOpt/core/task/variables.py
DiscreteVariable.round
def round(self, value_array): """ Rounds a discrete variable by selecting the closest point in the domain Assumes an 1d array with a single element as an input. """ value = value_array[0] rounded_value = self.domain[0] for domain_value in self.domain: if np.abs(domain_value - value) < np.abs(rounded_value - value): rounded_value = domain_value return [rounded_value]
python
def round(self, value_array): """ Rounds a discrete variable by selecting the closest point in the domain Assumes an 1d array with a single element as an input. """ value = value_array[0] rounded_value = self.domain[0] for domain_value in self.domain: if np.abs(domain_value - value) < np.abs(rounded_value - value): rounded_value = domain_value return [rounded_value]
[ "def", "round", "(", "self", ",", "value_array", ")", ":", "value", "=", "value_array", "[", "0", "]", "rounded_value", "=", "self", ".", "domain", "[", "0", "]", "for", "domain_value", "in", "self", ".", "domain", ":", "if", "np", ".", "abs", "(", "domain_value", "-", "value", ")", "<", "np", ".", "abs", "(", "rounded_value", "-", "value", ")", ":", "rounded_value", "=", "domain_value", "return", "[", "rounded_value", "]" ]
Rounds a discrete variable by selecting the closest point in the domain Assumes an 1d array with a single element as an input.
[ "Rounds", "a", "discrete", "variable", "by", "selecting", "the", "closest", "point", "in", "the", "domain", "Assumes", "an", "1d", "array", "with", "a", "single", "element", "as", "an", "input", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/variables.py#L175-L187
228,212
SheffieldML/GPyOpt
GPyOpt/optimization/acquisition_optimizer.py
AcquisitionOptimizer.optimize
def optimize(self, f=None, df=None, f_df=None, duplicate_manager=None): """ Optimizes the input function. :param f: function to optimize. :param df: gradient of the function to optimize. :param f_df: returns both the function to optimize and its gradient. """ self.f = f self.df = df self.f_df = f_df ## --- Update the optimizer, in case context has beee passed. self.optimizer = choose_optimizer(self.optimizer_name, self.context_manager.noncontext_bounds) ## --- Selecting the anchor points and removing duplicates if self.type_anchor_points_logic == max_objective_anchor_points_logic: anchor_points_generator = ObjectiveAnchorPointsGenerator(self.space, random_design_type, f) elif self.type_anchor_points_logic == thompson_sampling_anchor_points_logic: anchor_points_generator = ThompsonSamplingAnchorPointsGenerator(self.space, sobol_design_type, self.model) ## -- Select the anchor points (with context) anchor_points = anchor_points_generator.get(duplicate_manager=duplicate_manager, context_manager=self.context_manager) ## --- Applying local optimizers at the anchor points and update bounds of the optimizer (according to the context) optimized_points = [apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points] x_min, fx_min = min(optimized_points, key=lambda t:t[1]) #x_min, fx_min = min([apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points], key=lambda t:t[1]) return x_min, fx_min
python
def optimize(self, f=None, df=None, f_df=None, duplicate_manager=None): """ Optimizes the input function. :param f: function to optimize. :param df: gradient of the function to optimize. :param f_df: returns both the function to optimize and its gradient. """ self.f = f self.df = df self.f_df = f_df ## --- Update the optimizer, in case context has beee passed. self.optimizer = choose_optimizer(self.optimizer_name, self.context_manager.noncontext_bounds) ## --- Selecting the anchor points and removing duplicates if self.type_anchor_points_logic == max_objective_anchor_points_logic: anchor_points_generator = ObjectiveAnchorPointsGenerator(self.space, random_design_type, f) elif self.type_anchor_points_logic == thompson_sampling_anchor_points_logic: anchor_points_generator = ThompsonSamplingAnchorPointsGenerator(self.space, sobol_design_type, self.model) ## -- Select the anchor points (with context) anchor_points = anchor_points_generator.get(duplicate_manager=duplicate_manager, context_manager=self.context_manager) ## --- Applying local optimizers at the anchor points and update bounds of the optimizer (according to the context) optimized_points = [apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points] x_min, fx_min = min(optimized_points, key=lambda t:t[1]) #x_min, fx_min = min([apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points], key=lambda t:t[1]) return x_min, fx_min
[ "def", "optimize", "(", "self", ",", "f", "=", "None", ",", "df", "=", "None", ",", "f_df", "=", "None", ",", "duplicate_manager", "=", "None", ")", ":", "self", ".", "f", "=", "f", "self", ".", "df", "=", "df", "self", ".", "f_df", "=", "f_df", "## --- Update the optimizer, in case context has beee passed.", "self", ".", "optimizer", "=", "choose_optimizer", "(", "self", ".", "optimizer_name", ",", "self", ".", "context_manager", ".", "noncontext_bounds", ")", "## --- Selecting the anchor points and removing duplicates", "if", "self", ".", "type_anchor_points_logic", "==", "max_objective_anchor_points_logic", ":", "anchor_points_generator", "=", "ObjectiveAnchorPointsGenerator", "(", "self", ".", "space", ",", "random_design_type", ",", "f", ")", "elif", "self", ".", "type_anchor_points_logic", "==", "thompson_sampling_anchor_points_logic", ":", "anchor_points_generator", "=", "ThompsonSamplingAnchorPointsGenerator", "(", "self", ".", "space", ",", "sobol_design_type", ",", "self", ".", "model", ")", "## -- Select the anchor points (with context)", "anchor_points", "=", "anchor_points_generator", ".", "get", "(", "duplicate_manager", "=", "duplicate_manager", ",", "context_manager", "=", "self", ".", "context_manager", ")", "## --- Applying local optimizers at the anchor points and update bounds of the optimizer (according to the context)", "optimized_points", "=", "[", "apply_optimizer", "(", "self", ".", "optimizer", ",", "a", ",", "f", "=", "f", ",", "df", "=", "None", ",", "f_df", "=", "f_df", ",", "duplicate_manager", "=", "duplicate_manager", ",", "context_manager", "=", "self", ".", "context_manager", ",", "space", "=", "self", ".", "space", ")", "for", "a", "in", "anchor_points", "]", "x_min", ",", "fx_min", "=", "min", "(", "optimized_points", ",", "key", "=", "lambda", "t", ":", "t", "[", "1", "]", ")", "#x_min, fx_min = min([apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points], key=lambda t:t[1])", "return", "x_min", ",", "fx_min" ]
Optimizes the input function. :param f: function to optimize. :param df: gradient of the function to optimize. :param f_df: returns both the function to optimize and its gradient.
[ "Optimizes", "the", "input", "function", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/optimization/acquisition_optimizer.py#L46-L77
228,213
SheffieldML/GPyOpt
GPyOpt/core/task/objective.py
SingleObjective.evaluate
def evaluate(self, x): """ Performs the evaluation of the objective at x. """ if self.n_procs == 1: f_evals, cost_evals = self._eval_func(x) else: try: f_evals, cost_evals = self._syncronous_batch_evaluation(x) except: if not hasattr(self, 'parallel_error'): print('Error in parallel computation. Fall back to single process!') else: self.parallel_error = True f_evals, cost_evals = self._eval_func(x) return f_evals, cost_evals
python
def evaluate(self, x): """ Performs the evaluation of the objective at x. """ if self.n_procs == 1: f_evals, cost_evals = self._eval_func(x) else: try: f_evals, cost_evals = self._syncronous_batch_evaluation(x) except: if not hasattr(self, 'parallel_error'): print('Error in parallel computation. Fall back to single process!') else: self.parallel_error = True f_evals, cost_evals = self._eval_func(x) return f_evals, cost_evals
[ "def", "evaluate", "(", "self", ",", "x", ")", ":", "if", "self", ".", "n_procs", "==", "1", ":", "f_evals", ",", "cost_evals", "=", "self", ".", "_eval_func", "(", "x", ")", "else", ":", "try", ":", "f_evals", ",", "cost_evals", "=", "self", ".", "_syncronous_batch_evaluation", "(", "x", ")", "except", ":", "if", "not", "hasattr", "(", "self", ",", "'parallel_error'", ")", ":", "print", "(", "'Error in parallel computation. Fall back to single process!'", ")", "else", ":", "self", ".", "parallel_error", "=", "True", "f_evals", ",", "cost_evals", "=", "self", ".", "_eval_func", "(", "x", ")", "return", "f_evals", ",", "cost_evals" ]
Performs the evaluation of the objective at x.
[ "Performs", "the", "evaluation", "of", "the", "objective", "at", "x", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/objective.py#L44-L61
228,214
SheffieldML/GPyOpt
GPyOpt/core/task/objective.py
SingleObjective._syncronous_batch_evaluation
def _syncronous_batch_evaluation(self,x): """ Evaluates the function a x, where x can be a single location or a batch. The evaluation is performed in parallel according to the number of accessible cores. """ from multiprocessing import Process, Pipe # --- parallel evaluation of the function divided_samples = [x[i::self.n_procs] for i in range(self.n_procs)] pipe = [Pipe() for i in range(self.n_procs)] proc = [Process(target=spawn(self._eval_func),args=(c,k)) for k,(p,c) in zip(divided_samples,pipe)] [p.start() for p in proc] [p.join() for p in proc] # --- time of evaluation is set to constant (=1). This is one of the hypothesis of synchronous batch methods. f_evals = np.zeros((x.shape[0],1)) cost_evals = np.ones((x.shape[0],1)) i = 0 for (p,c) in pipe: f_evals[i::self.n_procs] = p.recv()[0] # throw away costs i += 1 return f_evals, cost_evals
python
def _syncronous_batch_evaluation(self,x): """ Evaluates the function a x, where x can be a single location or a batch. The evaluation is performed in parallel according to the number of accessible cores. """ from multiprocessing import Process, Pipe # --- parallel evaluation of the function divided_samples = [x[i::self.n_procs] for i in range(self.n_procs)] pipe = [Pipe() for i in range(self.n_procs)] proc = [Process(target=spawn(self._eval_func),args=(c,k)) for k,(p,c) in zip(divided_samples,pipe)] [p.start() for p in proc] [p.join() for p in proc] # --- time of evaluation is set to constant (=1). This is one of the hypothesis of synchronous batch methods. f_evals = np.zeros((x.shape[0],1)) cost_evals = np.ones((x.shape[0],1)) i = 0 for (p,c) in pipe: f_evals[i::self.n_procs] = p.recv()[0] # throw away costs i += 1 return f_evals, cost_evals
[ "def", "_syncronous_batch_evaluation", "(", "self", ",", "x", ")", ":", "from", "multiprocessing", "import", "Process", ",", "Pipe", "# --- parallel evaluation of the function", "divided_samples", "=", "[", "x", "[", "i", ":", ":", "self", ".", "n_procs", "]", "for", "i", "in", "range", "(", "self", ".", "n_procs", ")", "]", "pipe", "=", "[", "Pipe", "(", ")", "for", "i", "in", "range", "(", "self", ".", "n_procs", ")", "]", "proc", "=", "[", "Process", "(", "target", "=", "spawn", "(", "self", ".", "_eval_func", ")", ",", "args", "=", "(", "c", ",", "k", ")", ")", "for", "k", ",", "(", "p", ",", "c", ")", "in", "zip", "(", "divided_samples", ",", "pipe", ")", "]", "[", "p", ".", "start", "(", ")", "for", "p", "in", "proc", "]", "[", "p", ".", "join", "(", ")", "for", "p", "in", "proc", "]", "# --- time of evaluation is set to constant (=1). This is one of the hypothesis of synchronous batch methods.", "f_evals", "=", "np", ".", "zeros", "(", "(", "x", ".", "shape", "[", "0", "]", ",", "1", ")", ")", "cost_evals", "=", "np", ".", "ones", "(", "(", "x", ".", "shape", "[", "0", "]", ",", "1", ")", ")", "i", "=", "0", "for", "(", "p", ",", "c", ")", "in", "pipe", ":", "f_evals", "[", "i", ":", ":", "self", ".", "n_procs", "]", "=", "p", ".", "recv", "(", ")", "[", "0", "]", "# throw away costs", "i", "+=", "1", "return", "f_evals", ",", "cost_evals" ]
Evaluates the function a x, where x can be a single location or a batch. The evaluation is performed in parallel according to the number of accessible cores.
[ "Evaluates", "the", "function", "a", "x", "where", "x", "can", "be", "a", "single", "location", "or", "a", "batch", ".", "The", "evaluation", "is", "performed", "in", "parallel", "according", "to", "the", "number", "of", "accessible", "cores", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/objective.py#L80-L101
228,215
SheffieldML/GPyOpt
GPyOpt/core/evaluators/sequential.py
Sequential.compute_batch
def compute_batch(self, duplicate_manager=None,context_manager=None): """ Selects the new location to evaluate the objective. """ x, _ = self.acquisition.optimize(duplicate_manager=duplicate_manager) return x
python
def compute_batch(self, duplicate_manager=None,context_manager=None): """ Selects the new location to evaluate the objective. """ x, _ = self.acquisition.optimize(duplicate_manager=duplicate_manager) return x
[ "def", "compute_batch", "(", "self", ",", "duplicate_manager", "=", "None", ",", "context_manager", "=", "None", ")", ":", "x", ",", "_", "=", "self", ".", "acquisition", ".", "optimize", "(", "duplicate_manager", "=", "duplicate_manager", ")", "return", "x" ]
Selects the new location to evaluate the objective.
[ "Selects", "the", "new", "location", "to", "evaluate", "the", "objective", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/evaluators/sequential.py#L18-L23
228,216
SheffieldML/GPyOpt
GPyOpt/acquisitions/LCB_mcmc.py
AcquisitionLCB_MCMC._compute_acq
def _compute_acq(self,x): """ Integrated GP-Lower Confidence Bound """ means, stds = self.model.predict(x) f_acqu = 0 for m,s in zip(means, stds): f_acqu += -m + self.exploration_weight * s return f_acqu/(len(means))
python
def _compute_acq(self,x): """ Integrated GP-Lower Confidence Bound """ means, stds = self.model.predict(x) f_acqu = 0 for m,s in zip(means, stds): f_acqu += -m + self.exploration_weight * s return f_acqu/(len(means))
[ "def", "_compute_acq", "(", "self", ",", "x", ")", ":", "means", ",", "stds", "=", "self", ".", "model", ".", "predict", "(", "x", ")", "f_acqu", "=", "0", "for", "m", ",", "s", "in", "zip", "(", "means", ",", "stds", ")", ":", "f_acqu", "+=", "-", "m", "+", "self", ".", "exploration_weight", "*", "s", "return", "f_acqu", "/", "(", "len", "(", "means", ")", ")" ]
Integrated GP-Lower Confidence Bound
[ "Integrated", "GP", "-", "Lower", "Confidence", "Bound" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/LCB_mcmc.py#L26-L34
228,217
SheffieldML/GPyOpt
GPyOpt/acquisitions/LCB_mcmc.py
AcquisitionLCB_MCMC._compute_acq_withGradients
def _compute_acq_withGradients(self, x): """ Integrated GP-Lower Confidence Bound and its derivative """ means, stds, dmdxs, dsdxs = self.model.predict_withGradients(x) f_acqu = None df_acqu = None for m, s, dmdx, dsdx in zip(means, stds, dmdxs, dsdxs): f = -m + self.exploration_weight * s df = -dmdx + self.exploration_weight * dsdx if f_acqu is None: f_acqu = f df_acqu = df else: f_acqu += f df_acqu += df return f_acqu/(len(means)), df_acqu/(len(means))
python
def _compute_acq_withGradients(self, x): """ Integrated GP-Lower Confidence Bound and its derivative """ means, stds, dmdxs, dsdxs = self.model.predict_withGradients(x) f_acqu = None df_acqu = None for m, s, dmdx, dsdx in zip(means, stds, dmdxs, dsdxs): f = -m + self.exploration_weight * s df = -dmdx + self.exploration_weight * dsdx if f_acqu is None: f_acqu = f df_acqu = df else: f_acqu += f df_acqu += df return f_acqu/(len(means)), df_acqu/(len(means))
[ "def", "_compute_acq_withGradients", "(", "self", ",", "x", ")", ":", "means", ",", "stds", ",", "dmdxs", ",", "dsdxs", "=", "self", ".", "model", ".", "predict_withGradients", "(", "x", ")", "f_acqu", "=", "None", "df_acqu", "=", "None", "for", "m", ",", "s", ",", "dmdx", ",", "dsdx", "in", "zip", "(", "means", ",", "stds", ",", "dmdxs", ",", "dsdxs", ")", ":", "f", "=", "-", "m", "+", "self", ".", "exploration_weight", "*", "s", "df", "=", "-", "dmdx", "+", "self", ".", "exploration_weight", "*", "dsdx", "if", "f_acqu", "is", "None", ":", "f_acqu", "=", "f", "df_acqu", "=", "df", "else", ":", "f_acqu", "+=", "f", "df_acqu", "+=", "df", "return", "f_acqu", "/", "(", "len", "(", "means", ")", ")", ",", "df_acqu", "/", "(", "len", "(", "means", ")", ")" ]
Integrated GP-Lower Confidence Bound and its derivative
[ "Integrated", "GP", "-", "Lower", "Confidence", "Bound", "and", "its", "derivative" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/LCB_mcmc.py#L36-L52
228,218
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space._expand_config_space
def _expand_config_space(self): """ Expands the config input space into a list of diccionaries, one for each variable_dic in which the dimensionality is always one. Example: It would transform config_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':2}, into config_expande_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}, {'name': 'var_2_1', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}] """ self.config_space_expanded = [] for variable in self.config_space: variable_dic = variable.copy() if 'dimensionality' in variable_dic.keys(): dimensionality = variable_dic['dimensionality'] variable_dic['dimensionality'] = 1 variables_set = [variable_dic.copy() for d in range(dimensionality)] k=1 for variable in variables_set: variable['name'] = variable['name'] + '_'+str(k) k+=1 self.config_space_expanded += variables_set else: self.config_space_expanded += [variable_dic]
python
def _expand_config_space(self): """ Expands the config input space into a list of diccionaries, one for each variable_dic in which the dimensionality is always one. Example: It would transform config_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':2}, into config_expande_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}, {'name': 'var_2_1', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}] """ self.config_space_expanded = [] for variable in self.config_space: variable_dic = variable.copy() if 'dimensionality' in variable_dic.keys(): dimensionality = variable_dic['dimensionality'] variable_dic['dimensionality'] = 1 variables_set = [variable_dic.copy() for d in range(dimensionality)] k=1 for variable in variables_set: variable['name'] = variable['name'] + '_'+str(k) k+=1 self.config_space_expanded += variables_set else: self.config_space_expanded += [variable_dic]
[ "def", "_expand_config_space", "(", "self", ")", ":", "self", ".", "config_space_expanded", "=", "[", "]", "for", "variable", "in", "self", ".", "config_space", ":", "variable_dic", "=", "variable", ".", "copy", "(", ")", "if", "'dimensionality'", "in", "variable_dic", ".", "keys", "(", ")", ":", "dimensionality", "=", "variable_dic", "[", "'dimensionality'", "]", "variable_dic", "[", "'dimensionality'", "]", "=", "1", "variables_set", "=", "[", "variable_dic", ".", "copy", "(", ")", "for", "d", "in", "range", "(", "dimensionality", ")", "]", "k", "=", "1", "for", "variable", "in", "variables_set", ":", "variable", "[", "'name'", "]", "=", "variable", "[", "'name'", "]", "+", "'_'", "+", "str", "(", "k", ")", "k", "+=", "1", "self", ".", "config_space_expanded", "+=", "variables_set", "else", ":", "self", ".", "config_space_expanded", "+=", "[", "variable_dic", "]" ]
Expands the config input space into a list of diccionaries, one for each variable_dic in which the dimensionality is always one. Example: It would transform config_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':2}, into config_expande_space =[ {'name': 'var_1', 'type': 'continuous', 'domain':(-1,1), 'dimensionality':1}, {'name': 'var_2', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}, {'name': 'var_2_1', 'type': 'continuous', 'domain':(-3,1), 'dimensionality':1}]
[ "Expands", "the", "config", "input", "space", "into", "a", "list", "of", "diccionaries", "one", "for", "each", "variable_dic", "in", "which", "the", "dimensionality", "is", "always", "one", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L101-L131
228,219
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space._create_variables_dic
def _create_variables_dic(self): """ Returns the variable by passing its name """ self.name_to_variable = {} for variable in self.space_expanded: self.name_to_variable[variable.name] = variable
python
def _create_variables_dic(self): """ Returns the variable by passing its name """ self.name_to_variable = {} for variable in self.space_expanded: self.name_to_variable[variable.name] = variable
[ "def", "_create_variables_dic", "(", "self", ")", ":", "self", ".", "name_to_variable", "=", "{", "}", "for", "variable", "in", "self", ".", "space_expanded", ":", "self", ".", "name_to_variable", "[", "variable", ".", "name", "]", "=", "variable" ]
Returns the variable by passing its name
[ "Returns", "the", "variable", "by", "passing", "its", "name" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L162-L168
228,220
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space._translate_space
def _translate_space(self, space): """ Translates a list of dictionaries into internal list of variables """ self.space = [] self.dimensionality = 0 self.has_types = d = {t: False for t in self.supported_types} for i, d in enumerate(space): descriptor = deepcopy(d) descriptor['name'] = descriptor.get('name', 'var_' + str(i)) descriptor['type'] = descriptor.get('type', 'continuous') if 'domain' not in descriptor: raise InvalidConfigError('Domain attribute is missing for variable ' + descriptor['name']) variable = create_variable(descriptor) self.space.append(variable) self.dimensionality += variable.dimensionality self.has_types[variable.type] = True # Check if there are any bandit and non-bandit variables together in the space if any(v.is_bandit() for v in self.space) and any(not v.is_bandit() for v in self.space): raise InvalidConfigError('Invalid mixed domain configuration. Bandit variables cannot be mixed with other types.')
python
def _translate_space(self, space): """ Translates a list of dictionaries into internal list of variables """ self.space = [] self.dimensionality = 0 self.has_types = d = {t: False for t in self.supported_types} for i, d in enumerate(space): descriptor = deepcopy(d) descriptor['name'] = descriptor.get('name', 'var_' + str(i)) descriptor['type'] = descriptor.get('type', 'continuous') if 'domain' not in descriptor: raise InvalidConfigError('Domain attribute is missing for variable ' + descriptor['name']) variable = create_variable(descriptor) self.space.append(variable) self.dimensionality += variable.dimensionality self.has_types[variable.type] = True # Check if there are any bandit and non-bandit variables together in the space if any(v.is_bandit() for v in self.space) and any(not v.is_bandit() for v in self.space): raise InvalidConfigError('Invalid mixed domain configuration. Bandit variables cannot be mixed with other types.')
[ "def", "_translate_space", "(", "self", ",", "space", ")", ":", "self", ".", "space", "=", "[", "]", "self", ".", "dimensionality", "=", "0", "self", ".", "has_types", "=", "d", "=", "{", "t", ":", "False", "for", "t", "in", "self", ".", "supported_types", "}", "for", "i", ",", "d", "in", "enumerate", "(", "space", ")", ":", "descriptor", "=", "deepcopy", "(", "d", ")", "descriptor", "[", "'name'", "]", "=", "descriptor", ".", "get", "(", "'name'", ",", "'var_'", "+", "str", "(", "i", ")", ")", "descriptor", "[", "'type'", "]", "=", "descriptor", ".", "get", "(", "'type'", ",", "'continuous'", ")", "if", "'domain'", "not", "in", "descriptor", ":", "raise", "InvalidConfigError", "(", "'Domain attribute is missing for variable '", "+", "descriptor", "[", "'name'", "]", ")", "variable", "=", "create_variable", "(", "descriptor", ")", "self", ".", "space", ".", "append", "(", "variable", ")", "self", ".", "dimensionality", "+=", "variable", ".", "dimensionality", "self", ".", "has_types", "[", "variable", ".", "type", "]", "=", "True", "# Check if there are any bandit and non-bandit variables together in the space", "if", "any", "(", "v", ".", "is_bandit", "(", ")", "for", "v", "in", "self", ".", "space", ")", "and", "any", "(", "not", "v", ".", "is_bandit", "(", ")", "for", "v", "in", "self", ".", "space", ")", ":", "raise", "InvalidConfigError", "(", "'Invalid mixed domain configuration. Bandit variables cannot be mixed with other types.'", ")" ]
Translates a list of dictionaries into internal list of variables
[ "Translates", "a", "list", "of", "dictionaries", "into", "internal", "list", "of", "variables" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L170-L191
228,221
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space._expand_space
def _expand_space(self): """ Creates an internal list where the variables with dimensionality larger than one are expanded. This list is the one that is used internally to do the optimization. """ ## --- Expand the config space self._expand_config_space() ## --- Expand the space self.space_expanded = [] for variable in self.space: self.space_expanded += variable.expand()
python
def _expand_space(self): """ Creates an internal list where the variables with dimensionality larger than one are expanded. This list is the one that is used internally to do the optimization. """ ## --- Expand the config space self._expand_config_space() ## --- Expand the space self.space_expanded = [] for variable in self.space: self.space_expanded += variable.expand()
[ "def", "_expand_space", "(", "self", ")", ":", "## --- Expand the config space", "self", ".", "_expand_config_space", "(", ")", "## --- Expand the space", "self", ".", "space_expanded", "=", "[", "]", "for", "variable", "in", "self", ".", "space", ":", "self", ".", "space_expanded", "+=", "variable", ".", "expand", "(", ")" ]
Creates an internal list where the variables with dimensionality larger than one are expanded. This list is the one that is used internally to do the optimization.
[ "Creates", "an", "internal", "list", "where", "the", "variables", "with", "dimensionality", "larger", "than", "one", "are", "expanded", ".", "This", "list", "is", "the", "one", "that", "is", "used", "internally", "to", "do", "the", "optimization", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L193-L205
228,222
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.objective_to_model
def objective_to_model(self, x_objective): ''' This function serves as interface between objective input vectors and model input vectors''' x_model = [] for k in range(self.objective_dimensionality): variable = self.space_expanded[k] new_entry = variable.objective_to_model(x_objective[0,k]) x_model += new_entry return x_model
python
def objective_to_model(self, x_objective): ''' This function serves as interface between objective input vectors and model input vectors''' x_model = [] for k in range(self.objective_dimensionality): variable = self.space_expanded[k] new_entry = variable.objective_to_model(x_objective[0,k]) x_model += new_entry return x_model
[ "def", "objective_to_model", "(", "self", ",", "x_objective", ")", ":", "x_model", "=", "[", "]", "for", "k", "in", "range", "(", "self", ".", "objective_dimensionality", ")", ":", "variable", "=", "self", ".", "space_expanded", "[", "k", "]", "new_entry", "=", "variable", ".", "objective_to_model", "(", "x_objective", "[", "0", ",", "k", "]", ")", "x_model", "+=", "new_entry", "return", "x_model" ]
This function serves as interface between objective input vectors and model input vectors
[ "This", "function", "serves", "as", "interface", "between", "objective", "input", "vectors", "and", "model", "input", "vectors" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L207-L218
228,223
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.model_to_objective
def model_to_objective(self, x_model): ''' This function serves as interface between model input vectors and objective input vectors ''' idx_model = 0 x_objective = [] for idx_obj in range(self.objective_dimensionality): variable = self.space_expanded[idx_obj] new_entry = variable.model_to_objective(x_model, idx_model) x_objective += new_entry idx_model += variable.dimensionality_in_model return x_objective
python
def model_to_objective(self, x_model): ''' This function serves as interface between model input vectors and objective input vectors ''' idx_model = 0 x_objective = [] for idx_obj in range(self.objective_dimensionality): variable = self.space_expanded[idx_obj] new_entry = variable.model_to_objective(x_model, idx_model) x_objective += new_entry idx_model += variable.dimensionality_in_model return x_objective
[ "def", "model_to_objective", "(", "self", ",", "x_model", ")", ":", "idx_model", "=", "0", "x_objective", "=", "[", "]", "for", "idx_obj", "in", "range", "(", "self", ".", "objective_dimensionality", ")", ":", "variable", "=", "self", ".", "space_expanded", "[", "idx_obj", "]", "new_entry", "=", "variable", ".", "model_to_objective", "(", "x_model", ",", "idx_model", ")", "x_objective", "+=", "new_entry", "idx_model", "+=", "variable", ".", "dimensionality_in_model", "return", "x_objective" ]
This function serves as interface between model input vectors and objective input vectors
[ "This", "function", "serves", "as", "interface", "between", "model", "input", "vectors", "and", "objective", "input", "vectors" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L238-L251
228,224
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_subspace
def get_subspace(self, dims): ''' Extracts subspace from the reference of a list of variables in the inputs of the model. ''' subspace = [] k = 0 for variable in self.space_expanded: if k in dims: subspace.append(variable) k += variable.dimensionality_in_model return subspace
python
def get_subspace(self, dims): ''' Extracts subspace from the reference of a list of variables in the inputs of the model. ''' subspace = [] k = 0 for variable in self.space_expanded: if k in dims: subspace.append(variable) k += variable.dimensionality_in_model return subspace
[ "def", "get_subspace", "(", "self", ",", "dims", ")", ":", "subspace", "=", "[", "]", "k", "=", "0", "for", "variable", "in", "self", ".", "space_expanded", ":", "if", "k", "in", "dims", ":", "subspace", ".", "append", "(", "variable", ")", "k", "+=", "variable", ".", "dimensionality_in_model", "return", "subspace" ]
Extracts subspace from the reference of a list of variables in the inputs of the model.
[ "Extracts", "subspace", "from", "the", "reference", "of", "a", "list", "of", "variables", "in", "the", "inputs", "of", "the", "model", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L283-L294
228,225
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.indicator_constraints
def indicator_constraints(self,x): """ Returns array of ones and zeros indicating if x is within the constraints """ x = np.atleast_2d(x) I_x = np.ones((x.shape[0],1)) if self.constraints is not None: for d in self.constraints: try: exec('constraint = lambda x:' + d['constraint'], globals()) ind_x = (constraint(x) <= 0) * 1 I_x *= ind_x.reshape(x.shape[0],1) except: print('Fail to compile the constraint: ' + str(d)) raise return I_x
python
def indicator_constraints(self,x): """ Returns array of ones and zeros indicating if x is within the constraints """ x = np.atleast_2d(x) I_x = np.ones((x.shape[0],1)) if self.constraints is not None: for d in self.constraints: try: exec('constraint = lambda x:' + d['constraint'], globals()) ind_x = (constraint(x) <= 0) * 1 I_x *= ind_x.reshape(x.shape[0],1) except: print('Fail to compile the constraint: ' + str(d)) raise return I_x
[ "def", "indicator_constraints", "(", "self", ",", "x", ")", ":", "x", "=", "np", ".", "atleast_2d", "(", "x", ")", "I_x", "=", "np", ".", "ones", "(", "(", "x", ".", "shape", "[", "0", "]", ",", "1", ")", ")", "if", "self", ".", "constraints", "is", "not", "None", ":", "for", "d", "in", "self", ".", "constraints", ":", "try", ":", "exec", "(", "'constraint = lambda x:'", "+", "d", "[", "'constraint'", "]", ",", "globals", "(", ")", ")", "ind_x", "=", "(", "constraint", "(", "x", ")", "<=", "0", ")", "*", "1", "I_x", "*=", "ind_x", ".", "reshape", "(", "x", ".", "shape", "[", "0", "]", ",", "1", ")", "except", ":", "print", "(", "'Fail to compile the constraint: '", "+", "str", "(", "d", ")", ")", "raise", "return", "I_x" ]
Returns array of ones and zeros indicating if x is within the constraints
[ "Returns", "array", "of", "ones", "and", "zeros", "indicating", "if", "x", "is", "within", "the", "constraints" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L297-L312
228,226
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.input_dim
def input_dim(self): """ Extracts the input dimension of the domain. """ n_cont = len(self.get_continuous_dims()) n_disc = len(self.get_discrete_dims()) return n_cont + n_disc
python
def input_dim(self): """ Extracts the input dimension of the domain. """ n_cont = len(self.get_continuous_dims()) n_disc = len(self.get_discrete_dims()) return n_cont + n_disc
[ "def", "input_dim", "(", "self", ")", ":", "n_cont", "=", "len", "(", "self", ".", "get_continuous_dims", "(", ")", ")", "n_disc", "=", "len", "(", "self", ".", "get_discrete_dims", "(", ")", ")", "return", "n_cont", "+", "n_disc" ]
Extracts the input dimension of the domain.
[ "Extracts", "the", "input", "dimension", "of", "the", "domain", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L314-L320
228,227
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.round_optimum
def round_optimum(self, x): """ Rounds some value x to a feasible value in the design space. x is expected to be a vector or an array with a single row """ x = np.array(x) if not ((x.ndim == 1) or (x.ndim == 2 and x.shape[0] == 1)): raise ValueError("Unexpected dimentionality of x. Got {}, expected (1, N) or (N,)".format(x.ndim)) if x.ndim == 2: x = x[0] x_rounded = [] value_index = 0 for variable in self.space_expanded: var_value = x[value_index : value_index + variable.dimensionality_in_model] var_value_rounded = variable.round(var_value) x_rounded.append(var_value_rounded) value_index += variable.dimensionality_in_model return np.atleast_2d(np.concatenate(x_rounded))
python
def round_optimum(self, x): """ Rounds some value x to a feasible value in the design space. x is expected to be a vector or an array with a single row """ x = np.array(x) if not ((x.ndim == 1) or (x.ndim == 2 and x.shape[0] == 1)): raise ValueError("Unexpected dimentionality of x. Got {}, expected (1, N) or (N,)".format(x.ndim)) if x.ndim == 2: x = x[0] x_rounded = [] value_index = 0 for variable in self.space_expanded: var_value = x[value_index : value_index + variable.dimensionality_in_model] var_value_rounded = variable.round(var_value) x_rounded.append(var_value_rounded) value_index += variable.dimensionality_in_model return np.atleast_2d(np.concatenate(x_rounded))
[ "def", "round_optimum", "(", "self", ",", "x", ")", ":", "x", "=", "np", ".", "array", "(", "x", ")", "if", "not", "(", "(", "x", ".", "ndim", "==", "1", ")", "or", "(", "x", ".", "ndim", "==", "2", "and", "x", ".", "shape", "[", "0", "]", "==", "1", ")", ")", ":", "raise", "ValueError", "(", "\"Unexpected dimentionality of x. Got {}, expected (1, N) or (N,)\"", ".", "format", "(", "x", ".", "ndim", ")", ")", "if", "x", ".", "ndim", "==", "2", ":", "x", "=", "x", "[", "0", "]", "x_rounded", "=", "[", "]", "value_index", "=", "0", "for", "variable", "in", "self", ".", "space_expanded", ":", "var_value", "=", "x", "[", "value_index", ":", "value_index", "+", "variable", ".", "dimensionality_in_model", "]", "var_value_rounded", "=", "variable", ".", "round", "(", "var_value", ")", "x_rounded", ".", "append", "(", "var_value_rounded", ")", "value_index", "+=", "variable", ".", "dimensionality_in_model", "return", "np", ".", "atleast_2d", "(", "np", ".", "concatenate", "(", "x_rounded", ")", ")" ]
Rounds some value x to a feasible value in the design space. x is expected to be a vector or an array with a single row
[ "Rounds", "some", "value", "x", "to", "a", "feasible", "value", "in", "the", "design", "space", ".", "x", "is", "expected", "to", "be", "a", "vector", "or", "an", "array", "with", "a", "single", "row" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L322-L343
228,228
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_continuous_bounds
def get_continuous_bounds(self): """ Extracts the bounds of the continuous variables. """ bounds = [] for d in self.space: if d.type == 'continuous': bounds.extend([d.domain]*d.dimensionality) return bounds
python
def get_continuous_bounds(self): """ Extracts the bounds of the continuous variables. """ bounds = [] for d in self.space: if d.type == 'continuous': bounds.extend([d.domain]*d.dimensionality) return bounds
[ "def", "get_continuous_bounds", "(", "self", ")", ":", "bounds", "=", "[", "]", "for", "d", "in", "self", ".", "space", ":", "if", "d", ".", "type", "==", "'continuous'", ":", "bounds", ".", "extend", "(", "[", "d", ".", "domain", "]", "*", "d", ".", "dimensionality", ")", "return", "bounds" ]
Extracts the bounds of the continuous variables.
[ "Extracts", "the", "bounds", "of", "the", "continuous", "variables", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L353-L361
228,229
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_continuous_dims
def get_continuous_dims(self): """ Returns the dimension of the continuous components of the domain. """ continuous_dims = [] for i in range(self.dimensionality): if self.space_expanded[i].type == 'continuous': continuous_dims += [i] return continuous_dims
python
def get_continuous_dims(self): """ Returns the dimension of the continuous components of the domain. """ continuous_dims = [] for i in range(self.dimensionality): if self.space_expanded[i].type == 'continuous': continuous_dims += [i] return continuous_dims
[ "def", "get_continuous_dims", "(", "self", ")", ":", "continuous_dims", "=", "[", "]", "for", "i", "in", "range", "(", "self", ".", "dimensionality", ")", ":", "if", "self", ".", "space_expanded", "[", "i", "]", ".", "type", "==", "'continuous'", ":", "continuous_dims", "+=", "[", "i", "]", "return", "continuous_dims" ]
Returns the dimension of the continuous components of the domain.
[ "Returns", "the", "dimension", "of", "the", "continuous", "components", "of", "the", "domain", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L364-L372
228,230
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_discrete_grid
def get_discrete_grid(self): """ Computes a Numpy array with the grid of points that results after crossing the possible outputs of the discrete variables """ sets_grid = [] for d in self.space: if d.type == 'discrete': sets_grid.extend([d.domain]*d.dimensionality) return np.array(list(itertools.product(*sets_grid)))
python
def get_discrete_grid(self): """ Computes a Numpy array with the grid of points that results after crossing the possible outputs of the discrete variables """ sets_grid = [] for d in self.space: if d.type == 'discrete': sets_grid.extend([d.domain]*d.dimensionality) return np.array(list(itertools.product(*sets_grid)))
[ "def", "get_discrete_grid", "(", "self", ")", ":", "sets_grid", "=", "[", "]", "for", "d", "in", "self", ".", "space", ":", "if", "d", ".", "type", "==", "'discrete'", ":", "sets_grid", ".", "extend", "(", "[", "d", ".", "domain", "]", "*", "d", ".", "dimensionality", ")", "return", "np", ".", "array", "(", "list", "(", "itertools", ".", "product", "(", "*", "sets_grid", ")", ")", ")" ]
Computes a Numpy array with the grid of points that results after crossing the possible outputs of the discrete variables
[ "Computes", "a", "Numpy", "array", "with", "the", "grid", "of", "points", "that", "results", "after", "crossing", "the", "possible", "outputs", "of", "the", "discrete", "variables" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L387-L396
228,231
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_discrete_dims
def get_discrete_dims(self): """ Returns the dimension of the discrete components of the domain. """ discrete_dims = [] for i in range(self.dimensionality): if self.space_expanded[i].type == 'discrete': discrete_dims += [i] return discrete_dims
python
def get_discrete_dims(self): """ Returns the dimension of the discrete components of the domain. """ discrete_dims = [] for i in range(self.dimensionality): if self.space_expanded[i].type == 'discrete': discrete_dims += [i] return discrete_dims
[ "def", "get_discrete_dims", "(", "self", ")", ":", "discrete_dims", "=", "[", "]", "for", "i", "in", "range", "(", "self", ".", "dimensionality", ")", ":", "if", "self", ".", "space_expanded", "[", "i", "]", ".", "type", "==", "'discrete'", ":", "discrete_dims", "+=", "[", "i", "]", "return", "discrete_dims" ]
Returns the dimension of the discrete components of the domain.
[ "Returns", "the", "dimension", "of", "the", "discrete", "components", "of", "the", "domain", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L399-L407
228,232
SheffieldML/GPyOpt
GPyOpt/core/task/space.py
Design_space.get_bandit
def get_bandit(self): """ Extracts the arms of the bandit if any. """ arms_bandit = [] for d in self.space: if d.type == 'bandit': arms_bandit += tuple(map(tuple, d.domain)) return np.asarray(arms_bandit)
python
def get_bandit(self): """ Extracts the arms of the bandit if any. """ arms_bandit = [] for d in self.space: if d.type == 'bandit': arms_bandit += tuple(map(tuple, d.domain)) return np.asarray(arms_bandit)
[ "def", "get_bandit", "(", "self", ")", ":", "arms_bandit", "=", "[", "]", "for", "d", "in", "self", ".", "space", ":", "if", "d", ".", "type", "==", "'bandit'", ":", "arms_bandit", "+=", "tuple", "(", "map", "(", "tuple", ",", "d", ".", "domain", ")", ")", "return", "np", ".", "asarray", "(", "arms_bandit", ")" ]
Extracts the arms of the bandit if any.
[ "Extracts", "the", "arms", "of", "the", "bandit", "if", "any", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/task/space.py#L422-L430
228,233
SheffieldML/GPyOpt
GPyOpt/models/rfmodel.py
RFModel.predict
def predict(self, X): """ Predictions with the model. Returns posterior means and standard deviations at X. """ X = np.atleast_2d(X) m = np.empty(shape=(0,1)) s = np.empty(shape=(0,1)) for k in range(X.shape[0]): preds = [] for pred in self.model.estimators_: preds.append(pred.predict(X[k,:])[0]) m = np.vstack((m ,np.array(preds).mean())) s = np.vstack((s ,np.array(preds).std())) return m, s
python
def predict(self, X): """ Predictions with the model. Returns posterior means and standard deviations at X. """ X = np.atleast_2d(X) m = np.empty(shape=(0,1)) s = np.empty(shape=(0,1)) for k in range(X.shape[0]): preds = [] for pred in self.model.estimators_: preds.append(pred.predict(X[k,:])[0]) m = np.vstack((m ,np.array(preds).mean())) s = np.vstack((s ,np.array(preds).std())) return m, s
[ "def", "predict", "(", "self", ",", "X", ")", ":", "X", "=", "np", ".", "atleast_2d", "(", "X", ")", "m", "=", "np", ".", "empty", "(", "shape", "=", "(", "0", ",", "1", ")", ")", "s", "=", "np", ".", "empty", "(", "shape", "=", "(", "0", ",", "1", ")", ")", "for", "k", "in", "range", "(", "X", ".", "shape", "[", "0", "]", ")", ":", "preds", "=", "[", "]", "for", "pred", "in", "self", ".", "model", ".", "estimators_", ":", "preds", ".", "append", "(", "pred", ".", "predict", "(", "X", "[", "k", ",", ":", "]", ")", "[", "0", "]", ")", "m", "=", "np", ".", "vstack", "(", "(", "m", ",", "np", ".", "array", "(", "preds", ")", ".", "mean", "(", ")", ")", ")", "s", "=", "np", ".", "vstack", "(", "(", "s", ",", "np", ".", "array", "(", "preds", ")", ".", "std", "(", ")", ")", ")", "return", "m", ",", "s" ]
Predictions with the model. Returns posterior means and standard deviations at X.
[ "Predictions", "with", "the", "model", ".", "Returns", "posterior", "means", "and", "standard", "deviations", "at", "X", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/rfmodel.py#L79-L93
228,234
SheffieldML/GPyOpt
GPyOpt/acquisitions/MPI_mcmc.py
AcquisitionMPI_MCMC._compute_acq
def _compute_acq(self,x): """ Integrated Expected Improvement """ means, stds = self.model.predict(x) fmins = self.model.get_fmin() f_acqu = 0 for m,s,fmin in zip(means, stds, fmins): _, Phi, _ = get_quantiles(self.jitter, fmin, m, s) f_acqu += Phi return f_acqu/len(means)
python
def _compute_acq(self,x): """ Integrated Expected Improvement """ means, stds = self.model.predict(x) fmins = self.model.get_fmin() f_acqu = 0 for m,s,fmin in zip(means, stds, fmins): _, Phi, _ = get_quantiles(self.jitter, fmin, m, s) f_acqu += Phi return f_acqu/len(means)
[ "def", "_compute_acq", "(", "self", ",", "x", ")", ":", "means", ",", "stds", "=", "self", ".", "model", ".", "predict", "(", "x", ")", "fmins", "=", "self", ".", "model", ".", "get_fmin", "(", ")", "f_acqu", "=", "0", "for", "m", ",", "s", ",", "fmin", "in", "zip", "(", "means", ",", "stds", ",", "fmins", ")", ":", "_", ",", "Phi", ",", "_", "=", "get_quantiles", "(", "self", ".", "jitter", ",", "fmin", ",", "m", ",", "s", ")", "f_acqu", "+=", "Phi", "return", "f_acqu", "/", "len", "(", "means", ")" ]
Integrated Expected Improvement
[ "Integrated", "Expected", "Improvement" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/MPI_mcmc.py#L29-L39
228,235
SheffieldML/GPyOpt
GPyOpt/acquisitions/MPI_mcmc.py
AcquisitionMPI_MCMC._compute_acq_withGradients
def _compute_acq_withGradients(self, x): """ Integrated Expected Improvement and its derivative """ means, stds, dmdxs, dsdxs = self.model.predict_withGradients(x) fmins = self.model.get_fmin() f_acqu = None df_acqu = None for m, s, fmin, dmdx, dsdx in zip(means, stds, fmins, dmdxs, dsdxs): phi, Phi, u = get_quantiles(self.jitter, fmin, m, s) f = Phi df = -(phi/s)* (dmdx + dsdx * u) if f_acqu is None: f_acqu = f df_acqu = df else: f_acqu += f df_acqu += df return f_acqu/(len(means)), df_acqu/(len(means))
python
def _compute_acq_withGradients(self, x): """ Integrated Expected Improvement and its derivative """ means, stds, dmdxs, dsdxs = self.model.predict_withGradients(x) fmins = self.model.get_fmin() f_acqu = None df_acqu = None for m, s, fmin, dmdx, dsdx in zip(means, stds, fmins, dmdxs, dsdxs): phi, Phi, u = get_quantiles(self.jitter, fmin, m, s) f = Phi df = -(phi/s)* (dmdx + dsdx * u) if f_acqu is None: f_acqu = f df_acqu = df else: f_acqu += f df_acqu += df return f_acqu/(len(means)), df_acqu/(len(means))
[ "def", "_compute_acq_withGradients", "(", "self", ",", "x", ")", ":", "means", ",", "stds", ",", "dmdxs", ",", "dsdxs", "=", "self", ".", "model", ".", "predict_withGradients", "(", "x", ")", "fmins", "=", "self", ".", "model", ".", "get_fmin", "(", ")", "f_acqu", "=", "None", "df_acqu", "=", "None", "for", "m", ",", "s", ",", "fmin", ",", "dmdx", ",", "dsdx", "in", "zip", "(", "means", ",", "stds", ",", "fmins", ",", "dmdxs", ",", "dsdxs", ")", ":", "phi", ",", "Phi", ",", "u", "=", "get_quantiles", "(", "self", ".", "jitter", ",", "fmin", ",", "m", ",", "s", ")", "f", "=", "Phi", "df", "=", "-", "(", "phi", "/", "s", ")", "*", "(", "dmdx", "+", "dsdx", "*", "u", ")", "if", "f_acqu", "is", "None", ":", "f_acqu", "=", "f", "df_acqu", "=", "df", "else", ":", "f_acqu", "+=", "f", "df_acqu", "+=", "df", "return", "f_acqu", "/", "(", "len", "(", "means", ")", ")", ",", "df_acqu", "/", "(", "len", "(", "means", ")", ")" ]
Integrated Expected Improvement and its derivative
[ "Integrated", "Expected", "Improvement", "and", "its", "derivative" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/MPI_mcmc.py#L41-L59
228,236
SheffieldML/GPyOpt
GPyOpt/plotting/plots_bo.py
plot_convergence
def plot_convergence(Xdata,best_Y, filename = None): ''' Plots to evaluate the convergence of standard Bayesian optimization algorithms ''' n = Xdata.shape[0] aux = (Xdata[1:n,:]-Xdata[0:n-1,:])**2 distances = np.sqrt(aux.sum(axis=1)) ## Distances between consecutive x's plt.figure(figsize=(10,5)) plt.subplot(1, 2, 1) plt.plot(list(range(n-1)), distances, '-ro') plt.xlabel('Iteration') plt.ylabel('d(x[n], x[n-1])') plt.title('Distance between consecutive x\'s') grid(True) # Estimated m(x) at the proposed sampling points plt.subplot(1, 2, 2) plt.plot(list(range(n)),best_Y,'-o') plt.title('Value of the best selected sample') plt.xlabel('Iteration') plt.ylabel('Best y') grid(True) if filename!=None: savefig(filename) else: plt.show()
python
def plot_convergence(Xdata,best_Y, filename = None): ''' Plots to evaluate the convergence of standard Bayesian optimization algorithms ''' n = Xdata.shape[0] aux = (Xdata[1:n,:]-Xdata[0:n-1,:])**2 distances = np.sqrt(aux.sum(axis=1)) ## Distances between consecutive x's plt.figure(figsize=(10,5)) plt.subplot(1, 2, 1) plt.plot(list(range(n-1)), distances, '-ro') plt.xlabel('Iteration') plt.ylabel('d(x[n], x[n-1])') plt.title('Distance between consecutive x\'s') grid(True) # Estimated m(x) at the proposed sampling points plt.subplot(1, 2, 2) plt.plot(list(range(n)),best_Y,'-o') plt.title('Value of the best selected sample') plt.xlabel('Iteration') plt.ylabel('Best y') grid(True) if filename!=None: savefig(filename) else: plt.show()
[ "def", "plot_convergence", "(", "Xdata", ",", "best_Y", ",", "filename", "=", "None", ")", ":", "n", "=", "Xdata", ".", "shape", "[", "0", "]", "aux", "=", "(", "Xdata", "[", "1", ":", "n", ",", ":", "]", "-", "Xdata", "[", "0", ":", "n", "-", "1", ",", ":", "]", ")", "**", "2", "distances", "=", "np", ".", "sqrt", "(", "aux", ".", "sum", "(", "axis", "=", "1", ")", ")", "## Distances between consecutive x's", "plt", ".", "figure", "(", "figsize", "=", "(", "10", ",", "5", ")", ")", "plt", ".", "subplot", "(", "1", ",", "2", ",", "1", ")", "plt", ".", "plot", "(", "list", "(", "range", "(", "n", "-", "1", ")", ")", ",", "distances", ",", "'-ro'", ")", "plt", ".", "xlabel", "(", "'Iteration'", ")", "plt", ".", "ylabel", "(", "'d(x[n], x[n-1])'", ")", "plt", ".", "title", "(", "'Distance between consecutive x\\'s'", ")", "grid", "(", "True", ")", "# Estimated m(x) at the proposed sampling points", "plt", ".", "subplot", "(", "1", ",", "2", ",", "2", ")", "plt", ".", "plot", "(", "list", "(", "range", "(", "n", ")", ")", ",", "best_Y", ",", "'-o'", ")", "plt", ".", "title", "(", "'Value of the best selected sample'", ")", "plt", ".", "xlabel", "(", "'Iteration'", ")", "plt", ".", "ylabel", "(", "'Best y'", ")", "grid", "(", "True", ")", "if", "filename", "!=", "None", ":", "savefig", "(", "filename", ")", "else", ":", "plt", ".", "show", "(", ")" ]
Plots to evaluate the convergence of standard Bayesian optimization algorithms
[ "Plots", "to", "evaluate", "the", "convergence", "of", "standard", "Bayesian", "optimization", "algorithms" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/plotting/plots_bo.py#L122-L150
228,237
SheffieldML/GPyOpt
GPyOpt/core/evaluators/batch_local_penalization.py
LocalPenalization.compute_batch
def compute_batch(self, duplicate_manager=None, context_manager=None): """ Computes the elements of the batch sequentially by penalizing the acquisition. """ from ...acquisitions import AcquisitionLP assert isinstance(self.acquisition, AcquisitionLP) self.acquisition.update_batches(None,None,None) # --- GET first element in the batch X_batch = self.acquisition.optimize()[0] k=1 if self.batch_size >1: # ---------- Approximate the constants of the the method L = estimate_L(self.acquisition.model.model,self.acquisition.space.get_bounds()) Min = self.acquisition.model.model.Y.min() # --- GET the remaining elements while k<self.batch_size: self.acquisition.update_batches(X_batch,L,Min) new_sample = self.acquisition.optimize()[0] X_batch = np.vstack((X_batch,new_sample)) k +=1 # --- Back to the non-penalized acquisition self.acquisition.update_batches(None,None,None) return X_batch
python
def compute_batch(self, duplicate_manager=None, context_manager=None): """ Computes the elements of the batch sequentially by penalizing the acquisition. """ from ...acquisitions import AcquisitionLP assert isinstance(self.acquisition, AcquisitionLP) self.acquisition.update_batches(None,None,None) # --- GET first element in the batch X_batch = self.acquisition.optimize()[0] k=1 if self.batch_size >1: # ---------- Approximate the constants of the the method L = estimate_L(self.acquisition.model.model,self.acquisition.space.get_bounds()) Min = self.acquisition.model.model.Y.min() # --- GET the remaining elements while k<self.batch_size: self.acquisition.update_batches(X_batch,L,Min) new_sample = self.acquisition.optimize()[0] X_batch = np.vstack((X_batch,new_sample)) k +=1 # --- Back to the non-penalized acquisition self.acquisition.update_batches(None,None,None) return X_batch
[ "def", "compute_batch", "(", "self", ",", "duplicate_manager", "=", "None", ",", "context_manager", "=", "None", ")", ":", "from", ".", ".", ".", "acquisitions", "import", "AcquisitionLP", "assert", "isinstance", "(", "self", ".", "acquisition", ",", "AcquisitionLP", ")", "self", ".", "acquisition", ".", "update_batches", "(", "None", ",", "None", ",", "None", ")", "# --- GET first element in the batch", "X_batch", "=", "self", ".", "acquisition", ".", "optimize", "(", ")", "[", "0", "]", "k", "=", "1", "if", "self", ".", "batch_size", ">", "1", ":", "# ---------- Approximate the constants of the the method", "L", "=", "estimate_L", "(", "self", ".", "acquisition", ".", "model", ".", "model", ",", "self", ".", "acquisition", ".", "space", ".", "get_bounds", "(", ")", ")", "Min", "=", "self", ".", "acquisition", ".", "model", ".", "model", ".", "Y", ".", "min", "(", ")", "# --- GET the remaining elements", "while", "k", "<", "self", ".", "batch_size", ":", "self", ".", "acquisition", ".", "update_batches", "(", "X_batch", ",", "L", ",", "Min", ")", "new_sample", "=", "self", ".", "acquisition", ".", "optimize", "(", ")", "[", "0", "]", "X_batch", "=", "np", ".", "vstack", "(", "(", "X_batch", ",", "new_sample", ")", ")", "k", "+=", "1", "# --- Back to the non-penalized acquisition", "self", ".", "acquisition", ".", "update_batches", "(", "None", ",", "None", ",", "None", ")", "return", "X_batch" ]
Computes the elements of the batch sequentially by penalizing the acquisition.
[ "Computes", "the", "elements", "of", "the", "batch", "sequentially", "by", "penalizing", "the", "acquisition", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/evaluators/batch_local_penalization.py#L22-L49
228,238
SheffieldML/GPyOpt
manual/notebooks_check.py
check_notebooks_for_errors
def check_notebooks_for_errors(notebooks_directory): ''' Evaluates all notebooks in given directory and prints errors, if any ''' print("Checking notebooks in directory {} for errors".format(notebooks_directory)) failed_notebooks_count = 0 for file in os.listdir(notebooks_directory): if file.endswith(".ipynb"): print("Checking notebook " + file) full_file_path = os.path.join(notebooks_directory, file) output, errors = run_notebook(full_file_path) if errors is not None and len(errors) > 0: failed_notebooks_count += 1 print("Errors in notebook " + file) print(errors) if failed_notebooks_count == 0: print("No errors found in notebooks under " + notebooks_directory)
python
def check_notebooks_for_errors(notebooks_directory): ''' Evaluates all notebooks in given directory and prints errors, if any ''' print("Checking notebooks in directory {} for errors".format(notebooks_directory)) failed_notebooks_count = 0 for file in os.listdir(notebooks_directory): if file.endswith(".ipynb"): print("Checking notebook " + file) full_file_path = os.path.join(notebooks_directory, file) output, errors = run_notebook(full_file_path) if errors is not None and len(errors) > 0: failed_notebooks_count += 1 print("Errors in notebook " + file) print(errors) if failed_notebooks_count == 0: print("No errors found in notebooks under " + notebooks_directory)
[ "def", "check_notebooks_for_errors", "(", "notebooks_directory", ")", ":", "print", "(", "\"Checking notebooks in directory {} for errors\"", ".", "format", "(", "notebooks_directory", ")", ")", "failed_notebooks_count", "=", "0", "for", "file", "in", "os", ".", "listdir", "(", "notebooks_directory", ")", ":", "if", "file", ".", "endswith", "(", "\".ipynb\"", ")", ":", "print", "(", "\"Checking notebook \"", "+", "file", ")", "full_file_path", "=", "os", ".", "path", ".", "join", "(", "notebooks_directory", ",", "file", ")", "output", ",", "errors", "=", "run_notebook", "(", "full_file_path", ")", "if", "errors", "is", "not", "None", "and", "len", "(", "errors", ")", ">", "0", ":", "failed_notebooks_count", "+=", "1", "print", "(", "\"Errors in notebook \"", "+", "file", ")", "print", "(", "errors", ")", "if", "failed_notebooks_count", "==", "0", ":", "print", "(", "\"No errors found in notebooks under \"", "+", "notebooks_directory", ")" ]
Evaluates all notebooks in given directory and prints errors, if any
[ "Evaluates", "all", "notebooks", "in", "given", "directory", "and", "prints", "errors", "if", "any" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/manual/notebooks_check.py#L8-L24
228,239
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel.predict
def predict(self, X, with_noise=True): """ Predictions with the model. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True. """ m, v = self._predict(X, False, with_noise) # We can take the square root because v is just a diagonal matrix of variances return m, np.sqrt(v)
python
def predict(self, X, with_noise=True): """ Predictions with the model. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True. """ m, v = self._predict(X, False, with_noise) # We can take the square root because v is just a diagonal matrix of variances return m, np.sqrt(v)
[ "def", "predict", "(", "self", ",", "X", ",", "with_noise", "=", "True", ")", ":", "m", ",", "v", "=", "self", ".", "_predict", "(", "X", ",", "False", ",", "with_noise", ")", "# We can take the square root because v is just a diagonal matrix of variances", "return", "m", ",", "np", ".", "sqrt", "(", "v", ")" ]
Predictions with the model. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True.
[ "Predictions", "with", "the", "model", ".", "Returns", "posterior", "means", "and", "standard", "deviations", "at", "X", ".", "Note", "that", "this", "is", "different", "in", "GPy", "where", "the", "variances", "are", "given", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L100-L110
228,240
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel.predict_covariance
def predict_covariance(self, X, with_noise=True): """ Predicts the covariance matric for points in X. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True. """ _, v = self._predict(X, True, with_noise) return v
python
def predict_covariance(self, X, with_noise=True): """ Predicts the covariance matric for points in X. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True. """ _, v = self._predict(X, True, with_noise) return v
[ "def", "predict_covariance", "(", "self", ",", "X", ",", "with_noise", "=", "True", ")", ":", "_", ",", "v", "=", "self", ".", "_predict", "(", "X", ",", "True", ",", "with_noise", ")", "return", "v" ]
Predicts the covariance matric for points in X. Parameters: X (np.ndarray) - points to run the prediction for. with_noise (bool) - whether to add noise to the prediction. Default is True.
[ "Predicts", "the", "covariance", "matric", "for", "points", "in", "X", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L112-L121
228,241
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel.predict_withGradients
def predict_withGradients(self, X): """ Returns the mean, standard deviation, mean gradient and standard deviation gradient at X. """ if X.ndim==1: X = X[None,:] m, v = self.model.predict(X) v = np.clip(v, 1e-10, np.inf) dmdx, dvdx = self.model.predictive_gradients(X) dmdx = dmdx[:,:,0] dsdx = dvdx / (2*np.sqrt(v)) return m, np.sqrt(v), dmdx, dsdx
python
def predict_withGradients(self, X): """ Returns the mean, standard deviation, mean gradient and standard deviation gradient at X. """ if X.ndim==1: X = X[None,:] m, v = self.model.predict(X) v = np.clip(v, 1e-10, np.inf) dmdx, dvdx = self.model.predictive_gradients(X) dmdx = dmdx[:,:,0] dsdx = dvdx / (2*np.sqrt(v)) return m, np.sqrt(v), dmdx, dsdx
[ "def", "predict_withGradients", "(", "self", ",", "X", ")", ":", "if", "X", ".", "ndim", "==", "1", ":", "X", "=", "X", "[", "None", ",", ":", "]", "m", ",", "v", "=", "self", ".", "model", ".", "predict", "(", "X", ")", "v", "=", "np", ".", "clip", "(", "v", ",", "1e-10", ",", "np", ".", "inf", ")", "dmdx", ",", "dvdx", "=", "self", ".", "model", ".", "predictive_gradients", "(", "X", ")", "dmdx", "=", "dmdx", "[", ":", ",", ":", ",", "0", "]", "dsdx", "=", "dvdx", "/", "(", "2", "*", "np", ".", "sqrt", "(", "v", ")", ")", "return", "m", ",", "np", ".", "sqrt", "(", "v", ")", ",", "dmdx", ",", "dsdx" ]
Returns the mean, standard deviation, mean gradient and standard deviation gradient at X.
[ "Returns", "the", "mean", "standard", "deviation", "mean", "gradient", "and", "standard", "deviation", "gradient", "at", "X", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L129-L140
228,242
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel_MCMC.predict
def predict(self, X): """ Predictions with the model for all the MCMC samples. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given. """ if X.ndim==1: X = X[None,:] ps = self.model.param_array.copy() means = [] stds = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() m, v = self.model.predict(X) means.append(m) stds.append(np.sqrt(np.clip(v, 1e-10, np.inf))) self.model.param_array[:] = ps self.model._trigger_params_changed() return means, stds
python
def predict(self, X): """ Predictions with the model for all the MCMC samples. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given. """ if X.ndim==1: X = X[None,:] ps = self.model.param_array.copy() means = [] stds = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() m, v = self.model.predict(X) means.append(m) stds.append(np.sqrt(np.clip(v, 1e-10, np.inf))) self.model.param_array[:] = ps self.model._trigger_params_changed() return means, stds
[ "def", "predict", "(", "self", ",", "X", ")", ":", "if", "X", ".", "ndim", "==", "1", ":", "X", "=", "X", "[", "None", ",", ":", "]", "ps", "=", "self", ".", "model", ".", "param_array", ".", "copy", "(", ")", "means", "=", "[", "]", "stds", "=", "[", "]", "for", "s", "in", "self", ".", "hmc_samples", ":", "if", "self", ".", "model", ".", "_fixes_", "is", "None", ":", "self", ".", "model", "[", ":", "]", "=", "s", "else", ":", "self", ".", "model", "[", "self", ".", "model", ".", "_fixes_", "]", "=", "s", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "m", ",", "v", "=", "self", ".", "model", ".", "predict", "(", "X", ")", "means", ".", "append", "(", "m", ")", "stds", ".", "append", "(", "np", ".", "sqrt", "(", "np", ".", "clip", "(", "v", ",", "1e-10", ",", "np", ".", "inf", ")", ")", ")", "self", ".", "model", ".", "param_array", "[", ":", "]", "=", "ps", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "return", "means", ",", "stds" ]
Predictions with the model for all the MCMC samples. Returns posterior means and standard deviations at X. Note that this is different in GPy where the variances are given.
[ "Predictions", "with", "the", "model", "for", "all", "the", "MCMC", "samples", ".", "Returns", "posterior", "means", "and", "standard", "deviations", "at", "X", ".", "Note", "that", "this", "is", "different", "in", "GPy", "where", "the", "variances", "are", "given", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L255-L275
228,243
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel_MCMC.get_fmin
def get_fmin(self): """ Returns the location where the posterior mean is takes its minimal value. """ ps = self.model.param_array.copy() fmins = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() fmins.append(self.model.predict(self.model.X)[0].min()) self.model.param_array[:] = ps self.model._trigger_params_changed() return fmins
python
def get_fmin(self): """ Returns the location where the posterior mean is takes its minimal value. """ ps = self.model.param_array.copy() fmins = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() fmins.append(self.model.predict(self.model.X)[0].min()) self.model.param_array[:] = ps self.model._trigger_params_changed() return fmins
[ "def", "get_fmin", "(", "self", ")", ":", "ps", "=", "self", ".", "model", ".", "param_array", ".", "copy", "(", ")", "fmins", "=", "[", "]", "for", "s", "in", "self", ".", "hmc_samples", ":", "if", "self", ".", "model", ".", "_fixes_", "is", "None", ":", "self", ".", "model", "[", ":", "]", "=", "s", "else", ":", "self", ".", "model", "[", "self", ".", "model", ".", "_fixes_", "]", "=", "s", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "fmins", ".", "append", "(", "self", ".", "model", ".", "predict", "(", "self", ".", "model", ".", "X", ")", "[", "0", "]", ".", "min", "(", ")", ")", "self", ".", "model", ".", "param_array", "[", ":", "]", "=", "ps", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "return", "fmins" ]
Returns the location where the posterior mean is takes its minimal value.
[ "Returns", "the", "location", "where", "the", "posterior", "mean", "is", "takes", "its", "minimal", "value", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L277-L293
228,244
SheffieldML/GPyOpt
GPyOpt/models/gpmodel.py
GPModel_MCMC.predict_withGradients
def predict_withGradients(self, X): """ Returns the mean, standard deviation, mean gradient and standard deviation gradient at X for all the MCMC samples. """ if X.ndim==1: X = X[None,:] ps = self.model.param_array.copy() means = [] stds = [] dmdxs = [] dsdxs = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() m, v = self.model.predict(X) std = np.sqrt(np.clip(v, 1e-10, np.inf)) dmdx, dvdx = self.model.predictive_gradients(X) dmdx = dmdx[:,:,0] dsdx = dvdx / (2*std) means.append(m) stds.append(std) dmdxs.append(dmdx) dsdxs.append(dsdx) self.model.param_array[:] = ps self.model._trigger_params_changed() return means, stds, dmdxs, dsdxs
python
def predict_withGradients(self, X): """ Returns the mean, standard deviation, mean gradient and standard deviation gradient at X for all the MCMC samples. """ if X.ndim==1: X = X[None,:] ps = self.model.param_array.copy() means = [] stds = [] dmdxs = [] dsdxs = [] for s in self.hmc_samples: if self.model._fixes_ is None: self.model[:] = s else: self.model[self.model._fixes_] = s self.model._trigger_params_changed() m, v = self.model.predict(X) std = np.sqrt(np.clip(v, 1e-10, np.inf)) dmdx, dvdx = self.model.predictive_gradients(X) dmdx = dmdx[:,:,0] dsdx = dvdx / (2*std) means.append(m) stds.append(std) dmdxs.append(dmdx) dsdxs.append(dsdx) self.model.param_array[:] = ps self.model._trigger_params_changed() return means, stds, dmdxs, dsdxs
[ "def", "predict_withGradients", "(", "self", ",", "X", ")", ":", "if", "X", ".", "ndim", "==", "1", ":", "X", "=", "X", "[", "None", ",", ":", "]", "ps", "=", "self", ".", "model", ".", "param_array", ".", "copy", "(", ")", "means", "=", "[", "]", "stds", "=", "[", "]", "dmdxs", "=", "[", "]", "dsdxs", "=", "[", "]", "for", "s", "in", "self", ".", "hmc_samples", ":", "if", "self", ".", "model", ".", "_fixes_", "is", "None", ":", "self", ".", "model", "[", ":", "]", "=", "s", "else", ":", "self", ".", "model", "[", "self", ".", "model", ".", "_fixes_", "]", "=", "s", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "m", ",", "v", "=", "self", ".", "model", ".", "predict", "(", "X", ")", "std", "=", "np", ".", "sqrt", "(", "np", ".", "clip", "(", "v", ",", "1e-10", ",", "np", ".", "inf", ")", ")", "dmdx", ",", "dvdx", "=", "self", ".", "model", ".", "predictive_gradients", "(", "X", ")", "dmdx", "=", "dmdx", "[", ":", ",", ":", ",", "0", "]", "dsdx", "=", "dvdx", "/", "(", "2", "*", "std", ")", "means", ".", "append", "(", "m", ")", "stds", ".", "append", "(", "std", ")", "dmdxs", ".", "append", "(", "dmdx", ")", "dsdxs", ".", "append", "(", "dsdx", ")", "self", ".", "model", ".", "param_array", "[", ":", "]", "=", "ps", "self", ".", "model", ".", "_trigger_params_changed", "(", ")", "return", "means", ",", "stds", ",", "dmdxs", ",", "dsdxs" ]
Returns the mean, standard deviation, mean gradient and standard deviation gradient at X for all the MCMC samples.
[ "Returns", "the", "mean", "standard", "deviation", "mean", "gradient", "and", "standard", "deviation", "gradient", "at", "X", "for", "all", "the", "MCMC", "samples", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/models/gpmodel.py#L295-L322
228,245
SheffieldML/GPyOpt
GPyOpt/interface/func_loader.py
load_objective
def load_objective(config): """ Loads the objective function from a .json file. """ assert 'prjpath' in config assert 'main-file' in config, "The problem file ('main-file') is missing!" os.chdir(config['prjpath']) if config['language'].lower()=='python': assert config['main-file'].endswith('.py'), 'The python problem file has to end with .py!' import imp m = imp.load_source(config['main-file'][:-3], os.path.join(config['prjpath'],config['main-file'])) func = m.__dict__[config['main-file'][:-3]] return func
python
def load_objective(config): """ Loads the objective function from a .json file. """ assert 'prjpath' in config assert 'main-file' in config, "The problem file ('main-file') is missing!" os.chdir(config['prjpath']) if config['language'].lower()=='python': assert config['main-file'].endswith('.py'), 'The python problem file has to end with .py!' import imp m = imp.load_source(config['main-file'][:-3], os.path.join(config['prjpath'],config['main-file'])) func = m.__dict__[config['main-file'][:-3]] return func
[ "def", "load_objective", "(", "config", ")", ":", "assert", "'prjpath'", "in", "config", "assert", "'main-file'", "in", "config", ",", "\"The problem file ('main-file') is missing!\"", "os", ".", "chdir", "(", "config", "[", "'prjpath'", "]", ")", "if", "config", "[", "'language'", "]", ".", "lower", "(", ")", "==", "'python'", ":", "assert", "config", "[", "'main-file'", "]", ".", "endswith", "(", "'.py'", ")", ",", "'The python problem file has to end with .py!'", "import", "imp", "m", "=", "imp", ".", "load_source", "(", "config", "[", "'main-file'", "]", "[", ":", "-", "3", "]", ",", "os", ".", "path", ".", "join", "(", "config", "[", "'prjpath'", "]", ",", "config", "[", "'main-file'", "]", ")", ")", "func", "=", "m", ".", "__dict__", "[", "config", "[", "'main-file'", "]", "[", ":", "-", "3", "]", "]", "return", "func" ]
Loads the objective function from a .json file.
[ "Loads", "the", "objective", "function", "from", "a", ".", "json", "file", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/func_loader.py#L7-L21
228,246
SheffieldML/GPyOpt
GPyOpt/acquisitions/EI.py
AcquisitionEI._compute_acq
def _compute_acq(self, x): """ Computes the Expected Improvement per unit of cost """ m, s = self.model.predict(x) fmin = self.model.get_fmin() phi, Phi, u = get_quantiles(self.jitter, fmin, m, s) f_acqu = s * (u * Phi + phi) return f_acqu
python
def _compute_acq(self, x): """ Computes the Expected Improvement per unit of cost """ m, s = self.model.predict(x) fmin = self.model.get_fmin() phi, Phi, u = get_quantiles(self.jitter, fmin, m, s) f_acqu = s * (u * Phi + phi) return f_acqu
[ "def", "_compute_acq", "(", "self", ",", "x", ")", ":", "m", ",", "s", "=", "self", ".", "model", ".", "predict", "(", "x", ")", "fmin", "=", "self", ".", "model", ".", "get_fmin", "(", ")", "phi", ",", "Phi", ",", "u", "=", "get_quantiles", "(", "self", ".", "jitter", ",", "fmin", ",", "m", ",", "s", ")", "f_acqu", "=", "s", "*", "(", "u", "*", "Phi", "+", "phi", ")", "return", "f_acqu" ]
Computes the Expected Improvement per unit of cost
[ "Computes", "the", "Expected", "Improvement", "per", "unit", "of", "cost" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/acquisitions/EI.py#L32-L40
228,247
SheffieldML/GPyOpt
GPyOpt/interface/config_parser.py
update_config
def update_config(config_new, config_default): ''' Updates the loaded method configuration with default values. ''' if any([isinstance(v, dict) for v in list(config_new.values())]): for k,v in list(config_new.items()): if isinstance(v,dict) and k in config_default: update_config(config_new[k],config_default[k]) else: config_default[k] = v else: config_default.update(config_new) return config_default
python
def update_config(config_new, config_default): ''' Updates the loaded method configuration with default values. ''' if any([isinstance(v, dict) for v in list(config_new.values())]): for k,v in list(config_new.items()): if isinstance(v,dict) and k in config_default: update_config(config_new[k],config_default[k]) else: config_default[k] = v else: config_default.update(config_new) return config_default
[ "def", "update_config", "(", "config_new", ",", "config_default", ")", ":", "if", "any", "(", "[", "isinstance", "(", "v", ",", "dict", ")", "for", "v", "in", "list", "(", "config_new", ".", "values", "(", ")", ")", "]", ")", ":", "for", "k", ",", "v", "in", "list", "(", "config_new", ".", "items", "(", ")", ")", ":", "if", "isinstance", "(", "v", ",", "dict", ")", "and", "k", "in", "config_default", ":", "update_config", "(", "config_new", "[", "k", "]", ",", "config_default", "[", "k", "]", ")", "else", ":", "config_default", "[", "k", "]", "=", "v", "else", ":", "config_default", ".", "update", "(", "config_new", ")", "return", "config_default" ]
Updates the loaded method configuration with default values.
[ "Updates", "the", "loaded", "method", "configuration", "with", "default", "values", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/config_parser.py#L62-L75
228,248
SheffieldML/GPyOpt
GPyOpt/interface/config_parser.py
parser
def parser(input_file_path='config.json'): ''' Parser for the .json file containing the configuration of the method. ''' # --- Read .json file try: with open(input_file_path, 'r') as config_file: config_new = json.load(config_file) config_file.close() except: raise Exception('Config file "'+input_file_path+'" not loaded properly. Please check it an try again.') import copy options = update_config(config_new, copy.deepcopy(default_config)) return options
python
def parser(input_file_path='config.json'): ''' Parser for the .json file containing the configuration of the method. ''' # --- Read .json file try: with open(input_file_path, 'r') as config_file: config_new = json.load(config_file) config_file.close() except: raise Exception('Config file "'+input_file_path+'" not loaded properly. Please check it an try again.') import copy options = update_config(config_new, copy.deepcopy(default_config)) return options
[ "def", "parser", "(", "input_file_path", "=", "'config.json'", ")", ":", "# --- Read .json file", "try", ":", "with", "open", "(", "input_file_path", ",", "'r'", ")", "as", "config_file", ":", "config_new", "=", "json", ".", "load", "(", "config_file", ")", "config_file", ".", "close", "(", ")", "except", ":", "raise", "Exception", "(", "'Config file \"'", "+", "input_file_path", "+", "'\" not loaded properly. Please check it an try again.'", ")", "import", "copy", "options", "=", "update_config", "(", "config_new", ",", "copy", ".", "deepcopy", "(", "default_config", ")", ")", "return", "options" ]
Parser for the .json file containing the configuration of the method.
[ "Parser", "for", "the", ".", "json", "file", "containing", "the", "configuration", "of", "the", "method", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/interface/config_parser.py#L78-L94
228,249
SheffieldML/GPyOpt
GPyOpt/util/mcmc_sampler.py
AffineInvariantEnsembleSampler.get_samples
def get_samples(self, n_samples, log_p_function, burn_in_steps=50): """ Generates samples. Parameters: n_samples - number of samples to generate log_p_function - a function that returns log density for a specific sample burn_in_steps - number of burn-in steps for sampling Returns a tuple of two array: (samples, log_p_function values for samples) """ restarts = initial_design('random', self.space, n_samples) sampler = emcee.EnsembleSampler(n_samples, self.space.input_dim(), log_p_function) samples, samples_log, _ = sampler.run_mcmc(restarts, burn_in_steps) # make sure we have an array of shape (n samples, space input dim) if len(samples.shape) == 1: samples = samples.reshape(-1, 1) samples_log = samples_log.reshape(-1, 1) return samples, samples_log
python
def get_samples(self, n_samples, log_p_function, burn_in_steps=50): """ Generates samples. Parameters: n_samples - number of samples to generate log_p_function - a function that returns log density for a specific sample burn_in_steps - number of burn-in steps for sampling Returns a tuple of two array: (samples, log_p_function values for samples) """ restarts = initial_design('random', self.space, n_samples) sampler = emcee.EnsembleSampler(n_samples, self.space.input_dim(), log_p_function) samples, samples_log, _ = sampler.run_mcmc(restarts, burn_in_steps) # make sure we have an array of shape (n samples, space input dim) if len(samples.shape) == 1: samples = samples.reshape(-1, 1) samples_log = samples_log.reshape(-1, 1) return samples, samples_log
[ "def", "get_samples", "(", "self", ",", "n_samples", ",", "log_p_function", ",", "burn_in_steps", "=", "50", ")", ":", "restarts", "=", "initial_design", "(", "'random'", ",", "self", ".", "space", ",", "n_samples", ")", "sampler", "=", "emcee", ".", "EnsembleSampler", "(", "n_samples", ",", "self", ".", "space", ".", "input_dim", "(", ")", ",", "log_p_function", ")", "samples", ",", "samples_log", ",", "_", "=", "sampler", ".", "run_mcmc", "(", "restarts", ",", "burn_in_steps", ")", "# make sure we have an array of shape (n samples, space input dim)", "if", "len", "(", "samples", ".", "shape", ")", "==", "1", ":", "samples", "=", "samples", ".", "reshape", "(", "-", "1", ",", "1", ")", "samples_log", "=", "samples_log", ".", "reshape", "(", "-", "1", ",", "1", ")", "return", "samples", ",", "samples_log" ]
Generates samples. Parameters: n_samples - number of samples to generate log_p_function - a function that returns log density for a specific sample burn_in_steps - number of burn-in steps for sampling Returns a tuple of two array: (samples, log_p_function values for samples)
[ "Generates", "samples", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/util/mcmc_sampler.py#L39-L59
228,250
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO.suggest_next_locations
def suggest_next_locations(self, context = None, pending_X = None, ignored_X = None): """ Run a single optimization step and return the next locations to evaluate the objective. Number of suggested locations equals to batch_size. :param context: fixes specified variables to a particular context (values) for the optimization run (default, None). :param pending_X: matrix of input configurations that are in a pending state (i.e., do not have an evaluation yet) (default, None). :param ignored_X: matrix of input configurations that the user black-lists, i.e., those configurations will not be suggested again (default, None). """ self.model_parameters_iterations = None self.num_acquisitions = 0 self.context = context self._update_model(self.normalization_type) suggested_locations = self._compute_next_evaluations(pending_zipped_X = pending_X, ignored_zipped_X = ignored_X) return suggested_locations
python
def suggest_next_locations(self, context = None, pending_X = None, ignored_X = None): """ Run a single optimization step and return the next locations to evaluate the objective. Number of suggested locations equals to batch_size. :param context: fixes specified variables to a particular context (values) for the optimization run (default, None). :param pending_X: matrix of input configurations that are in a pending state (i.e., do not have an evaluation yet) (default, None). :param ignored_X: matrix of input configurations that the user black-lists, i.e., those configurations will not be suggested again (default, None). """ self.model_parameters_iterations = None self.num_acquisitions = 0 self.context = context self._update_model(self.normalization_type) suggested_locations = self._compute_next_evaluations(pending_zipped_X = pending_X, ignored_zipped_X = ignored_X) return suggested_locations
[ "def", "suggest_next_locations", "(", "self", ",", "context", "=", "None", ",", "pending_X", "=", "None", ",", "ignored_X", "=", "None", ")", ":", "self", ".", "model_parameters_iterations", "=", "None", "self", ".", "num_acquisitions", "=", "0", "self", ".", "context", "=", "context", "self", ".", "_update_model", "(", "self", ".", "normalization_type", ")", "suggested_locations", "=", "self", ".", "_compute_next_evaluations", "(", "pending_zipped_X", "=", "pending_X", ",", "ignored_zipped_X", "=", "ignored_X", ")", "return", "suggested_locations" ]
Run a single optimization step and return the next locations to evaluate the objective. Number of suggested locations equals to batch_size. :param context: fixes specified variables to a particular context (values) for the optimization run (default, None). :param pending_X: matrix of input configurations that are in a pending state (i.e., do not have an evaluation yet) (default, None). :param ignored_X: matrix of input configurations that the user black-lists, i.e., those configurations will not be suggested again (default, None).
[ "Run", "a", "single", "optimization", "step", "and", "return", "the", "next", "locations", "to", "evaluate", "the", "objective", ".", "Number", "of", "suggested", "locations", "equals", "to", "batch_size", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L55-L71
228,251
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO._print_convergence
def _print_convergence(self): """ Prints the reason why the optimization stopped. """ if self.verbosity: if (self.num_acquisitions == self.max_iter) and (not self.initial_iter): print(' ** Maximum number of iterations reached **') return 1 elif (self._distance_last_evaluations() < self.eps) and (not self.initial_iter): print(' ** Two equal location selected **') return 1 elif (self.max_time < self.cum_time) and not (self.initial_iter): print(' ** Evaluation time reached **') return 0 if self.initial_iter: print('** GPyOpt Bayesian Optimization class initialized successfully **') self.initial_iter = False
python
def _print_convergence(self): """ Prints the reason why the optimization stopped. """ if self.verbosity: if (self.num_acquisitions == self.max_iter) and (not self.initial_iter): print(' ** Maximum number of iterations reached **') return 1 elif (self._distance_last_evaluations() < self.eps) and (not self.initial_iter): print(' ** Two equal location selected **') return 1 elif (self.max_time < self.cum_time) and not (self.initial_iter): print(' ** Evaluation time reached **') return 0 if self.initial_iter: print('** GPyOpt Bayesian Optimization class initialized successfully **') self.initial_iter = False
[ "def", "_print_convergence", "(", "self", ")", ":", "if", "self", ".", "verbosity", ":", "if", "(", "self", ".", "num_acquisitions", "==", "self", ".", "max_iter", ")", "and", "(", "not", "self", ".", "initial_iter", ")", ":", "print", "(", "' ** Maximum number of iterations reached **'", ")", "return", "1", "elif", "(", "self", ".", "_distance_last_evaluations", "(", ")", "<", "self", ".", "eps", ")", "and", "(", "not", "self", ".", "initial_iter", ")", ":", "print", "(", "' ** Two equal location selected **'", ")", "return", "1", "elif", "(", "self", ".", "max_time", "<", "self", ".", "cum_time", ")", "and", "not", "(", "self", ".", "initial_iter", ")", ":", "print", "(", "' ** Evaluation time reached **'", ")", "return", "0", "if", "self", ".", "initial_iter", ":", "print", "(", "'** GPyOpt Bayesian Optimization class initialized successfully **'", ")", "self", ".", "initial_iter", "=", "False" ]
Prints the reason why the optimization stopped.
[ "Prints", "the", "reason", "why", "the", "optimization", "stopped", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L172-L190
228,252
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO.evaluate_objective
def evaluate_objective(self): """ Evaluates the objective """ self.Y_new, cost_new = self.objective.evaluate(self.suggested_sample) self.cost.update_cost_model(self.suggested_sample, cost_new) self.Y = np.vstack((self.Y,self.Y_new))
python
def evaluate_objective(self): """ Evaluates the objective """ self.Y_new, cost_new = self.objective.evaluate(self.suggested_sample) self.cost.update_cost_model(self.suggested_sample, cost_new) self.Y = np.vstack((self.Y,self.Y_new))
[ "def", "evaluate_objective", "(", "self", ")", ":", "self", ".", "Y_new", ",", "cost_new", "=", "self", ".", "objective", ".", "evaluate", "(", "self", ".", "suggested_sample", ")", "self", ".", "cost", ".", "update_cost_model", "(", "self", ".", "suggested_sample", ",", "cost_new", ")", "self", ".", "Y", "=", "np", ".", "vstack", "(", "(", "self", ".", "Y", ",", "self", ".", "Y_new", ")", ")" ]
Evaluates the objective
[ "Evaluates", "the", "objective" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L193-L199
228,253
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO._compute_results
def _compute_results(self): """ Computes the optimum and its value. """ self.Y_best = best_value(self.Y) self.x_opt = self.X[np.argmin(self.Y),:] self.fx_opt = np.min(self.Y)
python
def _compute_results(self): """ Computes the optimum and its value. """ self.Y_best = best_value(self.Y) self.x_opt = self.X[np.argmin(self.Y),:] self.fx_opt = np.min(self.Y)
[ "def", "_compute_results", "(", "self", ")", ":", "self", ".", "Y_best", "=", "best_value", "(", "self", ".", "Y", ")", "self", ".", "x_opt", "=", "self", ".", "X", "[", "np", ".", "argmin", "(", "self", ".", "Y", ")", ",", ":", "]", "self", ".", "fx_opt", "=", "np", ".", "min", "(", "self", ".", "Y", ")" ]
Computes the optimum and its value.
[ "Computes", "the", "optimum", "and", "its", "value", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L201-L207
228,254
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO._distance_last_evaluations
def _distance_last_evaluations(self): """ Computes the distance between the last two evaluations. """ if self.X.shape[0] < 2: # less than 2 evaluations return np.inf return np.sqrt(np.sum((self.X[-1, :] - self.X[-2, :]) ** 2))
python
def _distance_last_evaluations(self): """ Computes the distance between the last two evaluations. """ if self.X.shape[0] < 2: # less than 2 evaluations return np.inf return np.sqrt(np.sum((self.X[-1, :] - self.X[-2, :]) ** 2))
[ "def", "_distance_last_evaluations", "(", "self", ")", ":", "if", "self", ".", "X", ".", "shape", "[", "0", "]", "<", "2", ":", "# less than 2 evaluations", "return", "np", ".", "inf", "return", "np", ".", "sqrt", "(", "np", ".", "sum", "(", "(", "self", ".", "X", "[", "-", "1", ",", ":", "]", "-", "self", ".", "X", "[", "-", "2", ",", ":", "]", ")", "**", "2", ")", ")" ]
Computes the distance between the last two evaluations.
[ "Computes", "the", "distance", "between", "the", "last", "two", "evaluations", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L209-L216
228,255
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO.save_evaluations
def save_evaluations(self, evaluations_file = None): """ Saves evaluations at each iteration of the optimization :param evaluations_file: name of the file in which the results are saved. """ iterations = np.array(range(1, self.Y.shape[0] + 1))[:, None] results = np.hstack((iterations, self.Y, self.X)) header = ['Iteration', 'Y'] + ['var_' + str(k) for k in range(1, self.X.shape[1] + 1)] data = [header] + results.tolist() self._write_csv(evaluations_file, data)
python
def save_evaluations(self, evaluations_file = None): """ Saves evaluations at each iteration of the optimization :param evaluations_file: name of the file in which the results are saved. """ iterations = np.array(range(1, self.Y.shape[0] + 1))[:, None] results = np.hstack((iterations, self.Y, self.X)) header = ['Iteration', 'Y'] + ['var_' + str(k) for k in range(1, self.X.shape[1] + 1)] data = [header] + results.tolist() self._write_csv(evaluations_file, data)
[ "def", "save_evaluations", "(", "self", ",", "evaluations_file", "=", "None", ")", ":", "iterations", "=", "np", ".", "array", "(", "range", "(", "1", ",", "self", ".", "Y", ".", "shape", "[", "0", "]", "+", "1", ")", ")", "[", ":", ",", "None", "]", "results", "=", "np", ".", "hstack", "(", "(", "iterations", ",", "self", ".", "Y", ",", "self", ".", "X", ")", ")", "header", "=", "[", "'Iteration'", ",", "'Y'", "]", "+", "[", "'var_'", "+", "str", "(", "k", ")", "for", "k", "in", "range", "(", "1", ",", "self", ".", "X", ".", "shape", "[", "1", "]", "+", "1", ")", "]", "data", "=", "[", "header", "]", "+", "results", ".", "tolist", "(", ")", "self", ".", "_write_csv", "(", "evaluations_file", ",", "data", ")" ]
Saves evaluations at each iteration of the optimization :param evaluations_file: name of the file in which the results are saved.
[ "Saves", "evaluations", "at", "each", "iteration", "of", "the", "optimization" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L367-L378
228,256
SheffieldML/GPyOpt
GPyOpt/core/bo.py
BO.save_models
def save_models(self, models_file): """ Saves model parameters at each iteration of the optimization :param models_file: name of the file or a file buffer, in which the results are saved. """ if self.model_parameters_iterations is None: raise ValueError("No iterations have been carried out yet and hence no iterations of the BO can be saved") iterations = np.array(range(1,self.model_parameters_iterations.shape[0]+1))[:,None] results = np.hstack((iterations,self.model_parameters_iterations)) header = ['Iteration'] + self.model.get_model_parameters_names() data = [header] + results.tolist() self._write_csv(models_file, data)
python
def save_models(self, models_file): """ Saves model parameters at each iteration of the optimization :param models_file: name of the file or a file buffer, in which the results are saved. """ if self.model_parameters_iterations is None: raise ValueError("No iterations have been carried out yet and hence no iterations of the BO can be saved") iterations = np.array(range(1,self.model_parameters_iterations.shape[0]+1))[:,None] results = np.hstack((iterations,self.model_parameters_iterations)) header = ['Iteration'] + self.model.get_model_parameters_names() data = [header] + results.tolist() self._write_csv(models_file, data)
[ "def", "save_models", "(", "self", ",", "models_file", ")", ":", "if", "self", ".", "model_parameters_iterations", "is", "None", ":", "raise", "ValueError", "(", "\"No iterations have been carried out yet and hence no iterations of the BO can be saved\"", ")", "iterations", "=", "np", ".", "array", "(", "range", "(", "1", ",", "self", ".", "model_parameters_iterations", ".", "shape", "[", "0", "]", "+", "1", ")", ")", "[", ":", ",", "None", "]", "results", "=", "np", ".", "hstack", "(", "(", "iterations", ",", "self", ".", "model_parameters_iterations", ")", ")", "header", "=", "[", "'Iteration'", "]", "+", "self", ".", "model", ".", "get_model_parameters_names", "(", ")", "data", "=", "[", "header", "]", "+", "results", ".", "tolist", "(", ")", "self", ".", "_write_csv", "(", "models_file", ",", "data", ")" ]
Saves model parameters at each iteration of the optimization :param models_file: name of the file or a file buffer, in which the results are saved.
[ "Saves", "model", "parameters", "at", "each", "iteration", "of", "the", "optimization" ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/core/bo.py#L380-L394
228,257
SheffieldML/GPyOpt
GPyOpt/methods/bayesian_optimization.py
BayesianOptimization._init_design_chooser
def _init_design_chooser(self): """ Initializes the choice of X and Y based on the selected initial design and number of points selected. """ # If objective function was not provided, we require some initial sample data if self.f is None and (self.X is None or self.Y is None): raise InvalidConfigError("Initial data for both X and Y is required when objective function is not provided") # Case 1: if self.X is None: self.X = initial_design(self.initial_design_type, self.space, self.initial_design_numdata) self.Y, _ = self.objective.evaluate(self.X) # Case 2 elif self.X is not None and self.Y is None: self.Y, _ = self.objective.evaluate(self.X)
python
def _init_design_chooser(self): """ Initializes the choice of X and Y based on the selected initial design and number of points selected. """ # If objective function was not provided, we require some initial sample data if self.f is None and (self.X is None or self.Y is None): raise InvalidConfigError("Initial data for both X and Y is required when objective function is not provided") # Case 1: if self.X is None: self.X = initial_design(self.initial_design_type, self.space, self.initial_design_numdata) self.Y, _ = self.objective.evaluate(self.X) # Case 2 elif self.X is not None and self.Y is None: self.Y, _ = self.objective.evaluate(self.X)
[ "def", "_init_design_chooser", "(", "self", ")", ":", "# If objective function was not provided, we require some initial sample data", "if", "self", ".", "f", "is", "None", "and", "(", "self", ".", "X", "is", "None", "or", "self", ".", "Y", "is", "None", ")", ":", "raise", "InvalidConfigError", "(", "\"Initial data for both X and Y is required when objective function is not provided\"", ")", "# Case 1:", "if", "self", ".", "X", "is", "None", ":", "self", ".", "X", "=", "initial_design", "(", "self", ".", "initial_design_type", ",", "self", ".", "space", ",", "self", ".", "initial_design_numdata", ")", "self", ".", "Y", ",", "_", "=", "self", ".", "objective", ".", "evaluate", "(", "self", ".", "X", ")", "# Case 2", "elif", "self", ".", "X", "is", "not", "None", "and", "self", ".", "Y", "is", "None", ":", "self", ".", "Y", ",", "_", "=", "self", ".", "objective", ".", "evaluate", "(", "self", ".", "X", ")" ]
Initializes the choice of X and Y based on the selected initial design and number of points selected.
[ "Initializes", "the", "choice", "of", "X", "and", "Y", "based", "on", "the", "selected", "initial", "design", "and", "number", "of", "points", "selected", "." ]
255539dc5927819ca701e44fe3d76cd4864222fa
https://github.com/SheffieldML/GPyOpt/blob/255539dc5927819ca701e44fe3d76cd4864222fa/GPyOpt/methods/bayesian_optimization.py#L183-L198
228,258
coleifer/huey
huey/api.py
crontab
def crontab(minute='*', hour='*', day='*', month='*', day_of_week='*'): """ Convert a "crontab"-style set of parameters into a test function that will return True when the given datetime matches the parameters set forth in the crontab. For day-of-week, 0=Sunday and 6=Saturday. Acceptable inputs: * = every distinct value */n = run every "n" times, i.e. hours='*/4' == 0, 4, 8, 12, 16, 20 m-n = run every time m..n m,n = run on m and n """ validation = ( ('m', month, range(1, 13)), ('d', day, range(1, 32)), ('w', day_of_week, range(8)), # 0-6, but also 7 for Sunday. ('H', hour, range(24)), ('M', minute, range(60)) ) cron_settings = [] for (date_str, value, acceptable) in validation: settings = set([]) if isinstance(value, int): value = str(value) for piece in value.split(','): if piece == '*': settings.update(acceptable) continue if piece.isdigit(): piece = int(piece) if piece not in acceptable: raise ValueError('%d is not a valid input' % piece) elif date_str == 'w': piece %= 7 settings.add(piece) else: dash_match = dash_re.match(piece) if dash_match: lhs, rhs = map(int, dash_match.groups()) if lhs not in acceptable or rhs not in acceptable: raise ValueError('%s is not a valid input' % piece) elif date_str == 'w': lhs %= 7 rhs %= 7 settings.update(range(lhs, rhs + 1)) continue # Handle stuff like */3, */6. every_match = every_re.match(piece) if every_match: if date_str == 'w': raise ValueError('Cannot perform this kind of matching' ' on day-of-week.') interval = int(every_match.groups()[0]) settings.update(acceptable[::interval]) cron_settings.append(sorted(list(settings))) def validate_date(timestamp): _, m, d, H, M, _, w, _, _ = timestamp.timetuple() # fix the weekday to be sunday=0 w = (w + 1) % 7 for (date_piece, selection) in zip((m, d, w, H, M), cron_settings): if date_piece not in selection: return False return True return validate_date
python
def crontab(minute='*', hour='*', day='*', month='*', day_of_week='*'): """ Convert a "crontab"-style set of parameters into a test function that will return True when the given datetime matches the parameters set forth in the crontab. For day-of-week, 0=Sunday and 6=Saturday. Acceptable inputs: * = every distinct value */n = run every "n" times, i.e. hours='*/4' == 0, 4, 8, 12, 16, 20 m-n = run every time m..n m,n = run on m and n """ validation = ( ('m', month, range(1, 13)), ('d', day, range(1, 32)), ('w', day_of_week, range(8)), # 0-6, but also 7 for Sunday. ('H', hour, range(24)), ('M', minute, range(60)) ) cron_settings = [] for (date_str, value, acceptable) in validation: settings = set([]) if isinstance(value, int): value = str(value) for piece in value.split(','): if piece == '*': settings.update(acceptable) continue if piece.isdigit(): piece = int(piece) if piece not in acceptable: raise ValueError('%d is not a valid input' % piece) elif date_str == 'w': piece %= 7 settings.add(piece) else: dash_match = dash_re.match(piece) if dash_match: lhs, rhs = map(int, dash_match.groups()) if lhs not in acceptable or rhs not in acceptable: raise ValueError('%s is not a valid input' % piece) elif date_str == 'w': lhs %= 7 rhs %= 7 settings.update(range(lhs, rhs + 1)) continue # Handle stuff like */3, */6. every_match = every_re.match(piece) if every_match: if date_str == 'w': raise ValueError('Cannot perform this kind of matching' ' on day-of-week.') interval = int(every_match.groups()[0]) settings.update(acceptable[::interval]) cron_settings.append(sorted(list(settings))) def validate_date(timestamp): _, m, d, H, M, _, w, _, _ = timestamp.timetuple() # fix the weekday to be sunday=0 w = (w + 1) % 7 for (date_piece, selection) in zip((m, d, w, H, M), cron_settings): if date_piece not in selection: return False return True return validate_date
[ "def", "crontab", "(", "minute", "=", "'*'", ",", "hour", "=", "'*'", ",", "day", "=", "'*'", ",", "month", "=", "'*'", ",", "day_of_week", "=", "'*'", ")", ":", "validation", "=", "(", "(", "'m'", ",", "month", ",", "range", "(", "1", ",", "13", ")", ")", ",", "(", "'d'", ",", "day", ",", "range", "(", "1", ",", "32", ")", ")", ",", "(", "'w'", ",", "day_of_week", ",", "range", "(", "8", ")", ")", ",", "# 0-6, but also 7 for Sunday.", "(", "'H'", ",", "hour", ",", "range", "(", "24", ")", ")", ",", "(", "'M'", ",", "minute", ",", "range", "(", "60", ")", ")", ")", "cron_settings", "=", "[", "]", "for", "(", "date_str", ",", "value", ",", "acceptable", ")", "in", "validation", ":", "settings", "=", "set", "(", "[", "]", ")", "if", "isinstance", "(", "value", ",", "int", ")", ":", "value", "=", "str", "(", "value", ")", "for", "piece", "in", "value", ".", "split", "(", "','", ")", ":", "if", "piece", "==", "'*'", ":", "settings", ".", "update", "(", "acceptable", ")", "continue", "if", "piece", ".", "isdigit", "(", ")", ":", "piece", "=", "int", "(", "piece", ")", "if", "piece", "not", "in", "acceptable", ":", "raise", "ValueError", "(", "'%d is not a valid input'", "%", "piece", ")", "elif", "date_str", "==", "'w'", ":", "piece", "%=", "7", "settings", ".", "add", "(", "piece", ")", "else", ":", "dash_match", "=", "dash_re", ".", "match", "(", "piece", ")", "if", "dash_match", ":", "lhs", ",", "rhs", "=", "map", "(", "int", ",", "dash_match", ".", "groups", "(", ")", ")", "if", "lhs", "not", "in", "acceptable", "or", "rhs", "not", "in", "acceptable", ":", "raise", "ValueError", "(", "'%s is not a valid input'", "%", "piece", ")", "elif", "date_str", "==", "'w'", ":", "lhs", "%=", "7", "rhs", "%=", "7", "settings", ".", "update", "(", "range", "(", "lhs", ",", "rhs", "+", "1", ")", ")", "continue", "# Handle stuff like */3, */6.", "every_match", "=", "every_re", ".", "match", "(", "piece", ")", "if", "every_match", ":", "if", "date_str", "==", "'w'", ":", "raise", "ValueError", "(", "'Cannot perform this kind of matching'", "' on day-of-week.'", ")", "interval", "=", "int", "(", "every_match", ".", "groups", "(", ")", "[", "0", "]", ")", "settings", ".", "update", "(", "acceptable", "[", ":", ":", "interval", "]", ")", "cron_settings", ".", "append", "(", "sorted", "(", "list", "(", "settings", ")", ")", ")", "def", "validate_date", "(", "timestamp", ")", ":", "_", ",", "m", ",", "d", ",", "H", ",", "M", ",", "_", ",", "w", ",", "_", ",", "_", "=", "timestamp", ".", "timetuple", "(", ")", "# fix the weekday to be sunday=0", "w", "=", "(", "w", "+", "1", ")", "%", "7", "for", "(", "date_piece", ",", "selection", ")", "in", "zip", "(", "(", "m", ",", "d", ",", "w", ",", "H", ",", "M", ")", ",", "cron_settings", ")", ":", "if", "date_piece", "not", "in", "selection", ":", "return", "False", "return", "True", "return", "validate_date" ]
Convert a "crontab"-style set of parameters into a test function that will return True when the given datetime matches the parameters set forth in the crontab. For day-of-week, 0=Sunday and 6=Saturday. Acceptable inputs: * = every distinct value */n = run every "n" times, i.e. hours='*/4' == 0, 4, 8, 12, 16, 20 m-n = run every time m..n m,n = run on m and n
[ "Convert", "a", "crontab", "-", "style", "set", "of", "parameters", "into", "a", "test", "function", "that", "will", "return", "True", "when", "the", "given", "datetime", "matches", "the", "parameters", "set", "forth", "in", "the", "crontab", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/api.py#L913-L990
228,259
coleifer/huey
huey/storage.py
BaseStorage.put_if_empty
def put_if_empty(self, key, value): """ Atomically write data only if the key is not already set. :param bytes key: Key to check/set. :param bytes value: Arbitrary data. :return: Boolean whether key/value was set. """ if self.has_data_for_key(key): return False self.put_data(key, value) return True
python
def put_if_empty(self, key, value): """ Atomically write data only if the key is not already set. :param bytes key: Key to check/set. :param bytes value: Arbitrary data. :return: Boolean whether key/value was set. """ if self.has_data_for_key(key): return False self.put_data(key, value) return True
[ "def", "put_if_empty", "(", "self", ",", "key", ",", "value", ")", ":", "if", "self", ".", "has_data_for_key", "(", "key", ")", ":", "return", "False", "self", ".", "put_data", "(", "key", ",", "value", ")", "return", "True" ]
Atomically write data only if the key is not already set. :param bytes key: Key to check/set. :param bytes value: Arbitrary data. :return: Boolean whether key/value was set.
[ "Atomically", "write", "data", "only", "if", "the", "key", "is", "not", "already", "set", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/storage.py#L190-L201
228,260
coleifer/huey
huey/utils.py
make_naive
def make_naive(dt): """ Makes an aware datetime.datetime naive in local time zone. """ tt = dt.utctimetuple() ts = calendar.timegm(tt) local_tt = time.localtime(ts) return datetime.datetime(*local_tt[:6])
python
def make_naive(dt): """ Makes an aware datetime.datetime naive in local time zone. """ tt = dt.utctimetuple() ts = calendar.timegm(tt) local_tt = time.localtime(ts) return datetime.datetime(*local_tt[:6])
[ "def", "make_naive", "(", "dt", ")", ":", "tt", "=", "dt", ".", "utctimetuple", "(", ")", "ts", "=", "calendar", ".", "timegm", "(", "tt", ")", "local_tt", "=", "time", ".", "localtime", "(", "ts", ")", "return", "datetime", ".", "datetime", "(", "*", "local_tt", "[", ":", "6", "]", ")" ]
Makes an aware datetime.datetime naive in local time zone.
[ "Makes", "an", "aware", "datetime", ".", "datetime", "naive", "in", "local", "time", "zone", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/utils.py#L48-L55
228,261
coleifer/huey
huey/consumer.py
BaseProcess.sleep_for_interval
def sleep_for_interval(self, start_ts, nseconds): """ Sleep for a given interval with respect to the start timestamp. So, if the start timestamp is 1337 and nseconds is 10, the method will actually sleep for nseconds - (current_timestamp - start_timestamp). So if the current timestamp is 1340, we'll only sleep for 7 seconds (the goal being to sleep until 1347, or 1337 + 10). """ sleep_time = nseconds - (time.time() - start_ts) if sleep_time <= 0: return self._logger.debug('Sleeping for %s', sleep_time) # Recompute time to sleep to improve accuracy in case the process was # pre-empted by the kernel while logging. sleep_time = nseconds - (time.time() - start_ts) if sleep_time > 0: time.sleep(sleep_time)
python
def sleep_for_interval(self, start_ts, nseconds): """ Sleep for a given interval with respect to the start timestamp. So, if the start timestamp is 1337 and nseconds is 10, the method will actually sleep for nseconds - (current_timestamp - start_timestamp). So if the current timestamp is 1340, we'll only sleep for 7 seconds (the goal being to sleep until 1347, or 1337 + 10). """ sleep_time = nseconds - (time.time() - start_ts) if sleep_time <= 0: return self._logger.debug('Sleeping for %s', sleep_time) # Recompute time to sleep to improve accuracy in case the process was # pre-empted by the kernel while logging. sleep_time = nseconds - (time.time() - start_ts) if sleep_time > 0: time.sleep(sleep_time)
[ "def", "sleep_for_interval", "(", "self", ",", "start_ts", ",", "nseconds", ")", ":", "sleep_time", "=", "nseconds", "-", "(", "time", ".", "time", "(", ")", "-", "start_ts", ")", "if", "sleep_time", "<=", "0", ":", "return", "self", ".", "_logger", ".", "debug", "(", "'Sleeping for %s'", ",", "sleep_time", ")", "# Recompute time to sleep to improve accuracy in case the process was", "# pre-empted by the kernel while logging.", "sleep_time", "=", "nseconds", "-", "(", "time", ".", "time", "(", ")", "-", "start_ts", ")", "if", "sleep_time", ">", "0", ":", "time", ".", "sleep", "(", "sleep_time", ")" ]
Sleep for a given interval with respect to the start timestamp. So, if the start timestamp is 1337 and nseconds is 10, the method will actually sleep for nseconds - (current_timestamp - start_timestamp). So if the current timestamp is 1340, we'll only sleep for 7 seconds (the goal being to sleep until 1347, or 1337 + 10).
[ "Sleep", "for", "a", "given", "interval", "with", "respect", "to", "the", "start", "timestamp", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/consumer.py#L39-L56
228,262
coleifer/huey
huey/consumer.py
Consumer.start
def start(self): """ Start all consumer processes and register signal handlers. """ if self.huey.immediate: raise ConfigurationError( 'Consumer cannot be run with Huey instances where immediate ' 'is enabled. Please check your configuration and ensure that ' '"huey.immediate = False".') # Log startup message. self._logger.info('Huey consumer started with %s %s, PID %s at %s', self.workers, self.worker_type, os.getpid(), self.huey._get_timestamp()) self._logger.info('Scheduler runs every %s second(s).', self.scheduler_interval) self._logger.info('Periodic tasks are %s.', 'enabled' if self.periodic else 'disabled') self._set_signal_handlers() msg = ['The following commands are available:'] for command in self.huey._registry._registry: msg.append('+ %s' % command) self._logger.info('\n'.join(msg)) # We'll temporarily ignore SIGINT and SIGHUP (so that it is inherited # by the child-processes). Once the child processes are created, we # restore the handler. original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(signal, 'SIGHUP'): original_sighup_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) self.scheduler.start() for _, worker_process in self.worker_threads: worker_process.start() signal.signal(signal.SIGINT, original_sigint_handler) if hasattr(signal, 'SIGHUP'): signal.signal(signal.SIGHUP, original_sighup_handler)
python
def start(self): """ Start all consumer processes and register signal handlers. """ if self.huey.immediate: raise ConfigurationError( 'Consumer cannot be run with Huey instances where immediate ' 'is enabled. Please check your configuration and ensure that ' '"huey.immediate = False".') # Log startup message. self._logger.info('Huey consumer started with %s %s, PID %s at %s', self.workers, self.worker_type, os.getpid(), self.huey._get_timestamp()) self._logger.info('Scheduler runs every %s second(s).', self.scheduler_interval) self._logger.info('Periodic tasks are %s.', 'enabled' if self.periodic else 'disabled') self._set_signal_handlers() msg = ['The following commands are available:'] for command in self.huey._registry._registry: msg.append('+ %s' % command) self._logger.info('\n'.join(msg)) # We'll temporarily ignore SIGINT and SIGHUP (so that it is inherited # by the child-processes). Once the child processes are created, we # restore the handler. original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(signal, 'SIGHUP'): original_sighup_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) self.scheduler.start() for _, worker_process in self.worker_threads: worker_process.start() signal.signal(signal.SIGINT, original_sigint_handler) if hasattr(signal, 'SIGHUP'): signal.signal(signal.SIGHUP, original_sighup_handler)
[ "def", "start", "(", "self", ")", ":", "if", "self", ".", "huey", ".", "immediate", ":", "raise", "ConfigurationError", "(", "'Consumer cannot be run with Huey instances where immediate '", "'is enabled. Please check your configuration and ensure that '", "'\"huey.immediate = False\".'", ")", "# Log startup message.", "self", ".", "_logger", ".", "info", "(", "'Huey consumer started with %s %s, PID %s at %s'", ",", "self", ".", "workers", ",", "self", ".", "worker_type", ",", "os", ".", "getpid", "(", ")", ",", "self", ".", "huey", ".", "_get_timestamp", "(", ")", ")", "self", ".", "_logger", ".", "info", "(", "'Scheduler runs every %s second(s).'", ",", "self", ".", "scheduler_interval", ")", "self", ".", "_logger", ".", "info", "(", "'Periodic tasks are %s.'", ",", "'enabled'", "if", "self", ".", "periodic", "else", "'disabled'", ")", "self", ".", "_set_signal_handlers", "(", ")", "msg", "=", "[", "'The following commands are available:'", "]", "for", "command", "in", "self", ".", "huey", ".", "_registry", ".", "_registry", ":", "msg", ".", "append", "(", "'+ %s'", "%", "command", ")", "self", ".", "_logger", ".", "info", "(", "'\\n'", ".", "join", "(", "msg", ")", ")", "# We'll temporarily ignore SIGINT and SIGHUP (so that it is inherited", "# by the child-processes). Once the child processes are created, we", "# restore the handler.", "original_sigint_handler", "=", "signal", ".", "signal", "(", "signal", ".", "SIGINT", ",", "signal", ".", "SIG_IGN", ")", "if", "hasattr", "(", "signal", ",", "'SIGHUP'", ")", ":", "original_sighup_handler", "=", "signal", ".", "signal", "(", "signal", ".", "SIGHUP", ",", "signal", ".", "SIG_IGN", ")", "self", ".", "scheduler", ".", "start", "(", ")", "for", "_", ",", "worker_process", "in", "self", ".", "worker_threads", ":", "worker_process", ".", "start", "(", ")", "signal", ".", "signal", "(", "signal", ".", "SIGINT", ",", "original_sigint_handler", ")", "if", "hasattr", "(", "signal", ",", "'SIGHUP'", ")", ":", "signal", ".", "signal", "(", "signal", ".", "SIGHUP", ",", "original_sighup_handler", ")" ]
Start all consumer processes and register signal handlers.
[ "Start", "all", "consumer", "processes", "and", "register", "signal", "handlers", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/consumer.py#L344-L383
228,263
coleifer/huey
huey/consumer.py
Consumer.stop
def stop(self, graceful=False): """ Set the stop-flag. If `graceful=True`, this method blocks until the workers to finish executing any tasks they might be currently working on. """ self.stop_flag.set() if graceful: self._logger.info('Shutting down gracefully...') try: for _, worker_process in self.worker_threads: worker_process.join() except KeyboardInterrupt: self._logger.info('Received request to shut down now.') else: self._logger.info('All workers have stopped.') else: self._logger.info('Shutting down')
python
def stop(self, graceful=False): """ Set the stop-flag. If `graceful=True`, this method blocks until the workers to finish executing any tasks they might be currently working on. """ self.stop_flag.set() if graceful: self._logger.info('Shutting down gracefully...') try: for _, worker_process in self.worker_threads: worker_process.join() except KeyboardInterrupt: self._logger.info('Received request to shut down now.') else: self._logger.info('All workers have stopped.') else: self._logger.info('Shutting down')
[ "def", "stop", "(", "self", ",", "graceful", "=", "False", ")", ":", "self", ".", "stop_flag", ".", "set", "(", ")", "if", "graceful", ":", "self", ".", "_logger", ".", "info", "(", "'Shutting down gracefully...'", ")", "try", ":", "for", "_", ",", "worker_process", "in", "self", ".", "worker_threads", ":", "worker_process", ".", "join", "(", ")", "except", "KeyboardInterrupt", ":", "self", ".", "_logger", ".", "info", "(", "'Received request to shut down now.'", ")", "else", ":", "self", ".", "_logger", ".", "info", "(", "'All workers have stopped.'", ")", "else", ":", "self", ".", "_logger", ".", "info", "(", "'Shutting down'", ")" ]
Set the stop-flag. If `graceful=True`, this method blocks until the workers to finish executing any tasks they might be currently working on.
[ "Set", "the", "stop", "-", "flag", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/consumer.py#L385-L403
228,264
coleifer/huey
huey/consumer.py
Consumer.run
def run(self): """ Run the consumer. """ self.start() timeout = self._stop_flag_timeout health_check_ts = time.time() while True: try: self.stop_flag.wait(timeout=timeout) except KeyboardInterrupt: self._logger.info('Received SIGINT') self.stop(graceful=True) except: self._logger.exception('Error in consumer.') self.stop() else: if self._received_signal: self.stop(graceful=self._graceful) if self.stop_flag.is_set(): break if self._health_check: now = time.time() if now >= health_check_ts + self._health_check_interval: health_check_ts = now self.check_worker_health() if self._restart: self._logger.info('Consumer will restart.') python = sys.executable os.execl(python, python, *sys.argv) else: self._logger.info('Consumer exiting.')
python
def run(self): """ Run the consumer. """ self.start() timeout = self._stop_flag_timeout health_check_ts = time.time() while True: try: self.stop_flag.wait(timeout=timeout) except KeyboardInterrupt: self._logger.info('Received SIGINT') self.stop(graceful=True) except: self._logger.exception('Error in consumer.') self.stop() else: if self._received_signal: self.stop(graceful=self._graceful) if self.stop_flag.is_set(): break if self._health_check: now = time.time() if now >= health_check_ts + self._health_check_interval: health_check_ts = now self.check_worker_health() if self._restart: self._logger.info('Consumer will restart.') python = sys.executable os.execl(python, python, *sys.argv) else: self._logger.info('Consumer exiting.')
[ "def", "run", "(", "self", ")", ":", "self", ".", "start", "(", ")", "timeout", "=", "self", ".", "_stop_flag_timeout", "health_check_ts", "=", "time", ".", "time", "(", ")", "while", "True", ":", "try", ":", "self", ".", "stop_flag", ".", "wait", "(", "timeout", "=", "timeout", ")", "except", "KeyboardInterrupt", ":", "self", ".", "_logger", ".", "info", "(", "'Received SIGINT'", ")", "self", ".", "stop", "(", "graceful", "=", "True", ")", "except", ":", "self", ".", "_logger", ".", "exception", "(", "'Error in consumer.'", ")", "self", ".", "stop", "(", ")", "else", ":", "if", "self", ".", "_received_signal", ":", "self", ".", "stop", "(", "graceful", "=", "self", ".", "_graceful", ")", "if", "self", ".", "stop_flag", ".", "is_set", "(", ")", ":", "break", "if", "self", ".", "_health_check", ":", "now", "=", "time", ".", "time", "(", ")", "if", "now", ">=", "health_check_ts", "+", "self", ".", "_health_check_interval", ":", "health_check_ts", "=", "now", "self", ".", "check_worker_health", "(", ")", "if", "self", ".", "_restart", ":", "self", ".", "_logger", ".", "info", "(", "'Consumer will restart.'", ")", "python", "=", "sys", ".", "executable", "os", ".", "execl", "(", "python", ",", "python", ",", "*", "sys", ".", "argv", ")", "else", ":", "self", ".", "_logger", ".", "info", "(", "'Consumer exiting.'", ")" ]
Run the consumer.
[ "Run", "the", "consumer", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/consumer.py#L405-L440
228,265
coleifer/huey
huey/consumer.py
Consumer.check_worker_health
def check_worker_health(self): """ Check the health of the worker processes. Workers that have died will be replaced with new workers. """ self._logger.debug('Checking worker health.') workers = [] restart_occurred = False for i, (worker, worker_t) in enumerate(self.worker_threads): if not self.environment.is_alive(worker_t): self._logger.warning('Worker %d died, restarting.', i + 1) worker = self._create_worker() worker_t = self._create_process(worker, 'Worker-%d' % (i + 1)) worker_t.start() restart_occurred = True workers.append((worker, worker_t)) if restart_occurred: self.worker_threads = workers else: self._logger.debug('Workers are up and running.') if not self.environment.is_alive(self.scheduler): self._logger.warning('Scheduler died, restarting.') scheduler = self._create_scheduler() self.scheduler = self._create_process(scheduler, 'Scheduler') self.scheduler.start() else: self._logger.debug('Scheduler is up and running.') return not restart_occurred
python
def check_worker_health(self): """ Check the health of the worker processes. Workers that have died will be replaced with new workers. """ self._logger.debug('Checking worker health.') workers = [] restart_occurred = False for i, (worker, worker_t) in enumerate(self.worker_threads): if not self.environment.is_alive(worker_t): self._logger.warning('Worker %d died, restarting.', i + 1) worker = self._create_worker() worker_t = self._create_process(worker, 'Worker-%d' % (i + 1)) worker_t.start() restart_occurred = True workers.append((worker, worker_t)) if restart_occurred: self.worker_threads = workers else: self._logger.debug('Workers are up and running.') if not self.environment.is_alive(self.scheduler): self._logger.warning('Scheduler died, restarting.') scheduler = self._create_scheduler() self.scheduler = self._create_process(scheduler, 'Scheduler') self.scheduler.start() else: self._logger.debug('Scheduler is up and running.') return not restart_occurred
[ "def", "check_worker_health", "(", "self", ")", ":", "self", ".", "_logger", ".", "debug", "(", "'Checking worker health.'", ")", "workers", "=", "[", "]", "restart_occurred", "=", "False", "for", "i", ",", "(", "worker", ",", "worker_t", ")", "in", "enumerate", "(", "self", ".", "worker_threads", ")", ":", "if", "not", "self", ".", "environment", ".", "is_alive", "(", "worker_t", ")", ":", "self", ".", "_logger", ".", "warning", "(", "'Worker %d died, restarting.'", ",", "i", "+", "1", ")", "worker", "=", "self", ".", "_create_worker", "(", ")", "worker_t", "=", "self", ".", "_create_process", "(", "worker", ",", "'Worker-%d'", "%", "(", "i", "+", "1", ")", ")", "worker_t", ".", "start", "(", ")", "restart_occurred", "=", "True", "workers", ".", "append", "(", "(", "worker", ",", "worker_t", ")", ")", "if", "restart_occurred", ":", "self", ".", "worker_threads", "=", "workers", "else", ":", "self", ".", "_logger", ".", "debug", "(", "'Workers are up and running.'", ")", "if", "not", "self", ".", "environment", ".", "is_alive", "(", "self", ".", "scheduler", ")", ":", "self", ".", "_logger", ".", "warning", "(", "'Scheduler died, restarting.'", ")", "scheduler", "=", "self", ".", "_create_scheduler", "(", ")", "self", ".", "scheduler", "=", "self", ".", "_create_process", "(", "scheduler", ",", "'Scheduler'", ")", "self", ".", "scheduler", ".", "start", "(", ")", "else", ":", "self", ".", "_logger", ".", "debug", "(", "'Scheduler is up and running.'", ")", "return", "not", "restart_occurred" ]
Check the health of the worker processes. Workers that have died will be replaced with new workers.
[ "Check", "the", "health", "of", "the", "worker", "processes", ".", "Workers", "that", "have", "died", "will", "be", "replaced", "with", "new", "workers", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/consumer.py#L442-L472
228,266
coleifer/huey
huey/contrib/djhuey/__init__.py
close_db
def close_db(fn): """Decorator to be used with tasks that may operate on the database.""" @wraps(fn) def inner(*args, **kwargs): try: return fn(*args, **kwargs) finally: if not HUEY.immediate: close_old_connections() return inner
python
def close_db(fn): """Decorator to be used with tasks that may operate on the database.""" @wraps(fn) def inner(*args, **kwargs): try: return fn(*args, **kwargs) finally: if not HUEY.immediate: close_old_connections() return inner
[ "def", "close_db", "(", "fn", ")", ":", "@", "wraps", "(", "fn", ")", "def", "inner", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "return", "fn", "(", "*", "args", ",", "*", "*", "kwargs", ")", "finally", ":", "if", "not", "HUEY", ".", "immediate", ":", "close_old_connections", "(", ")", "return", "inner" ]
Decorator to be used with tasks that may operate on the database.
[ "Decorator", "to", "be", "used", "with", "tasks", "that", "may", "operate", "on", "the", "database", "." ]
416e8da1ca18442c08431a91bce373de7d2d200f
https://github.com/coleifer/huey/blob/416e8da1ca18442c08431a91bce373de7d2d200f/huey/contrib/djhuey/__init__.py#L123-L132
228,267
johnwheeler/flask-ask
samples/purchase/model.py
Product.list
def list(self): """ return list of purchasable and not entitled products""" mylist = [] for prod in self.product_list: if self.purchasable(prod) and not self.entitled(prod): mylist.append(prod) return mylist
python
def list(self): """ return list of purchasable and not entitled products""" mylist = [] for prod in self.product_list: if self.purchasable(prod) and not self.entitled(prod): mylist.append(prod) return mylist
[ "def", "list", "(", "self", ")", ":", "mylist", "=", "[", "]", "for", "prod", "in", "self", ".", "product_list", ":", "if", "self", ".", "purchasable", "(", "prod", ")", "and", "not", "self", ".", "entitled", "(", "prod", ")", ":", "mylist", ".", "append", "(", "prod", ")", "return", "mylist" ]
return list of purchasable and not entitled products
[ "return", "list", "of", "purchasable", "and", "not", "entitled", "products" ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/samples/purchase/model.py#L51-L57
228,268
johnwheeler/flask-ask
samples/tidepooler/tidepooler.py
_find_tide_info
def _find_tide_info(predictions): """ Algorithm to find the 2 high tides for the day, the first of which is smaller and occurs mid-day, the second of which is larger and typically in the evening. """ last_prediction = None first_high_tide = None second_high_tide = None low_tide = None first_tide_done = False for prediction in predictions: if last_prediction is None: last_prediction = prediction continue if last_prediction['v'] < prediction['v']: if not first_tide_done: first_high_tide = prediction else: second_high_tide = prediction else: # we're decreasing if not first_tide_done and first_high_tide is not None: first_tide_done = True elif second_high_tide is not None: break # we're decreasing after having found the 2nd tide. We're done. if first_tide_done: low_tide = prediction last_prediction = prediction fmt = '%Y-%m-%d %H:%M' parse = datetime.datetime.strptime tideinfo = TideInfo() tideinfo.first_high_tide_time = parse(first_high_tide['t'], fmt) tideinfo.first_high_tide_height = float(first_high_tide['v']) tideinfo.second_high_tide_time = parse(second_high_tide['t'], fmt) tideinfo.second_high_tide_height = float(second_high_tide['v']) tideinfo.low_tide_time = parse(low_tide['t'], fmt) tideinfo.low_tide_height = float(low_tide['v']) return tideinfo
python
def _find_tide_info(predictions): """ Algorithm to find the 2 high tides for the day, the first of which is smaller and occurs mid-day, the second of which is larger and typically in the evening. """ last_prediction = None first_high_tide = None second_high_tide = None low_tide = None first_tide_done = False for prediction in predictions: if last_prediction is None: last_prediction = prediction continue if last_prediction['v'] < prediction['v']: if not first_tide_done: first_high_tide = prediction else: second_high_tide = prediction else: # we're decreasing if not first_tide_done and first_high_tide is not None: first_tide_done = True elif second_high_tide is not None: break # we're decreasing after having found the 2nd tide. We're done. if first_tide_done: low_tide = prediction last_prediction = prediction fmt = '%Y-%m-%d %H:%M' parse = datetime.datetime.strptime tideinfo = TideInfo() tideinfo.first_high_tide_time = parse(first_high_tide['t'], fmt) tideinfo.first_high_tide_height = float(first_high_tide['v']) tideinfo.second_high_tide_time = parse(second_high_tide['t'], fmt) tideinfo.second_high_tide_height = float(second_high_tide['v']) tideinfo.low_tide_time = parse(low_tide['t'], fmt) tideinfo.low_tide_height = float(low_tide['v']) return tideinfo
[ "def", "_find_tide_info", "(", "predictions", ")", ":", "last_prediction", "=", "None", "first_high_tide", "=", "None", "second_high_tide", "=", "None", "low_tide", "=", "None", "first_tide_done", "=", "False", "for", "prediction", "in", "predictions", ":", "if", "last_prediction", "is", "None", ":", "last_prediction", "=", "prediction", "continue", "if", "last_prediction", "[", "'v'", "]", "<", "prediction", "[", "'v'", "]", ":", "if", "not", "first_tide_done", ":", "first_high_tide", "=", "prediction", "else", ":", "second_high_tide", "=", "prediction", "else", ":", "# we're decreasing", "if", "not", "first_tide_done", "and", "first_high_tide", "is", "not", "None", ":", "first_tide_done", "=", "True", "elif", "second_high_tide", "is", "not", "None", ":", "break", "# we're decreasing after having found the 2nd tide. We're done.", "if", "first_tide_done", ":", "low_tide", "=", "prediction", "last_prediction", "=", "prediction", "fmt", "=", "'%Y-%m-%d %H:%M'", "parse", "=", "datetime", ".", "datetime", ".", "strptime", "tideinfo", "=", "TideInfo", "(", ")", "tideinfo", ".", "first_high_tide_time", "=", "parse", "(", "first_high_tide", "[", "'t'", "]", ",", "fmt", ")", "tideinfo", ".", "first_high_tide_height", "=", "float", "(", "first_high_tide", "[", "'v'", "]", ")", "tideinfo", ".", "second_high_tide_time", "=", "parse", "(", "second_high_tide", "[", "'t'", "]", ",", "fmt", ")", "tideinfo", ".", "second_high_tide_height", "=", "float", "(", "second_high_tide", "[", "'v'", "]", ")", "tideinfo", ".", "low_tide_time", "=", "parse", "(", "low_tide", "[", "'t'", "]", ",", "fmt", ")", "tideinfo", ".", "low_tide_height", "=", "float", "(", "low_tide", "[", "'v'", "]", ")", "return", "tideinfo" ]
Algorithm to find the 2 high tides for the day, the first of which is smaller and occurs mid-day, the second of which is larger and typically in the evening.
[ "Algorithm", "to", "find", "the", "2", "high", "tides", "for", "the", "day", "the", "first", "of", "which", "is", "smaller", "and", "occurs", "mid", "-", "day", "the", "second", "of", "which", "is", "larger", "and", "typically", "in", "the", "evening", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/samples/tidepooler/tidepooler.py#L252-L290
228,269
johnwheeler/flask-ask
flask_ask/models.py
audio.enqueue
def enqueue(self, stream_url, offset=0, opaque_token=None): """Adds stream to the queue. Does not impact the currently playing stream.""" directive = self._play_directive('ENQUEUE') audio_item = self._audio_item(stream_url=stream_url, offset=offset, push_buffer=False, opaque_token=opaque_token) audio_item['stream']['expectedPreviousToken'] = current_stream.token directive['audioItem'] = audio_item self._response['directives'].append(directive) return self
python
def enqueue(self, stream_url, offset=0, opaque_token=None): """Adds stream to the queue. Does not impact the currently playing stream.""" directive = self._play_directive('ENQUEUE') audio_item = self._audio_item(stream_url=stream_url, offset=offset, push_buffer=False, opaque_token=opaque_token) audio_item['stream']['expectedPreviousToken'] = current_stream.token directive['audioItem'] = audio_item self._response['directives'].append(directive) return self
[ "def", "enqueue", "(", "self", ",", "stream_url", ",", "offset", "=", "0", ",", "opaque_token", "=", "None", ")", ":", "directive", "=", "self", ".", "_play_directive", "(", "'ENQUEUE'", ")", "audio_item", "=", "self", ".", "_audio_item", "(", "stream_url", "=", "stream_url", ",", "offset", "=", "offset", ",", "push_buffer", "=", "False", ",", "opaque_token", "=", "opaque_token", ")", "audio_item", "[", "'stream'", "]", "[", "'expectedPreviousToken'", "]", "=", "current_stream", ".", "token", "directive", "[", "'audioItem'", "]", "=", "audio_item", "self", ".", "_response", "[", "'directives'", "]", ".", "append", "(", "directive", ")", "return", "self" ]
Adds stream to the queue. Does not impact the currently playing stream.
[ "Adds", "stream", "to", "the", "queue", ".", "Does", "not", "impact", "the", "currently", "playing", "stream", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/models.py#L364-L375
228,270
johnwheeler/flask-ask
flask_ask/models.py
audio.play_next
def play_next(self, stream_url=None, offset=0, opaque_token=None): """Replace all streams in the queue but does not impact the currently playing stream.""" directive = self._play_directive('REPLACE_ENQUEUED') directive['audioItem'] = self._audio_item(stream_url=stream_url, offset=offset, opaque_token=opaque_token) self._response['directives'].append(directive) return self
python
def play_next(self, stream_url=None, offset=0, opaque_token=None): """Replace all streams in the queue but does not impact the currently playing stream.""" directive = self._play_directive('REPLACE_ENQUEUED') directive['audioItem'] = self._audio_item(stream_url=stream_url, offset=offset, opaque_token=opaque_token) self._response['directives'].append(directive) return self
[ "def", "play_next", "(", "self", ",", "stream_url", "=", "None", ",", "offset", "=", "0", ",", "opaque_token", "=", "None", ")", ":", "directive", "=", "self", ".", "_play_directive", "(", "'REPLACE_ENQUEUED'", ")", "directive", "[", "'audioItem'", "]", "=", "self", ".", "_audio_item", "(", "stream_url", "=", "stream_url", ",", "offset", "=", "offset", ",", "opaque_token", "=", "opaque_token", ")", "self", ".", "_response", "[", "'directives'", "]", ".", "append", "(", "directive", ")", "return", "self" ]
Replace all streams in the queue but does not impact the currently playing stream.
[ "Replace", "all", "streams", "in", "the", "queue", "but", "does", "not", "impact", "the", "currently", "playing", "stream", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/models.py#L377-L383
228,271
johnwheeler/flask-ask
flask_ask/models.py
audio.resume
def resume(self): """Sends Play Directive to resume playback at the paused offset""" directive = self._play_directive('REPLACE_ALL') directive['audioItem'] = self._audio_item() self._response['directives'].append(directive) return self
python
def resume(self): """Sends Play Directive to resume playback at the paused offset""" directive = self._play_directive('REPLACE_ALL') directive['audioItem'] = self._audio_item() self._response['directives'].append(directive) return self
[ "def", "resume", "(", "self", ")", ":", "directive", "=", "self", ".", "_play_directive", "(", "'REPLACE_ALL'", ")", "directive", "[", "'audioItem'", "]", "=", "self", ".", "_audio_item", "(", ")", "self", ".", "_response", "[", "'directives'", "]", ".", "append", "(", "directive", ")", "return", "self" ]
Sends Play Directive to resume playback at the paused offset
[ "Sends", "Play", "Directive", "to", "resume", "playback", "at", "the", "paused", "offset" ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/models.py#L385-L390
228,272
johnwheeler/flask-ask
flask_ask/models.py
audio._audio_item
def _audio_item(self, stream_url=None, offset=0, push_buffer=True, opaque_token=None): """Builds an AudioPlayer Directive's audioItem and updates current_stream""" audio_item = {'stream': {}} stream = audio_item['stream'] # existing stream if not stream_url: # stream.update(current_stream.__dict__) stream['url'] = current_stream.url stream['token'] = current_stream.token stream['offsetInMilliseconds'] = current_stream.offsetInMilliseconds # new stream else: stream['url'] = stream_url stream['token'] = opaque_token or str(uuid.uuid4()) stream['offsetInMilliseconds'] = offset if push_buffer: # prevents enqueued streams from becoming current_stream push_stream(stream_cache, context['System']['user']['userId'], stream) return audio_item
python
def _audio_item(self, stream_url=None, offset=0, push_buffer=True, opaque_token=None): """Builds an AudioPlayer Directive's audioItem and updates current_stream""" audio_item = {'stream': {}} stream = audio_item['stream'] # existing stream if not stream_url: # stream.update(current_stream.__dict__) stream['url'] = current_stream.url stream['token'] = current_stream.token stream['offsetInMilliseconds'] = current_stream.offsetInMilliseconds # new stream else: stream['url'] = stream_url stream['token'] = opaque_token or str(uuid.uuid4()) stream['offsetInMilliseconds'] = offset if push_buffer: # prevents enqueued streams from becoming current_stream push_stream(stream_cache, context['System']['user']['userId'], stream) return audio_item
[ "def", "_audio_item", "(", "self", ",", "stream_url", "=", "None", ",", "offset", "=", "0", ",", "push_buffer", "=", "True", ",", "opaque_token", "=", "None", ")", ":", "audio_item", "=", "{", "'stream'", ":", "{", "}", "}", "stream", "=", "audio_item", "[", "'stream'", "]", "# existing stream", "if", "not", "stream_url", ":", "# stream.update(current_stream.__dict__)", "stream", "[", "'url'", "]", "=", "current_stream", ".", "url", "stream", "[", "'token'", "]", "=", "current_stream", ".", "token", "stream", "[", "'offsetInMilliseconds'", "]", "=", "current_stream", ".", "offsetInMilliseconds", "# new stream", "else", ":", "stream", "[", "'url'", "]", "=", "stream_url", "stream", "[", "'token'", "]", "=", "opaque_token", "or", "str", "(", "uuid", ".", "uuid4", "(", ")", ")", "stream", "[", "'offsetInMilliseconds'", "]", "=", "offset", "if", "push_buffer", ":", "# prevents enqueued streams from becoming current_stream", "push_stream", "(", "stream_cache", ",", "context", "[", "'System'", "]", "[", "'user'", "]", "[", "'userId'", "]", ",", "stream", ")", "return", "audio_item" ]
Builds an AudioPlayer Directive's audioItem and updates current_stream
[ "Builds", "an", "AudioPlayer", "Directive", "s", "audioItem", "and", "updates", "current_stream" ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/models.py#L398-L418
228,273
johnwheeler/flask-ask
flask_ask/models.py
audio.clear_queue
def clear_queue(self, stop=False): """Clears queued streams and optionally stops current stream. Keyword Arguments: stop {bool} set True to stop current current stream and clear queued streams. set False to clear queued streams and allow current stream to finish default: {False} """ directive = {} directive['type'] = 'AudioPlayer.ClearQueue' if stop: directive['clearBehavior'] = 'CLEAR_ALL' else: directive['clearBehavior'] = 'CLEAR_ENQUEUED' self._response['directives'].append(directive) return self
python
def clear_queue(self, stop=False): """Clears queued streams and optionally stops current stream. Keyword Arguments: stop {bool} set True to stop current current stream and clear queued streams. set False to clear queued streams and allow current stream to finish default: {False} """ directive = {} directive['type'] = 'AudioPlayer.ClearQueue' if stop: directive['clearBehavior'] = 'CLEAR_ALL' else: directive['clearBehavior'] = 'CLEAR_ENQUEUED' self._response['directives'].append(directive) return self
[ "def", "clear_queue", "(", "self", ",", "stop", "=", "False", ")", ":", "directive", "=", "{", "}", "directive", "[", "'type'", "]", "=", "'AudioPlayer.ClearQueue'", "if", "stop", ":", "directive", "[", "'clearBehavior'", "]", "=", "'CLEAR_ALL'", "else", ":", "directive", "[", "'clearBehavior'", "]", "=", "'CLEAR_ENQUEUED'", "self", ".", "_response", "[", "'directives'", "]", ".", "append", "(", "directive", ")", "return", "self" ]
Clears queued streams and optionally stops current stream. Keyword Arguments: stop {bool} set True to stop current current stream and clear queued streams. set False to clear queued streams and allow current stream to finish default: {False}
[ "Clears", "queued", "streams", "and", "optionally", "stops", "current", "stream", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/models.py#L425-L442
228,274
johnwheeler/flask-ask
flask_ask/core.py
find_ask
def find_ask(): """ Find our instance of Ask, navigating Local's and possible blueprints. Note: This only supports returning a reference to the first instance of Ask found. """ if hasattr(current_app, 'ask'): return getattr(current_app, 'ask') else: if hasattr(current_app, 'blueprints'): blueprints = getattr(current_app, 'blueprints') for blueprint_name in blueprints: if hasattr(blueprints[blueprint_name], 'ask'): return getattr(blueprints[blueprint_name], 'ask')
python
def find_ask(): """ Find our instance of Ask, navigating Local's and possible blueprints. Note: This only supports returning a reference to the first instance of Ask found. """ if hasattr(current_app, 'ask'): return getattr(current_app, 'ask') else: if hasattr(current_app, 'blueprints'): blueprints = getattr(current_app, 'blueprints') for blueprint_name in blueprints: if hasattr(blueprints[blueprint_name], 'ask'): return getattr(blueprints[blueprint_name], 'ask')
[ "def", "find_ask", "(", ")", ":", "if", "hasattr", "(", "current_app", ",", "'ask'", ")", ":", "return", "getattr", "(", "current_app", ",", "'ask'", ")", "else", ":", "if", "hasattr", "(", "current_app", ",", "'blueprints'", ")", ":", "blueprints", "=", "getattr", "(", "current_app", ",", "'blueprints'", ")", "for", "blueprint_name", "in", "blueprints", ":", "if", "hasattr", "(", "blueprints", "[", "blueprint_name", "]", ",", "'ask'", ")", ":", "return", "getattr", "(", "blueprints", "[", "blueprint_name", "]", ",", "'ask'", ")" ]
Find our instance of Ask, navigating Local's and possible blueprints. Note: This only supports returning a reference to the first instance of Ask found.
[ "Find", "our", "instance", "of", "Ask", "navigating", "Local", "s", "and", "possible", "blueprints", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L21-L35
228,275
johnwheeler/flask-ask
flask_ask/core.py
Ask.init_app
def init_app(self, app, path='templates.yaml'): """Initializes Ask app by setting configuration variables, loading templates, and maps Ask route to a flask view. The Ask instance is given the following configuration variables by calling on Flask's configuration: `ASK_APPLICATION_ID`: Turn on application ID verification by setting this variable to an application ID or a list of allowed application IDs. By default, application ID verification is disabled and a warning is logged. This variable should be set in production to ensure requests are being sent by the applications you specify. Default: None `ASK_VERIFY_REQUESTS`: Enables or disables Alexa request verification, which ensures requests sent to your skill are from Amazon's Alexa service. This setting should not be disabled in production. It is useful for mocking JSON requests in automated tests. Default: True `ASK_VERIFY_TIMESTAMP_DEBUG`: Turn on request timestamp verification while debugging by setting this to True. Timestamp verification helps mitigate against replay attacks. It relies on the system clock being synchronized with an NTP server. This setting should not be enabled in production. Default: False `ASK_PRETTY_DEBUG_LOGS`: Add tabs and linebreaks to the Alexa request and response printed to the debug log. This improves readability when printing to the console, but breaks formatting when logging to CloudWatch. Default: False """ if self._route is None: raise TypeError("route is a required argument when app is not None") self.app = app app.ask = self app.add_url_rule(self._route, view_func=self._flask_view_func, methods=['POST']) app.jinja_loader = ChoiceLoader([app.jinja_loader, YamlLoader(app, path)])
python
def init_app(self, app, path='templates.yaml'): """Initializes Ask app by setting configuration variables, loading templates, and maps Ask route to a flask view. The Ask instance is given the following configuration variables by calling on Flask's configuration: `ASK_APPLICATION_ID`: Turn on application ID verification by setting this variable to an application ID or a list of allowed application IDs. By default, application ID verification is disabled and a warning is logged. This variable should be set in production to ensure requests are being sent by the applications you specify. Default: None `ASK_VERIFY_REQUESTS`: Enables or disables Alexa request verification, which ensures requests sent to your skill are from Amazon's Alexa service. This setting should not be disabled in production. It is useful for mocking JSON requests in automated tests. Default: True `ASK_VERIFY_TIMESTAMP_DEBUG`: Turn on request timestamp verification while debugging by setting this to True. Timestamp verification helps mitigate against replay attacks. It relies on the system clock being synchronized with an NTP server. This setting should not be enabled in production. Default: False `ASK_PRETTY_DEBUG_LOGS`: Add tabs and linebreaks to the Alexa request and response printed to the debug log. This improves readability when printing to the console, but breaks formatting when logging to CloudWatch. Default: False """ if self._route is None: raise TypeError("route is a required argument when app is not None") self.app = app app.ask = self app.add_url_rule(self._route, view_func=self._flask_view_func, methods=['POST']) app.jinja_loader = ChoiceLoader([app.jinja_loader, YamlLoader(app, path)])
[ "def", "init_app", "(", "self", ",", "app", ",", "path", "=", "'templates.yaml'", ")", ":", "if", "self", ".", "_route", "is", "None", ":", "raise", "TypeError", "(", "\"route is a required argument when app is not None\"", ")", "self", ".", "app", "=", "app", "app", ".", "ask", "=", "self", "app", ".", "add_url_rule", "(", "self", ".", "_route", ",", "view_func", "=", "self", ".", "_flask_view_func", ",", "methods", "=", "[", "'POST'", "]", ")", "app", ".", "jinja_loader", "=", "ChoiceLoader", "(", "[", "app", ".", "jinja_loader", ",", "YamlLoader", "(", "app", ",", "path", ")", "]", ")" ]
Initializes Ask app by setting configuration variables, loading templates, and maps Ask route to a flask view. The Ask instance is given the following configuration variables by calling on Flask's configuration: `ASK_APPLICATION_ID`: Turn on application ID verification by setting this variable to an application ID or a list of allowed application IDs. By default, application ID verification is disabled and a warning is logged. This variable should be set in production to ensure requests are being sent by the applications you specify. Default: None `ASK_VERIFY_REQUESTS`: Enables or disables Alexa request verification, which ensures requests sent to your skill are from Amazon's Alexa service. This setting should not be disabled in production. It is useful for mocking JSON requests in automated tests. Default: True `ASK_VERIFY_TIMESTAMP_DEBUG`: Turn on request timestamp verification while debugging by setting this to True. Timestamp verification helps mitigate against replay attacks. It relies on the system clock being synchronized with an NTP server. This setting should not be enabled in production. Default: False `ASK_PRETTY_DEBUG_LOGS`: Add tabs and linebreaks to the Alexa request and response printed to the debug log. This improves readability when printing to the console, but breaks formatting when logging to CloudWatch. Default: False
[ "Initializes", "Ask", "app", "by", "setting", "configuration", "variables", "loading", "templates", "and", "maps", "Ask", "route", "to", "a", "flask", "view", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L101-L142
228,276
johnwheeler/flask-ask
flask_ask/core.py
Ask.launch
def launch(self, f): """Decorator maps a view function as the endpoint for an Alexa LaunchRequest and starts the skill. @ask.launch def launched(): return question('Welcome to Foo') The wrapped function is registered as the launch view function and renders the response for requests to the Launch URL. A request to the launch URL is verified with the Alexa server before the payload is passed to the view function. Arguments: f {function} -- Launch view function """ self._launch_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
python
def launch(self, f): """Decorator maps a view function as the endpoint for an Alexa LaunchRequest and starts the skill. @ask.launch def launched(): return question('Welcome to Foo') The wrapped function is registered as the launch view function and renders the response for requests to the Launch URL. A request to the launch URL is verified with the Alexa server before the payload is passed to the view function. Arguments: f {function} -- Launch view function """ self._launch_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
[ "def", "launch", "(", "self", ",", "f", ")", ":", "self", ".", "_launch_view_func", "=", "f", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kw", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kw", ")", "return", "f" ]
Decorator maps a view function as the endpoint for an Alexa LaunchRequest and starts the skill. @ask.launch def launched(): return question('Welcome to Foo') The wrapped function is registered as the launch view function and renders the response for requests to the Launch URL. A request to the launch URL is verified with the Alexa server before the payload is passed to the view function. Arguments: f {function} -- Launch view function
[ "Decorator", "maps", "a", "view", "function", "as", "the", "endpoint", "for", "an", "Alexa", "LaunchRequest", "and", "starts", "the", "skill", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L192-L212
228,277
johnwheeler/flask-ask
flask_ask/core.py
Ask.session_ended
def session_ended(self, f): """Decorator routes Alexa SessionEndedRequest to the wrapped view function to end the skill. @ask.session_ended def session_ended(): return "{}", 200 The wrapped function is registered as the session_ended view function and renders the response for requests to the end of the session. Arguments: f {function} -- session_ended view function """ self._session_ended_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
python
def session_ended(self, f): """Decorator routes Alexa SessionEndedRequest to the wrapped view function to end the skill. @ask.session_ended def session_ended(): return "{}", 200 The wrapped function is registered as the session_ended view function and renders the response for requests to the end of the session. Arguments: f {function} -- session_ended view function """ self._session_ended_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
[ "def", "session_ended", "(", "self", ",", "f", ")", ":", "self", ".", "_session_ended_view_func", "=", "f", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kw", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kw", ")", "return", "f" ]
Decorator routes Alexa SessionEndedRequest to the wrapped view function to end the skill. @ask.session_ended def session_ended(): return "{}", 200 The wrapped function is registered as the session_ended view function and renders the response for requests to the end of the session. Arguments: f {function} -- session_ended view function
[ "Decorator", "routes", "Alexa", "SessionEndedRequest", "to", "the", "wrapped", "view", "function", "to", "end", "the", "skill", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L214-L232
228,278
johnwheeler/flask-ask
flask_ask/core.py
Ask.intent
def intent(self, intent_name, mapping={}, convert={}, default={}): """Decorator routes an Alexa IntentRequest and provides the slot parameters to the wrapped function. Functions decorated as an intent are registered as the view function for the Intent's URL, and provide the backend responses to give your Skill its functionality. @ask.intent('WeatherIntent', mapping={'city': 'City'}) def weather(city): return statement('I predict great weather for {}'.format(city)) Arguments: intent_name {str} -- Name of the intent request to be mapped to the decorated function Keyword Arguments: mapping {dict} -- Maps parameters to intent slots of a different name default: {} convert {dict} -- Converts slot values to data types before assignment to parameters default: {} default {dict} -- Provides default values for Intent slots if Alexa reuqest returns no corresponding slot, or a slot with an empty value default: {} """ def decorator(f): self._intent_view_funcs[intent_name] = f self._intent_mappings[intent_name] = mapping self._intent_converts[intent_name] = convert self._intent_defaults[intent_name] = default @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f return decorator
python
def intent(self, intent_name, mapping={}, convert={}, default={}): """Decorator routes an Alexa IntentRequest and provides the slot parameters to the wrapped function. Functions decorated as an intent are registered as the view function for the Intent's URL, and provide the backend responses to give your Skill its functionality. @ask.intent('WeatherIntent', mapping={'city': 'City'}) def weather(city): return statement('I predict great weather for {}'.format(city)) Arguments: intent_name {str} -- Name of the intent request to be mapped to the decorated function Keyword Arguments: mapping {dict} -- Maps parameters to intent slots of a different name default: {} convert {dict} -- Converts slot values to data types before assignment to parameters default: {} default {dict} -- Provides default values for Intent slots if Alexa reuqest returns no corresponding slot, or a slot with an empty value default: {} """ def decorator(f): self._intent_view_funcs[intent_name] = f self._intent_mappings[intent_name] = mapping self._intent_converts[intent_name] = convert self._intent_defaults[intent_name] = default @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f return decorator
[ "def", "intent", "(", "self", ",", "intent_name", ",", "mapping", "=", "{", "}", ",", "convert", "=", "{", "}", ",", "default", "=", "{", "}", ")", ":", "def", "decorator", "(", "f", ")", ":", "self", ".", "_intent_view_funcs", "[", "intent_name", "]", "=", "f", "self", ".", "_intent_mappings", "[", "intent_name", "]", "=", "mapping", "self", ".", "_intent_converts", "[", "intent_name", "]", "=", "convert", "self", ".", "_intent_defaults", "[", "intent_name", "]", "=", "default", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kw", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kw", ")", "return", "f", "return", "decorator" ]
Decorator routes an Alexa IntentRequest and provides the slot parameters to the wrapped function. Functions decorated as an intent are registered as the view function for the Intent's URL, and provide the backend responses to give your Skill its functionality. @ask.intent('WeatherIntent', mapping={'city': 'City'}) def weather(city): return statement('I predict great weather for {}'.format(city)) Arguments: intent_name {str} -- Name of the intent request to be mapped to the decorated function Keyword Arguments: mapping {dict} -- Maps parameters to intent slots of a different name default: {} convert {dict} -- Converts slot values to data types before assignment to parameters default: {} default {dict} -- Provides default values for Intent slots if Alexa reuqest returns no corresponding slot, or a slot with an empty value default: {}
[ "Decorator", "routes", "an", "Alexa", "IntentRequest", "and", "provides", "the", "slot", "parameters", "to", "the", "wrapped", "function", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L234-L268
228,279
johnwheeler/flask-ask
flask_ask/core.py
Ask.default_intent
def default_intent(self, f): """Decorator routes any Alexa IntentRequest that is not matched by any existing @ask.intent routing.""" self._default_intent_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
python
def default_intent(self, f): """Decorator routes any Alexa IntentRequest that is not matched by any existing @ask.intent routing.""" self._default_intent_view_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
[ "def", "default_intent", "(", "self", ",", "f", ")", ":", "self", ".", "_default_intent_view_func", "=", "f", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kw", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kw", ")", "return", "f" ]
Decorator routes any Alexa IntentRequest that is not matched by any existing @ask.intent routing.
[ "Decorator", "routes", "any", "Alexa", "IntentRequest", "that", "is", "not", "matched", "by", "any", "existing" ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L270-L277
228,280
johnwheeler/flask-ask
flask_ask/core.py
Ask.display_element_selected
def display_element_selected(self, f): """Decorator routes Alexa Display.ElementSelected request to the wrapped view function. @ask.display_element_selected def eval_element(): return "", 200 The wrapped function is registered as the display_element_selected view function and renders the response for requests. Arguments: f {function} -- display_element_selected view function """ self._display_element_selected_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
python
def display_element_selected(self, f): """Decorator routes Alexa Display.ElementSelected request to the wrapped view function. @ask.display_element_selected def eval_element(): return "", 200 The wrapped function is registered as the display_element_selected view function and renders the response for requests. Arguments: f {function} -- display_element_selected view function """ self._display_element_selected_func = f @wraps(f) def wrapper(*args, **kw): self._flask_view_func(*args, **kw) return f
[ "def", "display_element_selected", "(", "self", ",", "f", ")", ":", "self", ".", "_display_element_selected_func", "=", "f", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kw", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kw", ")", "return", "f" ]
Decorator routes Alexa Display.ElementSelected request to the wrapped view function. @ask.display_element_selected def eval_element(): return "", 200 The wrapped function is registered as the display_element_selected view function and renders the response for requests. Arguments: f {function} -- display_element_selected view function
[ "Decorator", "routes", "Alexa", "Display", ".", "ElementSelected", "request", "to", "the", "wrapped", "view", "function", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L279-L297
228,281
johnwheeler/flask-ask
flask_ask/core.py
Ask.on_purchase_completed
def on_purchase_completed(self, mapping={'payload': 'payload','name':'name','status':'status','token':'token'}, convert={}, default={}): """Decorator routes an Connections.Response to the wrapped function. Request is sent when Alexa completes the purchase flow. See https://developer.amazon.com/docs/in-skill-purchase/add-isps-to-a-skill.html#handle-results The wrapped view function may accept parameters from the Request. In addition to locale, requestId, timestamp, and type @ask.on_purchase_completed( mapping={'payload': 'payload','name':'name','status':'status','token':'token'}) def completed(payload, name, status, token): logger.info(payload) logger.info(name) logger.info(status) logger.info(token) """ def decorator(f): self._intent_view_funcs['Connections.Response'] = f self._intent_mappings['Connections.Response'] = mapping self._intent_converts['Connections.Response'] = convert self._intent_defaults['Connections.Response'] = default @wraps(f) def wrapper(*args, **kwargs): self._flask_view_func(*args, **kwargs) return f return decorator
python
def on_purchase_completed(self, mapping={'payload': 'payload','name':'name','status':'status','token':'token'}, convert={}, default={}): """Decorator routes an Connections.Response to the wrapped function. Request is sent when Alexa completes the purchase flow. See https://developer.amazon.com/docs/in-skill-purchase/add-isps-to-a-skill.html#handle-results The wrapped view function may accept parameters from the Request. In addition to locale, requestId, timestamp, and type @ask.on_purchase_completed( mapping={'payload': 'payload','name':'name','status':'status','token':'token'}) def completed(payload, name, status, token): logger.info(payload) logger.info(name) logger.info(status) logger.info(token) """ def decorator(f): self._intent_view_funcs['Connections.Response'] = f self._intent_mappings['Connections.Response'] = mapping self._intent_converts['Connections.Response'] = convert self._intent_defaults['Connections.Response'] = default @wraps(f) def wrapper(*args, **kwargs): self._flask_view_func(*args, **kwargs) return f return decorator
[ "def", "on_purchase_completed", "(", "self", ",", "mapping", "=", "{", "'payload'", ":", "'payload'", ",", "'name'", ":", "'name'", ",", "'status'", ":", "'status'", ",", "'token'", ":", "'token'", "}", ",", "convert", "=", "{", "}", ",", "default", "=", "{", "}", ")", ":", "def", "decorator", "(", "f", ")", ":", "self", ".", "_intent_view_funcs", "[", "'Connections.Response'", "]", "=", "f", "self", ".", "_intent_mappings", "[", "'Connections.Response'", "]", "=", "mapping", "self", ".", "_intent_converts", "[", "'Connections.Response'", "]", "=", "convert", "self", ".", "_intent_defaults", "[", "'Connections.Response'", "]", "=", "default", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "_flask_view_func", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "f", "return", "decorator" ]
Decorator routes an Connections.Response to the wrapped function. Request is sent when Alexa completes the purchase flow. See https://developer.amazon.com/docs/in-skill-purchase/add-isps-to-a-skill.html#handle-results The wrapped view function may accept parameters from the Request. In addition to locale, requestId, timestamp, and type @ask.on_purchase_completed( mapping={'payload': 'payload','name':'name','status':'status','token':'token'}) def completed(payload, name, status, token): logger.info(payload) logger.info(name) logger.info(status) logger.info(token)
[ "Decorator", "routes", "an", "Connections", ".", "Response", "to", "the", "wrapped", "function", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L300-L328
228,282
johnwheeler/flask-ask
flask_ask/core.py
Ask.run_aws_lambda
def run_aws_lambda(self, event): """Invoke the Flask Ask application from an AWS Lambda function handler. Use this method to service AWS Lambda requests from a custom Alexa skill. This method will invoke your Flask application providing a WSGI-compatible environment that wraps the original Alexa event provided to the AWS Lambda handler. Returns the output generated by a Flask Ask application, which should be used as the return value to the AWS Lambda handler function. Example usage: from flask import Flask from flask_ask import Ask, statement app = Flask(__name__) ask = Ask(app, '/') # This function name is what you defined when you create an # AWS Lambda function. By default, AWS calls this function # lambda_handler. def lambda_handler(event, _context): return ask.run_aws_lambda(event) @ask.intent('HelloIntent') def hello(firstname): speech_text = "Hello %s" % firstname return statement(speech_text).simple_card('Hello', speech_text) """ # We are guaranteed to be called by AWS as a Lambda function does not # expose a public facing interface. self.app.config['ASK_VERIFY_REQUESTS'] = False # Convert an environment variable to a WSGI "bytes-as-unicode" string enc, esc = sys.getfilesystemencoding(), 'surrogateescape' def unicode_to_wsgi(u): return u.encode(enc, esc).decode('iso-8859-1') # Create a WSGI-compatible environ that can be passed to the # application. It is loaded with the OS environment variables, # mandatory CGI-like variables, as well as the mandatory WSGI # variables. environ = {k: unicode_to_wsgi(v) for k, v in os.environ.items()} environ['REQUEST_METHOD'] = 'POST' environ['PATH_INFO'] = '/' environ['SERVER_NAME'] = 'AWS-Lambda' environ['SERVER_PORT'] = '80' environ['SERVER_PROTOCOL'] = 'HTTP/1.0' environ['wsgi.version'] = (1, 0) environ['wsgi.url_scheme'] = 'http' environ['wsgi.errors'] = sys.stderr environ['wsgi.multithread'] = False environ['wsgi.multiprocess'] = False environ['wsgi.run_once'] = True # Convert the event provided by the AWS Lambda handler to a JSON # string that can be read as the body of a HTTP POST request. body = json.dumps(event) environ['CONTENT_TYPE'] = 'application/json' environ['CONTENT_LENGTH'] = len(body) PY3 = sys.version_info[0] == 3 if PY3: environ['wsgi.input'] = io.StringIO(body) else: environ['wsgi.input'] = io.BytesIO(body) # Start response is a required callback that must be passed when # the application is invoked. It is used to set HTTP status and # headers. Read the WSGI spec for details (PEP3333). headers = [] def start_response(status, response_headers, _exc_info=None): headers[:] = [status, response_headers] # Invoke the actual Flask application providing our environment, # with our Alexa event as the body of the HTTP request, as well # as the callback function above. The result will be an iterator # that provides a serialized JSON string for our Alexa response. result = self.app(environ, start_response) try: if not headers: raise AssertionError("start_response() not called by WSGI app") output = b"".join(result) if not headers[0].startswith("2"): raise AssertionError("Non-2xx from app: hdrs={}, body={}".format(headers, output)) # The Lambda handler expects a Python object that can be # serialized as JSON, so we need to take the already serialized # JSON and deserialize it. return json.loads(output) finally: # Per the WSGI spec, we need to invoke the close method if it # is implemented on the result object. if hasattr(result, 'close'): result.close()
python
def run_aws_lambda(self, event): """Invoke the Flask Ask application from an AWS Lambda function handler. Use this method to service AWS Lambda requests from a custom Alexa skill. This method will invoke your Flask application providing a WSGI-compatible environment that wraps the original Alexa event provided to the AWS Lambda handler. Returns the output generated by a Flask Ask application, which should be used as the return value to the AWS Lambda handler function. Example usage: from flask import Flask from flask_ask import Ask, statement app = Flask(__name__) ask = Ask(app, '/') # This function name is what you defined when you create an # AWS Lambda function. By default, AWS calls this function # lambda_handler. def lambda_handler(event, _context): return ask.run_aws_lambda(event) @ask.intent('HelloIntent') def hello(firstname): speech_text = "Hello %s" % firstname return statement(speech_text).simple_card('Hello', speech_text) """ # We are guaranteed to be called by AWS as a Lambda function does not # expose a public facing interface. self.app.config['ASK_VERIFY_REQUESTS'] = False # Convert an environment variable to a WSGI "bytes-as-unicode" string enc, esc = sys.getfilesystemencoding(), 'surrogateescape' def unicode_to_wsgi(u): return u.encode(enc, esc).decode('iso-8859-1') # Create a WSGI-compatible environ that can be passed to the # application. It is loaded with the OS environment variables, # mandatory CGI-like variables, as well as the mandatory WSGI # variables. environ = {k: unicode_to_wsgi(v) for k, v in os.environ.items()} environ['REQUEST_METHOD'] = 'POST' environ['PATH_INFO'] = '/' environ['SERVER_NAME'] = 'AWS-Lambda' environ['SERVER_PORT'] = '80' environ['SERVER_PROTOCOL'] = 'HTTP/1.0' environ['wsgi.version'] = (1, 0) environ['wsgi.url_scheme'] = 'http' environ['wsgi.errors'] = sys.stderr environ['wsgi.multithread'] = False environ['wsgi.multiprocess'] = False environ['wsgi.run_once'] = True # Convert the event provided by the AWS Lambda handler to a JSON # string that can be read as the body of a HTTP POST request. body = json.dumps(event) environ['CONTENT_TYPE'] = 'application/json' environ['CONTENT_LENGTH'] = len(body) PY3 = sys.version_info[0] == 3 if PY3: environ['wsgi.input'] = io.StringIO(body) else: environ['wsgi.input'] = io.BytesIO(body) # Start response is a required callback that must be passed when # the application is invoked. It is used to set HTTP status and # headers. Read the WSGI spec for details (PEP3333). headers = [] def start_response(status, response_headers, _exc_info=None): headers[:] = [status, response_headers] # Invoke the actual Flask application providing our environment, # with our Alexa event as the body of the HTTP request, as well # as the callback function above. The result will be an iterator # that provides a serialized JSON string for our Alexa response. result = self.app(environ, start_response) try: if not headers: raise AssertionError("start_response() not called by WSGI app") output = b"".join(result) if not headers[0].startswith("2"): raise AssertionError("Non-2xx from app: hdrs={}, body={}".format(headers, output)) # The Lambda handler expects a Python object that can be # serialized as JSON, so we need to take the already serialized # JSON and deserialize it. return json.loads(output) finally: # Per the WSGI spec, we need to invoke the close method if it # is implemented on the result object. if hasattr(result, 'close'): result.close()
[ "def", "run_aws_lambda", "(", "self", ",", "event", ")", ":", "# We are guaranteed to be called by AWS as a Lambda function does not", "# expose a public facing interface.", "self", ".", "app", ".", "config", "[", "'ASK_VERIFY_REQUESTS'", "]", "=", "False", "# Convert an environment variable to a WSGI \"bytes-as-unicode\" string", "enc", ",", "esc", "=", "sys", ".", "getfilesystemencoding", "(", ")", ",", "'surrogateescape'", "def", "unicode_to_wsgi", "(", "u", ")", ":", "return", "u", ".", "encode", "(", "enc", ",", "esc", ")", ".", "decode", "(", "'iso-8859-1'", ")", "# Create a WSGI-compatible environ that can be passed to the", "# application. It is loaded with the OS environment variables,", "# mandatory CGI-like variables, as well as the mandatory WSGI", "# variables.", "environ", "=", "{", "k", ":", "unicode_to_wsgi", "(", "v", ")", "for", "k", ",", "v", "in", "os", ".", "environ", ".", "items", "(", ")", "}", "environ", "[", "'REQUEST_METHOD'", "]", "=", "'POST'", "environ", "[", "'PATH_INFO'", "]", "=", "'/'", "environ", "[", "'SERVER_NAME'", "]", "=", "'AWS-Lambda'", "environ", "[", "'SERVER_PORT'", "]", "=", "'80'", "environ", "[", "'SERVER_PROTOCOL'", "]", "=", "'HTTP/1.0'", "environ", "[", "'wsgi.version'", "]", "=", "(", "1", ",", "0", ")", "environ", "[", "'wsgi.url_scheme'", "]", "=", "'http'", "environ", "[", "'wsgi.errors'", "]", "=", "sys", ".", "stderr", "environ", "[", "'wsgi.multithread'", "]", "=", "False", "environ", "[", "'wsgi.multiprocess'", "]", "=", "False", "environ", "[", "'wsgi.run_once'", "]", "=", "True", "# Convert the event provided by the AWS Lambda handler to a JSON", "# string that can be read as the body of a HTTP POST request.", "body", "=", "json", ".", "dumps", "(", "event", ")", "environ", "[", "'CONTENT_TYPE'", "]", "=", "'application/json'", "environ", "[", "'CONTENT_LENGTH'", "]", "=", "len", "(", "body", ")", "PY3", "=", "sys", ".", "version_info", "[", "0", "]", "==", "3", "if", "PY3", ":", "environ", "[", "'wsgi.input'", "]", "=", "io", ".", "StringIO", "(", "body", ")", "else", ":", "environ", "[", "'wsgi.input'", "]", "=", "io", ".", "BytesIO", "(", "body", ")", "# Start response is a required callback that must be passed when", "# the application is invoked. It is used to set HTTP status and", "# headers. Read the WSGI spec for details (PEP3333).", "headers", "=", "[", "]", "def", "start_response", "(", "status", ",", "response_headers", ",", "_exc_info", "=", "None", ")", ":", "headers", "[", ":", "]", "=", "[", "status", ",", "response_headers", "]", "# Invoke the actual Flask application providing our environment,", "# with our Alexa event as the body of the HTTP request, as well", "# as the callback function above. The result will be an iterator", "# that provides a serialized JSON string for our Alexa response.", "result", "=", "self", ".", "app", "(", "environ", ",", "start_response", ")", "try", ":", "if", "not", "headers", ":", "raise", "AssertionError", "(", "\"start_response() not called by WSGI app\"", ")", "output", "=", "b\"\"", ".", "join", "(", "result", ")", "if", "not", "headers", "[", "0", "]", ".", "startswith", "(", "\"2\"", ")", ":", "raise", "AssertionError", "(", "\"Non-2xx from app: hdrs={}, body={}\"", ".", "format", "(", "headers", ",", "output", ")", ")", "# The Lambda handler expects a Python object that can be", "# serialized as JSON, so we need to take the already serialized", "# JSON and deserialize it.", "return", "json", ".", "loads", "(", "output", ")", "finally", ":", "# Per the WSGI spec, we need to invoke the close method if it", "# is implemented on the result object.", "if", "hasattr", "(", "result", ",", "'close'", ")", ":", "result", ".", "close", "(", ")" ]
Invoke the Flask Ask application from an AWS Lambda function handler. Use this method to service AWS Lambda requests from a custom Alexa skill. This method will invoke your Flask application providing a WSGI-compatible environment that wraps the original Alexa event provided to the AWS Lambda handler. Returns the output generated by a Flask Ask application, which should be used as the return value to the AWS Lambda handler function. Example usage: from flask import Flask from flask_ask import Ask, statement app = Flask(__name__) ask = Ask(app, '/') # This function name is what you defined when you create an # AWS Lambda function. By default, AWS calls this function # lambda_handler. def lambda_handler(event, _context): return ask.run_aws_lambda(event) @ask.intent('HelloIntent') def hello(firstname): speech_text = "Hello %s" % firstname return statement(speech_text).simple_card('Hello', speech_text)
[ "Invoke", "the", "Flask", "Ask", "application", "from", "an", "AWS", "Lambda", "function", "handler", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L585-L683
228,283
johnwheeler/flask-ask
flask_ask/core.py
Ask._parse_timestamp
def _parse_timestamp(timestamp): """ Parse a given timestamp value, raising ValueError if None or Flasey """ if timestamp: try: return aniso8601.parse_datetime(timestamp) except AttributeError: # raised by aniso8601 if raw_timestamp is not valid string # in ISO8601 format try: return datetime.utcfromtimestamp(timestamp) except: # relax the timestamp a bit in case it was sent in millis return datetime.utcfromtimestamp(timestamp/1000) raise ValueError('Invalid timestamp value! Cannot parse from either ISO8601 string or UTC timestamp.')
python
def _parse_timestamp(timestamp): """ Parse a given timestamp value, raising ValueError if None or Flasey """ if timestamp: try: return aniso8601.parse_datetime(timestamp) except AttributeError: # raised by aniso8601 if raw_timestamp is not valid string # in ISO8601 format try: return datetime.utcfromtimestamp(timestamp) except: # relax the timestamp a bit in case it was sent in millis return datetime.utcfromtimestamp(timestamp/1000) raise ValueError('Invalid timestamp value! Cannot parse from either ISO8601 string or UTC timestamp.')
[ "def", "_parse_timestamp", "(", "timestamp", ")", ":", "if", "timestamp", ":", "try", ":", "return", "aniso8601", ".", "parse_datetime", "(", "timestamp", ")", "except", "AttributeError", ":", "# raised by aniso8601 if raw_timestamp is not valid string", "# in ISO8601 format", "try", ":", "return", "datetime", ".", "utcfromtimestamp", "(", "timestamp", ")", "except", ":", "# relax the timestamp a bit in case it was sent in millis", "return", "datetime", ".", "utcfromtimestamp", "(", "timestamp", "/", "1000", ")", "raise", "ValueError", "(", "'Invalid timestamp value! Cannot parse from either ISO8601 string or UTC timestamp.'", ")" ]
Parse a given timestamp value, raising ValueError if None or Flasey
[ "Parse", "a", "given", "timestamp", "value", "raising", "ValueError", "if", "None", "or", "Flasey" ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L724-L740
228,284
johnwheeler/flask-ask
flask_ask/core.py
Ask._map_player_request_to_func
def _map_player_request_to_func(self, player_request_type): """Provides appropriate parameters to the on_playback functions.""" # calbacks for on_playback requests are optional view_func = self._intent_view_funcs.get(player_request_type, lambda: None) argspec = inspect.getargspec(view_func) arg_names = argspec.args arg_values = self._map_params_to_view_args(player_request_type, arg_names) return partial(view_func, *arg_values)
python
def _map_player_request_to_func(self, player_request_type): """Provides appropriate parameters to the on_playback functions.""" # calbacks for on_playback requests are optional view_func = self._intent_view_funcs.get(player_request_type, lambda: None) argspec = inspect.getargspec(view_func) arg_names = argspec.args arg_values = self._map_params_to_view_args(player_request_type, arg_names) return partial(view_func, *arg_values)
[ "def", "_map_player_request_to_func", "(", "self", ",", "player_request_type", ")", ":", "# calbacks for on_playback requests are optional", "view_func", "=", "self", ".", "_intent_view_funcs", ".", "get", "(", "player_request_type", ",", "lambda", ":", "None", ")", "argspec", "=", "inspect", ".", "getargspec", "(", "view_func", ")", "arg_names", "=", "argspec", ".", "args", "arg_values", "=", "self", ".", "_map_params_to_view_args", "(", "player_request_type", ",", "arg_names", ")", "return", "partial", "(", "view_func", ",", "*", "arg_values", ")" ]
Provides appropriate parameters to the on_playback functions.
[ "Provides", "appropriate", "parameters", "to", "the", "on_playback", "functions", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L842-L851
228,285
johnwheeler/flask-ask
flask_ask/core.py
Ask._map_purchase_request_to_func
def _map_purchase_request_to_func(self, purchase_request_type): """Provides appropriate parameters to the on_purchase functions.""" if purchase_request_type in self._intent_view_funcs: view_func = self._intent_view_funcs[purchase_request_type] else: raise NotImplementedError('Request type "{}" not found and no default view specified.'.format(purchase_request_type)) argspec = inspect.getargspec(view_func) arg_names = argspec.args arg_values = self._map_params_to_view_args(purchase_request_type, arg_names) print('_map_purchase_request_to_func', arg_names, arg_values, view_func, purchase_request_type) return partial(view_func, *arg_values)
python
def _map_purchase_request_to_func(self, purchase_request_type): """Provides appropriate parameters to the on_purchase functions.""" if purchase_request_type in self._intent_view_funcs: view_func = self._intent_view_funcs[purchase_request_type] else: raise NotImplementedError('Request type "{}" not found and no default view specified.'.format(purchase_request_type)) argspec = inspect.getargspec(view_func) arg_names = argspec.args arg_values = self._map_params_to_view_args(purchase_request_type, arg_names) print('_map_purchase_request_to_func', arg_names, arg_values, view_func, purchase_request_type) return partial(view_func, *arg_values)
[ "def", "_map_purchase_request_to_func", "(", "self", ",", "purchase_request_type", ")", ":", "if", "purchase_request_type", "in", "self", ".", "_intent_view_funcs", ":", "view_func", "=", "self", ".", "_intent_view_funcs", "[", "purchase_request_type", "]", "else", ":", "raise", "NotImplementedError", "(", "'Request type \"{}\" not found and no default view specified.'", ".", "format", "(", "purchase_request_type", ")", ")", "argspec", "=", "inspect", ".", "getargspec", "(", "view_func", ")", "arg_names", "=", "argspec", ".", "args", "arg_values", "=", "self", ".", "_map_params_to_view_args", "(", "purchase_request_type", ",", "arg_names", ")", "print", "(", "'_map_purchase_request_to_func'", ",", "arg_names", ",", "arg_values", ",", "view_func", ",", "purchase_request_type", ")", "return", "partial", "(", "view_func", ",", "*", "arg_values", ")" ]
Provides appropriate parameters to the on_purchase functions.
[ "Provides", "appropriate", "parameters", "to", "the", "on_purchase", "functions", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/core.py#L853-L866
228,286
johnwheeler/flask-ask
flask_ask/cache.py
push_stream
def push_stream(cache, user_id, stream): """ Push a stream onto the stream stack in cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :param stream: stream object to push onto stack :return: True on successful update, False if failed to update, None if invalid input was given """ stack = cache.get(user_id) if stack is None: stack = [] if stream: stack.append(stream) return cache.set(user_id, stack) return None
python
def push_stream(cache, user_id, stream): """ Push a stream onto the stream stack in cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :param stream: stream object to push onto stack :return: True on successful update, False if failed to update, None if invalid input was given """ stack = cache.get(user_id) if stack is None: stack = [] if stream: stack.append(stream) return cache.set(user_id, stack) return None
[ "def", "push_stream", "(", "cache", ",", "user_id", ",", "stream", ")", ":", "stack", "=", "cache", ".", "get", "(", "user_id", ")", "if", "stack", "is", "None", ":", "stack", "=", "[", "]", "if", "stream", ":", "stack", ".", "append", "(", "stream", ")", "return", "cache", ".", "set", "(", "user_id", ",", "stack", ")", "return", "None" ]
Push a stream onto the stream stack in cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :param stream: stream object to push onto stack :return: True on successful update, False if failed to update, None if invalid input was given
[ "Push", "a", "stream", "onto", "the", "stream", "stack", "in", "cache", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/cache.py#L6-L24
228,287
johnwheeler/flask-ask
flask_ask/cache.py
pop_stream
def pop_stream(cache, user_id): """ Pop an item off the stack in the cache. If stack is empty after pop, it deletes the stack. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item from stack, otherwise None """ stack = cache.get(user_id) if stack is None: return None result = stack.pop() if len(stack) == 0: cache.delete(user_id) else: cache.set(user_id, stack) return result
python
def pop_stream(cache, user_id): """ Pop an item off the stack in the cache. If stack is empty after pop, it deletes the stack. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item from stack, otherwise None """ stack = cache.get(user_id) if stack is None: return None result = stack.pop() if len(stack) == 0: cache.delete(user_id) else: cache.set(user_id, stack) return result
[ "def", "pop_stream", "(", "cache", ",", "user_id", ")", ":", "stack", "=", "cache", ".", "get", "(", "user_id", ")", "if", "stack", "is", "None", ":", "return", "None", "result", "=", "stack", ".", "pop", "(", ")", "if", "len", "(", "stack", ")", "==", "0", ":", "cache", ".", "delete", "(", "user_id", ")", "else", ":", "cache", ".", "set", "(", "user_id", ",", "stack", ")", "return", "result" ]
Pop an item off the stack in the cache. If stack is empty after pop, it deletes the stack. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item from stack, otherwise None
[ "Pop", "an", "item", "off", "the", "stack", "in", "the", "cache", ".", "If", "stack", "is", "empty", "after", "pop", "it", "deletes", "the", "stack", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/cache.py#L27-L48
228,288
johnwheeler/flask-ask
flask_ask/cache.py
top_stream
def top_stream(cache, user_id): """ Peek at the top of the stack in the cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item in user's cached stack, otherwise None """ if not user_id: return None stack = cache.get(user_id) if stack is None: return None return stack.pop()
python
def top_stream(cache, user_id): """ Peek at the top of the stack in the cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item in user's cached stack, otherwise None """ if not user_id: return None stack = cache.get(user_id) if stack is None: return None return stack.pop()
[ "def", "top_stream", "(", "cache", ",", "user_id", ")", ":", "if", "not", "user_id", ":", "return", "None", "stack", "=", "cache", ".", "get", "(", "user_id", ")", "if", "stack", "is", "None", ":", "return", "None", "return", "stack", ".", "pop", "(", ")" ]
Peek at the top of the stack in the cache. :param cache: werkzeug BasicCache-like object :param user_id: id of user, used as key in cache :return: top item in user's cached stack, otherwise None
[ "Peek", "at", "the", "top", "of", "the", "stack", "in", "the", "cache", "." ]
fe407646ae404a8c90b363c86d5c4c201b6a5580
https://github.com/johnwheeler/flask-ask/blob/fe407646ae404a8c90b363c86d5c4c201b6a5580/flask_ask/cache.py#L65-L80
228,289
ecederstrand/exchangelib
exchangelib/transport.py
wrap
def wrap(content, version, account=None): """ Generate the necessary boilerplate XML for a raw SOAP request. The XML is specific to the server version. ExchangeImpersonation allows to act as the user we want to impersonate. """ envelope = create_element('s:Envelope', nsmap=ns_translation) header = create_element('s:Header') requestserverversion = create_element('t:RequestServerVersion', Version=version) header.append(requestserverversion) if account: if account.access_type == IMPERSONATION: exchangeimpersonation = create_element('t:ExchangeImpersonation') connectingsid = create_element('t:ConnectingSID') add_xml_child(connectingsid, 't:PrimarySmtpAddress', account.primary_smtp_address) exchangeimpersonation.append(connectingsid) header.append(exchangeimpersonation) timezonecontext = create_element('t:TimeZoneContext') timezonedefinition = create_element('t:TimeZoneDefinition', Id=account.default_timezone.ms_id) timezonecontext.append(timezonedefinition) header.append(timezonecontext) envelope.append(header) body = create_element('s:Body') body.append(content) envelope.append(body) return xml_to_str(envelope, encoding=DEFAULT_ENCODING, xml_declaration=True)
python
def wrap(content, version, account=None): """ Generate the necessary boilerplate XML for a raw SOAP request. The XML is specific to the server version. ExchangeImpersonation allows to act as the user we want to impersonate. """ envelope = create_element('s:Envelope', nsmap=ns_translation) header = create_element('s:Header') requestserverversion = create_element('t:RequestServerVersion', Version=version) header.append(requestserverversion) if account: if account.access_type == IMPERSONATION: exchangeimpersonation = create_element('t:ExchangeImpersonation') connectingsid = create_element('t:ConnectingSID') add_xml_child(connectingsid, 't:PrimarySmtpAddress', account.primary_smtp_address) exchangeimpersonation.append(connectingsid) header.append(exchangeimpersonation) timezonecontext = create_element('t:TimeZoneContext') timezonedefinition = create_element('t:TimeZoneDefinition', Id=account.default_timezone.ms_id) timezonecontext.append(timezonedefinition) header.append(timezonecontext) envelope.append(header) body = create_element('s:Body') body.append(content) envelope.append(body) return xml_to_str(envelope, encoding=DEFAULT_ENCODING, xml_declaration=True)
[ "def", "wrap", "(", "content", ",", "version", ",", "account", "=", "None", ")", ":", "envelope", "=", "create_element", "(", "'s:Envelope'", ",", "nsmap", "=", "ns_translation", ")", "header", "=", "create_element", "(", "'s:Header'", ")", "requestserverversion", "=", "create_element", "(", "'t:RequestServerVersion'", ",", "Version", "=", "version", ")", "header", ".", "append", "(", "requestserverversion", ")", "if", "account", ":", "if", "account", ".", "access_type", "==", "IMPERSONATION", ":", "exchangeimpersonation", "=", "create_element", "(", "'t:ExchangeImpersonation'", ")", "connectingsid", "=", "create_element", "(", "'t:ConnectingSID'", ")", "add_xml_child", "(", "connectingsid", ",", "'t:PrimarySmtpAddress'", ",", "account", ".", "primary_smtp_address", ")", "exchangeimpersonation", ".", "append", "(", "connectingsid", ")", "header", ".", "append", "(", "exchangeimpersonation", ")", "timezonecontext", "=", "create_element", "(", "'t:TimeZoneContext'", ")", "timezonedefinition", "=", "create_element", "(", "'t:TimeZoneDefinition'", ",", "Id", "=", "account", ".", "default_timezone", ".", "ms_id", ")", "timezonecontext", ".", "append", "(", "timezonedefinition", ")", "header", ".", "append", "(", "timezonecontext", ")", "envelope", ".", "append", "(", "header", ")", "body", "=", "create_element", "(", "'s:Body'", ")", "body", ".", "append", "(", "content", ")", "envelope", ".", "append", "(", "body", ")", "return", "xml_to_str", "(", "envelope", ",", "encoding", "=", "DEFAULT_ENCODING", ",", "xml_declaration", "=", "True", ")" ]
Generate the necessary boilerplate XML for a raw SOAP request. The XML is specific to the server version. ExchangeImpersonation allows to act as the user we want to impersonate.
[ "Generate", "the", "necessary", "boilerplate", "XML", "for", "a", "raw", "SOAP", "request", ".", "The", "XML", "is", "specific", "to", "the", "server", "version", ".", "ExchangeImpersonation", "allows", "to", "act", "as", "the", "user", "we", "want", "to", "impersonate", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/transport.py#L49-L73
228,290
ecederstrand/exchangelib
exchangelib/autodiscover.py
discover
def discover(email, credentials): """ Performs the autodiscover dance and returns the primary SMTP address of the account and a Protocol on success. The autodiscover and EWS server might not be the same, so we use a different Protocol to do the autodiscover request, and return a hopefully-cached Protocol to the callee. """ log.debug('Attempting autodiscover on email %s', email) if not isinstance(credentials, Credentials): raise ValueError("'credentials' %r must be a Credentials instance" % credentials) domain = get_domain(email) # We may be using multiple different credentials and changing our minds on TLS verification. This key combination # should be safe. autodiscover_key = (domain, credentials) # Use lock to guard against multiple threads competing to cache information log.debug('Waiting for _autodiscover_cache_lock') with _autodiscover_cache_lock: # Don't recurse while holding the lock! log.debug('_autodiscover_cache_lock acquired') if autodiscover_key in _autodiscover_cache: protocol = _autodiscover_cache[autodiscover_key] if not isinstance(protocol, AutodiscoverProtocol): raise ValueError('Unexpected autodiscover cache contents: %s' % protocol) log.debug('Cache hit for domain %s credentials %s: %s', domain, credentials, protocol.server) try: # This is the main path when the cache is primed return _autodiscover_quick(credentials=credentials, email=email, protocol=protocol) except AutoDiscoverFailed: # Autodiscover no longer works with this domain. Clear cache and try again after releasing the lock del _autodiscover_cache[autodiscover_key] except AutoDiscoverRedirect as e: log.debug('%s redirects to %s', email, e.redirect_email) if email.lower() == e.redirect_email.lower(): raise_from(AutoDiscoverCircularRedirect('Redirect to same email address: %s' % email), None) # Start over with the new email address after releasing the lock email = e.redirect_email else: log.debug('Cache miss for domain %s credentials %s', domain, credentials) log.debug('Cache contents: %s', _autodiscover_cache) try: # This eventually fills the cache in _autodiscover_hostname return _try_autodiscover(hostname=domain, credentials=credentials, email=email) except AutoDiscoverRedirect as e: if email.lower() == e.redirect_email.lower(): raise_from(AutoDiscoverCircularRedirect('Redirect to same email address: %s' % email), None) log.debug('%s redirects to %s', email, e.redirect_email) # Start over with the new email address after releasing the lock email = e.redirect_email log.debug('Released autodiscover_cache_lock') # We fell out of the with statement, so either cache was filled by someone else, or autodiscover redirected us to # another email address. Start over after releasing the lock. return discover(email=email, credentials=credentials)
python
def discover(email, credentials): """ Performs the autodiscover dance and returns the primary SMTP address of the account and a Protocol on success. The autodiscover and EWS server might not be the same, so we use a different Protocol to do the autodiscover request, and return a hopefully-cached Protocol to the callee. """ log.debug('Attempting autodiscover on email %s', email) if not isinstance(credentials, Credentials): raise ValueError("'credentials' %r must be a Credentials instance" % credentials) domain = get_domain(email) # We may be using multiple different credentials and changing our minds on TLS verification. This key combination # should be safe. autodiscover_key = (domain, credentials) # Use lock to guard against multiple threads competing to cache information log.debug('Waiting for _autodiscover_cache_lock') with _autodiscover_cache_lock: # Don't recurse while holding the lock! log.debug('_autodiscover_cache_lock acquired') if autodiscover_key in _autodiscover_cache: protocol = _autodiscover_cache[autodiscover_key] if not isinstance(protocol, AutodiscoverProtocol): raise ValueError('Unexpected autodiscover cache contents: %s' % protocol) log.debug('Cache hit for domain %s credentials %s: %s', domain, credentials, protocol.server) try: # This is the main path when the cache is primed return _autodiscover_quick(credentials=credentials, email=email, protocol=protocol) except AutoDiscoverFailed: # Autodiscover no longer works with this domain. Clear cache and try again after releasing the lock del _autodiscover_cache[autodiscover_key] except AutoDiscoverRedirect as e: log.debug('%s redirects to %s', email, e.redirect_email) if email.lower() == e.redirect_email.lower(): raise_from(AutoDiscoverCircularRedirect('Redirect to same email address: %s' % email), None) # Start over with the new email address after releasing the lock email = e.redirect_email else: log.debug('Cache miss for domain %s credentials %s', domain, credentials) log.debug('Cache contents: %s', _autodiscover_cache) try: # This eventually fills the cache in _autodiscover_hostname return _try_autodiscover(hostname=domain, credentials=credentials, email=email) except AutoDiscoverRedirect as e: if email.lower() == e.redirect_email.lower(): raise_from(AutoDiscoverCircularRedirect('Redirect to same email address: %s' % email), None) log.debug('%s redirects to %s', email, e.redirect_email) # Start over with the new email address after releasing the lock email = e.redirect_email log.debug('Released autodiscover_cache_lock') # We fell out of the with statement, so either cache was filled by someone else, or autodiscover redirected us to # another email address. Start over after releasing the lock. return discover(email=email, credentials=credentials)
[ "def", "discover", "(", "email", ",", "credentials", ")", ":", "log", ".", "debug", "(", "'Attempting autodiscover on email %s'", ",", "email", ")", "if", "not", "isinstance", "(", "credentials", ",", "Credentials", ")", ":", "raise", "ValueError", "(", "\"'credentials' %r must be a Credentials instance\"", "%", "credentials", ")", "domain", "=", "get_domain", "(", "email", ")", "# We may be using multiple different credentials and changing our minds on TLS verification. This key combination", "# should be safe.", "autodiscover_key", "=", "(", "domain", ",", "credentials", ")", "# Use lock to guard against multiple threads competing to cache information", "log", ".", "debug", "(", "'Waiting for _autodiscover_cache_lock'", ")", "with", "_autodiscover_cache_lock", ":", "# Don't recurse while holding the lock!", "log", ".", "debug", "(", "'_autodiscover_cache_lock acquired'", ")", "if", "autodiscover_key", "in", "_autodiscover_cache", ":", "protocol", "=", "_autodiscover_cache", "[", "autodiscover_key", "]", "if", "not", "isinstance", "(", "protocol", ",", "AutodiscoverProtocol", ")", ":", "raise", "ValueError", "(", "'Unexpected autodiscover cache contents: %s'", "%", "protocol", ")", "log", ".", "debug", "(", "'Cache hit for domain %s credentials %s: %s'", ",", "domain", ",", "credentials", ",", "protocol", ".", "server", ")", "try", ":", "# This is the main path when the cache is primed", "return", "_autodiscover_quick", "(", "credentials", "=", "credentials", ",", "email", "=", "email", ",", "protocol", "=", "protocol", ")", "except", "AutoDiscoverFailed", ":", "# Autodiscover no longer works with this domain. Clear cache and try again after releasing the lock", "del", "_autodiscover_cache", "[", "autodiscover_key", "]", "except", "AutoDiscoverRedirect", "as", "e", ":", "log", ".", "debug", "(", "'%s redirects to %s'", ",", "email", ",", "e", ".", "redirect_email", ")", "if", "email", ".", "lower", "(", ")", "==", "e", ".", "redirect_email", ".", "lower", "(", ")", ":", "raise_from", "(", "AutoDiscoverCircularRedirect", "(", "'Redirect to same email address: %s'", "%", "email", ")", ",", "None", ")", "# Start over with the new email address after releasing the lock", "email", "=", "e", ".", "redirect_email", "else", ":", "log", ".", "debug", "(", "'Cache miss for domain %s credentials %s'", ",", "domain", ",", "credentials", ")", "log", ".", "debug", "(", "'Cache contents: %s'", ",", "_autodiscover_cache", ")", "try", ":", "# This eventually fills the cache in _autodiscover_hostname", "return", "_try_autodiscover", "(", "hostname", "=", "domain", ",", "credentials", "=", "credentials", ",", "email", "=", "email", ")", "except", "AutoDiscoverRedirect", "as", "e", ":", "if", "email", ".", "lower", "(", ")", "==", "e", ".", "redirect_email", ".", "lower", "(", ")", ":", "raise_from", "(", "AutoDiscoverCircularRedirect", "(", "'Redirect to same email address: %s'", "%", "email", ")", ",", "None", ")", "log", ".", "debug", "(", "'%s redirects to %s'", ",", "email", ",", "e", ".", "redirect_email", ")", "# Start over with the new email address after releasing the lock", "email", "=", "e", ".", "redirect_email", "log", ".", "debug", "(", "'Released autodiscover_cache_lock'", ")", "# We fell out of the with statement, so either cache was filled by someone else, or autodiscover redirected us to", "# another email address. Start over after releasing the lock.", "return", "discover", "(", "email", "=", "email", ",", "credentials", "=", "credentials", ")" ]
Performs the autodiscover dance and returns the primary SMTP address of the account and a Protocol on success. The autodiscover and EWS server might not be the same, so we use a different Protocol to do the autodiscover request, and return a hopefully-cached Protocol to the callee.
[ "Performs", "the", "autodiscover", "dance", "and", "returns", "the", "primary", "SMTP", "address", "of", "the", "account", "and", "a", "Protocol", "on", "success", ".", "The", "autodiscover", "and", "EWS", "server", "might", "not", "be", "the", "same", "so", "we", "use", "a", "different", "Protocol", "to", "do", "the", "autodiscover", "request", "and", "return", "a", "hopefully", "-", "cached", "Protocol", "to", "the", "callee", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/autodiscover.py#L173-L223
228,291
ecederstrand/exchangelib
exchangelib/properties.py
TimeZone.to_server_timezone
def to_server_timezone(self, timezones, for_year): """Returns the Microsoft timezone ID corresponding to this timezone. There may not be a match at all, and there may be multiple matches. If so, we return a random timezone ID. :param timezones: A list of server timezones, as returned by list(account.protocol.get_timezones(return_full_timezone_data=True)) :param for_year: :return: A Microsoft timezone ID, as a string """ candidates = set() for tz_id, tz_name, tz_periods, tz_transitions, tz_transitions_groups in timezones: candidate = self.from_server_timezone(tz_periods, tz_transitions, tz_transitions_groups, for_year) if candidate == self: log.debug('Found exact candidate: %s (%s)', tz_id, tz_name) # We prefer this timezone over anything else. Return immediately. return tz_id # Reduce list based on base bias and standard / daylight bias values if candidate.bias != self.bias: continue if candidate.standard_time is None: if self.standard_time is not None: continue else: if self.standard_time is None: continue if candidate.standard_time.bias != self.standard_time.bias: continue if candidate.daylight_time is None: if self.daylight_time is not None: continue else: if self.daylight_time is None: continue if candidate.daylight_time.bias != self.daylight_time.bias: continue log.debug('Found candidate with matching biases: %s (%s)', tz_id, tz_name) candidates.add(tz_id) if not candidates: raise ValueError('No server timezones match this timezone definition') if len(candidates) == 1: log.info('Could not find an exact timezone match for %s. Selecting the best candidate', self) else: log.warning('Could not find an exact timezone match for %s. Selecting a random candidate', self) return candidates.pop()
python
def to_server_timezone(self, timezones, for_year): """Returns the Microsoft timezone ID corresponding to this timezone. There may not be a match at all, and there may be multiple matches. If so, we return a random timezone ID. :param timezones: A list of server timezones, as returned by list(account.protocol.get_timezones(return_full_timezone_data=True)) :param for_year: :return: A Microsoft timezone ID, as a string """ candidates = set() for tz_id, tz_name, tz_periods, tz_transitions, tz_transitions_groups in timezones: candidate = self.from_server_timezone(tz_periods, tz_transitions, tz_transitions_groups, for_year) if candidate == self: log.debug('Found exact candidate: %s (%s)', tz_id, tz_name) # We prefer this timezone over anything else. Return immediately. return tz_id # Reduce list based on base bias and standard / daylight bias values if candidate.bias != self.bias: continue if candidate.standard_time is None: if self.standard_time is not None: continue else: if self.standard_time is None: continue if candidate.standard_time.bias != self.standard_time.bias: continue if candidate.daylight_time is None: if self.daylight_time is not None: continue else: if self.daylight_time is None: continue if candidate.daylight_time.bias != self.daylight_time.bias: continue log.debug('Found candidate with matching biases: %s (%s)', tz_id, tz_name) candidates.add(tz_id) if not candidates: raise ValueError('No server timezones match this timezone definition') if len(candidates) == 1: log.info('Could not find an exact timezone match for %s. Selecting the best candidate', self) else: log.warning('Could not find an exact timezone match for %s. Selecting a random candidate', self) return candidates.pop()
[ "def", "to_server_timezone", "(", "self", ",", "timezones", ",", "for_year", ")", ":", "candidates", "=", "set", "(", ")", "for", "tz_id", ",", "tz_name", ",", "tz_periods", ",", "tz_transitions", ",", "tz_transitions_groups", "in", "timezones", ":", "candidate", "=", "self", ".", "from_server_timezone", "(", "tz_periods", ",", "tz_transitions", ",", "tz_transitions_groups", ",", "for_year", ")", "if", "candidate", "==", "self", ":", "log", ".", "debug", "(", "'Found exact candidate: %s (%s)'", ",", "tz_id", ",", "tz_name", ")", "# We prefer this timezone over anything else. Return immediately.", "return", "tz_id", "# Reduce list based on base bias and standard / daylight bias values", "if", "candidate", ".", "bias", "!=", "self", ".", "bias", ":", "continue", "if", "candidate", ".", "standard_time", "is", "None", ":", "if", "self", ".", "standard_time", "is", "not", "None", ":", "continue", "else", ":", "if", "self", ".", "standard_time", "is", "None", ":", "continue", "if", "candidate", ".", "standard_time", ".", "bias", "!=", "self", ".", "standard_time", ".", "bias", ":", "continue", "if", "candidate", ".", "daylight_time", "is", "None", ":", "if", "self", ".", "daylight_time", "is", "not", "None", ":", "continue", "else", ":", "if", "self", ".", "daylight_time", "is", "None", ":", "continue", "if", "candidate", ".", "daylight_time", ".", "bias", "!=", "self", ".", "daylight_time", ".", "bias", ":", "continue", "log", ".", "debug", "(", "'Found candidate with matching biases: %s (%s)'", ",", "tz_id", ",", "tz_name", ")", "candidates", ".", "add", "(", "tz_id", ")", "if", "not", "candidates", ":", "raise", "ValueError", "(", "'No server timezones match this timezone definition'", ")", "if", "len", "(", "candidates", ")", "==", "1", ":", "log", ".", "info", "(", "'Could not find an exact timezone match for %s. Selecting the best candidate'", ",", "self", ")", "else", ":", "log", ".", "warning", "(", "'Could not find an exact timezone match for %s. Selecting a random candidate'", ",", "self", ")", "return", "candidates", ".", "pop", "(", ")" ]
Returns the Microsoft timezone ID corresponding to this timezone. There may not be a match at all, and there may be multiple matches. If so, we return a random timezone ID. :param timezones: A list of server timezones, as returned by list(account.protocol.get_timezones(return_full_timezone_data=True)) :param for_year: :return: A Microsoft timezone ID, as a string
[ "Returns", "the", "Microsoft", "timezone", "ID", "corresponding", "to", "this", "timezone", ".", "There", "may", "not", "be", "a", "match", "at", "all", "and", "there", "may", "be", "multiple", "matches", ".", "If", "so", "we", "return", "a", "random", "timezone", "ID", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/properties.py#L560-L603
228,292
ecederstrand/exchangelib
exchangelib/protocol.py
BaseProtocol.decrease_poolsize
def decrease_poolsize(self): """Decreases the session pool size in response to error messages from the server requesting to rate-limit requests. We decrease by one session per call. """ # Take a single session from the pool and discard it. We need to protect this with a lock while we are changing # the pool size variable, to avoid race conditions. We must keep at least one session in the pool. if self._session_pool_size <= 1: raise SessionPoolMinSizeReached('Session pool size cannot be decreased further') with self._session_pool_lock: if self._session_pool_size <= 1: log.debug('Session pool size was decreased in another thread') return log.warning('Lowering session pool size from %s to %s', self._session_pool_size, self._session_pool_size - 1) self.get_session().close() self._session_pool_size -= 1
python
def decrease_poolsize(self): """Decreases the session pool size in response to error messages from the server requesting to rate-limit requests. We decrease by one session per call. """ # Take a single session from the pool and discard it. We need to protect this with a lock while we are changing # the pool size variable, to avoid race conditions. We must keep at least one session in the pool. if self._session_pool_size <= 1: raise SessionPoolMinSizeReached('Session pool size cannot be decreased further') with self._session_pool_lock: if self._session_pool_size <= 1: log.debug('Session pool size was decreased in another thread') return log.warning('Lowering session pool size from %s to %s', self._session_pool_size, self._session_pool_size - 1) self.get_session().close() self._session_pool_size -= 1
[ "def", "decrease_poolsize", "(", "self", ")", ":", "# Take a single session from the pool and discard it. We need to protect this with a lock while we are changing", "# the pool size variable, to avoid race conditions. We must keep at least one session in the pool.", "if", "self", ".", "_session_pool_size", "<=", "1", ":", "raise", "SessionPoolMinSizeReached", "(", "'Session pool size cannot be decreased further'", ")", "with", "self", ".", "_session_pool_lock", ":", "if", "self", ".", "_session_pool_size", "<=", "1", ":", "log", ".", "debug", "(", "'Session pool size was decreased in another thread'", ")", "return", "log", ".", "warning", "(", "'Lowering session pool size from %s to %s'", ",", "self", ".", "_session_pool_size", ",", "self", ".", "_session_pool_size", "-", "1", ")", "self", ".", "get_session", "(", ")", ".", "close", "(", ")", "self", ".", "_session_pool_size", "-=", "1" ]
Decreases the session pool size in response to error messages from the server requesting to rate-limit requests. We decrease by one session per call.
[ "Decreases", "the", "session", "pool", "size", "in", "response", "to", "error", "messages", "from", "the", "server", "requesting", "to", "rate", "-", "limit", "requests", ".", "We", "decrease", "by", "one", "session", "per", "call", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/protocol.py#L100-L115
228,293
ecederstrand/exchangelib
exchangelib/util.py
is_iterable
def is_iterable(value, generators_allowed=False): """ Checks if value is a list-like object. Don't match generators and generator-like objects here by default, because callers don't necessarily guarantee that they only iterate the value once. Take care to not match string types and bytes. :param value: any type of object :param generators_allowed: if True, generators will be treated as iterable :return: True or False """ if generators_allowed: if not isinstance(value, string_types + (bytes,)) and hasattr(value, '__iter__'): return True else: if isinstance(value, (tuple, list, set)): return True return False
python
def is_iterable(value, generators_allowed=False): """ Checks if value is a list-like object. Don't match generators and generator-like objects here by default, because callers don't necessarily guarantee that they only iterate the value once. Take care to not match string types and bytes. :param value: any type of object :param generators_allowed: if True, generators will be treated as iterable :return: True or False """ if generators_allowed: if not isinstance(value, string_types + (bytes,)) and hasattr(value, '__iter__'): return True else: if isinstance(value, (tuple, list, set)): return True return False
[ "def", "is_iterable", "(", "value", ",", "generators_allowed", "=", "False", ")", ":", "if", "generators_allowed", ":", "if", "not", "isinstance", "(", "value", ",", "string_types", "+", "(", "bytes", ",", ")", ")", "and", "hasattr", "(", "value", ",", "'__iter__'", ")", ":", "return", "True", "else", ":", "if", "isinstance", "(", "value", ",", "(", "tuple", ",", "list", ",", "set", ")", ")", ":", "return", "True", "return", "False" ]
Checks if value is a list-like object. Don't match generators and generator-like objects here by default, because callers don't necessarily guarantee that they only iterate the value once. Take care to not match string types and bytes. :param value: any type of object :param generators_allowed: if True, generators will be treated as iterable :return: True or False
[ "Checks", "if", "value", "is", "a", "list", "-", "like", "object", ".", "Don", "t", "match", "generators", "and", "generator", "-", "like", "objects", "here", "by", "default", "because", "callers", "don", "t", "necessarily", "guarantee", "that", "they", "only", "iterate", "the", "value", "once", ".", "Take", "care", "to", "not", "match", "string", "types", "and", "bytes", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/util.py#L64-L80
228,294
ecederstrand/exchangelib
exchangelib/util.py
chunkify
def chunkify(iterable, chunksize): """ Splits an iterable into chunks of size ``chunksize``. The last chunk may be smaller than ``chunksize``. """ from .queryset import QuerySet if hasattr(iterable, '__getitem__') and not isinstance(iterable, QuerySet): # tuple, list. QuerySet has __getitem__ but that evaluates the entire query greedily. We don't want that here. for i in range(0, len(iterable), chunksize): yield iterable[i:i + chunksize] else: # generator, set, map, QuerySet chunk = [] for i in iterable: chunk.append(i) if len(chunk) == chunksize: yield chunk chunk = [] if chunk: yield chunk
python
def chunkify(iterable, chunksize): """ Splits an iterable into chunks of size ``chunksize``. The last chunk may be smaller than ``chunksize``. """ from .queryset import QuerySet if hasattr(iterable, '__getitem__') and not isinstance(iterable, QuerySet): # tuple, list. QuerySet has __getitem__ but that evaluates the entire query greedily. We don't want that here. for i in range(0, len(iterable), chunksize): yield iterable[i:i + chunksize] else: # generator, set, map, QuerySet chunk = [] for i in iterable: chunk.append(i) if len(chunk) == chunksize: yield chunk chunk = [] if chunk: yield chunk
[ "def", "chunkify", "(", "iterable", ",", "chunksize", ")", ":", "from", ".", "queryset", "import", "QuerySet", "if", "hasattr", "(", "iterable", ",", "'__getitem__'", ")", "and", "not", "isinstance", "(", "iterable", ",", "QuerySet", ")", ":", "# tuple, list. QuerySet has __getitem__ but that evaluates the entire query greedily. We don't want that here.", "for", "i", "in", "range", "(", "0", ",", "len", "(", "iterable", ")", ",", "chunksize", ")", ":", "yield", "iterable", "[", "i", ":", "i", "+", "chunksize", "]", "else", ":", "# generator, set, map, QuerySet", "chunk", "=", "[", "]", "for", "i", "in", "iterable", ":", "chunk", ".", "append", "(", "i", ")", "if", "len", "(", "chunk", ")", "==", "chunksize", ":", "yield", "chunk", "chunk", "=", "[", "]", "if", "chunk", ":", "yield", "chunk" ]
Splits an iterable into chunks of size ``chunksize``. The last chunk may be smaller than ``chunksize``.
[ "Splits", "an", "iterable", "into", "chunks", "of", "size", "chunksize", ".", "The", "last", "chunk", "may", "be", "smaller", "than", "chunksize", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/util.py#L83-L101
228,295
ecederstrand/exchangelib
exchangelib/util.py
peek
def peek(iterable): """ Checks if an iterable is empty and returns status and the rewinded iterable """ from .queryset import QuerySet if isinstance(iterable, QuerySet): # QuerySet has __len__ but that evaluates the entire query greedily. We don't want that here. Instead, peek() # should be called on QuerySet.iterator() raise ValueError('Cannot peek on a QuerySet') if hasattr(iterable, '__len__'): # tuple, list, set return not iterable, iterable # generator try: first = next(iterable) except StopIteration: return True, iterable # We can't rewind a generator. Instead, chain the first element and the rest of the generator return False, itertools.chain([first], iterable)
python
def peek(iterable): """ Checks if an iterable is empty and returns status and the rewinded iterable """ from .queryset import QuerySet if isinstance(iterable, QuerySet): # QuerySet has __len__ but that evaluates the entire query greedily. We don't want that here. Instead, peek() # should be called on QuerySet.iterator() raise ValueError('Cannot peek on a QuerySet') if hasattr(iterable, '__len__'): # tuple, list, set return not iterable, iterable # generator try: first = next(iterable) except StopIteration: return True, iterable # We can't rewind a generator. Instead, chain the first element and the rest of the generator return False, itertools.chain([first], iterable)
[ "def", "peek", "(", "iterable", ")", ":", "from", ".", "queryset", "import", "QuerySet", "if", "isinstance", "(", "iterable", ",", "QuerySet", ")", ":", "# QuerySet has __len__ but that evaluates the entire query greedily. We don't want that here. Instead, peek()", "# should be called on QuerySet.iterator()", "raise", "ValueError", "(", "'Cannot peek on a QuerySet'", ")", "if", "hasattr", "(", "iterable", ",", "'__len__'", ")", ":", "# tuple, list, set", "return", "not", "iterable", ",", "iterable", "# generator", "try", ":", "first", "=", "next", "(", "iterable", ")", "except", "StopIteration", ":", "return", "True", ",", "iterable", "# We can't rewind a generator. Instead, chain the first element and the rest of the generator", "return", "False", ",", "itertools", ".", "chain", "(", "[", "first", "]", ",", "iterable", ")" ]
Checks if an iterable is empty and returns status and the rewinded iterable
[ "Checks", "if", "an", "iterable", "is", "empty", "and", "returns", "status", "and", "the", "rewinded", "iterable" ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/util.py#L104-L122
228,296
ecederstrand/exchangelib
exchangelib/util.py
xml_to_str
def xml_to_str(tree, encoding=None, xml_declaration=False): """Serialize an XML tree. Returns unicode if 'encoding' is None. Otherwise, we return encoded 'bytes'.""" if xml_declaration and not encoding: raise ValueError("'xml_declaration' is not supported when 'encoding' is None") if encoding: return tostring(tree, encoding=encoding, xml_declaration=True) return tostring(tree, encoding=text_type, xml_declaration=False)
python
def xml_to_str(tree, encoding=None, xml_declaration=False): """Serialize an XML tree. Returns unicode if 'encoding' is None. Otherwise, we return encoded 'bytes'.""" if xml_declaration and not encoding: raise ValueError("'xml_declaration' is not supported when 'encoding' is None") if encoding: return tostring(tree, encoding=encoding, xml_declaration=True) return tostring(tree, encoding=text_type, xml_declaration=False)
[ "def", "xml_to_str", "(", "tree", ",", "encoding", "=", "None", ",", "xml_declaration", "=", "False", ")", ":", "if", "xml_declaration", "and", "not", "encoding", ":", "raise", "ValueError", "(", "\"'xml_declaration' is not supported when 'encoding' is None\"", ")", "if", "encoding", ":", "return", "tostring", "(", "tree", ",", "encoding", "=", "encoding", ",", "xml_declaration", "=", "True", ")", "return", "tostring", "(", "tree", ",", "encoding", "=", "text_type", ",", "xml_declaration", "=", "False", ")" ]
Serialize an XML tree. Returns unicode if 'encoding' is None. Otherwise, we return encoded 'bytes'.
[ "Serialize", "an", "XML", "tree", ".", "Returns", "unicode", "if", "encoding", "is", "None", ".", "Otherwise", "we", "return", "encoded", "bytes", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/util.py#L125-L131
228,297
ecederstrand/exchangelib
exchangelib/util.py
is_xml
def is_xml(text): """ Helper function. Lightweight test if response is an XML doc """ # BOM_UTF8 is an UTF-8 byte order mark which may precede the XML from an Exchange server bom_len = len(BOM_UTF8) if text[:bom_len] == BOM_UTF8: return text[bom_len:bom_len + 5] == b'<?xml' return text[:5] == b'<?xml'
python
def is_xml(text): """ Helper function. Lightweight test if response is an XML doc """ # BOM_UTF8 is an UTF-8 byte order mark which may precede the XML from an Exchange server bom_len = len(BOM_UTF8) if text[:bom_len] == BOM_UTF8: return text[bom_len:bom_len + 5] == b'<?xml' return text[:5] == b'<?xml'
[ "def", "is_xml", "(", "text", ")", ":", "# BOM_UTF8 is an UTF-8 byte order mark which may precede the XML from an Exchange server", "bom_len", "=", "len", "(", "BOM_UTF8", ")", "if", "text", "[", ":", "bom_len", "]", "==", "BOM_UTF8", ":", "return", "text", "[", "bom_len", ":", "bom_len", "+", "5", "]", "==", "b'<?xml'", "return", "text", "[", ":", "5", "]", "==", "b'<?xml'" ]
Helper function. Lightweight test if response is an XML doc
[ "Helper", "function", ".", "Lightweight", "test", "if", "response", "is", "an", "XML", "doc" ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/util.py#L391-L399
228,298
ecederstrand/exchangelib
exchangelib/services.py
GetItem.call
def call(self, items, additional_fields, shape): """ Returns all items in an account that correspond to a list of ID's, in stable order. :param items: a list of (id, changekey) tuples or Item objects :param additional_fields: the extra fields that should be returned with the item, as FieldPath objects :param shape: The shape of returned objects :return: XML elements for the items, in stable order """ return self._pool_requests(payload_func=self.get_payload, **dict( items=items, additional_fields=additional_fields, shape=shape, ))
python
def call(self, items, additional_fields, shape): """ Returns all items in an account that correspond to a list of ID's, in stable order. :param items: a list of (id, changekey) tuples or Item objects :param additional_fields: the extra fields that should be returned with the item, as FieldPath objects :param shape: The shape of returned objects :return: XML elements for the items, in stable order """ return self._pool_requests(payload_func=self.get_payload, **dict( items=items, additional_fields=additional_fields, shape=shape, ))
[ "def", "call", "(", "self", ",", "items", ",", "additional_fields", ",", "shape", ")", ":", "return", "self", ".", "_pool_requests", "(", "payload_func", "=", "self", ".", "get_payload", ",", "*", "*", "dict", "(", "items", "=", "items", ",", "additional_fields", "=", "additional_fields", ",", "shape", "=", "shape", ",", ")", ")" ]
Returns all items in an account that correspond to a list of ID's, in stable order. :param items: a list of (id, changekey) tuples or Item objects :param additional_fields: the extra fields that should be returned with the item, as FieldPath objects :param shape: The shape of returned objects :return: XML elements for the items, in stable order
[ "Returns", "all", "items", "in", "an", "account", "that", "correspond", "to", "a", "list", "of", "ID", "s", "in", "stable", "order", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/services.py#L689-L702
228,299
ecederstrand/exchangelib
exchangelib/services.py
FindFolder.call
def call(self, additional_fields, restriction, shape, depth, max_items, offset): """ Find subfolders of a folder. :param additional_fields: the extra fields that should be returned with the folder, as FieldPath objects :param shape: The set of attributes to return :param depth: How deep in the folder structure to search for folders :param max_items: The maximum number of items to return :param offset: the offset relative to the first item in the item collection. Usually 0. :return: XML elements for the matching folders """ from .folders import Folder roots = {f.root for f in self.folders} if len(roots) != 1: raise ValueError('FindFolder must be called with folders in the same root hierarchy (%r)' % roots) root = roots.pop() for elem in self._paged_call(payload_func=self.get_payload, max_items=max_items, **dict( additional_fields=additional_fields, restriction=restriction, shape=shape, depth=depth, page_size=self.chunk_size, offset=offset, )): if isinstance(elem, Exception): yield elem continue yield Folder.from_xml(elem=elem, root=root)
python
def call(self, additional_fields, restriction, shape, depth, max_items, offset): """ Find subfolders of a folder. :param additional_fields: the extra fields that should be returned with the folder, as FieldPath objects :param shape: The set of attributes to return :param depth: How deep in the folder structure to search for folders :param max_items: The maximum number of items to return :param offset: the offset relative to the first item in the item collection. Usually 0. :return: XML elements for the matching folders """ from .folders import Folder roots = {f.root for f in self.folders} if len(roots) != 1: raise ValueError('FindFolder must be called with folders in the same root hierarchy (%r)' % roots) root = roots.pop() for elem in self._paged_call(payload_func=self.get_payload, max_items=max_items, **dict( additional_fields=additional_fields, restriction=restriction, shape=shape, depth=depth, page_size=self.chunk_size, offset=offset, )): if isinstance(elem, Exception): yield elem continue yield Folder.from_xml(elem=elem, root=root)
[ "def", "call", "(", "self", ",", "additional_fields", ",", "restriction", ",", "shape", ",", "depth", ",", "max_items", ",", "offset", ")", ":", "from", ".", "folders", "import", "Folder", "roots", "=", "{", "f", ".", "root", "for", "f", "in", "self", ".", "folders", "}", "if", "len", "(", "roots", ")", "!=", "1", ":", "raise", "ValueError", "(", "'FindFolder must be called with folders in the same root hierarchy (%r)'", "%", "roots", ")", "root", "=", "roots", ".", "pop", "(", ")", "for", "elem", "in", "self", ".", "_paged_call", "(", "payload_func", "=", "self", ".", "get_payload", ",", "max_items", "=", "max_items", ",", "*", "*", "dict", "(", "additional_fields", "=", "additional_fields", ",", "restriction", "=", "restriction", ",", "shape", "=", "shape", ",", "depth", "=", "depth", ",", "page_size", "=", "self", ".", "chunk_size", ",", "offset", "=", "offset", ",", ")", ")", ":", "if", "isinstance", "(", "elem", ",", "Exception", ")", ":", "yield", "elem", "continue", "yield", "Folder", ".", "from_xml", "(", "elem", "=", "elem", ",", "root", "=", "root", ")" ]
Find subfolders of a folder. :param additional_fields: the extra fields that should be returned with the folder, as FieldPath objects :param shape: The set of attributes to return :param depth: How deep in the folder structure to search for folders :param max_items: The maximum number of items to return :param offset: the offset relative to the first item in the item collection. Usually 0. :return: XML elements for the matching folders
[ "Find", "subfolders", "of", "a", "folder", "." ]
736347b337c239fcd6d592db5b29e819f753c1ba
https://github.com/ecederstrand/exchangelib/blob/736347b337c239fcd6d592db5b29e819f753c1ba/exchangelib/services.py#L1067-L1094