Python numpy.lib.stride_tricks.as_strided() Examples

The following are code examples for showing how to use numpy.lib.stride_tricks.as_strided(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: gnocchi   Author: gnocchixyz   File: operations.py    Apache License 2.0 6 votes vote down vote up
def handle_rolling(agg, granularity, timestamps, values, is_aggregated,
                   references, window):
    if window > len(values):
        raise exceptions.UnAggregableTimeseries(
            references,
            "Rolling window '%d' is greater than serie length '%d'" %
            (window, len(values))
        )

    timestamps = timestamps[window - 1:]
    values = values.T
    # rigtorp.se/2011/01/01/rolling-statistics-numpy.html
    shape = values.shape[:-1] + (values.shape[-1] - window + 1, window)
    strides = values.strides + (values.strides[-1],)
    new_values = AGG_MAP[agg](as_strided(values, shape=shape, strides=strides),
                              axis=-1)
    if agg.startswith("rate:"):
        timestamps = timestamps[1:]
    return granularity, timestamps, new_values.T, is_aggregated 
Example 2
Project: speech_density   Author: kastnerkyle   File: midify.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def apply_kaiserbessel_window(X, alpha=6.5):
    """
    Apply a Kaiser-Bessel window to X.

    Parameters
    ----------
    X : ndarray, shape=(n_samples, n_features)
        Input array of samples

    alpha : float, optional (default=6.5)
        Tuning parameter for Kaiser-Bessel function. alpha=6.5 should make
        perfect reconstruction possible for MDCT.

    Returns
    -------
    X_windowed : ndarray, shape=(n_samples, n_features)
        Windowed version of X.
    """
    beta = np.pi * alpha
    win = sg.kaiser(X.shape[1], beta)
    row_stride = 0
    col_stride = win.itemsize
    strided_win = as_strided(win, shape=X.shape,
                             strides=(row_stride, col_stride))
    return X * strided_win 
Example 3
Project: vnpy_crypto   Author: birforce   File: cov_struct.py    MIT License 6 votes vote down vote up
def initialize(self, model):

        super(CategoricalCovStruct, self).initialize(model)

        self.nlevel = len(model.endog_values)
        self._ncut = self.nlevel - 1

        from numpy.lib.stride_tricks import as_strided
        b = np.dtype(np.int64).itemsize

        ibd = []
        for v in model.endog_li:
            jj = np.arange(0, len(v) + 1, self._ncut, dtype=np.int64)
            jj = as_strided(jj, shape=(len(jj) - 1, 2), strides=(b, b))
            ibd.append(jj)

        self.ibd = ibd 
Example 4
Project: OnePy_Old   Author: Chandlercjy   File: statistics.py    MIT License 6 votes vote down vote up
def _windowed_view(x, window_size):
    """Create a 2d windowed view of a 1d array.

    `x` must be a 1d numpy array.

    `numpy.lib.stride_tricks.as_strided` is used to create the view.
    The data is not copied.

    Example:

    >>> x = np.array([1, 2, 3, 4, 5, 6])
    >>> _windowed_view(x, 3)
    array([[1, 2, 3],
           [2, 3, 4],
           [3, 4, 5],
           [4, 5, 6]])
    """
    y = as_strided(x, shape=(x.size - window_size + 1, window_size),
                   strides=(x.strides[0], x.strides[0]))
    return y 
Example 5
Project: qualipy   Author: vismantic-ohtuprojekti   File: blurred_context.py    MIT License 6 votes vote down vote up
def blurmap(img):
    """Constructs a blurmap from an image.

    :param img: the image matrix
    :type img: numpy.ndarray
    :returns: numpy.ndarray
    """
    patch_size = 5

    # for each pixel, create a view to a patch_size x patch_size
    # matrix where the pixel is in the center of the matrix
    patches = as_strided(img,
                         shape=(img.shape[0] - patch_size + 1,
                                img.shape[1] - patch_size + 1,
                                patch_size, patch_size),
                         strides=img.strides * 2)

    svd = numpy.linalg.svd(patches, full_matrices=False, compute_uv=False)
    return numpy.apply_along_axis(blurry_degree, 2, svd) 
Example 6
Project: mlens   Author: flennerhag   File: pool.py    MIT License 6 votes vote down vote up
def _strided_from_memmap(filename, dtype, mode, offset, order, shape, strides,
                         total_buffer_len):
    """Reconstruct an array view on a memory mapped file."""
    if mode == 'w+':
        # Do not zero the original data when unpickling
        mode = 'r+'

    if strides is None:
        # Simple, contiguous memmap
        return make_memmap(filename, dtype=dtype, shape=shape, mode=mode,
                           offset=offset, order=order)
    else:
        # For non-contiguous data, memmap the total enclosing buffer and then
        # extract the non-contiguous view with the stride-tricks API
        base = make_memmap(filename, dtype=dtype, shape=total_buffer_len,
                           mode=mode, offset=offset, order=order)
        return as_strided(base, shape=shape, strides=strides) 
Example 7
Project: BrainSpace   Author: MICA-MNI   File: base.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def in_notebook():
    is_nb = False
    if has_ipython:
        try:
            ipy = IPython.get_ipython()
            if ipy is not None:
                is_nb = type(ipy).__module__.startswith('ipykernel.')
        except:
            pass
    return is_nb


# def _create_grid(nrow, ncol):
#     dx, dy = 1 / ncol, 1 / nrow
#     x_min = np.tile(np.arange(0, 1, dx), nrow)
#     x_max = x_min + dx
#     y_min = np.repeat(np.arange(0, 1, dy), ncol)[::-1]
#     y_max = y_min + dy
#     g = np.column_stack([x_min, y_min, x_max, y_max])
#
#     strides = (4*g.itemsize*ncol, 4*g.itemsize, g.itemsize)
#     return as_strided(g, shape=(nrow, ncol, 4), strides=strides) 
Example 8
Project: VapourSynth-Super-Resolution-Helper   Author: AlphaAtlas   File: muvsfunc_numpy.py    MIT License 6 votes vote down vote up
def SigmaFilter_core(img_2D, radius=3, thr=0.01):
    """Sigma filter in NumPy

    The special step to handle sharp spot noise described in (4) in the paper is ignored 
    for better detail preservation and lower computation.

    For detailed documentation, please refer to the documentation of "SigmaFilter" funcion in current library.

    """

    pad_img = np.pad(img_2D, pad_width=radius, mode='constant')
    # img_view = as_strided(pad_img, shape=(*img_2D.shape, 2*radius+1, 2*radius+1), strides=pad_img.strides*2)
    img_view = get_blockwise_view(pad_img, block_size=2*radius+1, strides=1)
    select = np.where(np.absolute(img_view - img_2D[..., np.newaxis, np.newaxis]) < thr, img_view, 0) # Choose pixels inside the intensity range
    flt = np.sum(select, axis=(2, 3)) / (np.count_nonzero(select, axis=(2, 3)) + 1e-7) # Compute the average

    return flt 
Example 9
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def sine_window(X):
    """
    Apply a sinusoid window to X.

    Parameters
    ----------
    X : ndarray, shape=(n_samples, n_features)
        Input array of samples

    Returns
    -------
    X_windowed : ndarray, shape=(n_samples, n_features)
        Windowed version of X.
    """
    i = np.arange(X.shape[1])
    win = np.sin(np.pi * (i + 0.5) / X.shape[1])
    row_stride = 0
    col_stride = win.itemsize
    strided_win = as_strided(win, shape=X.shape,
                             strides=(row_stride, col_stride))
    return X * strided_win 
Example 10
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def kaiserbessel_window(X, alpha=6.5):
    """
    Apply a Kaiser-Bessel window to X.

    Parameters
    ----------
    X : ndarray, shape=(n_samples, n_features)
        Input array of samples

    alpha : float, optional (default=6.5)
        Tuning parameter for Kaiser-Bessel function. alpha=6.5 should make
        perfect reconstruction possible for DCT.

    Returns
    -------
    X_windowed : ndarray, shape=(n_samples, n_features)
        Windowed version of X.
    """
    beta = np.pi * alpha
    win = sg.kaiser(X.shape[1], beta)
    row_stride = 0
    col_stride = win.itemsize
    strided_win = as_strided(win, shape=X.shape,
                             strides=(row_stride, col_stride))
    return X * strided_win 
Example 11
Project: RPGOne   Author: RTHMaK   File: pool.py    Apache License 2.0 6 votes vote down vote up
def _strided_from_memmap(filename, dtype, mode, offset, order, shape, strides,
                         total_buffer_len):
    """Reconstruct an array view on a memory mapped file."""
    if mode == 'w+':
        # Do not zero the original data when unpickling
        mode = 'r+'

    if strides is None:
        # Simple, contiguous memmap
        return make_memmap(filename, dtype=dtype, shape=shape, mode=mode,
                           offset=offset, order=order)
    else:
        # For non-contiguous data, memmap the total enclosing buffer and then
        # extract the non-contiguous view with the stride-tricks API
        base = make_memmap(filename, dtype=dtype, shape=total_buffer_len,
                           mode=mode, offset=offset, order=order)
        return as_strided(base, shape=shape, strides=strides) 
Example 12
Project: Spoken-language-identification   Author: YerevaNN   File: augment_data.py    MIT License 5 votes vote down vote up
def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
    win = window(frameSize)
    hopSize = int(frameSize - np.floor(overlapFac * frameSize))
    
    # zeros at beginning (thus center of 1st window should be for sample nr. 0)
    samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)    
    # cols for windowing
    cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
    # zeros at end (thus samples can be fully covered by frames)
    samples = np.append(samples, np.zeros(frameSize))
    
    frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
    frames *= win
    
    return np.fft.rfft(frames) 
