Uniformly distribute `total_samples` within [0, 1)^{d}. Args: total_samples: total number of samples d: number of dimension method: 'random': 'LatinHypercube': fast_forward: number of samples alread
(
total_samples: int,
d: int,
method: str = 'random',
rng=None,
fast_forward_n: int = None,
shuffle: bool = False,
)
| 44 | |
| 45 | |
| 46 | def get_samples( |
| 47 | total_samples: int, |
| 48 | d: int, |
| 49 | method: str = 'random', |
| 50 | rng=None, |
| 51 | fast_forward_n: int = None, |
| 52 | shuffle: bool = False, |
| 53 | ) -> np.ndarray: |
| 54 | """ |
| 55 | Uniformly distribute `total_samples` within [0, 1)^{d}. |
| 56 | |
| 57 | Args: |
| 58 | total_samples: |
| 59 | total number of samples |
| 60 | d: |
| 61 | number of dimension |
| 62 | method: |
| 63 | 'random': |
| 64 | 'LatinHypercube': |
| 65 | fast_forward: |
| 66 | number of samples already generated |
| 67 | shuffle: |
| 68 | whether to shuffle the samples (along total_samples). |
| 69 | It is needed when combining the samples with other samples. |
| 70 | |
| 71 | Returns: |
| 72 | (total_samples, d) |
| 73 | """ |
| 74 | |
| 75 | if method == 'random': |
| 76 | if rng is None: |
| 77 | return np.random.rand(total_samples, d) |
| 78 | else: |
| 79 | return rng.rand(total_samples, d) |
| 80 | elif method.lower() == 'LatinHypercube'.lower(): |
| 81 | sampler = qmc.LatinHypercube(d=d, seed=rng) |
| 82 | if fast_forward_n is not None and fast_forward_n > 0: |
| 83 | sampler = sampler.fast_forward(fast_forward_n) |
| 84 | samples = sampler.random(n=total_samples) # (total_samples, d) |
| 85 | if shuffle: |
| 86 | samples = shuffle_along_axis(arr=samples, axis=0, rng=rng) |
| 87 | return samples |
| 88 | else: |
| 89 | raise NotImplementedError |
| 90 | |
| 91 | |
| 92 | def shuffle_along_axis(arr: np.ndarray, axis: int, rng=None) -> np.ndarray: |
nothing calls this directly
no test coverage detected