Mondaic
This API reference is not for the latest stable Salvus version.

salvus.toolbox.helpers.toolbox_helpers

Generic helper functions for this and that.

Functions

consecutive()

def consecutive(a: numpy.ndarray) -> List[numpy.ndarray]: ...

Return the indices of consecutive runs of numbers in an array.

Parameters
  • a numpy.ndarray — Array to inspect.
Returns List[numpy.ndarray] — List of arrays of consecutive indices.

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.

Parameters
  • a Sequence[~T] — The sequence to copy.
  • indices Union[List[Tuple[int, ~T]], Tuple[int, ~T]] — A collection of (index, value) of entries to replace.
Returns Sequence[~T] — A new sequence with the specified entries replaced.

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.

Parameters
  • a numpy.ndarray — The index set to fill.
  • direction str — Whether to fill forwards or backwards.
Returns numpy.ndarray — The filled index set.

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.

Parameters
  • a numpy.ndarray — The array of values to modify.
  • snap_to numpy.ndarray — The values to snap to.
Returns numpy.ndarray — An array with optimized spacing.

flat_meshgrid()

def flat_meshgrid(xi: numpy.ndarray) -> numpy.ndarray: ...

Get a meshgrid back as an “unstructured” array of points.

Parameters
  • xi numpy.ndarray — A variadic number of 1-D arrays.
Returns numpy.ndarray — A de-structured meshgrid.

flatten()

def flatten(i: Union[tuple, list]) -> Generator[Any, NoneType, NoneType]: ...

Flatten a list of lists of arbitrary depth.

Parameters
  • i Union[tuple, list] — The list of lists to flatten.
Returns Generator[Any, NoneType, NoneType] — A generator that iterates over the flattened list.

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.

Parameters
  • a numpy.ndarray — The array to group.
  • pred_col int — The predicate column. Defaults to 0.
  • target_col int — The target column group. Defaults to -1.
Returns List[numpy.ndarray] — typing.List[np.ndarray]: description

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.

Parameters
  • x Union[numpy.ndarray, List[float], List[List[float]]] — x values of the function.
  • y Union[numpy.ndarray, List[float], List[List[float]]] — y values of the function.
  • p Union[numpy.ndarray, List[float], List[List[float]]] — Points to interpolate at.
Returns numpy.ndarray — Interpolated values.

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.

Parameters
  • start float — The start value of the interval.
  • stop float — The end value of the interval.
  • min_num int — The minimum number of samples that should span the interval. Needs to be at least 2.
  • interval_bounds numpy.ndarray — The values between (start, stop) to hit.
Returns numpy.ndarray — An array which includes at least [start, interval_bounds …, stop] as values, in addition to any intermediate samples that will be at least piecewise linearly-spaced.

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.

Parameters
  • a numpy.ndarray — The array to insert entries into.
  • n int — The number of entries to insert between each value.
Returns numpy.ndarray — An array with new equidistant entries inserted.

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.

Parameters
  • a numpy.ndarray — The array of values to modify.
  • snap_to numpy.ndarray — The values to snap to.
  • distance_bounds Tuple[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.
Returns numpy.ndarray — An array with optimized spacing.

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.

Parameters
  • a numpy.ndarray — The arrays to promote.
Returns Tuple[numpy.ndarray, ...] — A tuple of arrays with a promoted rank.