Example 13
Project: Spoken-language-identification   Author: YerevaNN   File: create_spectrograms.py    MIT License 5 votes vote down vote up
def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
    win = window(frameSize)
    hopSize = int(frameSize - np.floor(overlapFac * frameSize))
    
    # zeros at beginning (thus center of 1st window should be for sample nr. 0)
    samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)    
    # cols for windowing
    cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
    # zeros at end (thus samples can be fully covered by frames)
    samples = np.append(samples, np.zeros(frameSize))
    
    frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
    frames *= win
    
    return np.fft.rfft(frames) 
Example 14
Project: speech_density   Author: kastnerkyle   File: midify.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def halfoverlap(X, window_size):
    """
    Create an overlapped version of X using 50% of window_size as overlap.

    Parameters
    ----------
    X : ndarray, shape=(n_samples,)
        Input signal to window and overlap

    window_size : int
        Size of windows to take

    Returns
    -------
    X_strided : shape=(n_windows, window_size)
        2D array of overlapped X
    """
    if window_size % 2 != 0:
        raise ValueError("Window size must be even!")
    window_step = window_size // 2
    # Make sure there are an even number of windows before stridetricks
    append = np.zeros((window_size - len(X) % window_size))
    X = np.hstack((X, append))
    num_frames = len(X) // window_step - 1
    row_stride = X.itemsize * window_step
    col_stride = X.itemsize
    X_strided = as_strided(X, shape=(num_frames, window_size),
                           strides=(row_stride, col_stride))
    return X_strided 
Example 15
Project: LaserTOF   Author: kyleuckert   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 16
Project: LaserTOF   Author: kyleuckert   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 17
Project: LaserTOF   Author: kyleuckert   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 18
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 19
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 20
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 21
Project: recruit   Author: Frank-qlu   File: index_tricks.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 22
Project: recruit   Author: Frank-qlu   File: test_mem_overlap.py    Apache License 2.0 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 23
Project: recruit   Author: Frank-qlu   File: test_mem_overlap.py    Apache License 2.0 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 24
Project: jr-tools   Author: kingjr   File: base.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def cross_correlation(x, y, maxlag):
    """
    Cross correlation with a maximum number of lags.

    `x` and `y` must be one-dimensional numpy arrays with the same length.

    This computes the same result as
        numpy.correlate(x, y, mode='full')[len(a)-maxlag-1:len(a)+maxlag]

    The return vaue has length 2*maxlag + 1.

    Author: http://stackoverflow.com/questions/30677241
            Warren Weckesser
    """
    from numpy.lib.stride_tricks import as_strided

    def _check_arg(x, xname):
        x = np.asarray(x)
        if x.ndim != 1:
            raise ValueError('%s must be one-dimensional.' % xname)
        return x

    x = _check_arg(x, 'x')
    y = _check_arg(y, 'y')
    py = np.pad(y.conj(), 2*maxlag, mode='constant')
    T = as_strided(py[2*maxlag:], shape=(2*maxlag+1, len(y) + 2*maxlag),
                   strides=(-py.strides[0], py.strides[0]))
    px = np.pad(x, maxlag, mode='constant')
    return T.dot(px) 
Example 25
Project: closed-form-matting   Author: MarcoForte   File: closed_form_matting.py    MIT License 5 votes vote down vote up
def _rolling_block(A, block=(3, 3)):
    """Applies sliding window to given matrix."""
    shape = (A.shape[0] - block[0] + 1, A.shape[1] - block[1] + 1) + block
    strides = (A.strides[0], A.strides[1]) + A.strides
    return as_strided(A, shape=shape, strides=strides) 
Example 26
Project: hierarchical-sparse-coding   Author: sbrodeur   File: modeling.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extractWindows(sequence, indices, width, centered=False):
    assert sequence.ndim == 1 or sequence.ndim == 2
    assert width > 0 and width < sequence.shape[0]

    if sequence.ndim == 1:
        sequence = sequence[:,np.newaxis]
        squeezeOutput = True
    else:
        squeezeOutput = False
    
    # Create a strided view of the sequence and using indexing to extract windows
    s = as_strided(sequence, shape=((sequence.shape[0] - width + 1), width, sequence.shape[1]),
                   strides=(sequence.strides[0], sequence.strides[0], sequence.strides[1]))
    
    if centered:
        if np.mod(width, 2) == 0:
            # Even
            windows = s[indices - (width/2-1)]
        else:
            # Odd
            windows = s[indices - (width/2)]
    else:
        windows = s[indices]
        
    if squeezeOutput:
        windows = np.squeeze(windows, axis=2)
    return windows 
Example 27
Project: hierarchical-sparse-coding   Author: sbrodeur   File: modeling.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extractWindowsBatch(sequences, indices, width, centered=False):
    assert sequences.ndim == 2 or sequences.ndim == 3
    assert width > 0 and width <= sequences.shape[1]
    
    if sequences.ndim == 2:
        sequences = sequences[:,:,np.newaxis]
        squeezeOutput = True
    else:
        squeezeOutput = False
    
    # Create a strided view of the sequence and using indexing to extract windows
    s = as_strided(sequences, shape=(sequences.shape[0], (sequences.shape[1] - width + 1), width, sequences.shape[2]),
                  strides=(sequences.strides[0], sequences.strides[1], sequences.strides[1], sequences.strides[2]))

    i = np.arange(s.shape[0])
    if centered:
        if np.mod(width, 2) == 0:
            # Even
            windows = s[i, indices - (width/2-1)]
        else:
            # Odd
            windows = s[i, indices - (width/2)]
    else:
        windows = s[i, indices]
        
    if squeezeOutput:
        windows = np.squeeze(windows, axis=2)
        
    return windows 
