ssapy_toolkit.Compute.sampling
Sampling utilities.
This module provides a collection of small helper functions for random sampling, including:
Sampling from sequences (with/without replacement)
Uniform scalar and array sampling
Gaussian (normal) scalar and array sampling
Log-normal, exponential, Poisson, binomial, Dirichlet, and multivariate normal sampling
Cached sigma perturbation generator for 6D state vectors (get_sigmas)
All random draws are implemented via NumPy’s RNG.
Functions
|
Binomial(n, p) array sample. |
|
Dirichlet(alpha) sampling. |
|
Exponential(scale) array sample. |
|
Return an (n, 6) array of random sigma perturbations, cached on disk. |
|
Log-normal array sample: exp( N(mean, sigma^2) ). |
|
Multivariate normal N(mean, cov) sampling. |
|
Gaussian N(mean, std^2) array sample. |
|
Gaussian N(mean, std^2) scalar sample. |
|
Perturb a 3D position and velocity with isotropic spherical uncertainty. |
|
Poisson(lam) array sample. |
|
Draw a single uniform random float in [low, high). |
|
Generate a random array with specified bounds and size. |
|
Uniformly sample n elements from a sequence. |
|
Shuffle a list in-place using Python's built-in RNG. |
|
Uniform( low, high ) array sample. |
|
Uniform( low, high ) scalar sample. |
- ssapy_toolkit.Compute.sampling.binomial_array(n: int = 1, p: float = 0.5, size: tuple = (1, 10), dtype: str = 'int64') ndarray[source]
Binomial(n, p) array sample.
- ssapy_toolkit.Compute.sampling.dirichlet_array(alpha: Sequence[float], size: int = 1) ndarray[source]
Dirichlet(alpha) sampling.
- ssapy_toolkit.Compute.sampling.exponential_array(scale: float = 1.0, size: tuple = (1, 10), dtype: str = 'float64') ndarray[source]
Exponential(scale) array sample.
- Parameters:
scale (float) – 1 / lambda, the mean of the distribution.
- Return type:
np.ndarray
- ssapy_toolkit.Compute.sampling.get_sigmas(n: int = 25, path: str | None = None) ndarray[source]
Return an (n, 6) array of random sigma perturbations, cached on disk.
- Each row is:
[dx, dy, dz, dvx, dvy, dvz]
- Generation logic (when cache is recomputed):
Positions are uniformly sampled inside a 3D ball of radius 10.
Velocities are uniformly sampled inside a 3D ball of radius 1.
- Cache path resolution when path is None:
Use directory from env var ssapy_toolkit_CACHE (file name: covariance_sigmas.npy)
Fallback to ~/.cache/ssapy_toolkit/covariance_sigmas.npy
If path is provided, it is treated as an explicit file path.
- ssapy_toolkit.Compute.sampling.lognormal_array(mean: float = 0.0, sigma: float = 1.0, size: tuple = (1, 10), dtype: str = 'float64') ndarray[source]
Log-normal array sample: exp( N(mean, sigma^2) ).
- Return type:
np.ndarray
- ssapy_toolkit.Compute.sampling.multivariate_normal_array(mean: Sequence[float], cov: ndarray, size: int = 1, dtype: str = 'float64') ndarray[source]
Multivariate normal N(mean, cov) sampling.
- ssapy_toolkit.Compute.sampling.normal_array(mean: float = 0.0, std: float = 1.0, size: tuple = (1, 10), dtype: str = 'float64') ndarray[source]
Gaussian N(mean, std^2) array sample.
- Return type:
np.ndarray
- ssapy_toolkit.Compute.sampling.normal_scalar(mean: float = 0.0, std: float = 1.0) float[source]
Gaussian N(mean, std^2) scalar sample.
- Return type:
- ssapy_toolkit.Compute.sampling.perturb_state_3d(r: ndarray, v: ndarray, pos_scale: float = 1.0, vel_scale: float = 0.1, pos_distribution: str = 'uniform', vel_distribution: str = 'uniform', rng: Generator | None = None) tuple[ndarray, ndarray][source]
Perturb a 3D position and velocity with isotropic spherical uncertainty.
- Parameters:
r (array-like, shape (3,)) – Nominal 3D position vector.
v (array-like, shape (3,)) – Nominal 3D velocity vector.
pos_scale (float, default 1.0) –
Characteristic scale of the position perturbation. Interpretation depends on pos_distribution:
’uniform’ : max radius of solid ball
’normal’ : standard deviation per component
’shell’ : fixed radius
’laplace’ : Laplace scale per component
vel_scale (float, default 0.1) – Same idea as pos_scale, but for the velocity perturbation.
pos_distribution ({'uniform', 'normal', 'gaussian', 'shell', 'surface', 'laplace'}) – Distribution for position offset (see _sample_3d_offset).
vel_distribution ({'uniform', 'normal', 'gaussian', 'shell', 'surface', 'laplace'}) – Distribution for velocity offset.
rng (np.random.Generator or None) – Optional RNG for reproducibility.
- Returns:
(r_pert, v_pert) – Perturbed position and velocity, each shape (3,).
- Return type:
tuple of np.ndarray
Examples
>>> r = np.array([7000e3, 0.0, 0.0]) >>> v = np.array([0.0, 7.5e3, 0.0]) >>> r_p, v_p = perturb_state_3d(r, v, pos_scale=100.0, vel_scale=0.1, ... pos_distribution='uniform', ... vel_distribution='normal')
- ssapy_toolkit.Compute.sampling.poisson_array(lam: float = 1.0, size: tuple = (1, 10), dtype: str = 'int64') ndarray[source]
Poisson(lam) array sample.
- Parameters:
lam (float) – Expected value (lambda).
- Return type:
np.ndarray
- ssapy_toolkit.Compute.sampling.rand_num(low: float = 0.0, high: float = 1.0) float[source]
Draw a single uniform random float in [low, high).
- ssapy_toolkit.Compute.sampling.random_arr(low: float = 0, high: float = 1, size: tuple = (1, 10), dtype: str = 'float64') ndarray[source]
Generate a random array with specified bounds and size.
- Parameters:
- Returns:
Random array.
- Return type:
np.ndarray
- ssapy_toolkit.Compute.sampling.sample_from_sequence(seq: Sequence, n: int, replacement: bool = False) ndarray[source]
Uniformly sample n elements from a sequence.
- Parameters:
- Returns:
Array of sampled elements.
- Return type:
np.ndarray
Notes
This is a thin wrapper around numpy.random.choice with p=None (uniform discrete sampling over the sequence indices).
- ssapy_toolkit.Compute.sampling.shuffle(x: list) None[source]
Shuffle a list in-place using Python’s built-in RNG.
- Parameters:
x (list) – List to shuffle.
- Return type:
None