Skip to content

Commit

Permalink
Replace is_numeric class attribute with is_numerical
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianSosic committed Jun 26, 2024
1 parent e79f37f commit 3fd5168
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _ `_optional` subpackage for managing optional dependencies
`SubspaceContinuous.sample_uniform`
- `SubspaceContinuous.samples_full_factorial` has been replaced with
`SubspaceContinuous.sample_from_full_factorial`
- `Parameter.is_numeric` has been replaced with `Parameter.is_numerical`

## [0.9.1] - 2024-06-04
### Changed
Expand Down
2 changes: 1 addition & 1 deletion baybe/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def add_measurements(
f"The parameter '{param.name}' has missing values or NaNs in the "
f"provided dataframe. Missing parameter values are not supported."
)
if param.is_numeric and (data[param.name].dtype.kind not in "iufb"):
if param.is_numerical and (data[param.name].dtype.kind not in "iufb"):
raise TypeError(
f"The numerical parameter '{param.name}' has non-numeric entries in"
f" the provided dataframe."
Expand Down
15 changes: 14 additions & 1 deletion baybe/parameters/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Base classes for all parameters."""

import warnings
from abc import ABC, abstractmethod
from functools import cached_property, partial
from typing import Any, ClassVar
Expand All @@ -16,6 +17,7 @@
get_base_structure_hook,
unstructure_base,
)
from baybe.utils.basic import classproperty

# TODO: Reactive slots in all classes once cached_property is supported:
# https://github.com/python-attrs/attrs/issues/164
Expand All @@ -30,13 +32,24 @@ class Parameter(ABC, SerialMixin):
"""

# class variables
is_numeric: ClassVar[bool]
is_numerical: ClassVar[bool]
"""Class variable encoding whether this parameter is numeric."""

# object variables
name: str = field(validator=(instance_of(str), min_len(1)))
"""The name of the parameter"""

@classproperty
def is_numeric(cls) -> bool:
"""Alias for `is_numerical`."""
assert "is_numerical" in cls.__dict__["__annotations__"]
warnings.warn(
"The class variable 'is_numeric' has been deprecated and will be removed "
"in a future version. Use 'is_numerical' instead.",
DeprecationWarning,
)
return cls.is_numerical

@abstractmethod
def is_in_range(self, item: Any) -> bool:
"""Return whether an item is within the parameter range.
Expand Down
2 changes: 1 addition & 1 deletion baybe/parameters/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CategoricalParameter(DiscreteParameter):
"""Parameter class for categorical parameters."""

# class variables
is_numeric: ClassVar[bool] = False
is_numerical: ClassVar[bool] = False
# See base class.

# object variables
Expand Down
2 changes: 1 addition & 1 deletion baybe/parameters/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CustomDiscreteParameter(DiscreteParameter):
"""

# class variables
is_numeric: ClassVar[bool] = False
is_numerical: ClassVar[bool] = False
# See base class.

# object variables
Expand Down
4 changes: 2 additions & 2 deletions baybe/parameters/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NumericalDiscreteParameter(DiscreteParameter):
"""Parameter class for discrete numerical parameters (a.k.a. setpoints)."""

# class variables
is_numeric: ClassVar[bool] = True
is_numerical: ClassVar[bool] = True
# See base class.

# object variables
Expand Down Expand Up @@ -102,7 +102,7 @@ class NumericalContinuousParameter(ContinuousParameter):
"""Parameter class for continuous numerical parameters."""

# class variables
is_numeric: ClassVar[bool] = True
is_numerical: ClassVar[bool] = True
# See base class.

# object variables
Expand Down
2 changes: 1 addition & 1 deletion baybe/parameters/substance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SubstanceParameter(DiscreteParameter):
"""

# class variables
is_numeric: ClassVar[bool] = False
is_numerical: ClassVar[bool] = False
# See base class.

# object variables
Expand Down
8 changes: 4 additions & 4 deletions baybe/utils/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def add_parameter_noise(
"'absolute' or 'relative_percent'."
)

for param in (p for p in parameters if p.is_numeric):
for param in (p for p in parameters if p.is_numerical):
# Add selected noise type
if noise_type == "relative_percent":
data[param.name] *= np.random.uniform(
Expand Down Expand Up @@ -384,7 +384,7 @@ def fuzzy_row_match(
# Check if the row represents a valid input
valid = True
for param in parameters:
if param.is_numeric:
if param.is_numerical:
if numerical_measurements_must_be_within_tolerance:
valid &= param.is_in_range(row[param.name])
else:
Expand All @@ -402,8 +402,8 @@ def fuzzy_row_match(
)

# Differentiate category-like and discrete numerical parameters
cat_cols = [p.name for p in parameters if not p.is_numeric]
num_cols = [p.name for p in parameters if (p.is_numeric and p.is_discrete)]
cat_cols = [p.name for p in parameters if not p.is_numerical]
num_cols = [p.name for p in parameters if (p.is_numerical and p.is_discrete)]

# Discrete parameters must match exactly
match = left_df[cat_cols].eq(row[cat_cols]).all(axis=1, skipna=False)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,9 @@ def test_deprecated_samples_full_factorial():
with pytest.warns(DeprecationWarning):
parameters = [NumericalContinuousParameter("x", (0, 1))]
SubspaceContinuous(parameters).samples_full_factorial(n_points=1)


def test_deprecated_is_numeric_class_attribute():
"""Using the deprecated `is_numeric` class attribute raises a warning."""
with pytest.warns(DeprecationWarning):
NumericalContinuousParameter.is_numeric

0 comments on commit 3fd5168

Please sign in to comment.