Example 28
Project: hierarchical-sparse-coding   Author: sbrodeur   File: modeling.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def convolve1d(sequence, filters, padding='valid'):

    sequence = np.atleast_2d(sequence).reshape((sequence.shape[0],-1))
    
    width = filters.shape[1]
    if padding == 'valid':
        # Nothing to do
        pass
    elif padding == 'same':
        # Pad sequences
        if np.mod(width, 2) == 0:
            # Even filter size
            sequence = np.pad(sequence, [(width/2-1, width/2), (0,0)], mode='constant')
        else:
            # Odd filter size
            sequence = np.pad(sequence, [(width/2, width/2), (0,0)], mode='constant')
    else:
        raise Exception('Padding not supported: %s' % (padding))

    if filters.ndim == 2:
        nbFeatures = 1
    else:
        nbFeatures = filters.shape[-1]
    assert nbFeatures == sequence.shape[-1]

    # TODO: when to use scipy.signal.fftconvolve?
    # convolve fiters by the signal
    # TODO: broadcast sequence over filter axis to avoid the loop
#     c = [scipy.signal.fftconvolve(sequence, filter[::-1, ::-1], mode='valid') for filter in filters]
#     c = np.squeeze(np.stack(c, axis=-1), axis=1)
    
    # Create a strided view of the sequence
    w = as_strided(sequence, shape=((sequence.shape[0] - width + 1), sequence.shape[1], width),
                  strides=(sequence.strides[0], sequence.strides[1], sequence.strides[0])) 
     
    # w has shape [length, features, width]
    nbTotalFeatureDim = np.prod(filters.shape[1:])
    c = np.dot(w.reshape((w.shape[0],nbTotalFeatureDim)),
               filters.T.reshape(nbTotalFeatureDim, filters.shape[0]))
    return c 
Example 29
Project: hierarchical-sparse-coding   Author: sbrodeur   File: modeling.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def convolve1d_batch(sequences, filters, padding='valid'):

    sequences = np.atleast_3d(sequences).reshape((sequences.shape[0], sequences.shape[1], -1))

    width = filters.shape[1]
    if padding == 'valid':
        # Nothing to do
        pass
    elif padding == 'same':
        # Pad sequences
        if np.mod(width, 2) == 0:
            # Even filter size
            sequences = np.pad(sequences, [(0,0), (width/2-1, width/2), (0,0)], mode='constant')
        else:
            # Odd filter size
            sequences = np.pad(sequences, [(0,0), (width/2, width/2), (0,0)], mode='constant')
    else:
        raise Exception('Padding not supported: %s' % (padding))
    
    if filters.ndim == 2:
        nbFeatures = 1
    else:
        nbFeatures = filters.shape[-1]
    assert nbFeatures == sequences.shape[-1]

    # Create a strided view of the sequence
    w = as_strided(sequences, shape=(sequences.shape[0], (sequences.shape[1] - width + 1), sequences.shape[2], width),
                  strides=(sequences.strides[0], sequences.strides[1], sequences.strides[2], sequences.strides[1]))
    
    # w has shape [batch, length, features, width, 1]
    nbTotalFeatureDim = np.prod(filters.shape[1:])
    c = np.dot(w.reshape((w.shape[0]*w.shape[1], nbTotalFeatureDim)),
               filters.T.reshape(nbTotalFeatureDim, filters.shape[0]))
    c = c.reshape(w.shape[0], w.shape[1], filters.shape[0])
    return c 
Example 30
Project: pyris   Author: fmonegaglia   File: axis.py    MIT License 5 votes vote down vote up
def BuildStrides( self ):
        '''
        Build cache-friendly Strides Array
        '''
        n = 3
        i = 1 + self.hits.shape[0] - 3
        j = 1 + self.hits.shape[1] - 3
        return stride_tricks.as_strided( self.hits, (i,j,n,n), strides=2*self.hits.strides ) 
Example 31
Project: pyris   Author: fmonegaglia   File: pruner.py    MIT License 5 votes vote down vote up
def BuildStrides( self ):
        '''Build cache-friendly Strides Array'''
        n = 3
        i = 1 + self.img.shape[0] - 3
        j = 1 + self.img.shape[1] - 3
        self.strides = stride_tricks.as_strided( self.img, (i,j,n,n),
                                                 strides=2*self.img.strides ) 
Example 32
Project: pyris   Author: fmonegaglia   File: pruner.py    MIT License 5 votes vote down vote up
def ValuesStrides( self ):
        '''Build cache-friendly Strides Array'''
        n = 3
        i = 1 + self.values.shape[0] - 3
        j = 1 + self.values.shape[1] - 3
        self.values_strides = stride_tricks.as_strided( self.values, (i,j,n,n),
                                                 strides=2*self.values.strides ) 
Example 33
Project: FUTU_Stop_Loss   Author: BigtoC   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 34
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 35
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 36
Project: hpimon   Author: jjnurminen   File: utils.py    MIT License 5 votes vote down vote up
def rolling_fun_strided(m, fun, win, axis=None):
    """ Window array along axis with window length win. Apply fun to the
    windowed data and return result. """
    if axis is None:
        m = m.flatten()
        axis = 0
    sh = m.shape
    st = m.strides
    # break up the given dim into windows, insert a new dim
    sh_ = sh[:axis] + (sh[axis] - win + 1, win) + sh[axis+1:]
    # insert a stride for the new dim, same as for the given dim
    st_ = st[:axis] + (st[axis], st[axis]) + st[axis+1:]
    # apply fun on the new dimension
    # ms = as_strided(m, sh_, st_)
    return fun(as_strided(m, sh_, st_), axis=axis+1) 
Example 37
Project: MARRtino-2.0   Author: DaniAffCH   File: index_tricks.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 38
Project: MARRtino-2.0   Author: DaniAffCH   File: test_mem_overlap.py    GNU General Public License v3.0 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 39
Project: MARRtino-2.0   Author: DaniAffCH   File: test_mem_overlap.py    GNU General Public License v3.0 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 40
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 41
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 42
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 43
Project: D-VAE   Author: muhanzhang   File: basic.py    MIT License 5 votes vote down vote up
def perform(self, node, inp, out_):
        out_shape, values, ilist = inp
        out, = out_
        rows, cols = values.shape
        assert rows == len(ilist)
        indptr = numpy.arange(cols + 1) * rows
        indices = as_strided(ilist,
                             strides=(0, ilist.strides[0]),
                             shape=(cols, ilist.shape[0])).flatten()
        data = values.T.flatten()
        out[0] = scipy.sparse.csc_matrix((data, indices, indptr),
                                         shape=out_shape,
                                         dtype=values.dtype) 
