Mondaic

salvus.toolbox.helpers.toolbox_helpers

salvus.toolbox.helpers.toolbox_helpers salvus toolbox helpers toolbox_helpers

Generic helper functions for this and that.

Functions

consecutive()

def consecutive(a: npt.NDArray) -> list[npt.NDArray]: ...

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

Parameters
  • a npt.NDArray — Array to inspect.
Returns list[npt.NDArray] — List of arrays of consecutive indices.

copy_and_modify()

def copy_and_modify(
    a: typing.Sequence[T], indices: list[tuple[int, T]] | tuple[int, T]
) -> typing.Sequence[T]: ...

Copy a sequence and replace some elements in that copy.

Parameters
  • a typing.Sequence[T] — The sequence to copy.
  • indices list[tuple[int, T]] | tuple[int, T] — A collection of (index, value) of entries to replace.
Returns typing.Sequence[T] — A new sequence with the specified entries replaced.

fill_index_space()

def fill_index_space(
    a: npt.NDArray, direction: str = "forwards"
) -> npt.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 npt.NDArray — The index set to fill.
  • direction str — Whether to fill forwards or backwards.
Returns npt.NDArray — The filled index set.

fix_spacing()

def fix_spacing(a: npt.NDArray, snap_to: npt.NDArray) -> npt.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 npt.NDArray — The array of values to modify.
  • snap_to npt.NDArray — The values to snap to.
Returns npt.NDArray — An array with optimized spacing.

flatten()

def flatten(i: tuple | list) -> typing.Generator[typing.Any, None, None]: ...

Flatten a list of lists of arbitrary depth.

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

group_by()

def group_by(
    a: npt.NDArray, pred_col: int = 0, target_col: int = -1
) -> list[npt.NDArray]: ...

Group a numpy array by a predicate column.

Parameters
  • a npt.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[npt.NDArray] — A list of arrays, each containing the values of the target column for each unique value in the predicate column.

interp1d_linear()

def interp1d_linear(
    x: ArrayLike, y: ArrayLike, p: ArrayLike
) -> npt.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 ArrayLike — x values of the function.
  • y ArrayLike — y values of the function.
  • p ArrayLike — Points to interpolate at.
Returns npt.NDArray — Interpolated values.

linspace_intervals()

def linspace_intervals(
    start: float, stop: float, min_num: int, interval_bounds: npt.NDArray
) -> npt.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 npt.NDArray — The values between (start, stop) to hit.
Returns 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()

def linspace_intervals_equidistant(a: npt.NDArray, n: int) -> npt.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 npt.NDArray — The array to insert entries into.
  • n int — The number of entries to insert between each value.
Returns npt.NDArray — An array with new equidistant entries inserted.

optimize_spacing()

def optimize_spacing(
    a: npt.NDArray,
    snap_to: npt.NDArray,
    distance_bounds: tuple[float, float] = (0.95, 1.05),
) -> npt.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 npt.NDArray — The array of values to modify.
  • snap_to npt.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 npt.NDArray — An array with optimized spacing.

promote()

def promote(a: npt.NDArray) -> tuple[npt.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 npt.NDArray — The arrays to promote.
Returns tuple[npt.NDArray, ...] — A tuple of arrays with a promoted rank.