|
| 1 | +from ConfigSpace.configuration_space import ConfigurationSpace |
| 2 | +from ConfigSpace.hyperparameters import CategoricalHyperparameter, UniformIntegerHyperparameter |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from autoPyTorch.pipeline.components.base_component import ThirdPartyComponents, autoPyTorchComponent |
| 7 | + |
| 8 | + |
| 9 | +class DummyComponentRequiredFailuire(autoPyTorchComponent): |
| 10 | + _required_properties = {'required'} |
| 11 | + |
| 12 | + def __init__(self, random_state=None): |
| 13 | + self.fitted = False |
| 14 | + self._cs_updates = {} |
| 15 | + |
| 16 | + def fit(self, X, y): |
| 17 | + self.fitted = True |
| 18 | + return self |
| 19 | + |
| 20 | + def get_properties(dataset_properties=None): |
| 21 | + return {"name": 'DummyComponentRequiredFailuire', |
| 22 | + "shortname": "Dummy"} |
| 23 | + |
| 24 | + |
| 25 | +class DummyComponentExtraPropFailuire(autoPyTorchComponent): |
| 26 | + |
| 27 | + def __init__(self, random_state=None): |
| 28 | + self.fitted = False |
| 29 | + self._cs_updates = {} |
| 30 | + |
| 31 | + def fit(self, X, y): |
| 32 | + self.fitted = True |
| 33 | + return self |
| 34 | + |
| 35 | + def get_properties(dataset_properties=None): |
| 36 | + return {"name": 'DummyComponentExtraPropFailuire', |
| 37 | + "shortname": 'Dummy', |
| 38 | + "must_not_be_there": True} |
| 39 | + |
| 40 | + |
| 41 | +class DummyComponent(autoPyTorchComponent): |
| 42 | + def __init__(self, a=0, b='orange', random_state=None): |
| 43 | + self.a = a |
| 44 | + self.b = b |
| 45 | + self.fitted = False |
| 46 | + self.random_state = random_state |
| 47 | + self._cs_updates = {} |
| 48 | + |
| 49 | + def get_hyperparameter_search_space(self, dataset_properties=None): |
| 50 | + cs = ConfigurationSpace() |
| 51 | + a = UniformIntegerHyperparameter('a', lower=10, upper=100, log=False) |
| 52 | + b = CategoricalHyperparameter('b', choices=['red', 'green', 'blue']) |
| 53 | + cs.add_hyperparameters([a, b]) |
| 54 | + return cs |
| 55 | + |
| 56 | + def fit(self, X, y): |
| 57 | + self.fitted = True |
| 58 | + return self |
| 59 | + |
| 60 | + def get_properties(dataset_properties=None): |
| 61 | + return {"name": 'DummyComponent', |
| 62 | + "shortname": 'Dummy'} |
| 63 | + |
| 64 | + |
| 65 | +def test_third_party_component_failure(): |
| 66 | + _addons = ThirdPartyComponents(autoPyTorchComponent) |
| 67 | + |
| 68 | + with pytest.raises(ValueError, match=r"Property required not specified for .*"): |
| 69 | + _addons.add_component(DummyComponentRequiredFailuire) |
| 70 | + |
| 71 | + with pytest.raises(ValueError, match=r"Property must_not_be_there must not be specified for algorithm .*"): |
| 72 | + _addons.add_component(DummyComponentExtraPropFailuire) |
| 73 | + |
| 74 | + with pytest.raises(TypeError, match=r"add_component works only with a subclass of .*"): |
| 75 | + _addons.add_component(1) |
| 76 | + |
| 77 | + |
| 78 | +def test_set_hyperparameters_not_found_failure(): |
| 79 | + dummy_component = DummyComponent() |
| 80 | + dummy_config_space = dummy_component.get_hyperparameter_search_space() |
| 81 | + success_configuration = dummy_config_space.sample_configuration() |
| 82 | + dummy_config_space.add_hyperparameter(CategoricalHyperparameter('c', choices=[1, 2])) |
| 83 | + failure_configuration = dummy_config_space.sample_configuration() |
| 84 | + with pytest.raises(ValueError, match=r"Cannot set hyperparameter c for autoPyTorch.pipeline " |
| 85 | + r"DummyComponent because the hyperparameter does not exist."): |
| 86 | + dummy_component.set_hyperparameters(failure_configuration) |
| 87 | + with pytest.raises(ValueError, match=r"Cannot set init param r for autoPyTorch.pipeline " |
| 88 | + r"DummyComponent because the init param does not exist."): |
| 89 | + dummy_component.set_hyperparameters(success_configuration, init_params={'r': 1}) |
0 commit comments