Example 44
Project: vnpy_crypto   Author: birforce   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 45
Project: vnpy_crypto   Author: birforce   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 46
Project: vnpy_crypto   Author: birforce   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 47
Project: ble5-nrf52-mac   Author: tomasero   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 48
Project: ble5-nrf52-mac   Author: tomasero   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 49
Project: ble5-nrf52-mac   Author: tomasero   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 50
Project: ble5-nrf52-mac   Author: tomasero   File: special_matrices.py    MIT License 5 votes vote down vote up
def circulant(c):
    """
    Construct a circulant matrix.

    Parameters
    ----------
    c : (N,) array_like
        1-D array, the first column of the matrix.

    Returns
    -------
    A : (N, N) ndarray
        A circulant matrix whose first column is `c`.

    See Also
    --------
    toeplitz : Toeplitz matrix
    hankel : Hankel matrix
    solve_circulant : Solve a circulant system.

    Notes
    -----
    .. versionadded:: 0.8.0

    Examples
    --------
    >>> from scipy.linalg import circulant
    >>> circulant([1, 2, 3])
    array([[1, 3, 2],
           [2, 1, 3],
           [3, 2, 1]])

    """
    c = np.asarray(c).ravel()
    # Form an extended array that could be strided to give circulant version
    c_ext = np.concatenate((c[::-1], c[:0:-1]))
    L = len(c)
    n = c_ext.strides[0]
    return as_strided(c_ext[L-1:], shape=(L, L), strides=(-n, n)).copy() 
Example 51
Project: Computable   Author: ktraunmueller   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 52
Project: poker   Author: surgebiswas   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 53
Project: poker   Author: surgebiswas   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 54
Project: poker   Author: surgebiswas   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 55
Project: poker   Author: surgebiswas   File: basic.py    MIT License 5 votes vote down vote up
def perform(self, node, inp, out_):
        out_shape, values, ilist = inp
        out, = out_
        rows, cols = values.shape
        assert rows == len(ilist)
        indptr = numpy.arange(cols + 1) * rows
        indices = as_strided(ilist,
                             strides=(0, ilist.strides[0]),
                             shape=(cols, ilist.shape[0])).flatten()
        data = values.T.flatten()
        out[0] = scipy.sparse.csc_matrix((data, indices, indptr),
                                         shape=out_shape,
                                         dtype=values.dtype) 
Example 56
Project: PyDeepMatching   Author: luxtonychen   File: conv.py    MIT License 5 votes vote down vote up
def slice_img(img, slice_shape):
    img_h, img_w = img.shape[1:]
    n_filters_h, n_filters_w = int(img_h/slice_shape[0]), int(img_w/slice_shape[1])
    filters_shape = (img.shape[0], n_filters_h, n_filters_w) + slice_shape
    isize = img.itemsize
    strides = (img_h*img_w*isize, slice_shape[0]*img_w*isize, slice_shape[1]*isize, img_w*isize, isize)
    return as_strided(img, filters_shape, strides).reshape((filters_shape[0], filters_shape[1]*filters_shape[2], filters_shape[3]*filters_shape[4]))

# Slicing image to small cell with shape slice_shape, keep for test
# Return pyTorch style filters(i.e. (out_channel, in_channel cell_h, cell_w)) 
Example 57
Project: PyDeepMatching   Author: luxtonychen   File: conv.py    MIT License 5 votes vote down vote up
def slide_window(img, window_size, stride=1):
    img_h, img_w = img.shape[1:]
    n_windows_h, n_windows_w = int((img_h-(window_size[0]-1)-1)/stride)+1, int((img_w-(window_size[1]-1)-1)/stride)+1
    out_shape = (img.shape[0], n_windows_h, n_windows_w) + window_size
    isize = img.itemsize
    strides = (img_h*img_w*isize, img_w*isize*stride, isize*stride, img_w*isize, isize)
    res = as_strided(img, out_shape, strides).reshape((out_shape[0], out_shape[1]*out_shape[2], out_shape[3]*out_shape[4]))
    return res.swapaxes(1, 2)

# convolution with group of filters with stride 1, and apply sum on channel axis
# input shape: image: (num_channel, img_h, img_w)
#              filters: ((num_channel, num_filters, filter_h*filter_w))
# output shape: (num_filters, response_map_h*response_map_w) 
Example 58
Project: PyDeepMatching   Author: luxtonychen   File: construct_response.py    MIT License 5 votes vote down vote up
def np_get_response(res1, res2, slice_shape = 8):
    img_h, img_w = res1.shape[1], res1.shape[2]
    high, width = int(img_h/slice_shape), int(img_w/slice_shape)
    real_slice_shape = (int(slice_shape/2), int(slice_shape/2))
    
    ds_res1 = conv.average_pool(res1, (2, 2), stride=2)
    ds_res2 = conv.average_pool(res2, (2, 2), stride=2)
    filters = conv.slice_img(ds_res1, real_slice_shape)
    
    n = np.square(filters)
    n = np.sqrt(np.sum(np.sum(n, 0), -1))
    n = as_strided(n, filters.shape[1:], n.strides+(0, ))
    filters = filters/n

    #padding img
    padded_shape = (ds_res2.shape[0], ds_res2.shape[1]+real_slice_shape[0], ds_res2.shape[2]+real_slice_shape[1])
    padding = int(real_slice_shape[0]/2), int(real_slice_shape[1]/2), 
    norm = np.zeros(padded_shape)
    norm[:, padding[0]:-padding[0], padding[1]:-padding[1]] = ds_res2
    
    norm = np.square(norm)
    norm = conv.slide_window(norm, real_slice_shape)
    norm = np.sqrt(np.sum(np.sum(norm, 1), 0))

    res = conv.batch_conv(ds_res2, filters, real_slice_shape, padding)
    print(res.shape)
    res /= norm
    print(res.shape)
    res_shape = (res.shape[0], padded_shape[1]-real_slice_shape[0]+1, padded_shape[2]-real_slice_shape[1]+1)
    return (high, width), res.reshape(res_shape) 
Example 59
Project: gaitutils   Author: jjnurminen   File: numutils.py    GNU General Public License v3.0 5 votes vote down vote up
def rolling_fun_strided(m, fun, win, axis=None):
    """ Window array along given axis and apply fun() to the windowed data.
    No padding, i.e. returned array is shorter in the axis dim by (win-1) """
    if axis is None:
        m = m.flatten()
        axis = 0
    sh = m.shape
    st = m.strides
    # break up the given dim into windows, insert a new dim
    sh_ = sh[:axis] + (sh[axis] - win + 1, win) + sh[axis + 1 :]
    # insert a stride for the new dim, same as for the given dim
    st_ = st[:axis] + (st[axis], st[axis]) + st[axis + 1 :]
    # apply fun on the new dimension
    return fun(as_strided(m, sh_, st_), axis=axis + 1) 
Example 60
Project: P3_image_processing   Author: latedude2   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 61
Project: P3_image_processing   Author: latedude2   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 62
Project: P3_image_processing   Author: latedude2   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 63
Project: pypiv   Author: jr7   File: direct_piv.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _grid_creator(self):
        """Creates a grid according to the GridSpec."""
        shape_fa    = self.grid_spec.get_interogation_grid_shape()
        shape_fb    = self.grid_spec.get_search_grid_shape()
        strides_fa  = self.grid_spec.get_interogation_grid_strides()
        strides_fb  = self.grid_spec.get_search_grid_strides()
        self.grid_a = as_strided(self.frame_a, strides=strides_fa, shape=shape_fa)
        self.grid_b = as_strided(self._padded_fb, strides=strides_fb, shape=shape_fb) 
