Skip to content

Domain

Constraints

Bases: BaseModel, Generic[C]

Source code in bofire/data_models/domain/constraints.py
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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

__call__(experiments)

Numerically evaluate all constraints

Parameters:

Name Type Description Default
experiments DataFrame

data to evaluate the constraint on

required

Returns:

Type Description
DataFrame

pd.DataFrame: Constraint evaluation for each of the constraints

Source code in bofire/data_models/domain/constraints.py
43
44
45
46
47
48
49
50
51
52
53
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(includes=Constraint, excludes=None, exact=False)

Get constraints of the domain

Parameters:

Name Type Description Default
includes Union[Type[CIncludes], Sequence[Type[CIncludes]]]

Constraint class or list of specific constraint classes to be returned. Defaults to Constraint.

Constraint
excludes Optional[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:

Name Type Description
Constraints Constraints[CIncludes]

constraints in the domain fitting to the passed requirements.

Source code in bofire/data_models/domain/constraints.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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()

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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(experiments, tol=1e-06)

Check if all constraints are fulfilled on all rows of the provided dataframe

Parameters:

Name Type Description Default
experiments 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:

Name Type Description
Boolean Series

True if all constraints are fulfilled for all rows, false if not

Source code in bofire/data_models/domain/constraints.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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(experiments)

Numerically evaluate the jacobians of all constraints

Parameters:

Name Type Description Default
experiments DataFrame

data to evaluate the constraint jacobians on

required

Returns:

Name Type Description
list list

A list containing the jacobians as pd.DataFrames

Source code in bofire/data_models/domain/constraints.py
55
56
57
58
59
60
61
62
63
64
65
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]