DiscreteLinearConstraint¶
- class baybe.constraints.discrete.DiscreteLinearConstraint[source]¶
Bases:
DiscreteFilteringConstraintClass for modeling linear (weighted-sum) constraints on discrete parameters.
The constraint compares the sum of the specified parameters, optionally weighted by
DiscreteLinearConstraint.coefficients, againstDiscreteLinearConstraint.rhsusingDiscreteLinearConstraint.operator.Examples
>>> df = pd.DataFrame({"A": [1.0, 3.0, 5.0], "B": [2.0, 1.0, 3.0]}) >>> df A B 0 1.0 2.0 1 3.0 1.0 2 5.0 3.0 >>> c = DiscreteLinearConstraint( ... parameters=["A", "B"], ... operator="<=", ... rhs=5.0, ... ) >>> list(c.get_invalid(df)) [2]
With coefficients, the weighted sum is checked instead:
>>> c = DiscreteLinearConstraint( ... parameters=["A", "B"], ... coefficients=(2.0, 1.0), ... operator="<=", ... rhs=5.0, ... ) >>> list(c.get_invalid(df)) [1, 2]
Public methods
__init__(parameters, operator[, ...])Method generated by attrs for class DiscreteLinearConstraint.
from_dict(dictionary)Create an object from its dictionary representation.
from_json(source, /)Create an object from its JSON representation.
get_invalid(df, /, *[, allow_missing])Get the indices of dataframe entries that are invalid under the constraint.
get_invalid_polars(schema)Translate the constraint to a Polars expression identifying rows to remove.
get_valid(df, /, *[, allow_missing])Get the indices of dataframe entries that are valid under the constraint.
summary()Return a custom summarization of the constraint.
to_dict()Create an object's dictionary representation.
to_json([sink, overwrite])Create an object's JSON representation.
Public attributes and properties
The comparison operator (e.g.
"=",">=","<").The coefficients for the weighted sum, one per entry in
parameters.Right-hand side value of the comparison.
Numerical tolerance for equality/inequality operators that support it.
Whether to invert the selection (keep the complement of the specification).
has_polars_implementationBoolean indicating if this is a constraint over continuous parameters.
Boolean indicating if this is a constraint over discrete parameters.
Class variable encoding whether the constraint is valid only for numerical parameters.
The list of parameters used for the constraint.
- __init__(parameters: list[str], operator: Literal['<', '<=', '=', '==', '!=', '>', '>='], coefficients=NOTHING, rhs=0.0, tolerance=None, *, exclude: bool = False)¶
Method generated by attrs for class DiscreteLinearConstraint.
For details on the parameters, see Public attributes and properties.
- classmethod from_json(source: str | Path | SupportsRead[str], /)¶
Create an object from its JSON representation.
- Parameters:
source (
Union[str,Path,SupportsRead[str]]) –The JSON source. Can be:
A string containing JSON content.
A file path or
Pathobject pointing to a JSON file.A file-like object with a
read()method.
- Raises:
ValueError – If
sourceis not one of the allowed types.- Return type:
TypeVar(_T, bound= SerialMixin)- Returns:
The reconstructed object.
- get_invalid(df: DataFrame, /, *, allow_missing: bool = False)¶
Get the indices of dataframe entries that are invalid under the constraint.
- Parameters:
df (
DataFrame) – A dataframe where each row represents a parameter configuration.allow_missing (
bool) – IfFalse, aValueErroris raised when the dataframe is missing required parameter columns. IfTrue, the subclass is asked whether it can perform (partial) constraint evaluation; if not, an empty index is returned, signaling to the caller there are no entries to be excluded *yet*.
- Raises:
ValueError – If
allow_missingisFalseand the dataframe is missing required parameter columns.- Return type:
Index- Returns:
The dataframe indices of rows that violate the constraint.
- get_invalid_polars(schema: Schema)¶
Translate the constraint to a Polars expression identifying rows to remove.
- Parameters:
schema (
Schema) – The Polars schema of the dataframe being filtered.- Return type:
Expr- Returns:
The Polars expression.
- get_valid(df: DataFrame, /, *, allow_missing: bool = False)¶
Get the indices of dataframe entries that are valid under the constraint.
- Parameters:
df (
DataFrame) – A dataframe where each row represents a parameter configuration.allow_missing (
bool) – IfFalse, aValueErroris raised when the dataframe is missing required parameter columns. IfTrue, the constraint performs partial filtering on the available columns.
- Return type:
Index- Returns:
The dataframe indices of rows that fulfill the constraint.
- to_dict()¶
Create an object’s dictionary representation.
- Return type:
- Returns:
The dictionary representation of the object.
- to_json(sink: str | Path | SupportsWrite[str] | None = None, /, *, overwrite: bool = False, **kwargs: Any)¶
Create an object’s JSON representation.
- Parameters:
sink (
Union[str,Path,SupportsWrite[str],None]) –The JSON sink. Can be:
None(only returns the JSON string).A file path or
Pathobject pointing to a location where to write the JSON content.A file-like object with a
write()method.
overwrite (
bool) – Boolean flag indicating if to overwrite the file if it already exists. Only relevant ifsinkis a file path orPathobject.**kwargs (
Any) – Additional keyword arguments to pass tojson.dumps().
- Raises:
FileExistsError – If
sinkpoints to an already existing file butoverwriteisFalse.- Return type:
- Returns:
The JSON representation as a string.
-
coefficients:
tuple[float,...]¶ The coefficients for the weighted sum, one per entry in
parameters.Defaults to all-ones, i.e. an unweighted sum.
- property is_continuous: bool¶
Boolean indicating if this is a constraint over continuous parameters.
-
numerical_only:
ClassVar[bool] = True¶ Class variable encoding whether the constraint is valid only for numerical parameters.