Example 64
Project: VapourSynth-Super-Resolution-Helper   Author: AlphaAtlas   File: muvsfunc_numpy.py    MIT License 5 votes vote down vote up
def get_blockwise_view(input_2D, block_size=8, strides=1, writeable=False):
    """Get block-wise view of an 2-D array.

    Args:
        input_2D: 2-D array.

        block_size: (int or [int, int]) The size of the block. It can be a single integer, which means the block is a square, 
            or a list of two integers specifying the height and width of the block respectively.
            Default is 8.

        strides: (int or [int, int]) The stride between the blocks. The format is similar to "patch_size".
            Default is 1.

        writeable: (bool) If set to False, the returned array will always be readonly.
            Otherwise it will be writable if the original array was. It is advisable to set this to False if possible.
            Default is False.

    """

    from numpy.lib.stride_tricks import as_strided

    w, h = input_2D.shape

    if isinstance(block_size, int):
        block_size = [block_size]

    block_size_h = block_size[0]
    block_size_v = block_size[-1]

    if isinstance(strides, int):
        strides = [strides]

    strides_h = strides[0]
    strides_v = strides[-1]

    # assert(not any([(w-block_size_h) % strides_h, (h-block_size_v) % strides_v]))
    return as_strided(input_2D, shape=[(w-block_size_h)//strides_h+1, (h-block_size_v)//strides_v+1, block_size_h, block_size_v], 
                    strides=(input_2D.strides[0]*strides_h, input_2D.strides[1]*strides_v, *input_2D.strides), writeable=writeable) 
Example 65
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def rolling_window(X, window_size):
    # for 1d data
    shape = X.shape[:-1] + (X.shape[-1] - window_size + 1, window_size)
    strides = X.strides + (X.strides[-1],)
    return np.lib.stride_tricks.as_strided(X, shape=shape, strides=strides) 
Example 66
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def overlap(X, window_size, window_step):
    """
    Create an overlapped version of X

    Parameters
    ----------
    X : ndarray, shape=(n_samples,)
        Input signal to window and overlap

    window_size : int
        Size of windows to take

    window_step : int
        Step size between windows

    Returns
    -------
    X_strided : shape=(n_windows, window_size)
        2D array of overlapped X
    """
    if window_size % 2 != 0:
        raise ValueError("Window size must be even!")
    # Make sure there are an even number of windows before stridetricks
    append = np.zeros((window_size - len(X) % window_size))
    X = np.hstack((X, append))
    overlap_sz = window_size - window_step
    new_shape = X.shape[:-1] + ((X.shape[-1] - overlap_sz) // window_step, window_size)
    new_strides = X.strides[:-1] + (window_step * X.strides[-1],) + X.strides[-1:]
    X_strided = as_strided(X, shape=new_shape, strides=new_strides)
    return X_strided 
Example 67
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def halfoverlap(X, window_size):
    """
    Create an overlapped version of X using 50% of window_size as overlap.

    Parameters
    ----------
    X : ndarray, shape=(n_samples,)
        Input signal to window and overlap

    window_size : int
        Size of windows to take

    Returns
    -------
    X_strided : shape=(n_windows, window_size)
        2D array of overlapped X
    """
    if window_size % 2 != 0:
        raise ValueError("Window size must be even!")
    window_step = window_size // 2
    # Make sure there are an even number of windows before stridetricks
    append = np.zeros((window_size - len(X) % window_size))
    X = np.hstack((X, append))
    num_frames = len(X) // window_step - 1
    row_stride = X.itemsize * window_step
    col_stride = X.itemsize
    X_strided = as_strided(X, shape=(num_frames, window_size),
                           strides=(row_stride, col_stride))
    return X_strided 
Example 68
Project: GraphicDesignPatternByPython   Author: Relph1119   File: index_tricks.py    MIT License 5 votes vote down vote up
def __init__(self, *shape):
        if len(shape) == 1 and isinstance(shape[0], tuple):
            shape = shape[0]
        x = as_strided(_nx.zeros(1), shape=shape,
                       strides=_nx.zeros_like(shape))
        self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'],
                              order='C') 
Example 69
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_manual():
    # Stride tricks can construct arrays with internal overlap

    # We don't care about memory bounds, the array is not
    # read/write accessed
    x = np.arange(1).astype(np.int8)

    # Check low-dimensional special cases

    check_internal_overlap(x, False) # 1-dim
    check_internal_overlap(x.reshape([]), False) # 0-dim

    a = as_strided(x, strides=(3, 4), shape=(4, 4))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(3, 4), shape=(5, 4))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0,), shape=(0,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(1,))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0,), shape=(2,))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(87, 22))
    check_internal_overlap(a, True)

    a = as_strided(x, strides=(0, -9993), shape=(1, 22))
    check_internal_overlap(a, False)

    a = as_strided(x, strides=(0, -9993), shape=(0, 22))
    check_internal_overlap(a, False) 
Example 70
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_mem_overlap.py    MIT License 5 votes vote down vote up
def test_internal_overlap_fuzz():
    # Fuzz check; the brute-force check is fairly slow

    x = np.arange(1).astype(np.int8)

    overlap = 0
    no_overlap = 0
    min_count = 100

    rng = np.random.RandomState(1234)

    while min(overlap, no_overlap) < min_count:
        ndim = rng.randint(1, 4, dtype=np.intp)

        strides = tuple(rng.randint(-1000, 1000, dtype=np.intp)
                        for j in range(ndim))
        shape = tuple(rng.randint(1, 30, dtype=np.intp)
                      for j in range(ndim))

        a = as_strided(x, strides=strides, shape=shape)
        result = check_internal_overlap(a)

        if result:
            overlap += 1
        else:
            no_overlap += 1 
Example 71
Project: autodmri   Author: samuelstjean   File: blocks.py    MIT License 4 votes vote down vote up
def extract_patches(arr, patch_shape, extraction_step, flatten=True):
    """Extracts patches of any n-dimensional array in place using strides.
    Given an n-dimensional array it will return a 2n-dimensional array with
    the first n dimensions indexing patch position and the last n indexing
    the patch content. This operation is immediate (O(1)). A reshape
    performed on the first n dimensions will cause numpy to copy data, leading
    to a list of extracted patches.

    Parameters
    ----------
    arr : ndarray
        n-dimensional array of which patches are to be extracted
    patch_shape : integer or tuple of length arr.ndim
        Indicates the shape of the patches to be extracted. If an
        integer is given, the shape will be a hypercube of
        sidelength given by its value.
    extraction_step : integer or tuple of length arr.ndim
        Indicates step size at which extraction shall be performed.
        If integer is given, then the step is uniform in all dimensions.
    Returns
    -------
    patches : strided ndarray
        2n-dimensional array indexing patches on first n dimensions and
        containing patches on the last n dimensions. These dimensions
        are fake, but this way no data is copied. A simple reshape invokes
        a copying operation to obtain a list of patches:
        result.reshape([-1] + list(patch_shape))
    """

    arr_ndim = arr.ndim

    if isinstance(patch_shape, numbers.Number):
        patch_shape = tuple([patch_shape] * arr_ndim)
    if isinstance(extraction_step, numbers.Number):
        extraction_step = tuple([extraction_step] * arr_ndim)

    patch_strides = arr.strides

    slices = tuple(slice(None, None, st) for st in extraction_step)
    indexing_strides = arr[slices].strides

    patch_indices_shape = ((np.array(arr.shape) - np.array(patch_shape)) //
                           np.array(extraction_step)) + 1

    shape = tuple(list(patch_indices_shape) + list(patch_shape))
    strides = tuple(list(indexing_strides) + list(patch_strides))

    patches = as_strided(arr, shape=shape, strides=strides)

    if flatten:
        patches = patches.reshape([-1] + list(patch_shape))

    return patches 
