Skip to content

predicates

PolarsPredicate(expr)

Bases: Predicate

Predicate for Polars DataFrame.

This predicate allows for two different ways to provide the predicate expression:

  1. As a Polars expression. This expression is used directly and must evaluate to a single boolean value. For example, the following expression checks if the values in the "feature_a" column are in the list [1, 2, 3] and if the values in the "feature_b" column are greater than 0:

        import polars as pl
    
        PolarsPredicate(
            pl.col("feature_a").is_in([1, 2, 3]).and_(pl.col("feature_b") > 0),
        )
    

  2. As a string. The string is parsed as a Polars expression. Any string identifier are replace with the respective feature during evaluation. The string expression must evaluate to a single boolean value as well. For example, the following expression checks if "feature_a" is always greater than "feature_b":

        import polars as pl
    
        PolarsPredicate(
            "feature_a > feature_b",
        )
    
    Boolean expressions like and, or, and not are not supported by this syntax. See AndPredicate, OrPredicate and NotPredicate for combined predicates or use the polars expression syntax above.

Initialize the predicate from a polars expression or a string.

Source code in src/flowcean/core/tool/testing/predicates/polars.py
44
45
46
def __init__(self, expr: pl.Expr | str) -> None:
    """Initialize the predicate from a polars expression or a string."""
    self.expr = _str_to_pl(expr) if isinstance(expr, str) else expr

AndPredicate(*predicates)

Bases: Predicate

Combine multiple predicates with a logical AND operation.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
64
65
def __init__(self, *predicates: Predicate) -> None:
    self.predicates = predicates

NotPredicate(predicate)

Bases: Predicate

Negate a predicate.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
88
89
def __init__(self, predicate: Predicate) -> None:
    self.predicate = predicate

OrPredicate(*predicates)

Bases: Predicate

Combine multiple predicates with a logical OR operation.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
76
77
def __init__(self, *predicates: Predicate) -> None:
    self.predicates = predicates

Predicate

Bases: ABC

Base class for predicates.

A predicate is a function that takes the prediction of an model and returns a boolean value indicating whether the prediction satisfies a certain condition. Predicates can be combined using logical operators (AND, OR, NOT) to create more complex predicates.

__call__(input_data, prediction) abstractmethod

Evaluate the predicate on a model prediction.

Parameters:

Name Type Description Default
input_data Data

The input data used to generate the prediction.

required
prediction Data

The prediction to evaluate.

required

Returns:

Name Type Description
bool bool

True if the prediction satisfies the predicate, False otherwise.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
15
16
17
18
19
20
21
22
23
24
25
26
@abstractmethod
def __call__(self, input_data: Data, prediction: Data) -> bool:
    """Evaluate the predicate on a model prediction.

    Args:
        input_data: The input data used to generate the prediction.
        prediction: The prediction to evaluate.

    Returns:
        bool: True if the prediction satisfies the predicate,
            False otherwise.
    """

__and__(other)

Combine two predicates with a logical AND operation.

Parameters:

Name Type Description Default
other Predicate

The other predicate to combine with.

required

Returns:

Name Type Description
Predicate AndPredicate

A new predicate that is the logical AND of this and other.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
28
29
30
31
32
33
34
35
36
37
38
def __and__(self, other: "Predicate") -> "AndPredicate":
    """Combine two predicates with a logical AND operation.

    Args:
        other: The other predicate to combine with.

    Returns:
        Predicate: A new predicate that is the logical AND of this and
            other.
    """
    return AndPredicate(self, other)

__or__(other)

Combine two predicates with a logical OR operation.

Parameters:

Name Type Description Default
other Predicate

The other predicate to combine with.

required

Returns:

Name Type Description
Predicate OrPredicate

A new predicate that is the logical OR of this and other.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
40
41
42
43
44
45
46
47
48
49
50
def __or__(self, other: "Predicate") -> "OrPredicate":
    """Combine two predicates with a logical OR operation.

    Args:
        other: The other predicate to combine with.

    Returns:
        Predicate: A new predicate that is the logical OR of this and
            other.
    """
    return OrPredicate(self, other)

__invert__()

Negate the predicate.

Returns:

Name Type Description
Predicate NotPredicate

A new predicate that is the negation of the original.

Source code in src/flowcean/core/tool/testing/predicates/predicate.py
52
53
54
55
56
57
58
def __invert__(self) -> "NotPredicate":
    """Negate the predicate.

    Returns:
        Predicate: A new predicate that is the negation of the original.
    """
    return NotPredicate(self)