def setup_study(trial_num, _intermediate_values): # type: (int, List[List[float]]) -> Study _study = optuna.study.create_study(direction="minimize") trial_ids = [ _study._storage.create_new_trial(_study._study_id) for _ in range(trial_num) ] for step, values in enumerate(_intermediate_values): # Study does not have any trials. with pytest.raises(ValueError): _all_trials = _study._storage.get_all_trials(_study._study_id) _direction = _study._storage.get_study_direction( _study._study_id) percentile._get_percentile_intermediate_result_over_trials( _all_trials, _direction, step, 25) for i in range(trial_num): trial_id = trial_ids[i] value = values[i] _study._storage.set_trial_intermediate_value( trial_id, step, value) # Set trial states complete because this method ignores incomplete trials. for trial_id in trial_ids: _study._storage.set_trial_state(trial_id, TrialState.COMPLETE) return _study
def test_get_percentile_intermediate_result_over_trials(): # type: () -> None def setup_study(trial_num, _intermediate_values): # type: (int, List[List[float]]) -> Study _study = optuna.study.create_study(direction="minimize") trial_ids = [_study._storage.create_new_trial( _study.study_id) for _ in range(trial_num)] for step, values in enumerate(_intermediate_values): # Study does not have any trials. with pytest.raises(ValueError): _all_trials = _study._storage.get_all_trials(_study.study_id) _direction = _study._storage.get_study_direction(_study.study_id) percentile._get_percentile_intermediate_result_over_trials( _all_trials, _direction, step, 25) for i in range(trial_num): trial_id = trial_ids[i] value = values[i] _study._storage.set_trial_intermediate_value(trial_id, step, value) # Set trial states complete because this method ignores incomplete trials. for trial_id in trial_ids: _study._storage.set_trial_state(trial_id, TrialState.COMPLETE) return _study # Input value has no NaNs but float values (step=0). intermediate_values = [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]] study = setup_study(9, intermediate_values) all_trials = study._storage.get_all_trials(study.study_id) direction = study._storage.get_study_direction(study.study_id) assert 0.3 == percentile._get_percentile_intermediate_result_over_trials( all_trials, direction, 0, 25.0) # Input value has a float value and NaNs (step=1). intermediate_values.append([ 0.1, 0.2, 0.3, 0.4, 0.5, float('nan'), float('nan'), float('nan'), float('nan')]) study = setup_study(9, intermediate_values) all_trials = study._storage.get_all_trials(study.study_id) direction = study._storage.get_study_direction(study.study_id) assert 0.2 == percentile._get_percentile_intermediate_result_over_trials( all_trials, direction, 1, 25.0) # Input value has NaNs only (step=2). intermediate_values.append([ float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')]) study = setup_study(9, intermediate_values) all_trials = study._storage.get_all_trials(study.study_id) direction = study._storage.get_study_direction(study.study_id) assert math.isnan(percentile._get_percentile_intermediate_result_over_trials( all_trials, direction, 2, 75))