apollon.tools module¶
Common tool library. Licensed under the terms of the BSD-3-Clause license.
Copyright (C) 2019 Michael Blaß
-
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.
assert_and_pass
(func: Callable, arg: Any)¶ Call
func`
witharg
and returnarg
. Additionally allow arg to beNone
.- Parameters
func – Test function.
arg – Function argument.
- Returns
Result of
func(arg)
.
-
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.
- Parameters
arr – Array to test.
ndim – Expected number of dimensions.
size – Expected total number of elements.
lower_bound – Lower bound for array elements.
upper_bound – Upper bound for array elements.
- Raises
ValueError –
-
apollon.tools.
fsum
(arr: numpy.ndarray, axis: Optional[int] = None, keepdims: bool = False, dtype: str = 'float64') → numpy.ndarray¶ Return math.fsum along the specifyed axis.
This function supports at most two-dimensional arrays.
- Parameters
arr – Input array.
axis – Reduction axis.
keepdims – If
True
, the output will have the same dimensionality as the input.dtype – Numpy data type.
- Returns
Sums along axis.
-
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: numpy.ndarray, mode: str = 'array')¶ Normalize an arbitrary array_like.
- Parameters
arr – Input signal.
mode – Normalization mode: ‘array’ -> (default) Normalize whole array. ‘rows’ -> Normalize each row separately. ‘cols’ -> Normalize each col separately.
- Returns
Normalized input.
-
apollon.tools.
pca
(data: numpy.ndarray, n_comps: int = 2) → Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]¶ Compute a PCA based on
numpy.linalg.svd
.Interanlly,
data
will be centered but not scaled.- Parameters
data – Data set.
n_comps – Number of principal components.
- Returns
n_comps
largest singular values,n_comps
largest eigen vectors, transformed input data.
-
apollon.tools.
rowdiag
(arr: numpy.ndarray, k: int = 0) → numpy.ndarray¶ 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.- Parameters
arr – Square array.
k – Number of diagonal.
- Returns
Flattened diagonal.
-
apollon.tools.
scale
(arr: numpy.ndarray, new_min: int = 0, new_max: int = 1, axis: int = - 1) → numpy.ndarray¶ Scale
arr
betweennew_min
andnew_max
.- Parameters
arr – Array to be scaled.
new_min – Lower bound.
new_max – Upper bound.
- Returns
One-dimensional array of transformed values.
-
apollon.tools.
smooth_stat
(arr: numpy.ndarray) → numpy.ndarray¶ Smooth the signal based on its mean and standard deviation.
- Parameters
arr – Input signal.
- Returns
smoothed input signal.
-
apollon.tools.
standardize
(arr: numpy.ndarray) → numpy.ndarray¶ Retrun z-transformed values of
arr
.- Parameters
arr – Input array.
- Returns
z-transformed values
-
apollon.tools.
time_stamp
(fmt: Optional[str] = None) → str¶ Report call time as UTC time stamp.
If
fmt
is not given, this function returns time stampes in ISO 8601 format.- Parameters
fmt – Format specification.
- Returns
Time stamp according to
fmt
.
-
apollon.tools.
within
(val: float, bounds: Tuple[float, float]) → bool¶ Return True if x is in window.
- Parameters
val – Value to test.
- Returns
True
, ifval
is withinbounds
.
-
apollon.tools.
within_any
(val: float, windows: numpy.ndarray) → bool¶ Return True if x is in any of the given windows.
- Parameters
val – Value to test.
windows – Array of bounds.
Returns: