Utility Functions¶
Contains utility functions and classes used in library.
-
spiegelib.core.utils.
spectrum_types
= ['complex', 'magnitude', 'power', 'magnitude_phase', 'power_phase']¶ Selection of spectrum outputs
-
spiegelib.core.utils.
convert_spectrum
(spectrum, type, dtype=<class 'numpy.float32'>, complex_dtype=<class 'numpy.complex64'>)¶ Convert a raw spectrum to magnitue, power, magnitude with phase, or power with phase. If output is magnitude or power, the output array shape is the same as the input. If either of the options with phase are selected, the output is the same shape but each element is replaced with a 2-element vector containing the magnitude or power in the first element and the phase in the second.
- Parameters
spectrum (np.ndarray) – array of complex values
type (str) – type of conversion to apply (‘magnitude’, ‘power’, ‘magnitude_phase’, ‘power_phase’)
dtype (np number type, optional) – number type of regular (non-complex) numbers to return, defaults to np.float32
complex_dtype (np complex type, optional) – complex number type, defaults to np.complex64
- Returns
converted spectrum
- Return type
np.ndarray
-
spiegelib.core.utils.
atoi
(text)¶ Convert string to integer if it is an integer, otherwise keep it a str
- Parameters
text (str) –
- Returns
int or str
-
spiegelib.core.utils.
natural_keys
(text)¶ Keys for sorting text in natural ordering. For text containing numbers, makes sure numbers are sorted in a natural way.
- Usage:
alist.sort(key=natural_keys) sorts in human order
From stackoverflow
- Parameters
text (str) – text to extract natural keys from
- Returns
List of text and integers separated from text to use as the key argument in a sort algorithm.
- Return type
list
-
class
spiegelib.core.utils.
NumpyNumberEncoder
(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)¶ Encodes Numpy number values into values that can be saved as JSON files.
Usage:
with open(path, 'w') as fp: json.dump(data, fp, indent=True, cls=NumpyNumberEncoder)