apollon.tools module

apollon/tools.py – Common tool library.


assert_array Raise if array does not match given params.

L1_Norm Compute L1_Norm. normalize Return _normalized version of input. in2out Create a save from an input path. offdiag Access to off-diagonal elements of square array. rowdiag kth array diagonal sorted by array’s rows. scale Scale array between bounds. smooth_stat Return smoothed input. standardize Scale to zero mean and unit standard deviation. time_stamp Return time stamp. within Test wether val is in window. within_any Test wether val is in any of windows. array2d_fsum Sum array entry with machine precision.

apollon.tools.L1_Norm(arr: numpy.ndarray) → float

Compute the L_1 norm of input vector x.

This implementation is generally faster than np.norm(arr, ord=1).

apollon.tools.array2d_fsum(arr: numpy.ndarray, axis: int = 0) → numpy.ndarray

Return math.fsum along the specifyed axis.

apollon.tools.assert_and_pass(assert_func, arg)

Call assert_func with arg and return arg. Additionally allow arg to be None.

apollon.tools.assert_array(arr: numpy.ndarray, ndim: int, size: int, lower_bound: float = -inf, upper_bound: float = inf, name: str = 'arr')

Raise an error if shape of arr does not match given arguments.

  • arr (np.ndarray) –
  • ndim (int) –
  • size (int) –
  • lower_bound (float) –
  • upper_bound (float) –


apollon.tools.jsonify(inp: Any)

Returns a representation of inp that can be serialized to JSON.

This method passes through Python objects of type dict, list, str, int float, True, False, and None. Tuples will be converted to list by the JSON encoder. Numpy arrays will be converted to list using thier .to_list() method. On all other types, the method will try to call str() and raises on error.

Parameters:inp – Input to be jsonified.
Returns:Jsonified input.
apollon.tools.normalize(arr, mode='array')

Normalize an arbitrary array_like.


arr (numerical array-like) Input signal. axis (str) Normalization mode:

‘array’ -> (default) Normalize whole array. ‘rows’ -> Normalize each row separately. ‘cols’ -> Normalize each col separately.
Returns:(array) Normalized input.
apollon.tools.rowdiag(v, k=0)

Get or set k’th diagonal of square matrix.

Get the k’th diagonal of a square matrix sorted by rows or construct a sqare matrix with the elements of v as the main diagonal of the second and third dimension.

v (array) Square matrix. k (int) Number diagonal.
Returns:(1d-array) Values of diagonal.
apollon.tools.scale(x, new_min=0, new_max=1, axis=-1)

Scale x between new_min and new_max.

x (np.array) Array to be scaled. new_min (real numerical) Lower bound. new_max (real numerical) Upper bound.
Returns:(np.ndarray) One-dimensional array of transformed values.

Smooth the signal based on its mean and standard deviation.

sig (array-like) Input signal.
Returns:(ndarray) smoothed input signal.
apollon.tools.standardize(x: numpy.ndarray) → numpy.ndarray

Retrun z-transformed values of x.

x (array) Input values
Returns:(array) z-transformed values
apollon.tools.time_stamp() → str

Return default time stamp.

apollon.tools.within(val: float, bounds: Tuple[float, float]) → bool

Return True if x is in window.

Parameters:val (float) –
(bool) True if val is within bounds.
apollon.tools.within_any(val: float, windows: numpy.ndarray) → bool

Return True if x is in any of the given windows