-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
17 sparse selection #30
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e0d2eef
initial implementation of sparse subset
rogerkuou 402f543
add tests classification
rogerkuou 7455919
remove screen print
rogerkuou 2cb55d4
more unit test
rogerkuou 179e55b
add depsi_processing script
rogerkuou 35e3917
include network point selection in example script
rogerkuou 173776d
update processing script
rogerkuou 72668a5
update processing script
rogerkuou 4ff78d7
formatting
rogerkuou 3b1e2c4
update example script according to code review comments
rogerkuou a6c8004
fix typo
rogerkuou 3aec2fa
add space coords in test
rogerkuou de4b146
use index for selection
rogerkuou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import numpy as np | ||
import xarray as xr | ||
from scipy.spatial import KDTree | ||
|
||
|
||
def ps_selection( | ||
|
@@ -126,6 +127,126 @@ def ps_selection( | |
return stm_masked | ||
|
||
|
||
def network_stm_selection( | ||
stm: xr.Dataset, | ||
min_dist: int | float, | ||
include_index: list[int] = None, | ||
sortby_var: str = "pnt_nmad", | ||
crs: int | str = "radar", | ||
x_var: str = "azimuth", | ||
y_var: str = "range", | ||
azimuth_spacing: float = None, | ||
range_spacing: float = None, | ||
): | ||
"""Select a Space-Time Matrix (STM) from a candidate STM for network processing. | ||
|
||
The selection is based on two criteria: | ||
1. A minimum distance between selected points. | ||
2. A sorting metric to select better points. | ||
|
||
The candidate STM will be sorted by the sorting metric. | ||
The selection will be performed iteratively, starting from the best point. | ||
In each iteration, the best point will be selected, and points within the minimum distance will be removed. | ||
The process will continue until no points are left in the candidate STM. | ||
|
||
Parameters | ||
---------- | ||
stm : xr.Dataset | ||
candidate Space-Time Matrix (STM). | ||
min_dist : int | float | ||
Minimum distance between selected points. | ||
include_index : list[int], optional | ||
Index of points in the candidate STM that must be included in the selection, by default None | ||
sortby_var : str, optional | ||
Sorting metric for selecting points, by default "pnt_nmad" | ||
crs : int | str, optional | ||
EPSG code of Coordinate Reference System of `x_var` and `y_var`, by default "radar". | ||
If crs is "radar", the distance will be calculated based on radar coordinates, and | ||
azimuth_spacing and range_spacing must be provided. | ||
x_var : str, optional | ||
Data variable name for x coordinate, by default "azimuth" | ||
y_var : str, optional | ||
Data variable name for y coordinate, by default "range" | ||
azimuth_spacing : float, optional | ||
Azimuth spacing, by default None. Required if crs is "radar". | ||
range_spacing : float, optional | ||
Range spacing, by default None. Required if crs is "radar". | ||
|
||
Returns | ||
------- | ||
xr.Dataset | ||
Selected network Space-Time Matrix (STM). | ||
|
||
Raises | ||
------ | ||
ValueError | ||
Raised when `azimuth_spacing` or `range_spacing` is not provided for radar coordinates. | ||
NotImplementedError | ||
Raised when an unsupported Coordinate Reference System is provided. | ||
""" | ||
match crs: | ||
case "radar": | ||
if (azimuth_spacing is None) or (range_spacing is None): | ||
raise ValueError("Azimuth and range spacing must be provided for radar coordinates.") | ||
case _: | ||
raise NotImplementedError | ||
|
||
# Get coordinates and sorting metric, load them into memory | ||
stm_select = None | ||
stm_remain = stm[[x_var, y_var, sortby_var]].compute() | ||
|
||
# Select the include_index if provided | ||
if include_index is not None: | ||
stm_select = stm_remain.isel(space=include_index) | ||
|
||
# Remove points within min_dist of the included points | ||
coords_include = np.column_stack( | ||
[stm_select["azimuth"].values * azimuth_spacing, stm_select["range"].values * range_spacing] | ||
) | ||
coords_remain = np.column_stack( | ||
[stm_remain["azimuth"].values * azimuth_spacing, stm_remain["range"].values * range_spacing] | ||
) | ||
idx_drop = _idx_within_distance(coords_include, coords_remain, min_dist) | ||
if idx_drop is not None: | ||
stm_remain = stm_remain.where(~(stm_remain["space"].isin(idx_drop)), drop=True) | ||
|
||
# Reorder the remaining points by the sorting metric | ||
stm_remain = stm_remain.sortby(sortby_var) | ||
|
||
# Build a list of the index of selected points | ||
if stm_select is None: | ||
space_idx_sel = [] | ||
else: | ||
space_idx_sel = stm_select["space"].values.tolist() | ||
|
||
while stm_remain.sizes["space"] > 0: | ||
# Select one point with best sorting metric | ||
stm_now = stm_remain.isel(space=0) | ||
|
||
# Append the selected point index | ||
space_idx_sel.append(stm_now["space"].values.tolist()) | ||
|
||
# Remove the selected point from the remaining points | ||
stm_remain = stm_remain.isel(space=slice(1, None)).copy() | ||
|
||
# Remove points in stm_remain within min_dist of stm_now | ||
coords_remain = np.column_stack( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This calculation is now repeated 1000+ times. Move outside loop. So store the coordinates, work with index. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see the comment above |
||
[stm_remain["azimuth"].values * azimuth_spacing, stm_remain["range"].values * range_spacing] | ||
) | ||
coords_stmnow = np.column_stack( | ||
[stm_now["azimuth"].values * azimuth_spacing, stm_now["range"].values * range_spacing] | ||
) | ||
idx_drop = _idx_within_distance(coords_stmnow, coords_remain, min_dist) | ||
if idx_drop is not None: | ||
stm_drop = stm_remain.isel(space=idx_drop) | ||
stm_remain = stm_remain.where(~(stm_remain["space"].isin(stm_drop["space"])), drop=True) | ||
|
||
# Get the selected points by space index from the original stm | ||
stm_out = stm.sel(space=space_idx_sel) | ||
|
||
return stm_out | ||
|
||
|
||
def _nad_block(amp: xr.DataArray) -> xr.DataArray: | ||
"""Compute Normalized Amplitude Dispersion (NAD) for a block of amplitude data. | ||
|
||
|
@@ -170,3 +291,30 @@ def _nmad_block(amp: xr.DataArray) -> xr.DataArray: | |
nmad = mad / (median_amplitude + np.finfo(amp.dtype).eps) # Normalized Median Absolute Deviation | ||
|
||
return nmad | ||
|
||
|
||
def _idx_within_distance(coords_ref, coords_others, min_dist): | ||
"""Get the index of points in coords_others that are within min_dist of coords_ref. | ||
|
||
Parameters | ||
---------- | ||
coords_ref : np.ndarray | ||
Coordinates of reference points. Shape (n, 2). | ||
coords_others : np.ndarray | ||
Coordinates of other points. Shape (m, 2). | ||
min_dist : int, float | ||
distance threshold. | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
Index of points in coords_others that are within `min_dist` of `coords_ref`. | ||
""" | ||
kd_ref = KDTree(coords_ref) | ||
kd_others = KDTree(coords_others) | ||
sdm = kd_ref.sparse_distance_matrix(kd_others, min_dist) | ||
if len(sdm) > 0: | ||
idx = np.array(list(sdm.keys()))[:, 1] | ||
return idx | ||
else: | ||
return None |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this a variable, specify file path at the beginning of the script.