def test_log_completed_trial_skip_storage_access() -> None: study = optuna.multi_objective.create_study(["minimize", "maximize"]) new_trial_id = study._study._storage.create_new_trial( study._study._study_id) trial = optuna.Trial(study._study, new_trial_id) storage = study._storage with patch.object(storage, "get_trial", wraps=storage.get_trial) as mock_object: optuna.multi_objective.study._log_completed_trial(study, trial, 1.0) # Trial.params and MultiObjectiveTrial._get_values access storage. assert mock_object.call_count == 2 optuna.logging.set_verbosity(optuna.logging.WARNING) with patch.object(storage, "get_trial", wraps=storage.get_trial) as mock_object: optuna.multi_objective.study._log_completed_trial(study, trial, 1.0) assert mock_object.call_count == 0 optuna.logging.set_verbosity(optuna.logging.DEBUG) with patch.object(storage, "get_trial", wraps=storage.get_trial) as mock_object: optuna.multi_objective.study._log_completed_trial(study, trial, 1.0) assert mock_object.call_count == 2
def test_log_completed_trial_skip_storage_access() -> None: study = optuna.create_study() # Create a trial to retrieve it as the `study.best_trial`. study.optimize(lambda _: 0.0, n_trials=1) trial = optuna.Trial(study, study._storage.create_new_trial(study._study_id)) storage = study._storage with patch.object(storage, "get_best_trial", wraps=storage.get_best_trial) as mock_object: study._log_completed_trial(trial, 1.0) # Trial.best_trial and Trial.best_params access storage. assert mock_object.call_count == 2 optuna.logging.set_verbosity(optuna.logging.WARNING) with patch.object(storage, "get_best_trial", wraps=storage.get_best_trial) as mock_object: study._log_completed_trial(trial, 1.0) assert mock_object.call_count == 0 optuna.logging.set_verbosity(optuna.logging.DEBUG) with patch.object(storage, "get_best_trial", wraps=storage.get_best_trial) as mock_object: study._log_completed_trial(trial, 1.0) assert mock_object.call_count == 2
def test_prepare_params(self): study = optuna.study.create_study() study.enqueue_trial({}) trial = optuna.Trial(study, 0) params_file = r"../autolearn\params\xgboost.yml" params = utils.read_yaml(params_file) params = autolearn._Objective._eval_params(trial, params) assert isinstance(params, dict)