Skip to content

nearest_value

nearest_values_sampler(samples, allowed_values, n)

A sampler which returns the nearest values between the input samples and the allowed values, without replacement.

Parameters:

Name Type Description Default
samples Union[Iterable, Sequence]

input conditions

required
allowed_samples

allowed conditions to sample from

required

Returns:

Type Description

the nearest values from allowed_samples to the samples

Source code in autora/experimentalist/sampler/nearest_value.py
 6
 7
 8
 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
def nearest_values_sampler(
    samples: Union[Iterable, Sequence],
    allowed_values: np.ndarray,
    n: int,
):
    """
    A sampler which returns the nearest values between the input samples and the allowed values,
    without replacement.

    Args:
        samples: input conditions
        allowed_samples: allowed conditions to sample from

    Returns:
        the nearest values from `allowed_samples` to the `samples`

    """

    if isinstance(allowed_values, Iterable):
        allowed_values = np.array(list(allowed_values))

    if len(allowed_values.shape) == 1:
        allowed_values = allowed_values.reshape(-1, 1)

    if isinstance(samples, Iterable):
        samples = np.array(list(samples))

    if allowed_values.shape[0] < n:
        raise Exception(
            "More samples requested than samples available in the set allowed of values."
        )

    if isinstance(samples, Iterable) or isinstance(samples, Sequence):
        samples = np.array(list(samples))

    if hasattr(samples, "shape"):
        if samples.shape[0] < n:
            raise Exception(
                "More samples requested than samples available in the pool."
            )

    x_new = np.empty((n, allowed_values.shape[1]))

    # get index of row in x that is closest to each sample
    for row, sample in enumerate(samples):

        if row >= n:
            break

        dist = np.linalg.norm(allowed_values - sample, axis=1)
        idx = np.argmin(dist)
        x_new[row, :] = allowed_values[idx, :]
        allowed_values = np.delete(allowed_values, idx, axis=0)

    return x_new