Python scipy.ndimage.filters.convolve1d() Examples

The following are 5 code examples of scipy.ndimage.filters.convolve1d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.ndimage.filters , or try the search function .
Example #1
Source File: _funcs.py    From oggm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def smooth1d(array, window_size=None, kernel='gaussian'):
    """Apply a centered window smoothing to a 1D array.

    Parameters
    ----------
    array : ndarray
        the array to apply the smoothing to
    window_size : int
        the size of the smoothing window
    kernel : str
        the type of smoothing (`gaussian`, `mean`)

    Returns
    -------
    the smoothed array (same dim as input)
    """

    # some defaults
    if window_size is None:
        if len(array) >= 9:
            window_size = 9
        elif len(array) >= 7:
            window_size = 7
        elif len(array) >= 5:
            window_size = 5
        elif len(array) >= 3:
            window_size = 3

    if window_size % 2 == 0:
        raise ValueError('Window should be an odd number.')

    if isinstance(kernel, str):
        if kernel == 'gaussian':
            kernel = gaussian(window_size, 1)
        elif kernel == 'mean':
            kernel = np.ones(window_size)
        else:
            raise NotImplementedError('Kernel: ' + kernel)
    kernel = kernel / np.asarray(kernel).sum()
    return filters.convolve1d(array, kernel, mode='mirror') 
Example #2
Source File: visualization.py    From caption-guided-saliency with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def temporal_feature_smoothing(video_features, kernel):
    #simple 1d convolution assuming that input is time x words x descriptors
    return convolve1d(video_features, weights = kernel, axis = 0) 
Example #3
Source File: extrapolation.py    From numdifftools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def convolve(sequence, rule, **kwds):
    """Wrapper around scipy.ndimage.convolve1d that allows complex input."""
    dtype = np.result_type(float, np.ravel(sequence)[0])
    seq = np.asarray(sequence, dtype=dtype)
    if np.iscomplexobj(seq):
        return (convolve1d(seq.real, rule, **kwds) + 1j * convolve1d(seq.imag, rule, **kwds))
    return convolve1d(seq, rule, **kwds) 
Example #4
Source File: locomotion_bends.py    From tierpsy-tracker with MIT License 4 votes vote down vote up
def h__foragingData(self, fps, nose_bend_angle_d, min_win_size):
        """
        Compute the foraging amplitude and angular speed.

        Parameters
        ----------
        fps :
        nose_bend_angle_d : [n_frames x 1]
        min_win_size : (scalar)

        Returns
        ---------------------------------------
        amplitudes : [1 x n_frames]
        speeds : [1 x n_frames]

        Notes
        ---------------------------------------
        Formerly [amps,speeds] = h__foragingData(nose_bend_angle_d,
                                                 min_win_size, fps)

        """
        if min_win_size > 0:
            # Clean up the signal with a gaussian filter.
            gauss_filter = utils.gausswin(2 * min_win_size + 1) / min_win_size
            nose_bend_angle_d = filters.convolve1d(nose_bend_angle_d,
                                                   gauss_filter,
                                                   cval=0,
                                                   mode='constant')

            # Remove partial data frames ...
            nose_bend_angle_d[:min_win_size] = np.NaN
            nose_bend_angle_d[-min_win_size:] = np.NaN

        # Calculate amplitudes
        amplitudes = self.h__getAmplitudes(nose_bend_angle_d)
        assert(np.shape(nose_bend_angle_d) == np.shape(amplitudes))

        # Calculate angular speed
        # Compute the speed centered between the back and front foraging movements.
        #
        # TODO: fix the below comments to conform to 0-based indexing
        # I believe I've fixed the code already.  - @MichaelCurrie
        #  1     2    3
        # d1    d2     d1 = 2 - 1,   d2 = 3 - 2
        #     x        assign to x, avg of d1 and d2

        #???? - why multiply and not divide by fps????

        d_data = np.diff(nose_bend_angle_d) * fps
        speeds = np.empty(amplitudes.size) * np.NaN
        # This will leave the first and last frame's speed as NaN:
        speeds[1:-1] = (d_data[:-1] + d_data[1:]) / 2

        # Propagate NaN for speeds to amplitudes
        amplitudes[np.isnan(speeds)] = np.NaN

        return amplitudes, speeds 
Example #5
Source File: locomotion_bends.py    From tierpsy-tracker with MIT License 4 votes vote down vote up
def h__foragingData(self, fps, nose_bend_angle_d, min_win_size):
        """
        Compute the foraging amplitude and angular speed.

        Parameters
        ----------
        fps :
        nose_bend_angle_d : [n_frames x 1]
        min_win_size : (scalar)

        Returns
        ---------------------------------------
        amplitudes : [1 x n_frames]
        speeds : [1 x n_frames]

        Notes
        ---------------------------------------
        Formerly [amps,speeds] = h__foragingData(nose_bend_angle_d,
                                                 min_win_size, fps)

        """
        if min_win_size > 0:
            # Clean up the signal with a gaussian filter.
            gauss_filter = utils.gausswin(2 * min_win_size + 1) / min_win_size
            nose_bend_angle_d = filters.convolve1d(nose_bend_angle_d,
                                                   gauss_filter,
                                                   cval=0,
                                                   mode='constant')

            # Remove partial data frames ...
            nose_bend_angle_d[:min_win_size] = np.NaN
            nose_bend_angle_d[-min_win_size:] = np.NaN

        # Calculate amplitudes
        amplitudes = self.h__getAmplitudes(nose_bend_angle_d)
        assert(np.shape(nose_bend_angle_d) == np.shape(amplitudes))

        # Calculate angular speed
        # Compute the speed centered between the back and front foraging movements.
        #
        #  0     1    2
        #    d1    d2     d1 = 1 - 0,   d2 = 2 - 1
        #       x        assign to x, avg of d1 and d2

        #???? - why multiply and not divide by fps????

        d_data = np.diff(nose_bend_angle_d) * fps
        speeds = np.empty(amplitudes.size) * np.NaN
        # This will leave the first and last frame's speed as NaN:
        speeds[1:-1] = (d_data[:-1] + d_data[1:]) / 2

        # Propagate NaN for speeds to amplitudes
        amplitudes[np.isnan(speeds)] = np.NaN

        return amplitudes, speeds