Example 72
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: stt_utils.py    Apache License 2.0 4 votes vote down vote up
def spectrogram(samples, fft_length=256, sample_rate=2, hop_length=128):
    """
    Compute the spectrogram for a real signal.
    The parameters follow the naming convention of
    matplotlib.mlab.specgram
    Args:
        samples (1D array): input audio signal
        fft_length (int): number of elements in fft window
        sample_rate (scalar): sample rate
        hop_length (int): hop length (relative offset between neighboring
            fft windows).
    Returns:
        x (2D array): spectrogram [frequency x time]
        freq (1D array): frequency of each row in x
    Note:
        This is a truncating computation e.g. if fft_length=10,
        hop_length=5 and the signal has 23 elements, then the
        last 3 elements will be truncated.
    """
    assert not np.iscomplexobj(samples), "Must not pass in complex numbers"

    window = np.hanning(fft_length)[:, None]
    window_norm = np.sum(window ** 2)

    # The scaling below follows the convention of
    # matplotlib.mlab.specgram which is the same as
    # matlabs specgram.
    scale = window_norm * sample_rate

    trunc = (len(samples) - fft_length) % hop_length
    x = samples[:len(samples) - trunc]

    # "stride trick" reshape to include overlap
    nshape = (fft_length, (len(x) - fft_length) // hop_length + 1)
    nstrides = (x.strides[0], x.strides[0] * hop_length)
    x = as_strided(x, shape=nshape, strides=nstrides)

    # window stride sanity check
    assert np.all(x[:, 1] == samples[hop_length:(hop_length + fft_length)])

    # broadcast window, compute fft over columns and square mod
    # This function computes the one-dimensional n-point discrete Fourier Transform (DFT) of a real-valued array by means of an efficient algorithm called the Fast Fourier Transform (FFT).
    x = np.fft.rfft(x * window, axis=0)
    x = np.absolute(x) ** 2

    # scale, 2.0 for everything except dc and fft_length/2
    x[1:-1, :] *= (2.0 / scale)
    x[(0, -1), :] /= scale

    freqs = float(sample_rate) / fft_length * np.arange(x.shape[0])

    return x, freqs 
Example 73
Project: ffn   Author: google   File: inference.py    Apache License 2.0 4 votes vote down vote up
def visualize_state(seed_logits, pos, movement_policy, dynimage):
  """Visualizes the inference state.

  Args:
    seed_logits: ndarray (z, y, x) with the current predicted mask
    pos: current FoV position within 'seed' as z, y, x
    movement_policy: movement policy object
    dynimage: DynamicImage object which is to be updated with the
        state visualization
  """
  from PIL import Image

  planes = ortho_plane_visualization.cut_ortho_planes(
      seed_logits, center=pos, cross_hair=True)
  to_vis = ortho_plane_visualization.concat_ortho_planes(planes)

  if isinstance(movement_policy.scored_coords, np.ndarray):
    scores = movement_policy.scored_coords
    # Upsample the grid.
    zf, yf, xf = movement_policy.deltas
    zz, yy, xx = scores.shape
    zs, ys, xs = scores.strides
    new_sh = (zz, zf, yy, yf, xx, xf)
    new_st = (zs, 0, ys, 0, xs, 0)
    scores_up = as_strided(scores, new_sh, new_st)
    scores_up = scores_up.reshape((zz * zf, yy * yf, xx * xf))
    # TODO(mkillinger) might need padding in some cases, if crashes: fix.
    # The grid might be too large, cut it to be symmetrical
    cut = (np.array(scores_up.shape) - np.array(seed_logits.shape)) // 2
    sh = seed_logits.shape
    scores_up = scores_up[cut[0]:cut[0] + sh[0],
                          cut[1]:cut[1] + sh[1],
                          cut[2]:cut[2] + sh[2]]
    grid_planes = ortho_plane_visualization.cut_ortho_planes(
        scores_up, center=pos, cross_hair=True)
    grid_view = ortho_plane_visualization.concat_ortho_planes(grid_planes)
    grid_view *= 4  # Looks better this way
    to_vis = np.concatenate((to_vis, grid_view), axis=1)

  val = _cmap_rgb1(expit(to_vis))
  y, x = pos[1:]

  # Mark seed in the xy plane.
  val[(y - 1):(y + 2), (x - 1):(x + 2), 0] = 255
  val[(y - 1):(y + 2), (x - 1):(x + 2), 1:] = 0

  vis = Image.fromarray(val)
  dynimage.UpdateFromPIL(vis)


# Self-prediction halting
# --------------------------------------------------------------------------- 
Example 74
Project: EarthAnnotator   Author: dbuscombe-usgs   File: tile_utils.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def sliding_window(a,ws,ss = None,flatten = True):
    '''
    Return a sliding window over a in any number of dimensions
    '''
    if None is ss:
        # ss was not provided. the windows will not overlap in any direction.
        ss = ws
    ws = norm_shape(ws)
    ss = norm_shape(ss)
    # convert ws, ss, and a.shape to numpy arrays
    ws = np.array(ws)
    ss = np.array(ss)
    shap = np.array(a.shape)
    # ensure that ws, ss, and a.shape all have the same number of dimensions
    ls = [len(shap),len(ws),len(ss)]
    if 1 != len(set(ls)):
        raise ValueError(\
        'a.shape, ws and ss must all have the same length. They were %s' % str(ls))

    # ensure that ws is smaller than a in every dimension
    if np.any(ws > shap):
        raise ValueError(\
        'ws cannot be larger than a in any dimension.\
 a.shape was %s and ws was %s' % (str(a.shape),str(ws)))
    # how many slices will there be in each dimension?
    newshape = norm_shape(((shap - ws) // ss) + 1)
    # the shape of the strided array will be the number of slices in each dimension
    # plus the shape of the window (tuple addition)
    newshape += norm_shape(ws)
    # the strides tuple will be the array's strides multiplied by step size, plus
    # the array's strides (tuple addition)
    newstrides = norm_shape(np.array(a.strides) * ss) + a.strides
    a = ast(a,shape = newshape,strides = newstrides)
    if not flatten:
        return a
    # Collapse strided so that it has one more dimension than the window.  I.e.,
    # the new array is a flat list of slices.
    meat = len(ws) if ws.shape else 0
    firstdim = (np.product(newshape[:-meat]),) if ws.shape else ()
    dim = firstdim + (newshape[-meat:])
    # remove any dimensions with size 1
    #dim = filter(lambda i : i != 1,dim)

    return a.reshape(dim), newshape 
Example 75
Project: ble5-nrf52-mac   Author: tomasero   File: special_matrices.py    MIT License 4 votes vote down vote up
def toeplitz(c, r=None):
    """
    Construct a Toeplitz matrix.

    The Toeplitz matrix has constant diagonals, with c as its first column
    and r as its first row.  If r is not given, ``r == conjugate(c)`` is
    assumed.

    Parameters
    ----------
    c : array_like
        First column of the matrix.  Whatever the actual shape of `c`, it
        will be converted to a 1-D array.
    r : array_like, optional
        First row of the matrix. If None, ``r = conjugate(c)`` is assumed;
        in this case, if c[0] is real, the result is a Hermitian matrix.
        r[0] is ignored; the first row of the returned matrix is
        ``[c[0], r[1:]]``.  Whatever the actual shape of `r`, it will be
        converted to a 1-D array.

    Returns
    -------
    A : (len(c), len(r)) ndarray
        The Toeplitz matrix. Dtype is the same as ``(c[0] + r[0]).dtype``.

    See Also
    --------
    circulant : circulant matrix
    hankel : Hankel matrix
    solve_toeplitz : Solve a Toeplitz system.

    Notes
    -----
    The behavior when `c` or `r` is a scalar, or when `c` is complex and
    `r` is None, was changed in version 0.8.0.  The behavior in previous
    versions was undocumented and is no longer supported.

    Examples
    --------
    >>> from scipy.linalg import toeplitz
    >>> toeplitz([1,2,3], [1,4,5,6])
    array([[1, 4, 5, 6],
           [2, 1, 4, 5],
           [3, 2, 1, 4]])
    >>> toeplitz([1.0, 2+3j, 4-1j])
    array([[ 1.+0.j,  2.-3.j,  4.+1.j],
           [ 2.+3.j,  1.+0.j,  2.-3.j],
           [ 4.-1.j,  2.+3.j,  1.+0.j]])

    """
    c = np.asarray(c).ravel()
    if r is None:
        r = c.conjugate()
    else:
        r = np.asarray(r).ravel()
    # Form a 1D array containing a reversed c followed by r[1:] that could be
    # strided to give us toeplitz matrix.
    vals = np.concatenate((c[::-1], r[1:]))
    out_shp = len(c), len(r)
    n = vals.strides[0]
    return as_strided(vals[len(c)-1:], shape=out_shp, strides=(-n, n)).copy() 
Example 76
Project: training_results_v0.6   Author: mlperf   File: stt_utils.py    Apache License 2.0 4 votes vote down vote up
def spectrogram(samples, fft_length=256, sample_rate=2, hop_length=128):
    """
    Compute the spectrogram for a real signal.
    The parameters follow the naming convention of
    matplotlib.mlab.specgram
    Args:
        samples (1D array): input audio signal
        fft_length (int): number of elements in fft window
        sample_rate (scalar): sample rate
        hop_length (int): hop length (relative offset between neighboring
            fft windows).
    Returns:
        x (2D array): spectrogram [frequency x time]
        freq (1D array): frequency of each row in x
    Note:
        This is a truncating computation e.g. if fft_length=10,
        hop_length=5 and the signal has 23 elements, then the
        last 3 elements will be truncated.
    """
    assert not np.iscomplexobj(samples), "Must not pass in complex numbers"

    window = np.hanning(fft_length)[:, None]
    window_norm = np.sum(window ** 2)

    # The scaling below follows the convention of
    # matplotlib.mlab.specgram which is the same as
    # matlabs specgram.
    scale = window_norm * sample_rate

    trunc = (len(samples) - fft_length) % hop_length
    x = samples[:len(samples) - trunc]

    # "stride trick" reshape to include overlap
    nshape = (fft_length, (len(x) - fft_length) // hop_length + 1)
    nstrides = (x.strides[0], x.strides[0] * hop_length)
    x = as_strided(x, shape=nshape, strides=nstrides)

    # window stride sanity check
    assert np.all(x[:, 1] == samples[hop_length:(hop_length + fft_length)])

    # broadcast window, compute fft over columns and square mod
    # This function computes the one-dimensional n-point discrete Fourier Transform (DFT) of a real-valued array by means of an efficient algorithm called the Fast Fourier Transform (FFT).
    x = np.fft.rfft(x * window, axis=0)
    x = np.absolute(x) ** 2

    # scale, 2.0 for everything except dc and fft_length/2
    x[1:-1, :] *= (2.0 / scale)
    x[(0, -1), :] /= scale

    freqs = float(sample_rate) / fft_length * np.arange(x.shape[0])

    return x, freqs 
Example 77
Project: VapourSynth-Super-Resolution-Helper   Author: AlphaAtlas   File: muvsfunc_numpy.py    MIT License 4 votes vote down vote up
def SSFDeband_core(img_2D, thr=1, smooth_taps=2, edge_taps=3, strides=3, copy=False):
    """Selective sparse filter debanding in NumPy

    For detailed documentation, please refer to the documentation of "SSFDeband" funcion in current library.

    """

    from numpy.lib.stride_tricks import as_strided

    img_2D_dtype = img_2D.dtype

    if copy or img_2D_dtype not in [np.float16, np.float32, np.float64]:
        img_2D = img_2D.astype('float32')

    isclose = lambda x, y, thr: np.abs(x - y) < thr

    h, w = img_2D.shape
    max_taps = max(smooth_taps, edge_taps)
    buff = strides * max_taps
    smooth_buff = strides * max(0, edge_taps - smooth_taps)
    edge_buff = strides * max(0, smooth_taps - edge_taps)

    cropped = img_2D[buff:-buff, :]
    # v_mask = isclose((img_2D[:-18, :], cropped) & isclose(img_2D[3:-15, :], cropped) & isclose(img_2D[6:-12, :], cropped) & isclose(img_2D[12:-6, :], cropped) & isclose(img_2D[15:-3, :], cropped) & isclose(img_2D[18:, :], cropped), atol=thr, rtol=0)
    upper_view = as_strided(img_2D[edge_buff:, :], shape=[h-2*buff, w, edge_taps], strides=(*img_2D.strides, strides*img_2D.strides[0]))
    upper_mask = np.logical_and.reduce(isclose(upper_view, cropped[..., np.newaxis], thr=thr), axis=2)
    lower_view = as_strided(img_2D[buff+strides:, :], shape=[h-2*buff, w, edge_taps], strides=(*img_2D.strides, strides*img_2D.strides[0]))
    lower_mask = np.logical_and.reduce(isclose(lower_view, cropped[..., np.newaxis], thr=thr), axis=2)
    v_mask = upper_mask & lower_mask
    # v_smooth = (img_2D[3:-15, :] + img_2D[6:-12, :] + img_2D[9:-9, :] + img_2D[12:-6, :] + img_2D[15:-3, :]) / 5
    v_smooth = as_strided(img_2D[smooth_buff:, :], shape=[h-2*buff, w, 2*smooth_taps+1], strides=(*img_2D.strides, strides*img_2D.strides[0])).mean(axis=2)
    cropped[:] = np.where(v_mask, v_smooth, cropped)

    cropped = img_2D[:, buff:-buff]
    # h_mask = isclose((img_2D[:, :-18], cropped) & isclose(img_2D[:, 3:-15], cropped) & isclose(img_2D[:, 6:-12], cropped) & isclose(img_2D[:, 12:-6], cropped) & isclose(img_2D[:, 15:-3], cropped) & isclose(img_2D[:, 18:], cropped, atol=thr, rtol=0)
    left_view = as_strided(img_2D[:, edge_buff:], shape=[h, w-2*buff, edge_taps], strides=(*img_2D.strides, strides*img_2D.strides[1]))
    left_mask = np.logical_and.reduce(isclose(left_view, cropped[..., np.newaxis], thr=thr), axis=2)
    right_view = as_strided(img_2D[:, buff+strides:], shape=[h, w-2*buff, edge_taps], strides=(*img_2D.strides, strides*img_2D.strides[1]))
    right_mask = np.logical_and.reduce(isclose(right_view, cropped[..., np.newaxis], thr=thr), axis=2)
    h_mask = left_mask & right_mask
    # h_smooth = (img_2D[:, 3:-15] + img_2D[:, 6:-12] + img_2D[:, 9:-9] + img_2D[:, 12:-6] + img_2D[:, 15:-3]) / 5
    h_smooth = as_strided(img_2D[:, smooth_buff:], shape=[h, w-2*buff, 2*smooth_taps+1], strides=(*img_2D.strides, strides*img_2D.strides[1])).mean(axis=2)
    cropped[:] = np.where(h_mask, h_smooth, cropped)

    return img_2D.astype(img_2D_dtype, copy=False) 
Example 78
Project: crepe   Author: marl   File: core.py    MIT License 4 votes vote down vote up
def get_activation(audio, sr, model_capacity='full', center=True, step_size=10,
                   verbose=1):
    """

    Parameters
    ----------
    audio : np.ndarray [shape=(N,) or (N, C)]
        The audio samples. Multichannel audio will be downmixed.
    sr : int
        Sample rate of the audio samples. The audio will be resampled if
        the sample rate is not 16 kHz, which is expected by the model.
    model_capacity : 'tiny', 'small', 'medium', 'large', or 'full'
        String specifying the model capacity; see the docstring of
        :func:`~crepe.core.build_and_load_model`
    center : boolean
        - If `True` (default), the signal `audio` is padded so that frame
          `D[:, t]` is centered at `audio[t * hop_length]`.
        - If `False`, then `D[:, t]` begins at `audio[t * hop_length]`
    step_size : int
        The step size in milliseconds for running pitch estimation.
    verbose : int
        Set the keras verbosity mode: 1 (default) will print out a progress bar
        during prediction, 0 will suppress all non-error printouts.

    Returns
    -------
    activation : np.ndarray [shape=(T, 360)]
        The raw activation matrix
    """
    model = build_and_load_model(model_capacity)

    if len(audio.shape) == 2:
        audio = audio.mean(1)  # make mono
    audio = audio.astype(np.float32)
    if sr != model_srate:
        # resample audio if necessary
        from resampy import resample
        audio = resample(audio, sr, model_srate)

    # pad so that frames are centered around their timestamps (i.e. first frame
    # is zero centered).
    if center:
        audio = np.pad(audio, 512, mode='constant', constant_values=0)

    # make 1024-sample frames of the audio with hop length of 10 milliseconds
    hop_length = int(model_srate * step_size / 1000)
    n_frames = 1 + int((len(audio) - 1024) / hop_length)
    frames = as_strided(audio, shape=(1024, n_frames),
                        strides=(audio.itemsize, hop_length * audio.itemsize))
    frames = frames.transpose()

    # normalize each frame -- this is expected by the model
    frames -= np.mean(frames, axis=1)[:, np.newaxis]
    frames /= np.std(frames, axis=1)[:, np.newaxis]

    # run prediction and convert the frequency bin weights to Hz
    return model.predict(frames, verbose=verbose) 
Example 79
Project: numpy-ml   Author: ddbourgin   File: dsp.py    GNU General Public License v3.0 4 votes vote down vote up
def to_frames(x, frame_width, stride, writeable=False):
    """
    Convert a 1D signal x into overlapping windows of width `frame_width` using
    a hop length of `stride`.

    Notes
    -----
    If ``(len(x) - frame_width) % stride != 0`` then some number of the samples
    in x will be dropped. Specifically::

        n_dropped_frames = len(x) - frame_width - stride * (n_frames - 1)

    where::

        n_frames = (len(x) - frame_width) // stride + 1

    This method uses low-level stride manipulation to avoid creating an
    additional copy of `x`. The downside is that if ``writeable`=True``,
    modifying the `frame` output can result in unexpected behavior:

        >>> out = to_frames(np.arange(6), 5, 1)
        >>> out
        array([[0, 1, 2, 3, 4],
               [1, 2, 3, 4, 5]])
        >>> out[0, 1] = 99
        >>> out
        array([[ 0, 99,  2,  3,  4],
               [99,  2,  3,  4,  5]])

    Parameters
    ----------
    x : :py:class:`ndarray <numpy.ndarray>` of shape `(N,)`
        A 1D signal consisting of N samples
    frame_width : int
        The width of a single frame window in samples
    stride : int
        The hop size / number of samples advanced between consecutive frames
    writeable : bool
        If set to False, the returned array will be readonly. Otherwise it will
        be writable if `x` was. It is advisable to set this to False whenever
        possible to avoid unexpected behavior (see NB 2 above). Default is False.

    Returns
    -------
    frame: :py:class:`ndarray <numpy.ndarray>` of shape `(n_frames, frame_width)`
        The collection of overlapping frames stacked into a matrix
    """
    assert x.ndim == 1
    assert stride >= 1
    assert len(x) >= frame_width

    # get the size for an element in x in bits
    byte = x.itemsize
    n_frames = (len(x) - frame_width) // stride + 1
    return as_strided(
        x,
        shape=(n_frames, frame_width),
        strides=(byte * stride, byte),
        writeable=writeable,
    ) 
Example 80
Project: autosynch   Author: SwagLyrics   File: data_feeder.py    GNU General Public License v3.0 4 votes vote down vote up
def _make_overlap_sequences(mixture, voice, bg, l_size, o_lap, b_size):
    """Makes the overlap sequences to be used for time-frequency transformation.

    :param mixture: The mixture signal
    :type mixture: numpy.core.multiarray.ndarray
    :param voice: The voice signal
    :type voice: numpy.core.multiarray.ndarray
    :param bg: The background signal
    :type bg: numpy.core.multiarray.ndarray
    :param l_size: The context length in frames
    :type l_size: int
    :param o_lap: The overlap in samples
    :type o_lap: int
    :param b_size: The batch size
    :type b_size: int
    :return: The overlapping sequences
    :rtype: numpy.core.multiarray.ndarray
    """
    trim_frame = mixture.shape[0] % (l_size - o_lap)
    trim_frame -= (l_size - o_lap)
    trim_frame = np.abs(trim_frame)

    if trim_frame != 0:
        mixture = np.pad(mixture, ((0, trim_frame), (0, 0)), 'constant', constant_values=(0, 0))
        voice = np.pad(voice, ((0, trim_frame), (0, 0)), 'constant', constant_values=(0, 0))
        bg = np.pad(bg, ((0, trim_frame), (0, 0)), 'constant', constant_values=(0, 0))

    mixture = stride_tricks.as_strided(
        mixture,
        shape=(int(mixture.shape[0] / (l_size - o_lap)), l_size, mixture.shape[1]),
        strides=(mixture.strides[0] * (l_size - o_lap), mixture.strides[0], mixture.strides[1])
    )
    mixture = mixture[:-1, :, :]

    voice = stride_tricks.as_strided(
        voice,
        shape=(int(voice.shape[0] / (l_size - o_lap)), l_size, voice.shape[1]),
        strides=(voice.strides[0] * (l_size - o_lap), voice.strides[0], voice.strides[1])
    )
    voice = voice[:-1, :, :]

    bg = stride_tricks.as_strided(
        bg,
        shape=(int(bg.shape[0] / (l_size - o_lap)), l_size, bg.shape[1]),
        strides=(bg.strides[0] * (l_size - o_lap), bg.strides[0], bg.strides[1])
    )
    bg = bg[:-1, :, :]

    b_trim_frame = (mixture.shape[0] % b_size)
    if b_trim_frame != 0:
        mixture = mixture[:-b_trim_frame, :, :]
        voice = voice[:-b_trim_frame, :, :]
        bg = bg[:-b_trim_frame, :, :]

    return mixture, voice, bg