ctlearn_optimizer.common

Module Contents

ctlearn_optimizer.common.authkey = b'1234'[source]
ctlearn_optimizer.common.set_value(dictionary, value, *keys)[source]

Modify the value the keys point to in a nested dictionary.

The dictionary can be a nested dictionary containing lists, these lists can also contain nested dictionaries, and so on. The keys list can contain strings (which refer dictionary keys) and integers (which refer list indices). Dictionary cannot be empty.

Parameters
  • dictionary (dict) – dictionary that contais the key-value pair the user wishes to modify.

  • value (int, float, string) – value to set.

  • keys (list) – list of keys containing strings and integers.

Returns

dict – modified dictionary.

Raises

TypeError – if type(dictionary) is not dict.

Example:

dictionary = {'a':[0,{'b':1},0]}
value = 2
keys = ['a', 1, 'b']
set_value(dictionary, value, *keys) = {'a':[0,{'b':2},0]}
ctlearn_optimizer.common.create_nested_item(dictionary, *keys)[source]

Create an empty item with specific keys and positions in a dictionary.

The dictionary can be a nested dictionary containing lists, these lists can also contain nested dictionaries, and so on. The keys list can contain string (which refer dictionary keys) and integers (which refer list indices). The dictionary may or may be not empty.

Parameters
  • dictionary (dict) – dictionary to modify.

  • keys (list) – list of keys containing strings and integers.

Returns

dict – modified dictionary.

Raises

TypeError – if type(dictionary) is not dict.

Example:

dictionary = {}
keys = ['a', 'b', 1, 'c', 2 , 'd']
create_nested_item(dictionary, *keys) =
    {'a': {'b': [0, {'c': [0, 0, {'d': {}}]}]}}
ctlearn_optimizer.common.auxiliar_modify_params(self, hyperparams)[source]

Modify the values of the hyperparameters in CTLearn configuration file.

This function also modifies the logging model_directory of CTLearn and the prediction_file_path.

Parameters
  • selfctlearn_optimizer.optimizer.Optimizer instance.

  • hyperparams (dict) – dictionary containing values of the hyperparameters.

ctlearn_optimizer.common.get_pred_metrics(self)[source]

Get CTLearn prediction metrics from the current CTLearn logging folder.

Parameters

selfctlearn_optimizer.optimizer.Optimizer instance.

Returns

dict – dictionary containing prediction set metrics to log to the optimization_results.csv file.

ctlearn_optimizer.common.get_val_metrics(self)[source]

Get CTLearn validation metrics from the current CTLearn logging folder.

Parameters

selfctlearn_optimizer.optimizer.Optimizer instance.

Returns

dict – dictionary containing validation set metrics to log to the optimization_results.csv file.

ctlearn_optimizer.common.set_basic_config(self)[source]

Set basic config and fixed hyperparameters in CTLearn config file.

Parameters

selfctlearn_optimizer.optimizer.Optimizer instance.

ctlearn_optimizer.common.train(self)[source]

Run a CTlearn model training.

Debug is set to False and log_to_file is set to True.

Parameters

selfctlearn_optimizer.optimizer.Optimizer instance.

ctlearn_optimizer.common.predict(self)[source]

Predict using a trained CTLearn model.

Debug is set to False and log_to_file is set to True.

Parameters

selfctlearn_optimizer.optimizer.Optimizer instance.

ctlearn_optimizer.common.modify_optimizable_params(self, hyperparams)[source]

Update CTLearn config file with new hyperparameters at each iteration.

This function takes the dictionary containing the values of the hyperparameters to optimize suggested by the optimizer, flattens and corrects the dictionary if required, then add the values of the dependent hyperparameters to the dictionary. Finally calls auxiliar_modify_params() to modify the hyperparameters.

Parameters
  • selfctlearn_optimizer.optimizer.Optimizer instance.

  • hyperparams (dict) – flat or nested dictionary containing the values of the hyperparameters to optimize suggested by the optimizer.

Returns

dict – flat dictionary containing the values of the dependent hyperparameters and hyperparameters to optimize.

ctlearn_optimizer.common.save(self)[source]

Save trials of the current run at the working folder as trials.pkl.

Currently, trial saving for only tree parzen estimators, random search or gaussian processes based optimization using Ray Tune is supported.

Raises

NotImplementedError – if self.optimization_type is genetic_algorithm.

ctlearn_optimizer.common.restore(self)[source]

Load trials.pkl of a previous run from the working_directory.

Currently, trial loading for only tree parzen_estimators, random search or gaussian processes based optimization using Ray Tune is supported.

Returns

skopt.optimizer.optimizer.Optimizer – optimizer provided from Skopt (only if self.optimization.type is gaussian_processes).

Raises

NotImplementedError – if self.optimization_type is genetic_algorithm.

ctlearn_optimizer.common.set_logger(log_path)[source]

Set up new logger writing to both log_path and stdout.

Ray Tune optimizator runs the objective function on a different Python process, so new loggers writing to the same file have to be created when necessary.

Parameters

log_path (str) – path to log file.

ctlearn_optimizer.common.optimization_results_logger(self, loss, hyperparams_dict, metrics_val, metrics_pred, run_time)[source]

Write loss, hyperparameters, metrics and run_time to the results file.

This function log the data to the optimization results file stored as optimization_results.csv at working_directory.

Parameters
  • selfctlearn_optimizer.optimizer.Optimizer instance.

  • loss (float) – value to optimize.

  • hyperparams_dict (dict) – values of the hyperparameters the user wishes to store.

  • metrics_val (dict) – values of the validation set metrics the user wishes to store.

  • metrics_pred (dict) – values of the prediction set metrics the user wishes to store.

  • run_time (float) – execution time the user wishes to store.

ctlearn_optimizer.common.ctlearn_objective(self, hyperparams)[source]

Evaluate a CTLearn model and return metric to optimize.

Train a CTLearn model and predict if necessary, get the metrics and log them to the optimization_results.csv file. Also save trials file for resuming training if it has been interrupted.

Parameters
  • selfctlearn_optimizer.optimizer.Optimizer instance.

  • hyperparams (dict) – values of the hyperparameters to evaluate suggested by the optimizer.

Returns

float – metric to optimize.