Skip to content

Instantly share code, notes, and snippets.

@b-adkins
Created November 19, 2021 00:20
Show Gist options
  • Save b-adkins/38a69655af97f7f55ecadd8127391d20 to your computer and use it in GitHub Desktop.
Save b-adkins/38a69655af97f7f55ecadd8127391d20 to your computer and use it in GitHub Desktop.
Two-step grid - a grid of points with two step sizes - the smaller one near "special points"
# Unfinished! I half-finished it before realizing I didn't neet it. It might be handy for
# me or somebody else later
import numpy as np
def two_step_grid(special_points: list[float],
min: Optional[float] = None,
max: Optional[float] = None,
neighborhood_size=0.01,
step_sizes=(0.05, 0.01)):
"""
Generates a grid that is denser near certain points. Ranges from smallest
special point to largest special point unless min and max are specified.
special_points: In the neighborhood of these values, the denser grid is used.
min: Smallest value in the grid.
max: Largest value in the grid.
neighborhood_size: Defines the size of the symmetric interval around special
points in which to sample more points
step_sizes: (general step size, boundary step size) Step sizes used generally and in
the neighborhood of the special points.
"""
# This implement assumes that min and max will either not exist or will be outside the range of
# special points
#
# A better implementation first figures out the neighborhood boundaries - alternating small
# to large step sizes then iterates through those.
ds1, ds2 = step_sizes
if ds1 >= ds2:
raise ValueError()
# Step in phase grid away from limit steps
ds1 = 0.05
# Step size in phase grid in the neighborhood of limit steps
ds2 = 0.01
grids = []
if min < special_points[0]:
grids.append(
np.arange(
min,
special_points[0] - neighborhood_size,
ds1,
))
special_point_prev = min
else:
# Cancels out to a half-neighborhood
special_point_prev = special_points - neighborhood_size
for special_point in special_points:
# Large steps between special point neighborhoods
grids.append(
np.arange(
special_point_prev + neighborhood_size,
special_point - neighborhood_size,
ds1,
)
)
# Small points in neighborhood
grids.append(
np.arange(
special_point - neighborhood_size,
special_point + neighborhood_size,
ds2,
)
)
if max > special_points[-1]:
grids.append(
np.arange(special_points[-1], max, ds1)
)
return np.concatenate(grids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment