pysgn.ordering#

random_order(gdf: GeoDataFrame, random_state: int | None = None) ndarray[source]#

Return a random ordering of node indices.

Parameters:

gdf: GeoDataFrame containing nodes.

random_state: Optional random seed for reproducibility.

Returns:

np.ndarray: A permutation of node indices.

attribute_order(gdf: GeoDataFrame, by: str | list[str], **kwargs) ndarray[source]#

Return an ordering of node indices based on one or more attribute columns.

The GeoDataFrame is sorted by the specified column(s) in ascending order by default. Additional keyword arguments are passed to gdf.sort_values.

Parameters:

gdf: GeoDataFrame containing nodes.

by: A column name or list of column names to sort by.

**kwargs: Additional keyword arguments to pass to gdf.sort_values.

These can include ‘ascending’, ‘na_position’, ‘ignore_index’, etc.

Returns:

np.ndarray: Array of indices representing the sorted order.

density_order_knn(gdf: GeoDataFrame, k: int = 5, **kwargs) ndarray[source]#

Return an ordering of node indices based on local density using k-nearest neighbors.

The density is estimated as the average distance to the k nearest neighbors. Nodes with a lower average distance (i.e. higher density) will be ordered first. Additional keyword arguments are passed to the KDTree query method: KDTree.query.

Parameters:

gdf: GeoDataFrame containing nodes.

k: Number of nearest neighbors to consider (default 5).

**kwargs: Additional keyword arguments passed to KDTree.query.

Returns:

np.ndarray: Array of indices representing the density ordering.

density_order_kde(gdf: GeoDataFrame, bandwidth: float = 1.0, kernel: str = 'gaussian', **kwargs) ndarray[source]#

Return an ordering of node indices based on density estimated by Kernel Density Estimation (KDE).

The density is estimated at each node position using KDE. Nodes with higher density estimates are ordered first. Additional keyword arguments are passed to the KernelDensity constructor: KernelDensity.

Parameters:

gdf: GeoDataFrame containing nodes.

bandwidth: Bandwidth parameter for the KDE (default 1.0).

kernel: The kernel to use in KDE (default “gaussian”).

**kwargs: Additional keyword arguments passed to KernelDensity.

Returns:

np.ndarray: Array of indices representing the density ordering.

density_order(gdf: GeoDataFrame, method: str = 'kde', **kwargs) ndarray[source]#

Return an ordering of node indices based on density using the specified method.

Parameters:

gdf: GeoDataFrame containing nodes.

method: The density ordering method to use. Options are:
  • “knn”: Uses the average distance to k-nearest neighbors.

  • “kde”: Uses kernel density estimation.

**kwargs: Additional keyword arguments passed to the selected density ordering function.

Returns:

np.ndarray: Array of indices representing the density ordering.