ptyrad.solver.grouping#

Grouping functions for scan positions

Functions

get_hilbert_key(p[, resolution])

Calculates the Hilbert curve integer index for a single normalized point.

remap_batches_to_global(local_batches, ...)

Maps batch indices from a local subset coordinate system back to the global coordinate system.

sparse_sampler_fps(points, n_groups[, seed])

Splits 2D points into G groups using Farthest Point Sampling (FPS) to ensure maximum spatial separation (hyperuniformity) within each group.

sparse_sampler_hilbert(points, n_groups[, ...])

Splits 2D points into G groups using Hilbert Curve sorting to ensure spatial stratification (hyperuniformity) within each group.

ptyrad.solver.grouping.sparse_sampler_fps(points, n_groups, seed=None)[source]#

Splits 2D points into G groups using Farthest Point Sampling (FPS) to ensure maximum spatial separation (hyperuniformity) within each group.

Mechanism:
  1. Maintains a distance cache dist_caches of shape (G, N), where entry [g, i] is the distance from point i to the nearest existing member of group g.

  2. Iteratively selects the point with the maximum distance value for the current target group (greedy maximin strategy).

  3. Marks selected points as “visited” globally by setting their distance to -1.0 in the cache, ensuring sampling without replacement.

Parameters:
  • points (np.ndarray) – (N, 2) array of coordinates.

  • n_groups (int) – Number of desired groups (mini-batches).

Returns:

A list of G arrays, where each inner array contains indices.

Return type:

list[np.ndarray[int]]

ptyrad.solver.grouping.sparse_sampler_hilbert(points, n_groups, resolution=16)[source]#

Splits 2D points into G groups using Hilbert Curve sorting to ensure spatial stratification (hyperuniformity) within each group.

Mechanism:
  1. Map continuous (x, y) coordinates to a discrete 1D Hilbert integer index.

  2. Sort points by this 1D index to group spatially local points together.

  3. Use strided indexing (modulo arithmetic) to peel off layers, ensuring that consecutive points in a group are distant in 2D space.

Parameters:
  • points (np.ndarray) – (N, 2) array of coordinates.

  • n_groups (int) – Number of desired groups (mini-batches).

  • resolution (int) – Grid resolution order. n=16 creates a 65536x65536 grid, sufficient for most float precision needs.

Returns:

A list of G arrays, where each inner array contains indices.

Return type:

list[np.ndarray[int]]

ptyrad.solver.grouping.get_hilbert_key(p, resolution=16)[source]#

Calculates the Hilbert curve integer index for a single normalized point. Uses bitwise operations to traverse the recursive quadrants.

ptyrad.solver.grouping.remap_batches_to_global(local_batches, global_lookup)[source]#

Maps batch indices from a local subset coordinate system back to the global coordinate system.

Parameters:
  • local_batches (list of arrays) – The output from the sampler (indices 0..M-1).

  • global_lookup (np.ndarray) – The actual global indices corresponding to the subset (values 0..N-1). This is your original ‘indices’ array.

Returns:

Batches containing the global indices.

Return type:

list of np.ndarrays