Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_experiment_chunk_split() -> None:
chunk = core.BenchmarkChunk(name="repeated_basic", seed=12, repetitions=2)
chunks = chunk.split(2)
chunks = [chunks[0]] + chunks[1].split(3)
chained = [x[0] for x in itertools.chain.from_iterable(chunks)]
# check full order (everythink only once)
np.testing.assert_array_equal(chained, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
1, 7, 13, 19,
3, 9, 15,
5, 11, 17])
testing.assert_set_equal(chained, range(20))
assert chunks[0].id.endswith("_i0m2"), f"Wrong id {chunks[0].id}"
assert chunks[0].id[:-4] == chunk.id[:-4], "Id prefix should be inherited"
def test_selector(criteria: Any, expected: List[str]) -> None:
df = tools.Selector(index=["i1", "i2", "i3"], columns=["c1", "c2"])
for i, c in itertools.product(df.index, df.columns):
df.loc[i, c] = f"{i}-{c}"
df_select = df.select(**criteria)
df_drop = df.select_and_drop(**criteria)
# indices
testing.assert_set_equal(df_select.index, expected)
testing.assert_set_equal(df_drop.index, expected)
# columns
testing.assert_set_equal(df_select.columns, df)
testing.assert_set_equal(df_drop.columns, set(df_select.columns) - set(criteria))
# values
for i, c in itertools.product(df_select.index, df_select.columns):
assert df.loc[i, c] == f"{i}-{c}", "Erroneous values"
# instance
assert isinstance(df_select, tools.Selector)
assert isinstance(df_drop, tools.Selector)
def test_pruning() -> None:
archive = utils.Archive[utils.Value]()
for k in range(3):
value = utils.Value(float(k))
archive[(float(k),)] = value
value = utils.Value(1.)
value.add_evaluation(1.)
archive[(3.,)] = value
# pruning
pruning = utils.Pruning(min_len=1, max_len=3)
# 0 is best optimistic and average, and 3 is best pessimistic (variance=0)
with pytest.warns(UserWarning):
archive = pruning(archive)
testing.assert_set_equal([x[0] for x in archive.keys_as_array()], [0, 3], err_msg=f"Repetition #{k+1}")
# should not change anything this time
archive = pruning(archive)
testing.assert_set_equal([x[0] for x in archive.keys_as_array()], [0, 3], err_msg=f"Repetition #{k+1}")
def test_commandline_launch() -> None:
with tempfile.TemporaryDirectory() as folder:
output = Path(folder) / "benchmark_launch_test.csv"
# commandline test
CommandFunction(command=["python", "-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 = core.tools.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 instantiate_to_folder(self, outfolder: Union[Path, str], kwargs: Dict[str, Any]) -> None:
testing.assert_set_equal(kwargs, {x.name for x in self.placeholders}, err_msg="Wrong input parameters.")
outfolder = Path(outfolder).expanduser().absolute()
assert outfolder != self.folder, "Do not instantiate on same folder!"
symlink_folder_tree(self.folder, outfolder)
for file_func in self.file_functions:
inst_fp = outfolder / file_func.filepath.relative_to(self.folder)
os.remove(str(inst_fp)) # remove symlink to avoid writing in original dir
with inst_fp.open("w") as f:
f.write(file_func(**{x: y for x, y in kwargs.items() if x in file_func.parameters}))
Parameter
---------
column_s: str or Sequence[str]
a column name, or list of column names
Returns
-------
set
a set of values if the input was a column name, or a set of tuple of values
if the name was a list of columns
"""
if isinstance(column_s, str):
return set(self.loc[:, column_s]) # equivalent to df..unique()
elif isinstance(column_s, (list, tuple)):
testing.assert_set_equal(set(column_s) - set(self.columns), {}, err_msg="Unknown column(s)")
df = self.loc[:, column_s]
assert not df.isnull().values.any(), "Cannot work with NaN values"
return set(tuple(row) for row in df.itertuples(index=False))
else:
raise NotImplementedError("Only strings, lists and tuples are allowed")
def assert_equivalent(self, other: pd.DataFrame, err_msg: str = "") -> None:
"""Asserts that two selectors are equal, up to row and column permutations
Note
----
Use sparsely, since it is quite slow to test
"""
testing.assert_set_equal(other.columns, self.columns, f"Different columns\n{err_msg}")
np.testing.assert_equal(len(other), len(self), "Different number of rows\n{err_msg}")
other_df = other.loc[:, self.columns]
df_rows: List[List[Tuple[Any, ...]]] = [[], []]
for k, df in enumerate([self, other_df]):
for row in df.itertuples(index=False):
df_rows[k].append(tuple(row))
df_rows[k].sort()
for row1, row2 in zip(*df_rows):
np.testing.assert_array_equal(row1, row2, err_msg=err_msg)
def __call__(self, **kwargs: Any) -> str:
testing.assert_set_equal(kwargs, self.parameters, err_msg="Wrong input parameters.")
return Placeholder.sub(self._text, self.filepath.suffix, replacers=kwargs)