Skip to content

continuous

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,
            )
        ],
    )