def test_dimension_deletion_backward_already_existing(self, trials): """Test :meth:`orion.core.evc.adapters.DimensionDeletion.backward` with valid param and invalid trials because param already exist """ new_param = Trial.Param(name="normal_prior", type="integer", value=1) dimension_deletion_adapter = DimensionDeletion(new_param) with pytest.raises(RuntimeError) as exc_info: dimension_deletion_adapter.backward(trials) assert "Provided trial does not have a compatible configuration" in str( exc_info.value)
def test_dimension_deletion_backward(self, dummy_param, trials): """Test :meth:`orion.core.evc.adapters.DimensionDeletion.backward` with valid param and valid trials """ new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_deletion_adapter = DimensionDeletion(new_param) adapted_trials = dimension_deletion_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
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_deletion_init_with_param(self, dummy_param): """Test initialization of :class:`orion.core.evc.adapters.DimensionDeletion` with valid param """ dimension_deletion_adapter = DimensionDeletion(dummy_param) assert dimension_deletion_adapter.param is dummy_param
def test_dimension_deletion_init_with_bad_dict(self): """Test initialization of :class:`orion.core.evc.adapters.DimensionDeletion` with a dictionary which is a bad definition of a param """ with pytest.raises(AttributeError) as exc_info: print(DimensionDeletion({"bad": "dict"}).param) assert "'Param' object has no attribute 'bad'" in str(exc_info.value)
def test_dimension_deletion_init_with_bad_param(self): """Test initialization of :class:`orion.core.evc.adapters.DimensionDeletion` with object which is not a param or a dictionary definition of it """ with pytest.raises(TypeError) as exc_info: DimensionDeletion("bad") assert "Invalid param argument type ('<class 'str'>')." in str( exc_info.value)
def test_dimension_deletion_configuration(dummy_param): """Test :meth:`orion.core.evc.adapters.DimensionDeletion.configuration`""" dimension_deletion_adapter = DimensionDeletion(dummy_param) configuration = dimension_deletion_adapter.configuration[0] assert configuration["of_type"] == "dimensiondeletion" 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_deletion_forward(self, trials): """Test :meth:`orion.core.evc.adapters.DimensionDeletion.forward` with valid param and valid trials """ new_param = Trial.Param(name="second_normal_prior", type="integer", value=1) dimension_deletion_adapter = DimensionDeletion(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_deletion_adapter.forward(trials) assert len(adapted_trials) == 0 trials[0]._params[-1].value = 1 assert trials[0]._params[-1] == new_param adapted_trials = dimension_deletion_adapter.forward(trials) assert len(adapted_trials) == 1 trials[4]._params[-1].value = 1 assert trials[4]._params[-1] == new_param adapted_trials = dimension_deletion_adapter.forward(trials) assert len(adapted_trials) == 2 trials[-1]._params[-1].value = 1 assert trials[-1]._params[-1] == new_param adapted_trials = dimension_deletion_adapter.forward(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_deletion_init_with_dict(self, dummy_param): """Test initialization of :class:`orion.core.evc.adapters.DimensionDeletion` with a dictionary definition of a param """ DimensionDeletion(dummy_param.to_dict())