round_to_vals

round_to_vals#

stimupy.utils.utils.round_to_vals(arr, vals, mode='nearest')#

Round each element of array to closest match in provided values

For each element in the input arr, find the closest value from the provided vals and replace the element with this closest value. If the element is equidistant to two values, the smaller value is chosen.

Parameters:
  • arr (np.ndarray) – array to be rounded

  • vals (Sequence(float, ...)) – values to which array will be rounded

  • mode (["nearest", "floor", "ceil"], optional) – rounding mode. Default is “nearest”.

Returns:

out_arr – Rounded output array

Return type:

np.ndarray

Raises:

ValueError – If mode is not one of [“nearest”, “floor”, “ceil”]. If arr contains values outside the bounds of vals when mode is “floor” or “ceil”.

Examples

>>> arr = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
>>> vals = [1, 3, 5]
>>> round_to_vals(arr, vals)
array([1., 3., 3., 5., 5.])