utils.py

Provides utilities for running nussl algorithms that do not belong to any specific algorithm or that are shared between algorithms.

nussl.utils.find_peak_indices(input_array, n_peaks, min_dist=None, do_min=False, threshold=0.5)

This function will find the indices of the peaks of an input n-dimensional numpy array. This can be configured to find max or min peak indices, distance between the peaks, and a lower bound, at which the algorithm will stop searching for peaks (or upper bound if searching for max). Used exactly the same as find_peak_values().

This function currently only accepts 1-D and 2-D numpy arrays.

Notes

  • This function only returns the indices of peaks. If you want to find peak values,

use find_peak_values().

  • min_dist can be an int or a tuple of length 2.
    If input_array is 1-D, min_dist must be an integer. If input_array is 2-D, min_dist can be an integer, in which case the minimum distance in both dimensions will be equal. min_dist can also be a tuple if you want each dimension to have a different minimum distance between peaks. In that case, the 0th value in the tuple represents the first dimension, and the 1st value represents the second dimension in the numpy array.

See also

:: find_peak_values()

Parameters:
  • input_array – a 1- or 2- dimensional numpy array that will be inspected.
  • n_peaks – (int) maximum number of peaks to find
  • min_dist – (int) minimum distance between peaks. Default value: len(input_array) / 4
  • do_min – (bool) if True, finds indices at minimum value instead of maximum
  • threshold – (float) the value (scaled between 0.0 and 1.0)
Returns:

peak_indices – (list) list of the indices of the peak values

nussl.utils.find_peak_values(input_array, n_peaks, min_dist=None, do_min=False, threshold=0.5)

Finds the values of the peaks in a 1-D or 2-D numpy array. Used exactly the same as find_peak_indices(). This function will find the values of the peaks of an input n-dimensional numpy array.

This can be configured to find max or min peak values, distance between the peaks, and a lower bound, at which the algorithm will stop searching for peaks (or upper bound if searching for max).

This function currently only accepts 1-D and 2-D numpy arrays.

Notes

  • This function only returns the indices of peaks. If you want to find peak values,

use find_peak_indices().

  • min_dist can be an int or a tuple of length 2.
    If input_array is 1-D, min_dist must be an integer. If input_array is 2-D, min_dist can be an integer, in which case the minimum distance in both dimensions will be equal. min_dist can also be a tuple if you want each dimension to have a different minimum distance between peaks. In that case, the 0th value in the tuple represents the first dimension, and the 1st value represents the second dimension in the numpy array.

See also

:: find_peak_indices()

Parameters:
  • input_array – a 1- or 2- dimensional numpy array that will be inspected.
  • n_peaks – (int) maximum number of peaks to find
  • min_dist – (int) minimum distance between peaks. Default value: len(input_array) / 4
  • do_min – (bool) if True, finds indices at minimum value instead of maximum
  • threshold – (float) the value (scaled between 0.0 and 1.0)
Returns:

peak_values – (list) list of the values of the peak values

nussl.utils.json_ready_numpy_array(array)

Adapted from: http://stackoverflow.com/a/27948073/5768001 :param array: np array to make json ready.

Returns:

nussl.utils.json_serialize_numpy_array(array)

Returns a JSON string of the numpy array.

Notes

The generated JSON strings can be converted back to numpy arrays with load_numpy_json()

Parameters:array – (numpy array) any numpy array to convert to JSON
Returns:(string) JSON-ified numpy array.

See also

load_numpy_json()

nussl.utils.load_numpy_json(array_json)

Turns a JSON-ified numpy array back into a regular numpy array.

Notes

This function is only guaranteed to work with JSON generated by json_serialize_numpy_array()

Parameters:array_json – (string) JSON-ified nump array
Returns:(numpy array) numpy array from the input JSON string

See also

json_serialize_numpy_array()

nussl.utils.json_numpy_obj_hook(dct)

Decodes a previously encoded numpy ndarray with proper shape and dtype from: http://stackoverflow.com/a/27948073/5768001 :param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray

nussl.utils.add_mismatched_arrays(array1, array2, truncate=False)

Will add two 1D numpy arrays of different length. If :param:`truncate` is False, it will expand the resultant array to the larger of the two, if True it will truncate the resultant array to the smaller of the two.

Parameters:
  • array1 – (np.ndarray) 1D numeric numpy array
  • array2 – (np.ndarray) 1D numeric numpy array
  • truncate – (bool) If ``Tru``e, will truncate the resultant array to the smaller of the two
Returns:

(np.ndarray) – A 1D numpy array that is the element-wise sum of the two input arrays.

nussl.utils.add_mismatched_arrays2D(array1, array2, truncate=False)

Will add two 2D numpy arrays of different length. If :param:`truncate` is False, it will expand the resultant array to the larger of the two, if True it will truncate the resultant array to the smaller of the two.

Parameters:
  • array1 – (np.array) 2D numeric numpy array.
  • array2 – (np.array) 2D numeric numpy array.
  • truncate – (bool) If True, will truncate the resultant array to the smaller of the two.
Returns:

(np.ndarray) – A 2D numpy array that is the element-wise sum of the two input arrays.

nussl.utils.complex_randn(shape)

Returns a complex-valued numpy array of random values with shape :param:`shape`.

Parameters:shape (tuple) – Tuple of ints that will be the shape of the resultant complex numpy array.
Returns:(np.ndarray) – a complex-valued numpy array of random values with shape shape
nussl.utils.print_all_separation_algorithms()

Displays the list of all separation algorithms in nussl to the console.

nussl.utils.verify_audio_signal_list_lax(audio_signal_list)

Verifies that an input (:param:`audio_signal_list`) is a list of AudioSignal objects. If not so, attempts to correct the list (if possible) and returns the corrected list.

Parameters:audio_signal_list (list) – List of AudioSignal objects
Returns:audio_signal_list (list) – Verified list of AudioSignal objects.
nussl.utils.verify_audio_signal_list_strict(audio_signal_list)

Verifies that an input (:param:`audio_signal_list`) is a list of AudioSignal objects and that they all have the same sample rate and same number of channels. If not true, attempts to correct the list (if possible) and returns the corrected list.

Parameters:audio_signal_list (list) – List of AudioSignal objects
Returns:audio_signal_list (list) – Verified list of AudioSignal objects, that all have the same sample rate and number of channels.
nussl.utils.verify_separation_base_list(separation_list)

Verifies that all items in :param:`separation_list` are SeparationBase -derived objects. If not so, attempts to correct the list if possible and returns the corrected list.

Parameters:separation_list (list) – List of SeparationBase -derived objects
Returns:separation_list (list) – Verified list of SeparationBase -derived objects
nussl.utils.verify_mask_separation_base_list(mask_separation_list)

Verifies that all items in :param:`separation_list` are MaskSeparationBase -derived objects. If not so, attempts to correct the list if possible and returns the corrected list.

Parameters:mask_separation_list (list) – List of MaskSeparationBase -derived objects
Returns:separation_list (list) – Verified list of MaskSeparationBase -derived objects