Domain
Constraints (BaseModel, Generic)
Source code in bofire/data_models/domain/constraints.py
class Constraints(BaseModel, Generic[C]):
type: Literal["Constraints"] = "Constraints"
constraints: Sequence[C] = Field(default_factory=list)
def __iter__(self) -> Iterator[C]:
return iter(self.constraints)
def __len__(self):
return len(self.constraints)
def __getitem__(self, i) -> C:
return self.constraints[i]
def __add__(
self,
other: Union[Sequence[CIncludes], "Constraints[CIncludes]"],
) -> "Constraints[Union[C, CIncludes]]":
if isinstance(other, collections.abc.Sequence):
other_constraints = other
else:
other_constraints = other.constraints
constraints = list(chain(self.constraints, other_constraints))
return Constraints(constraints=constraints)
def __call__(self, experiments: pd.DataFrame) -> pd.DataFrame:
"""Numerically evaluate all constraints
Args:
experiments (pd.DataFrame): data to evaluate the constraint on
Returns:
pd.DataFrame: Constraint evaluation for each of the constraints
"""
return pd.concat([c(experiments) for c in self.constraints], axis=1)
def jacobian(self, experiments: pd.DataFrame) -> list:
"""Numerically evaluate the jacobians of all constraints
Args:
experiments (pd.DataFrame): data to evaluate the constraint jacobians on
Returns:
list: A list containing the jacobians as pd.DataFrames
"""
return [c.jacobian(experiments) for c in self.constraints]
def is_fulfilled(self, experiments: pd.DataFrame, tol: float = 1e-6) -> pd.Series:
"""Check if all constraints are fulfilled on all rows of the provided dataframe
Args:
experiments (pd.DataFrame): Dataframe with data, the constraint validity should be tested on
tol (float, optional): tolerance parameter. A constraint is considered as not fulfilled if
the violation is larger than tol. Defaults to 0.
Returns:
Boolean: True if all constraints are fulfilled for all rows, false if not
"""
if len(self.constraints) == 0:
return pd.Series([True] * len(experiments), index=experiments.index)
return (
pd.concat(
[c.is_fulfilled(experiments, tol) for c in self.constraints],
axis=1,
)
.fillna(True)
.all(axis=1)
)
def get(
self,
includes: Union[Type[CIncludes], Sequence[Type[CIncludes]]] = Constraint,
excludes: Optional[Union[Type[CExcludes], List[Type[CExcludes]]]] = None,
exact: bool = False,
) -> "Constraints[CIncludes]":
"""Get constraints of the domain
Args:
includes: Constraint class or list of specific constraint classes to be returned. Defaults to Constraint.
excludes: Constraint class or list of specific constraint classes to be excluded from the return. Defaults to None.
exact: Boolean to distinguish if only the exact class listed in includes and no subclasses inherenting from this class shall be returned. Defaults to False.
Returns:
Constraints: constraints in the domain fitting to the passed requirements.
"""
return Constraints(
constraints=filter_by_class(
self.constraints,
includes=includes,
excludes=excludes,
exact=exact,
),
)
def get_reps_df(self):
"""Provides a tabular overwiev of all constraints within the domain
Returns:
pd.DataFrame: DataFrame listing all constraints of the domain with a description
"""
df = pd.DataFrame(
index=range(len(self.constraints)),
columns=["Type", "Description"],
data={
"Type": [feat.__class__.__name__ for feat in self.get(Constraint)],
"Description": [
constraint.__str__() for constraint in self.get(Constraint)
],
},
)
return df
__class_vars__
special
The names of the class variables defined on the model.
__private_attributes__
special
Metadata about the private attributes of the model.
__pydantic_complete__
special
Whether model building is completed, or if there are still undefined fields.
__pydantic_computed_fields__
special
A dictionary of computed field names and their corresponding [ComputedFieldInfo
][pydantic.fields.ComputedFieldInfo] objects.
__pydantic_custom_init__
special
Whether the model has a custom __init__
method.
__pydantic_decorators__
special
Metadata containing the decorators defined on the model.
This replaces Model.__validators__
and Model.__root_validators__
from Pydantic V1.
__pydantic_fields__
special
A dictionary of field names and their corresponding [FieldInfo
][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__
from Pydantic V1.
__pydantic_generic_metadata__
special
Metadata for generic models; contains data used for a similar purpose to args, origin, parameters in typing-module generics. May eventually be replaced by these.
__pydantic_parent_namespace__
special
Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__
special
The name of the post-init method for the model, if defined.
__signature__
special
The synthesized __init__
[Signature
][inspect.Signature] of the model.
model_config
Configuration for the model, should be a dictionary conforming to [ConfigDict
][pydantic.config.ConfigDict].
__call__(self, experiments)
special
Numerically evaluate all constraints
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments |
pd.DataFrame |
data to evaluate the constraint on |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame |
Constraint evaluation for each of the constraints |
Source code in bofire/data_models/domain/constraints.py
def __call__(self, experiments: pd.DataFrame) -> pd.DataFrame:
"""Numerically evaluate all constraints
Args:
experiments (pd.DataFrame): data to evaluate the constraint on
Returns:
pd.DataFrame: Constraint evaluation for each of the constraints
"""
return pd.concat([c(experiments) for c in self.constraints], axis=1)
get(self, includes=<class 'bofire.data_models.constraints.constraint.Constraint'>, excludes=None, exact=False)
Get constraints of the domain
Parameters:
Name | Type | Description | Default |
---|---|---|---|
includes |
Union[Type[~CIncludes], collections.abc.Sequence[Type[~CIncludes]]] |
Constraint class or list of specific constraint classes to be returned. Defaults to Constraint. |
<class 'bofire.data_models.constraints.constraint.Constraint'> |
excludes |
Union[Type[~CExcludes], List[Type[~CExcludes]]] |
Constraint class or list of specific constraint classes to be excluded from the return. Defaults to None. |
None |
exact |
bool |
Boolean to distinguish if only the exact class listed in includes and no subclasses inherenting from this class shall be returned. Defaults to False. |
False |
Returns:
Type | Description |
---|---|
Constraints |
constraints in the domain fitting to the passed requirements. |
Source code in bofire/data_models/domain/constraints.py
def get(
self,
includes: Union[Type[CIncludes], Sequence[Type[CIncludes]]] = Constraint,
excludes: Optional[Union[Type[CExcludes], List[Type[CExcludes]]]] = None,
exact: bool = False,
) -> "Constraints[CIncludes]":
"""Get constraints of the domain
Args:
includes: Constraint class or list of specific constraint classes to be returned. Defaults to Constraint.
excludes: Constraint class or list of specific constraint classes to be excluded from the return. Defaults to None.
exact: Boolean to distinguish if only the exact class listed in includes and no subclasses inherenting from this class shall be returned. Defaults to False.
Returns:
Constraints: constraints in the domain fitting to the passed requirements.
"""
return Constraints(
constraints=filter_by_class(
self.constraints,
includes=includes,
excludes=excludes,
exact=exact,
),
)
get_reps_df(self)
Provides a tabular overwiev of all constraints within the domain
Returns:
Type | Description |
---|---|
pd.DataFrame |
DataFrame listing all constraints of the domain with a description |
Source code in bofire/data_models/domain/constraints.py
def get_reps_df(self):
"""Provides a tabular overwiev of all constraints within the domain
Returns:
pd.DataFrame: DataFrame listing all constraints of the domain with a description
"""
df = pd.DataFrame(
index=range(len(self.constraints)),
columns=["Type", "Description"],
data={
"Type": [feat.__class__.__name__ for feat in self.get(Constraint)],
"Description": [
constraint.__str__() for constraint in self.get(Constraint)
],
},
)
return df
is_fulfilled(self, experiments, tol=1e-06)
Check if all constraints are fulfilled on all rows of the provided dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments |
pd.DataFrame |
Dataframe with data, the constraint validity should be tested on |
required |
tol |
float |
tolerance parameter. A constraint is considered as not fulfilled if the violation is larger than tol. Defaults to 0. |
1e-06 |
Returns:
Type | Description |
---|---|
Boolean |
True if all constraints are fulfilled for all rows, false if not |
Source code in bofire/data_models/domain/constraints.py
def is_fulfilled(self, experiments: pd.DataFrame, tol: float = 1e-6) -> pd.Series:
"""Check if all constraints are fulfilled on all rows of the provided dataframe
Args:
experiments (pd.DataFrame): Dataframe with data, the constraint validity should be tested on
tol (float, optional): tolerance parameter. A constraint is considered as not fulfilled if
the violation is larger than tol. Defaults to 0.
Returns:
Boolean: True if all constraints are fulfilled for all rows, false if not
"""
if len(self.constraints) == 0:
return pd.Series([True] * len(experiments), index=experiments.index)
return (
pd.concat(
[c.is_fulfilled(experiments, tol) for c in self.constraints],
axis=1,
)
.fillna(True)
.all(axis=1)
)
jacobian(self, experiments)
Numerically evaluate the jacobians of all constraints
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments |
pd.DataFrame |
data to evaluate the constraint jacobians on |
required |
Returns:
Type | Description |
---|---|
list |
A list containing the jacobians as pd.DataFrames |
Source code in bofire/data_models/domain/constraints.py
def jacobian(self, experiments: pd.DataFrame) -> list:
"""Numerically evaluate the jacobians of all constraints
Args:
experiments (pd.DataFrame): data to evaluate the constraint jacobians on
Returns:
list: A list containing the jacobians as pd.DataFrames
"""
return [c.jacobian(experiments) for c in self.constraints]