def test_dimension_addition_forward_already_existing(self, trials): """Test :meth:`orion.core.evc.adapters.DimensionAddition.forward` with valid param and incompatible trials because param already exists """ new_param = Trial.Param(name="normal_prior", type="integer", value=1) dimension_addition_adapter = DimensionAddition(new_param) with pytest.raises(RuntimeError) as exc_info: dimension_addition_adapter.forward(trials) assert "Provided trial does not have a compatible configuration" in str( exc_info.value)
def test_dimension_addition_backward_not_existing(self, trials): """Test :meth:`orion.core.evc.adapters.DimensionAddition.backward` with valid param and invalid trials because param does not exist """ new_param = Trial.Param(name='second_normal_prior', type='integer', value=1) dimension_addition_adapter = DimensionAddition(new_param) with pytest.raises(RuntimeError) as exc_info: dimension_addition_adapter.backward(trials) assert "Provided trial does not have a compatible configuration" in str( exc_info.value)
def test_dimension_addition_forward(self, trials): """Test :meth:`orion.core.evc.adapters.DimensionAddition.forward` with valid param and trials """ new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_addition_adapter = DimensionAddition(new_param) adapted_trials = dimension_addition_adapter.forward(trials) assert adapted_trials[0]._params[-1] == new_param assert adapted_trials[4]._params[-1] == new_param assert adapted_trials[-1]._params[-1] == new_param
def test_composite_adapter_backward(self, dummy_param, trials): """Test :meth:`orion.core.evc.adapters.CompositeAdapter.backward` with two adapters""" new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_addition_adapter = DimensionAddition(new_param) dimension_deletion_adapter = DimensionDeletion(new_param) composite_adapter = CompositeAdapter(dimension_deletion_adapter) adapted_trials = composite_adapter.backward(trials) assert adapted_trials[0]._params[-1] == new_param assert adapted_trials[4]._params[-1] == new_param assert adapted_trials[-1]._params[-1] == new_param composite_adapter = CompositeAdapter(dimension_addition_adapter, dimension_deletion_adapter) adapted_trials = composite_adapter.backward(trials) assert len(adapted_trials) == len(trials) assert new_param not in (adapted_trials[0]._params) assert new_param not in (adapted_trials[4]._params) assert new_param not in (adapted_trials[-1]._params)
def test_dimension_addition_init_with_param(self, dummy_param): """Test initialization of :class:`orion.core.evc.adapters.DimensionAddition` with valid param """ dimension_addition_adapter = DimensionAddition(dummy_param) assert dimension_addition_adapter.param is dummy_param
def test_dimension_addition_init_with_bad_dict(self): """Test initialization of :class:`orion.core.evc.adapters.DimensionAddition` with a dictionary which is a bad definition of a param """ with pytest.raises(AttributeError) as exc_info: DimensionAddition({"bad": "dict"}) assert "'Param' object has no attribute 'bad'" in str(exc_info.value)
def test_dimension_addition_init_with_bad_param(self): """Test initialization of :class:`orion.core.evc.adapters.DimensionAddition` with object which is not a param or a dictionary definition of it """ with pytest.raises(TypeError) as exc_info: DimensionAddition("bad") assert "Invalid param argument type ('<class 'str'>')." in str( exc_info.value)
def test_dimension_addition_configuration(dummy_param): """Test :meth:`orion.core.evc.adapters.DimensionAddition.configuration`""" dimension_addition_adapter = DimensionAddition(dummy_param) configuration = dimension_addition_adapter.configuration[0] assert configuration["of_type"] == "dimensionaddition" assert configuration["param"] == dummy_param.to_dict() assert (BaseAdapter.build( [configuration]).adapters[0].configuration[0] == configuration)
def test_composite_adapter_init_with_adapters(self, dummy_param): """Test initialization of :class:`orion.core.evc.adapters.CompositeAdapter` with valid adapters """ dimension_addition = DimensionAddition(dummy_param) dimension_deletion = DimensionDeletion(dummy_param) composite_adapter = CompositeAdapter(dimension_addition, dimension_deletion) assert len(composite_adapter.adapters) == 2 assert isinstance(composite_adapter.adapters[0], DimensionAddition) assert isinstance(composite_adapter.adapters[1], DimensionDeletion)
def test_composite_configuration(dummy_param): """Test :meth:`orion.core.evc.adapters.CompositeAdapter.configuration`""" new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_addition_adapter = DimensionAddition(dummy_param) dimension_deletion_adapter = DimensionDeletion(new_param) composite_adapter = CompositeAdapter(dimension_addition_adapter, dimension_deletion_adapter) configuration = composite_adapter.configuration assert configuration[0] == dimension_addition_adapter.configuration[0] assert configuration[1] == dimension_deletion_adapter.configuration[0] assert (BaseAdapter.build(configuration).adapters[0].configuration[0] == configuration[0]) assert (BaseAdapter.build(configuration).adapters[1].configuration[0] == configuration[1])
def test_dimension_addition_backward(self, dummy_param, trials): """Test :meth:`orion.core.evc.adapters.DimensionAddition.backward` with valid param and valid trials """ new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_addition_adapter = DimensionAddition(new_param) sampler = DimensionBuilder().build("random", "uniform(10, 100, discrete=True)") for trial in trials: random_param = new_param.to_dict() random_param["value"] = sampler.sample() trial._params.append(Trial.Param(**random_param)) adapted_trials = dimension_addition_adapter.backward(trials) assert len(adapted_trials) == 0 trials[0]._params[-1].value = 1 assert trials[0]._params[-1] == new_param adapted_trials = dimension_addition_adapter.backward(trials) assert len(adapted_trials) == 1 trials[4]._params[-1].value = 1 assert trials[4]._params[-1] == new_param adapted_trials = dimension_addition_adapter.backward(trials) assert len(adapted_trials) == 2 trials[-1]._params[-1].value = 1 assert trials[-1]._params[-1] == new_param adapted_trials = dimension_addition_adapter.backward(trials) assert len(adapted_trials) == 3 assert new_param not in (adapted_trials[0]._params) assert new_param not in (adapted_trials[1]._params) assert new_param not in (adapted_trials[2]._params)
def test_dimension_addition_init_with_dict(self, dummy_param): """Test initialization of :class:`orion.core.evc.adapters.DimensionAddition` with a dictionary definition of a param """ DimensionAddition(dummy_param.to_dict())