Beispiel #1
0
def test_commandline_launch() -> None:
    with tempfile.TemporaryDirectory() as folder:
        output = Path(folder) / "benchmark_launch_test.csv"
        # commandline test
        # TODO make it work on Windows!
        # TODO make it work again on the CI (Linux), this started failing with #630 for no reason
        with testing.skip_error_on_systems(FailedJobError, systems=("Windows", "Linux")):
            CommandFunction(
                command=[
                    sys.executable,
                    "-m",
                    "nevergrad.benchmark",
                    "additional_experiment",
                    "--cap_index",
                    "2",
                    "--num_workers",
                    "2",
                    "--output",
                    str(output),
                    "--imports",
                    str(Path(__file__).parent / "additional" / "example.py"),
                ]
            )()
        assert output.exists()
        df = utils.Selector.read_csv(str(output))
        testing.assert_set_equal(
            df.columns, DESCRIPTION_KEYS | {"offset"}
        )  # "offset" comes from the custom function
        np.testing.assert_equal(len(df), 2)
def test_folder_function() -> None:
    folder = Path(__file__).parent / "examples"
    func = instantiate.FolderFunction(str(folder), ["python", "examples/script.py"], clean_copy=True)
    with testing.skip_error_on_systems(OSError, systems=("Windows",)):
        output = func(value1=98, value2=12, string="plop")
    np.testing.assert_equal(output, 24)
    output = func(value1=98, value2=12, string="blublu")
    np.testing.assert_equal(output, 12)
def test_folder_instantiator(clean_copy: bool) -> None:
    path = Path(__file__).parent / "examples"
    ifolder = instantiate.FolderInstantiator(path, clean_copy=clean_copy)
    testing.printed_assert_equal(ifolder.placeholders, _EXPECTED)
    np.testing.assert_equal(len(ifolder.file_functions), 1)
    with testing.skip_error_on_systems(OSError, systems=("Windows", )):
        with ifolder.instantiate(value1=12, value2=110., string="") as tmp:
            with (tmp / "script.py").open("r") as f:
                lines = f.readlines()
    np.testing.assert_equal(lines[10], "value2 = 110.0\n")
Beispiel #4
0
def test_messaging_thread(num_iter: int, output: tp.Optional[int]) -> None:
    thread = recaster.MessagingThread(fake_caller)
    num_answers = 0
    while num_answers < num_iter:
        if thread.messages and not thread.messages[0].done:
            thread.messages[0].result = 3
            num_answers += 1
        time.sleep(0.001)
    with testing.skip_error_on_systems(AssertionError, systems=("Windows",)):  # TODO fix
        np.testing.assert_equal(thread.output, output)
def test_folder_function_doc() -> None:
    with testing.skip_error_on_systems(OSError, systems=("Windows", )):
        # DOC_INSTANTIATE_0
        import sys
        from pathlib import Path
        import nevergrad as ng
        from nevergrad.parametrization import FolderFunction

        # nevergrad/parametrization/examples contains a script
        example_folder = Path(
            ng.__file__).parent / "parametrization" / "examples"
        python = sys.executable
        command = [python, "examples/script.py"
                   ]  # command to run from right outside the provided folder
        # create a function from the folder
        func = FolderFunction(example_folder, command, clean_copy=True)

        # print the number of variables of the function:
        print(func.placeholders)
        # prints: [Placeholder('value1', 'this is a comment'), Placeholder('value2', None), Placeholder('string', None)]
        # and run it (the script prints 12 at the end)
        assert func(value1=2, value2=3, string="blublu") == 12.0
def test_symlink_folder_tree() -> None:
    path = Path(__file__).absolute().parents[1]
    assert path.name == "nevergrad"
    with tempfile.TemporaryDirectory() as folder:
        with testing.skip_error_on_systems(OSError, systems=("Windows", )):
            instantiate.symlink_folder_tree(path, folder)