diff --git a/src/ConfigSpace/configuration.py b/src/ConfigSpace/configuration.py index 65149baa..8eec67f4 100644 --- a/src/ConfigSpace/configuration.py +++ b/src/ConfigSpace/configuration.py @@ -163,10 +163,15 @@ def check_valid_configuration(self) -> None: allow_inactive_with_values=self.allow_inactive_with_values, ) - def get_array(self) -> Array[f64]: + def __array__(self, dtype: type[DType]=None, copy: bool=False) -> Array[f64]: """The internal vector representation of this config. All continuous values are scaled between zero and one. + Conditional hyperparameters that are not active are represented with nan. + + Args: + dtype: Ignored, required by numpy (dtype enforced by configuration) + copy: Ignored, required by numpy (is always a copy) Returns: The vector representation of the configuration @@ -289,4 +294,17 @@ def get_dictionary(self) -> dict[str, Any]: """ return dict(self) + @deprecated( + "Please use `np.array(config)` instead of `config.get_array()`.", + ) + def get_array(self) -> Array[f64]: + """The internal vector representation of this config. + + All continuous values are scaled between zero and one. + + Returns: + The vector representation of the configuration + """ + return self._vector + # --------------------------------------------------- diff --git a/test/test_configuration_space.py b/test/test_configuration_space.py index e6429c55..cb151887 100644 --- a/test/test_configuration_space.py +++ b/test/test_configuration_space.py @@ -877,6 +877,18 @@ def test_sample_configuration(): for j in range(100): assert samples[-1][j] == samples[-2][j] + # Test that sample configuration to np.array works as expected + cs.seed(42) + for _ in range(5): + sample = cs.sample_configuration(2) + for configuration in sample: + array = np.array(configuration) + expected_array = configuration._vector + assert len(array) == len(expected_array) + for value, expected in zip(array, expected_array): + # Values are either equal in both arrays or both are np.nan + assert value == expected or (np.isnan(value) and np.isnan(expected)) + def test_sample_configuration_with_or_conjunction(): cs = ConfigurationSpace(seed=1)