Skip to content

domain

Continuous(feature_name, min_value, max_value, *, distribution='uniform', mean=None, stddev=None)

Bases: Domain

A domain of continuous values.

This domain describes a continuous distribution of values between a minimum and maximum value for a feature.

Initialize the uniform feature domain.

Parameters:

Name Type Description Default
feature_name str

The name of the feature the domain belongs to.

required
min_value float

The minimum value of the domain.

required
max_value float

The maximum value of the domain.

required
distribution Distribution

The distribution of values inside the domain. Can be either "uniform" or "normal". Defaults to "uniform".

'uniform'
mean float | None

The mean of the normal distribution. Required if distribution is "normal".

None
stddev float | None

The standard deviation of the normal distribution. Required if distribution is "normal".

None
Source code in src/flowcean/core/tool/testing/domain/continuous.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
def __init__(
    self,
    feature_name: str,
    min_value: float,
    max_value: float,
    *,
    distribution: Distribution = "uniform",
    mean: float | None = None,
    stddev: float | None = None,
) -> None:
    """Initialize the uniform feature domain.

    Args:
        feature_name: The name of the feature the domain belongs to.
        min_value: The minimum value of the domain.
        max_value: The maximum value of the domain.
        distribution: The distribution of values inside the domain.
            Can be either "uniform" or "normal". Defaults to "uniform".
        mean: The mean of the normal distribution. Required if
            distribution is "normal".
        stddev: The standard deviation of the normal distribution.
            Required if distribution is "normal".
    """
    super().__init__(feature_name)
    if min_value >= max_value:
        msg = (
            f"min_value ({min_value}) must be less"
            "than max_value ({max_value})"
        )
        raise ValueError(
            msg,
        )
    self.distribution = distribution
    if self.distribution == "normal":
        if mean is None or stddev is None:
            msg = (
                "mean and stddev must be provided for normal distribution"
            )
            raise ValueError(msg)
        if not (min_value <= mean <= max_value):
            msg = (
                f"mean ({mean}) must be between min_value ({min_value})"
                f"and max_value ({max_value})"
            )
            raise ValueError(msg)
        self.mean = mean
        self.stddev = stddev

    self.min_value = min_value
    self.max_value = max_value
    self.rng = random.Random()

get_value()

Get a random value from the domain.

Returns:

Type Description
float

A random value uniformly distributed between min_value and

float

max_value.

Source code in src/flowcean/core/tool/testing/domain/continuous.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_value(self) -> float:
    """Get a random value from the domain.

    Returns:
        A random value uniformly distributed between min_value and
        max_value.
    """
    if self.distribution == "normal":
        # We ignore the min and max values for the normal distribution
        # They are only used to check the mean and when the domain is
        # converted to discrete.
        return self.rng.gauss(self.mean, self.stddev)
    return (
        self.min_value
        + (self.max_value - self.min_value) * self.rng.random()
    )

to_discrete(sampling_distance)

Discretize the continuous domain into a discrete domain.

Parameters:

Name Type Description Default
sampling_distance float

The distance between two discrete values.

required

Returns:

Type Description
Discrete

A discrete domain with the same feature name and a list of

Discrete

uniformly distributed values.

Source code in src/flowcean/core/tool/testing/domain/continuous.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def to_discrete(self, sampling_distance: float) -> Discrete:
    """Discretize the continuous domain into a discrete domain.

    Args:
        sampling_distance: The distance between two discrete values.

    Returns:
        A discrete domain with the same feature name and a list of
        uniformly distributed values.
    """
    return Discrete(
        self.feature_name,
        [
            self.min_value + i * sampling_distance
            for i in range(
                int((self.max_value - self.min_value) / sampling_distance)
                + 1,
            )
        ],
    )

Discrete(feature_name, values)

Bases: Domain, Iterable[tuple[str, float]]

A domain of discrete values.

This domain describes a discrete set of values for a feature.

