Skip to content

spacefilling

spacefilling

Bases: designs

Source code in spotpython/design/spacefilling.py
 9
10
11
12
13
14
15
16
17
18
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
class spacefilling(designs):
    def __init__(self, k: int = 2, seed: int = 123) -> None:
        """
        Spacefilling design class

        Args:
            k (int, optional): number of design variables (dimensions). Defaults to 2.
            seed (int, optional): random seed. Defaults to 123.
        """
        self.k = k
        self.seed = seed
        super().__init__(k, seed)
        self.sampler = LatinHypercube(d=self.k, seed=self.seed)

    def scipy_lhd(
        self,
        n: int,
        repeats: int = 1,
        lower: Optional[Union[int, float]] = None,
        upper: Optional[Union[int, float]] = None,
    ) -> ndarray:
        """
        Latin hypercube sampling based on scipy.

        Args:
            n (int): number of samples
            repeats (int): number of repeats (replicates)
            lower (int or float, optional): lower bound. Defaults to 0.
            upper (int or float, optional): upper bound. Defaults to 1.

        Returns:
            (ndarray): Latin hypercube design.

        Examples:
            >>> from spotpython.design.spacefilling import spacefilling
                import numpy as np
                lhd = spacefilling(k=2, seed=123)
                lhd.scipy_lhd(n=5, repeats=2, lower=np.array([0,0]), upper=np.array([1,1]))
                array([[0.66352963, 0.5892358 ],
                    [0.66352963, 0.5892358 ],
                    [0.55592803, 0.96312564],
                    [0.55592803, 0.96312564],
                    [0.16481882, 0.0375811 ],
                    [0.16481882, 0.0375811 ],
                    [0.215331  , 0.34468512],
                    [0.215331  , 0.34468512],
                    [0.83604909, 0.62202146],
                    [0.83604909, 0.62202146]])
        """
        if lower is None:
            lower = zeros(self.k)
        if upper is None:
            upper = ones(self.k)
        sample = self.sampler.random(n=n)
        des = scale(sample, lower, upper)
        return repeat(des, repeats, axis=0)

__init__(k=2, seed=123)

Spacefilling design class

Parameters:

Name Type Description Default
k int

number of design variables (dimensions). Defaults to 2.

2
seed int

random seed. Defaults to 123.

123
Source code in spotpython/design/spacefilling.py
10
11
12
13
14
15
16
17
18
19
20
21
def __init__(self, k: int = 2, seed: int = 123) -> None:
    """
    Spacefilling design class

    Args:
        k (int, optional): number of design variables (dimensions). Defaults to 2.
        seed (int, optional): random seed. Defaults to 123.
    """
    self.k = k
    self.seed = seed
    super().__init__(k, seed)
    self.sampler = LatinHypercube(d=self.k, seed=self.seed)

scipy_lhd(n, repeats=1, lower=None, upper=None)

Latin hypercube sampling based on scipy.

Parameters:

Name Type Description Default
n int

number of samples

required
repeats int

number of repeats (replicates)

1
lower int or float

lower bound. Defaults to 0.

None
upper int or float

upper bound. Defaults to 1.

None

Returns:

Type Description
ndarray

Latin hypercube design.

Examples:

>>> from spotpython.design.spacefilling import spacefilling
    import numpy as np
    lhd = spacefilling(k=2, seed=123)
    lhd.scipy_lhd(n=5, repeats=2, lower=np.array([0,0]), upper=np.array([1,1]))
    array([[0.66352963, 0.5892358 ],
        [0.66352963, 0.5892358 ],
        [0.55592803, 0.96312564],
        [0.55592803, 0.96312564],
        [0.16481882, 0.0375811 ],
        [0.16481882, 0.0375811 ],
        [0.215331  , 0.34468512],
        [0.215331  , 0.34468512],
        [0.83604909, 0.62202146],
        [0.83604909, 0.62202146]])
Source code in spotpython/design/spacefilling.py
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
def scipy_lhd(
    self,
    n: int,
    repeats: int = 1,
    lower: Optional[Union[int, float]] = None,
    upper: Optional[Union[int, float]] = None,
) -> ndarray:
    """
    Latin hypercube sampling based on scipy.

    Args:
        n (int): number of samples
        repeats (int): number of repeats (replicates)
        lower (int or float, optional): lower bound. Defaults to 0.
        upper (int or float, optional): upper bound. Defaults to 1.

    Returns:
        (ndarray): Latin hypercube design.

    Examples:
        >>> from spotpython.design.spacefilling import spacefilling
            import numpy as np
            lhd = spacefilling(k=2, seed=123)
            lhd.scipy_lhd(n=5, repeats=2, lower=np.array([0,0]), upper=np.array([1,1]))
            array([[0.66352963, 0.5892358 ],
                [0.66352963, 0.5892358 ],
                [0.55592803, 0.96312564],
                [0.55592803, 0.96312564],
                [0.16481882, 0.0375811 ],
                [0.16481882, 0.0375811 ],
                [0.215331  , 0.34468512],
                [0.215331  , 0.34468512],
                [0.83604909, 0.62202146],
                [0.83604909, 0.62202146]])
    """
    if lower is None:
        lower = zeros(self.k)
    if upper is None:
        upper = ones(self.k)
    sample = self.sampler.random(n=n)
    des = scale(sample, lower, upper)
    return repeat(des, repeats, axis=0)