utils.py¶

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

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

Will add two 1D numpy arrays of different length. If 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) 1D numeric array array2 – (np.array) 1D numeric array truncate – (Bool) If True, will truncate the resultant array to the smaller of the two One 1D array added from the two input arrays
`utils.``add_mismatched_arrays2D`(array1, array2, truncate=False)

Will add two 2D numpy arrays of different length. If 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 array array2 – (np.array) 2D numeric array truncate – (Bool) If True, will truncate the resultant array to the smaller of the two One 2D array added from the two input arrays
`utils.``complex_randn`(shape)

Returns a complex-valued numpy array of random values with shape shape :param 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

`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). Use 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.

:: 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) peak_indices – (list) list of the indices of the peak values
`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. Use 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_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.

:: 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) peak_values – (list) list of the values of the peak values
`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

`utils.``json_ready_numpy_array`(array)

Returns:

`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 (string) JSON-ified numpy array.

`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 (numpy array) numpy array from the input JSON string