コード例 #1
0
ファイル: test_cli.py プロジェクト: rafaeldelrey/optuna
def test_study_optimize_command(options):
    # type: (List[str]) -> None

    with StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url,
                                                         config_path):
        storage = RDBStorage(storage_url)

        study_name = storage.get_study_name_from_id(storage.create_new_study())
        command = [
            'optuna', 'study', 'optimize', '--study', study_name, '--n-trials',
            '10', __file__, 'objective_func'
        ]
        command = _add_option(command, '--storage', storage_url, 'storage'
                              in options)
        command = _add_option(command, '--config', config_path, 'config'
                              in options)
        subprocess.check_call(command)

        study = optuna.load_study(storage=storage_url, study_name=study_name)
        assert len(study.trials) == 10
        assert 'x' in study.best_params

        # Check if a default value of study_name is stored in the storage.
        assert storage.get_study_name_from_id(
            study.study_id).startswith(DEFAULT_STUDY_NAME_PREFIX)
コード例 #2
0
ファイル: test_cli.py プロジェクト: rafaeldelrey/optuna
def test_create_study_command_with_skip_if_exists():
    # type: () -> None

    with StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url,
                                                         config_path):
        storage = RDBStorage(storage_url)
        study_name = 'test_study'

        # Create study with name.
        command = [
            'optuna', 'create-study', '--storage', storage_url, '--study-name',
            study_name
        ]
        study_name = str(subprocess.check_output(command).decode().strip())

        # Check if study_name is stored in the storage.
        study_id = storage.get_study_id_from_name(study_name)
        assert storage.get_study_name_from_id(study_id) == study_name

        # Try to create the same name study without `--skip-if-exists` flag (error).
        command = [
            'optuna', 'create-study', '--storage', storage_url, '--study-name',
            study_name
        ]
        with pytest.raises(subprocess.CalledProcessError):
            subprocess.check_output(command)

        # Try to create the same name study with `--skip-if-exists` flag (OK).
        command = [
            'optuna', 'create-study', '--storage', storage_url, '--study-name',
            study_name, '--skip-if-exists'
        ]
        study_name = str(subprocess.check_output(command).decode().strip())
        new_study_id = storage.get_study_id_from_name(study_name)
        assert study_id == new_study_id  # The existing study instance is reused.
コード例 #3
0
ファイル: test_cli.py プロジェクト: rafaeldelrey/optuna
def test_study_set_user_attr_command(options):
    # type: (List[str]) -> None

    with StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url,
                                                         config_path):
        storage = RDBStorage(storage_url)

        # Create study.
        study_name = storage.get_study_name_from_id(storage.create_new_study())

        base_command = [
            'optuna', 'study', 'set-user-attr', '--study', study_name
        ]
        base_command = _add_option(base_command, '--storage', storage_url,
                                   'storage' in options)
        base_command = _add_option(base_command, '--config', config_path,
                                   'config' in options)

        example_attrs = {'architecture': 'ResNet', 'baselen_score': '0.002'}
        for key, value in example_attrs.items():
            subprocess.check_call(base_command +
                                  ['--key', key, '--value', value])

        # Attrs should be stored in storage.
        study_id = storage.get_study_id_from_name(study_name)
        study_user_attrs = storage.get_study_user_attrs(study_id)
        assert len(study_user_attrs) == 2
        assert all(
            [study_user_attrs[k] == v for k, v in example_attrs.items()])
コード例 #4
0
ファイル: test_cli.py プロジェクト: x10-utils/optuna
def test_create_study_command_with_study_name():
    # type: () -> None

    with StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url, config_path):
        storage = RDBStorage(storage_url)
        study_name = 'test_study'

        # Create study with name.
        command = ['optuna', 'create-study', '--storage', storage_url, '--study-name', study_name]
        study_name = str(subprocess.check_output(command).decode().strip())

        # Check if study_name is stored in the storage.
        study_id = storage.get_study_id_from_name(study_name)
        assert storage.get_study_name_from_id(study_id) == study_name
コード例 #5
0
ファイル: test_cli.py プロジェクト: x10-utils/optuna
def test_dashboard_command(options):
    # type: (List[str]) -> None

    with \
            StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url, config_path), \
            tempfile.NamedTemporaryFile('r') as tf_report:

        storage = RDBStorage(storage_url)
        study_name = storage.get_study_name_from_id(storage.create_new_study_id())

        command = ['optuna', 'dashboard', '--study', study_name, '--out', tf_report.name]
        command = _add_option(command, '--storage', storage_url, 'storage' in options)
        command = _add_option(command, '--config', config_path, 'config' in options)
        subprocess.check_call(command)

        html = tf_report.read()
        assert '<body>' in html
        assert 'bokeh' in html
コード例 #6
0
def test_dashboard_command_with_allow_websocket_origin(origins):
    with \
            StorageConfigSupplier(TEST_CONFIG_TEMPLATE) as (storage_url, config_path), \
            tempfile.NamedTemporaryFile('r') as tf_report:

        storage = RDBStorage(storage_url)
        study_name = storage.get_study_name_from_id(
            storage.create_new_study_id())
        command = [
            'optuna', 'dashboard', '--study', study_name, '--out',
            tf_report.name, '--storage', storage_url
        ]
        for origin in origins:
            command.extend(['--allow-websocket-origin', origin])
        subprocess.check_call(command)

        html = tf_report.read()
        assert '<body>' in html
        assert 'bokeh' in html