Data
sgptools.utils.data
¶
Dataset
¶
A class to load, preprocess, and manage access to a dataset for sensor placement and informative path planning tasks.
It handles the following operations:
- Loading from a GeoTIFF file, loading from a numpy array, and generating a synthetic dataset.
- Sampling training, testing, and candidate points from valid (non-NaN) locations.
- Standardizing both the input coordinates (X) and the labels (y) using
StandardScaler
. - Providing methods to retrieve different subsets of the data (train, test, candidates) and to sample sensor data at specified locations or along a path.
The dataset is expected to be a 2D array where each element represents a label (e.g., elevation, temperature, environmental reading).
Source code in sgptools/utils/data.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
__init__(dataset_path=None, num_train=1000, num_test=2500, num_candidates=150, verbose=True, data=None, dtype=np.float64, **kwargs)
¶
Initializes the Dataset class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_path
|
Optional[str]
|
Path to the dataset file (e.g., '.tif'). If None,
a synthetic dataset will be generated. Defaults to None.
Alternatively, pass an array of data to the constructor
with the |
None
|
num_train
|
int
|
Number of training points to sample from the dataset. Defaults to 1000. |
1000
|
num_test
|
int
|
Number of testing points to sample from the dataset. Defaults to 2500. |
2500
|
num_candidates
|
int
|
Number of candidate points for potential sensor placements to sample from the dataset. Defaults to 150. |
150
|
verbose
|
bool
|
If |
True
|
data
|
Optional[ndarray]
|
(height, width, d); 2D n-dimensional array of data. |
None
|
dtype
|
Optional[dtype]
|
The type of the output arrays. If dtype is not given, it will be set to np.float64. |
float64
|
**kwargs
|
Any
|
Additional keyword arguments passed to |
{}
|
Source code in sgptools/utils/data.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
get_candidates()
¶
Retrieves the preprocessed candidate locations for sensor placement.
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (num_candidates, 2); Normalized candidate locations. |
Source code in sgptools/utils/data.py
get_sensor_data(locations, continuous_sening=False, max_samples=500)
¶
Samples sensor data (labels) at specified normalized locations. Can simulate discrete point sensing or continuous path sensing by interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
locations
|
ndarray
|
(N, 2); Array of locations (normalized x, y coordinates) where sensor data is to be sampled. |
required |
continuous_sening
|
bool
|
If |
False
|
max_samples
|
int
|
Maximum number of samples to return if |
500
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing: - sampled_locations (np.ndarray): (M, 2); Normalized locations where sensor data was effectively sampled. - sampled_data (np.ndarray): (M, 1); Standardized sensor data sampled at these locations. Returns empty arrays if no valid data points are found. |
Usage
# dataset_obj = Dataset(...)
# X_path_normalized = np.array([[0.1, 0.2], [0.5, 0.7], [0.9, 0.8]])
# # Discrete sensing
# sensed_X_discrete, sensed_y_discrete = dataset_obj.get_sensor_data(X_path_normalized)
# # Continuous sensing with interpolation
# sensed_X_continuous, sensed_y_continuous = dataset_obj.get_sensor_data(X_path_normalized, continuous_sening=True, max_samples=100)
Source code in sgptools/utils/data.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
get_test()
¶
Retrieves the preprocessed testing data.
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing: - X_test (np.ndarray): (num_test, 2); Normalized testing input features. - y_test (np.ndarray): (num_test, 1); Standardized testing labels. |
Source code in sgptools/utils/data.py
get_train()
¶
Retrieves the preprocessed training data.
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing: - X_train (np.ndarray): (num_train, 2); Normalized training input features. - y_train (np.ndarray): (num_train, 1); Standardized training labels. |
Source code in sgptools/utils/data.py
point_pos(point, d, theta)
¶
Generates a new point at a specified distance d
and angle theta
(in radians) from an existing point. This function applies the
transformation to multiple points simultaneously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point
|
ndarray
|
(N, 2); Array of original 2D points (x, y). |
required |
d
|
float
|
The distance from the original point to the new point. |
required |
theta
|
float
|
The angle in radians for the direction of displacement. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (N, 2); An array of new points after displacement. |
Usage
Source code in sgptools/utils/data.py
prep_synthetic_dataset(shape=(1000, 1000), min_height=0.0, max_height=30.0, roughness=0.5, random_seed=None, **kwargs)
¶
Generates a 2D synthetic elevation (or similar) dataset using the diamond-square algorithm.
Reference: https://github.com/buckinha/DiamondSquare
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape
|
Tuple[int, int]
|
(width, height); The dimensions of the generated grid. Defaults to (1000, 1000). |
(1000, 1000)
|
min_height
|
float
|
Minimum allowed value in the generated data. Defaults to 0.0. |
0.0
|
max_height
|
float
|
Maximum allowed value in the generated data. Defaults to 30.0. |
30.0
|
roughness
|
float
|
Controls the fractal dimension of the generated terrain. Higher values produce rougher terrain. Defaults to 0.5. |
0.5
|
random_seed
|
Optional[int]
|
Seed for reproducibility of the generated data. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed directly to the |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (height, width); The generated 2D synthetic dataset. |
Usage
Source code in sgptools/utils/data.py
prep_tif_dataset(dataset_path, dim_max=2500, verbose=True)
¶
Loads and preprocesses a dataset from a GeoTIFF (.tif) file. The function handles downsampling for large files and replaces NoData values (-999999.0) with NaN.
For very large .tif files, it's recommended to downsample them externally using GDAL:
gdalwarp -tr 50 50 <input>.tif <output>.tif
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_path
|
str
|
Path to the GeoTIFF dataset file. |
required |
dim_max
|
int
|
Maximum allowed dimension (width or height) for the loaded dataset.
If either dimension exceeds |
2500
|
verbose
|
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (H, W); The preprocessed 2D NumPy array representing the dataset, with NoData values converted to NaN. |
Usage
Source code in sgptools/utils/data.py
remove_circle_patches(X, Y, circle_patches)
¶
Removes points that fall inside a list of matplotlib Circle patches.
Note: This function assumes that the circle_patch
objects have a contains_points
method,
similar to matplotlib.patches.Circle
or matplotlib.path.Path
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
(N,); Array of x-coordinates. |
required |
Y
|
ndarray
|
(N,); Array of y-coordinates. |
required |
circle_patches
|
List[Any]
|
A list of objects representing circle patches.
Each object must have a |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing two 1D NumPy arrays: (filtered_X_coordinates, filtered_Y_coordinates). |
Usage
import numpy as np
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
# Example points
X_coords = np.array([0, 1, 2, 3, 4, 5])
Y_coords = np.array([0, 1, 2, 3, 4, 5])
# Define a circle patch centered at (2,2) with radius 1.5
circle = Circle((2, 2), 1.5)
filtered_X, filtered_Y = remove_circle_patches(X_coords, Y_coords, [circle])
Source code in sgptools/utils/data.py
remove_polygons(X, Y, polygons)
¶
Removes points that fall inside a list of matplotlib Path polygons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
(N,); Array of x-coordinates. |
required |
Y
|
ndarray
|
(N,); Array of y-coordinates. |
required |
polygons
|
List[Path]
|
A list of |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing two 1D NumPy arrays: (filtered_X_coordinates, filtered_Y_coordinates). |
Usage
import matplotlib.path as mpath
import numpy as np
# Example points
X_coords = np.array([0, 1, 2, 3, 4, 5])
Y_coords = np.array([0, 1, 2, 3, 4, 5])
# Define a square polygon (points inside will be removed)
polygon_vertices = np.array([[1, 1], [1, 3], [3, 3], [3, 1]])
square_polygon = mpath.Path(polygon_vertices)
filtered_X, filtered_Y = remove_polygons(X_coords, Y_coords, [square_polygon])