Version:

salvus.toolbox.helpers.toolbox_helpers

salvus.toolbox.helpers.toolbox_helpers salvus toolbox helpers toolbox_helpers
Generic helper functions for this and that.

Functions

consecutive()

Return the indices of consecutive runs of numbers in an array.
SIGNATURE
def consecutive(a: npt.NDArray) -> list[npt.NDArray]: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
Array to inspect.
RETURNS
Return type: list[npt.NDArray]
List of arrays of consecutive indices.

copy_and_modify()

Copy a sequence and replace some elements in that copy.
SIGNATURE
def copy_and_modify(
    a: typing.Sequence[T], indices: list[tuple[int, T]] | tuple[int, T]
) -> typing.Sequence[T]: ...
ARGUMENTS
Required
a
Type:typing.Sequence[T]
Description:
The sequence to copy.
Required
indices
Type:list[tuple[int, T]] | tuple[int, T]
Description:
A collection of (index, value) of entries to replace.
RETURNS
Return type: typing.Sequence[T]
A new sequence with the specified entries replaced.

fill_index_space()

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.
SIGNATURE
def fill_index_space(
    a: npt.NDArray, direction: str = "forwards"
) -> npt.NDArray: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The index set to fill.
Optional
direction
Type:str
Default value:'forwards'
Description:
Whether to fill forwards or backwards.
RETURNS
Return type: npt.NDArray
The filled index set.

fix_spacing()

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.
SIGNATURE
def fix_spacing(a: npt.NDArray, snap_to: npt.NDArray) -> npt.NDArray: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The array of values to modify.
Required
snap_to
Type:npt.NDArray
Description:
The values to snap to.
RETURNS
Return type: npt.NDArray
An array with optimized spacing.

flatten()

Flatten a list of lists of arbitrary depth.
SIGNATURE
def flatten(i: tuple | list) -> typing.Generator[typing.Any, None, None]: ...
ARGUMENTS
Required
i
Type:tuple | list
Description:
The list of lists to flatten.
RETURNS
Return type: typing.Generator[typing.Any, None, None]
A generator that iterates over the flattened list.

group_by()

Group a numpy array by a predicate column.
SIGNATURE
def group_by(
    a: npt.NDArray, pred_col: int = 0, target_col: int = -1
) -> list[npt.NDArray]: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The array to group.
Optional
pred_col
Type:int
Default value:0
Description:
The predicate column. Defaults to 0.
Optional
target_col
Type:int
Default value:-1
Description:
The target column group. Defaults to -1.
RETURNS
Return type: list[npt.NDArray]
A list of arrays, each containing the values of the target column for each unique value in the predicate column.

interp1d_linear()

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.
SIGNATURE
def interp1d_linear(
    x: ArrayLike, y: ArrayLike, p: ArrayLike
) -> npt.NDArray: ...
ARGUMENTS
Required
x
Type:ArrayLike
Description:
x values of the function.
Required
y
Type:ArrayLike
Description:
y values of the function.
Required
p
Type:ArrayLike
Description:
Points to interpolate at.
RETURNS
Return type: npt.NDArray
Interpolated values.

linspace_intervals()

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.
SIGNATURE
def linspace_intervals(
    start: float, stop: float, min_num: int, interval_bounds: npt.NDArray
) -> npt.NDArray: ...
ARGUMENTS
Required
start
Type:float
Description:
The start value of the interval.
Required
stop
Type:float
Description:
The end value of the interval.
Required
min_num
Type:int
Description:
The minimum number of samples that should span the interval. Needs to be at least 2.
Required
interval_bounds
Type:npt.NDArray
Description:
The values between (start, stop) to hit.
EXCEPTIONS
ValueError
If stop <= start.
ValueError
If the interval bounds are not contained in (start, stop).
RETURNS
Return type: npt.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()

Insert an additional n entries between each entry of a.
Each of the n numbers will be spaced equidistantly between entries of a.
SIGNATURE
def linspace_intervals_equidistant(a: npt.NDArray, n: int) -> npt.NDArray: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The array to insert entries into.
Required
n
Type:int
Description:
The number of entries to insert between each value.
RETURNS
Return type: npt.NDArray
An array with new equidistant entries inserted.

optimize_spacing()

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.
SIGNATURE
def optimize_spacing(
    a: npt.NDArray,
    snap_to: npt.NDArray,
    distance_bounds: tuple[float, float] = (0.95, 1.05),
) -> npt.NDArray: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The array of values to modify.
Required
snap_to
Type:npt.NDArray
Description:
The values to snap to.
Optional
distance_bounds
Type:tuple[float, float]
Default value:(0.95, 1.05)
Description:
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
Return type: npt.NDArray
An array with optimized spacing.

promote()

Promote ranks of a collection of arrays to the highest in that collection.
Scalars will be promoted to at least rank 1.
SIGNATURE
def promote(a: npt.NDArray) -> tuple[npt.NDArray, ...]: ...
ARGUMENTS
Required
a
Type:npt.NDArray
Description:
The arrays to promote.
RETURNS
Return type: tuple[npt.NDArray, ...]
A tuple of arrays with a promoted rank.
PAGE CONTENTS