Misc
sgptools.utils.misc
¶
cont2disc(Xu, candidates, candidate_labels=None)
¶
Maps continuous space locations (Xu
) to the closest points in a discrete
set of candidate locations (candidates
) using a Hungarian algorithm
(linear sum assignment) for optimal matching. This ensures each Xu
point
is matched to a unique candidate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Xu
|
ndarray
|
(m, d); Continuous space points (e.g., optimized sensor locations).
|
required |
candidates
|
ndarray
|
(n, d); Discrete set of candidate locations.
|
required |
candidate_labels
|
Optional[ndarray]
|
(n, 1); Optional labels corresponding to the discrete set of candidate locations. If provided, the matched labels are also returned. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Union[ndarray, Tuple[ndarray, ndarray]]
|
Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: |
Union[ndarray, Tuple[ndarray, ndarray]]
|
|
Union[ndarray, Tuple[ndarray, ndarray]]
|
|
Usage
import numpy as np
from sgptools.utils.misc import cont2disc
# Example continuous points
continuous_points = np.array([[0.1, 0.1], [0.9, 0.9], [0.5, 0.5]])
# Example discrete candidates
discrete_candidates = np.array([[0.0, 0.0], [1.0, 1.0], [0.4, 0.6]])
# Example candidate labels (optional)
discrete_labels = np.array([[10.0], [20.0], [15.0]])
# 1. Map without labels
mapped_points = cont2disc(continuous_points, discrete_candidates)
# 2. Map with labels
mapped_points_X, mapped_points_y = cont2disc(continuous_points, discrete_candidates, discrete_labels)
Source code in sgptools/utils/misc.py
get_inducing_pts(data, num_inducing, orientation=False, random=False)
¶
Selects a subset of data points to be used as inducing points.
By default, it uses k-means clustering to select representative points.
Alternatively, it can select points randomly.
If orientation
is True, an additional dimension representing a rotation angle
is appended to each inducing point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
(n, d_in); Input data points from which to select inducing points.
|
required |
num_inducing
|
int
|
The desired number of inducing points to select. |
required |
orientation
|
bool
|
If True, a random orientation angle (in radians, from 0 to 2*pi) is added as an additional dimension to each inducing point. Defaults to False. |
False
|
random
|
bool
|
If True, inducing points are selected randomly from |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (m, d_out); Inducing points. |
Usage
import numpy as np
from sgptools.utils.misc import get_inducing_pts
# Example data (1000 2D points)
data_points = np.random.rand(1000, 2) * 100
# 1. Select 50 inducing points using k-means (default)
inducing_points_kmeans = get_inducing_pts(data_points, 50)
# 2. Select 20 inducing points randomly with orientation
inducing_points_random_oriented = get_inducing_pts(data_points, 20, orientation=True, random=True)
Source code in sgptools/utils/misc.py
polygon2candidates(vertices, num_samples=5000, random_seed=None)
¶
Samples a specified number of candidate points randomly within a polygon defined by its vertices.
This function leverages geopandas
for geometric operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vertices
|
ndarray
|
(v, 2); A NumPy array where each row represents the (x, y)
coordinates of a vertex defining the polygon. |
required |
num_samples
|
int
|
The desired number of candidate points to sample within the polygon. Defaults to 5000. |
5000
|
random_seed
|
Optional[int]
|
Seed for reproducibility of the random point sampling. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (n, 2); A NumPy array where each row represents the (x, y) coordinates
of a sampled candidate sensor placement location. |
Usage
import numpy as np
# from sgptools.utils.misc import polygon2candidates
# Define vertices for a square polygon
square_vertices = np.array([[0, 0], [10, 0], [10, 10], [0, 10]])
# Sample 100 candidate points within the square
sampled_candidates = polygon2candidates(square_vertices, num_samples=100, random_seed=42)