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_sampler_values(self, name: str, seq1: List[float], seq2: List[float]) -> None:
budget = 4
np.random.seed(12)
sampler = samplers[name](3, budget)
samples = list(sampler)
for k, expected in enumerate([seq1, seq2]):
np.testing.assert_almost_equal(samples[k], expected, decimal=3)
np.testing.assert_raises(AssertionError, sampler) # budget is over
sampler.reinitialize()
samples2 = list(sampler)
testing.printed_assert_equal(samples2, samples)
def test_printed_assert_equal() -> None:
testing.printed_assert_equal(0, 0)
np.testing.assert_raises(AssertionError, testing.printed_assert_equal, 0, 1)
def test_instrumented_function() -> None:
ifunc = mvar.InstrumentedFunction(
_arg_return,
var.SoftmaxCategorical([1, 12]),
"constant",
var.Gaussian(0, 1, [2, 2]),
constkwarg="blublu",
plop=var.SoftmaxCategorical([3, 4]),
)
np.testing.assert_equal(ifunc.dimension, 8)
data = [-100.0, 100, 1, 2, 3, 4, 100, -100]
args, kwargs = ifunc(np.array(data))
testing.printed_assert_equal(args, [12, "constant", [[1, 2], [3, 4]]])
testing.printed_assert_equal(kwargs, {"constkwarg": "blublu", "plop": 3})
testing.printed_assert_equal(
ifunc.descriptors,
{
"dimension": 8,
"name": "_arg_return",
"function_class": "InstrumentedFunction",
"instrumentation": "SC(1,12|0),constant,G(0,1),constkwarg=blublu,plop=SC(3,4|0)",
},
)
print(ifunc.get_summary(data))
def check_maker(maker: Callable[[], Iterator[experiments.Experiment]]) -> None:
generators = [maker() for _ in range(2)]
# check 1 sample
sample = next(maker())
assert isinstance(sample, experiments.Experiment)
# check names, coherence and non-randomness
for k, (elem1, elem2) in enumerate(itertools.zip_longest(*generators)):
assert not elem1.is_incoherent, f"Incoherent settings should be filtered out from generator:\n{elem1}"
try:
assert elem1 == elem2 # much faster but lacks explicit message
except AssertionError:
testing.printed_assert_equal(
elem1.get_description(),
elem2.get_description(),
err_msg=f"Two paths on the generator differed (see element #{k})\n"
"Generators need to be deterministic in order to split the workload!",
def test_instrumented_function() -> None:
ifunc = mvar.InstrumentedFunction(
_arg_return,
var.SoftmaxCategorical([1, 12]),
"constant",
var.Gaussian(0, 1, [2, 2]),
constkwarg="blublu",
plop=var.SoftmaxCategorical([3, 4]),
)
np.testing.assert_equal(ifunc.dimension, 8)
data = [-100.0, 100, 1, 2, 3, 4, 100, -100]
args, kwargs = ifunc(np.array(data))
testing.printed_assert_equal(args, [12, "constant", [[1, 2], [3, 4]]])
testing.printed_assert_equal(kwargs, {"constkwarg": "blublu", "plop": 3})
testing.printed_assert_equal(
ifunc.descriptors,
{
"dimension": 8,
"name": "_arg_return",
"function_class": "InstrumentedFunction",
"instrumentation": "SC(1,12|0),constant,G(0,1),constkwarg=blublu,plop=SC(3,4|0)",
},
)
print(ifunc.get_summary(data))
def test_get_nash() -> None:
zeroptim = Zero(instrumentation=1, budget=4, num_workers=1)
for k in range(4):
array = (float(k),)
zeroptim.archive[array] = utils.Value(k)
zeroptim.archive[array].count += (4 - k)
nash = utils._get_nash(zeroptim)
testing.printed_assert_equal(nash, [((2,), 3), ((1,), 4), ((0,), 5)])
np.random.seed(12)
output = utils.sample_nash(zeroptim)
np.testing.assert_equal(output, (2,))
# check deterministic
data = np.array([0.0, 0, 0, 0, 0, 0])
total = 0
for _ in range(24):
total += instru.data_to_arguments(data, deterministic=True)[1]["b"]
np.testing.assert_equal(total, 0)
# check stochastic
for _ in range(24):
total += instru.data_to_arguments(data, deterministic=False)[1]["b"]
assert total != 0
# check duplicate
instru2 = mvar.Instrumentation(*instru.args, **instru.kwargs)
data = np.random.normal(0, 1, size=6)
testing.printed_assert_equal(instru2.data_to_arguments(data, deterministic=True), instru.data_to_arguments(data, deterministic=True))
# check naming
testing.printed_assert_equal("G(0,1),3,a=OD(0,1,2,3),b=SC(0,1,2,3|0)", instru.name)
testing.printed_assert_equal("blublu", instru.with_name("blublu").name)
def test_split_data(tokens: List[utils.Variable[Any]], data: List[float], expected: List[List[float]]) -> None:
output = utils.split_data(data, tokens)
testing.printed_assert_equal(output, expected)