Initialize the discrete domain.

Parameters:

Name Type Description Default
feature_name str

The name of the feature the domain belongs to.

required
values list[float]

The list of values of the domain.

required
Source code in src/flowcean/core/tool/testing/domain/discrete.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def __init__(
    self,
    feature_name: str,
    values: list[float],
) -> None:
    """Initialize the discrete domain.

    Args:
        feature_name: The name of the feature the domain belongs to.
        values: The list of values of the domain.
    """
    super().__init__(feature_name)
    self.values = values

    self.rng = random.Random()

__len__()

Get the number of discrete values in the domain.

Returns:

Type Description
int

The number of discrete values in the domain.

Source code in src/flowcean/core/tool/testing/domain/discrete.py
31
32
33
34
35
36
37
def __len__(self) -> int:
    """Get the number of discrete values in the domain.

    Returns:
        The number of discrete values in the domain.
    """
    return len(self.values)

get_value()

Get a random value from the domain.

Source code in src/flowcean/core/tool/testing/domain/discrete.py
39
40
41
def get_value(self) -> float:
    """Get a random value from the domain."""
    return self.rng.choice(self.values)

__iter__()

Iterate over the values of the range.

Source code in src/flowcean/core/tool/testing/domain/discrete.py
46
47
48
49
def __iter__(self) -> Iterator[tuple[str, float]]:
    """Iterate over the values of the range."""
    for value in self.values:
        yield (self.feature_name, value)

Domain(feature_name)

Bases: ABC

An abstract base class for describing the value domain for a feature.

Initialize the domain.

Parameters:

Name Type Description Default
feature_name str

The name of the feature the domain belongs to.

required
Source code in src/flowcean/core/tool/testing/domain/domain.py
 9
10
11
12
13
14
15
16
17
18
def __init__(
    self,
    feature_name: str,
) -> None:
    """Initialize the domain.

    Args:
        feature_name: The name of the feature the domain belongs to.
    """
    self.feature_name = feature_name

get_value() abstractmethod

Get a random value from the domain for the feature.

Source code in src/flowcean/core/tool/testing/domain/domain.py
20
21
22
@abstractmethod
def get_value(self) -> float:
    """Get a random value from the domain for the feature."""

__call__()

Get a random value from the domain for the feature.

Source code in src/flowcean/core/tool/testing/domain/domain.py
24
25
26
def __call__(self) -> float:
    """Get a random value from the domain for the feature."""
    return self.get_value()

set_seed(seed)

Set the seed for the random number generator.

Parameters:

Name Type Description Default
seed int

The seed to set.

required
Source code in src/flowcean/core/tool/testing/domain/domain.py
28
29
30
31
32
33
34
35
36
37
38
def set_seed(self, seed: int) -> None:  # noqa: ARG002
    """Set the seed for the random number generator.

    Args:
        seed: The seed to set.
    """
    # This is a no-op by default, as the base class does not maintain
    # any state related to random number generation.
    # Subclasses that maintain state can override this method to set
    # the seed for their random number generator.
    return

Fixed(feature_name, value)

Bases: Discrete

A domain with a single value.

This domain contains a single fixed value for a feature.

Initialize the fixed domain.

Parameters:

Name Type Description Default
feature_name str

The name of the feature the domain belongs to.

required
value float

The fixed value to return.

required
Source code in src/flowcean/core/tool/testing/domain/fixed.py
10
11
12
13
14
15
16
17
def __init__(self, feature_name: str, value: float) -> None:
    """Initialize the fixed domain.

    Args:
        feature_name: The name of the feature the domain belongs to.
        value: The fixed value to return.
    """
    super().__init__(feature_name, [value])

get_value()

Get the fixed value.

Source code in src/flowcean/core/tool/testing/domain/fixed.py
19
20
21
def get_value(self) -> float:
    """Get the fixed value."""
    return self.values[0]