salvus.toolbox.helpers.toolbox_helpers
Generic helper functions for this and that.
Functions
consecutive()
consecutive()def consecutive(a: numpy.ndarray) -> List[numpy.ndarray]: ...Return the indices of consecutive runs of numbers in an array.
anumpy.ndarray — Array to inspect.
copy_and_modify()
copy_and_modify()def copy_and_modify(
a: Sequence[~T], indices: Union[List[Tuple[int, ~T]], Tuple[int, ~T]]
) -> Sequence[~T]: ...Copy a sequence and replace some elements in that copy.
aSequence[~T] — The sequence to copy.indicesUnion[List[Tuple[int, ~T]], Tuple[int, ~T]] — A collection of (index, value) of entries to replace.
fill_index_space()
fill_index_space()def fill_index_space(
a: numpy.ndarray, direction: str = "forwards"
) -> numpy.ndarray: ...Ensure that all numbers in an index space are unique.
This function is useful when multiple entries in an index space are mapped to identical entries in another, and when that latter state is undesirable. For instance, if one requires a unique set of indices [23, 24, 27, 28, 29], and some mapping produces a set of indices [23, 23, 27, 27, 29], this function can transform the former into the latter.
anumpy.ndarray — The index set to fill.directionstr — Whether to fill forwards or backwards.
fix_spacing()
fix_spacing()def fix_spacing(a: numpy.ndarray, snap_to: numpy.ndarray) -> numpy.ndarray: ...Snap entries of a to values in snap_to.
Simple implementation that snaps the closest values in a to the values in
snap_to. Attempts are made to precondition the index space to ensure
there is no overlap and that multiple values of snap_to are not
associated with the same index of a.
anumpy.ndarray — The array of values to modify.snap_tonumpy.ndarray — The values to snap to.
flat_meshgrid()
flat_meshgrid()def flat_meshgrid(xi: numpy.ndarray) -> numpy.ndarray: ...Get a meshgrid back as an “unstructured” array of points.
xinumpy.ndarray — A variadic number of 1-D arrays.
flatten()
flatten()def flatten(i: Union[tuple, list]) -> Generator[Any, NoneType, NoneType]: ...Flatten a list of lists of arbitrary depth.
iUnion[tuple, list] — The list of lists to flatten.
group_by()
group_by()def group_by(
a: numpy.ndarray, pred_col: int = 0, target_col: int = -1
) -> List[numpy.ndarray]: ...Group a numpy array by a predicate column.
anumpy.ndarray — The array to group.pred_colint — The predicate column. Defaults to 0.target_colint — The target column group. Defaults to -1.
interp1d_linear()
interp1d_linear()def interp1d_linear(
x: Union[numpy.ndarray, List[float], List[List[float]]],
y: Union[numpy.ndarray, List[float], List[List[float]]],
p: Union[numpy.ndarray, List[float], List[List[float]]],
) -> numpy.ndarray: ...Linearly interpolate a function defined by x and y at p.
Could not figure out how to get what I want using scipy, so wrote this thing. Interpolation is preformed over the last axis exclusively. For stability, extrapolation is not allowed: values are clipped at the maximum and minimum of the inputs.
xUnion[numpy.ndarray, List[float], List[List[float]]] — x values of the function.yUnion[numpy.ndarray, List[float], List[List[float]]] — y values of the function.pUnion[numpy.ndarray, List[float], List[List[float]]] — Points to interpolate at.
linspace_intervals()
linspace_intervals()def linspace_intervals(
start: float, stop: float, min_num: int, interval_bounds: numpy.ndarray
) -> numpy.ndarray: ...Construct an array that is linearly spaced between specified intervals.
The full returned array will no longer have the same dx in general, as the
specified interval bounds may be at arbitrary locations between start and
stop. Useful when you want to have a more-or-less lin-spaced array, but
it’s more important to hit specific values rather than achieving a fixed
step size.
startfloat — The start value of the interval.stopfloat — The end value of the interval.min_numint — The minimum number of samples that should span the interval. Needs to be at least 2.interval_boundsnumpy.ndarray — The values between (start, stop) to hit.
linspace_intervals_equidistant()
linspace_intervals_equidistant()def linspace_intervals_equidistant(
a: numpy.ndarray, n: int
) -> numpy.ndarray: ...Insert an additional n entries between each entry of a.
Each of the n numbers will be spaced equidistantly between entries of a.
anumpy.ndarray — The array to insert entries into.nint — The number of entries to insert between each value.
optimize_spacing()
optimize_spacing()def optimize_spacing(
a: numpy.ndarray,
snap_to: numpy.ndarray,
distance_bounds: Tuple[float, float] = (0.95, 1.05),
) -> numpy.ndarray: ...Snap entries of a to values in snap_to.
Sets up a small linear program to snap the closest values in a to the
values in snap_to while ensuring that the distance bounds are respected.
Additional checks are performed to attempt to distribute the entries of
snap_to to neighboring entries of a if it is determined that multiple
values are competing for the same entry of a.
The first and last indices of a will never be moved, as these are assumed to be fixed to the bounds of the domain.
This function is probably already deprecated as a simpler way has been found to solve the problem. Leaving here though as an example — perhaps it is useful.
anumpy.ndarray — The array of values to modify.snap_tonumpy.ndarray — The values to snap to.distance_boundsTuple[float, float] — A tuple of the minimum and maximum values of allowed for the relative distance between the computed points, as a percentage of their distance upon input.
promote()
promote()def promote(a: numpy.ndarray) -> Tuple[numpy.ndarray, ...]: ...Promote ranks of a collection of arrays to the highest in that collection.
Scalars will be promoted to at least rank 1.
anumpy.ndarray — The arrays to promote.