Source code for spikify.encoding.temporal.deconvolution.hough_spiker_algorithm
""".. raw:: html <h2>Hough Spiker Algorithm</h2>"""importnumpyasnpfromscipy.signal.windowsimportget_windowfrom.utilsimportWindowType
[docs]defhough_spiker(signal:np.ndarray,window_length:int|list[int],window_type:WindowType="boxcar")->np.ndarray:""" Perform spike detection using the Hough Spiker Algorithm (HSA). This function detects spikes in an input signal by performing a progressive subtraction operation, where the signal is compared with a convolution result using a boxcar filter. If the signal value exceeds the filter result, the signal is modified by subtracting the filter, and a spike is recorded. Refer to the :ref:`hough_spiker_algorithm_desc` for a detailed explanation of the HSA. **Code Example** .. code-block:: python import numpy as np from spikify.encoding.temporal.deconvolution import hough_spiker signal = np.array([0.1, 0.2, 4.1, 1.0, 3.0, 0.3, 0.1]) window_length = 3 spikes = hough_spiker(signal, window_length) .. doctest:: :hide: >>> import numpy as np >>> from spikify.encoding.temporal.deconvolution import hough_spiker >>> signal = np.array([0.1, 0.2, 4.1, 1.0, 3.0, 0.3, 0.1]) >>> window_length = 3 >>> spikes = hough_spiker(signal, window_length) >>> spikes array([0, 0, 1, 0, 0, 0, 0], dtype=int8) :param signal: The input signal to be analyzed. This should be a numpy ndarray. :type signal: numpy.ndarray :param window_length: The length of the boxcar filter window. Can be a int or a list of ints. :type window_length: int | list[int] :return: A 1D numpy array representing the detected spikes. :rtype: numpy.ndarray :raises ValueError: If the input signal is empty or if the window length is greater than the signal length. :raises TypeError: If the signal is not a numpy ndarray. """iflen(signal)==0:raiseValueError("Signal cannot be empty.")ifsignal.ndim==1:signal=signal.reshape(-1,1)S,F=signal.shapeifisinstance(window_length,int):window_lengths=[window_length]*Felifisinstance(window_length,list):ifnotall(isinstance(w,int)forwinwindow_length):raiseTypeError("All elements in window_length list must be integers.")window_lengths=window_lengthelse:raiseTypeError("Window lengths must be an int or a list of ints.")iflen(window_lengths)!=F:raiseValueError("Window lengths must match the number of features in the signal.")ifnp.any(np.array(window_lengths)>S):raiseValueError("All filter window sizes must be less than the length of the signal.")# Initialize the spike arrayspikes=np.zeros_like(signal,dtype=np.int8)# Create the boxcar filter windowfilter_window=[get_window(window_type,w)forwinwindow_lengths]# Copy the signal for modificationsignal_copy=np.copy(np.array(signal,dtype=np.float64))forfeatureinrange(F):# Iterate over the signal to detect spikesfortinrange(len(signal_copy[:,feature])-window_lengths[feature]+1):# Count how many values match or exceed the filter window valuesmatch_count=np.sum(signal_copy[t:t+window_lengths[feature],feature]>=filter_window[feature])# If all values match or exceed, a spike is detectedifmatch_count==window_lengths[feature]:signal_copy[t:t+window_lengths[feature],feature]-=filter_window[feature]spikes[t,feature]=1ifspikes.shape[-1]==1:spikes=spikes.flatten()returnspikes