Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/ConfigSpace/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

# ---------------------------------------------------
12 changes: 12 additions & 0 deletions test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down