Python numpy.lib.stride_tricks.as_strided() Examples
The following are 30
code examples of numpy.lib.stride_tricks.as_strided().
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
numpy.lib.stride_tricks
, or try the search function
.
Example #1
Source File: audio_tools.py From tools with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #2
Source File: _vad.py From vad with GNU General Public License v3.0 | 6 votes |
def stft(self, sig): """ Short term fourier transform. Parameters ---------- sig : ndarray signal Returns ------- windowed fourier transformed signal """ s = np.pad(sig, (self.wlen//2, 0), 'constant') cols = int(np.ceil((s.shape[0] - self.wlen) / self.fshift + 1)) s = np.pad(s, (0, self.wlen), 'constant') frames = as_strided(s, shape=(cols, self.wlen), strides=(s.strides[0]*self.fshift, s.strides[0])).copy() return np.fft.rfft(frames*self.win, self.NFFT)
Example #3
Source File: spectrogram2.py From crnn-lid with GNU General Public License v3.0 | 6 votes |
def stft(self, 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 #4
Source File: operations.py From gnocchi with Apache License 2.0 | 6 votes |
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 #5
Source File: utils.py From ALOCC-CVPR2018 with MIT License | 6 votes |
def kh_make_patches(arr, patch_shape=2, extraction_step=1): 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 = [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) return patches, shape
Example #6
Source File: evaluate.py From deepIQA with MIT License | 6 votes |
def extract_patches(arr, patch_shape=(32,32,3), extraction_step=32): 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) return patches
Example #7
Source File: audio_eval.py From Multi-channel-speech-extraction-using-DNN with MIT License | 6 votes |
def stft(sig, frameSize, overlapFac=0.75, window=np.hanning): """ short time fourier transform of audio signal """ 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) samples = np.array(sig, dtype='float64') # 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) # all the definition of the flowing variable can be found # train_net.py
Example #8
Source File: spectrogram.py From Multi-channel-speech-extraction-using-DNN with MIT License | 6 votes |
def stft(sig, frameSize, overlapFac=0.75, window=np.hanning): """ short time fourier transform of audio signal """ 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) samples = np.array(sig, dtype='float64') # cols for windowing cols = np.floor((len(samples) - frameSize) / float(hopSize)) # 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 #9
Source File: audio_eval.py From Multi-channel-speech-extraction-using-DNN with MIT License | 6 votes |
def stft(sig, frameSize, overlapFac=0.75, window=np.hanning): """ short time fourier transform of audio signal """ 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) samples = np.array(sig, dtype='float64') # 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 #10
Source File: cov_struct.py From vnpy_crypto with MIT License | 6 votes |
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 #11
Source File: pool.py From mlens with MIT License | 6 votes |
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
Source File: statistics.py From pinkfish with MIT License | 6 votes |
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 #13
Source File: spectrogram.py From SampleScanner with MIT License | 6 votes |
def stft(sig, frame_size, overlap_fac=0.5, window=np.hanning): """ short time fourier transform of audio signal """ win = window(frame_size) hop_size = int(frame_size - np.floor(overlap_fac * frame_size)) # zeros at beginning (thus center of 1st window should be for sample nr. 0) samples = np.append(np.zeros(np.floor(frame_size / 2.0)), sig) # cols for windowing cols = np.ceil((len(samples) - frame_size) / float(hop_size)) + 1 # zeros at end (thus samples can be fully covered by frames) samples = np.append(samples, np.zeros(frame_size)) frames = stride_tricks.as_strided( samples, shape=(cols, frame_size), strides=( samples.strides[0] * hop_size, samples.strides[0] ) ).copy() frames *= win return np.fft.rfft(frames)
Example #14
Source File: audio_tools.py From tools with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #15
Source File: laplacian.py From portrait_matting with GNU General Public License v3.0 | 5 votes |
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 #16
Source File: numpy_utils.py From catalyst with Apache License 2.0 | 5 votes |
def repeat_first_axis(array, count): """ Restride `array` to repeat `count` times along the first axis. Parameters ---------- array : np.array The array to restride. count : int Number of times to repeat `array`. Returns ------- result : array Array of shape (count,) + array.shape, composed of `array` repeated `count` times along the first axis. Example ------- >>> from numpy import arange >>> a = arange(3); a array([0, 1, 2]) >>> repeat_first_axis(a, 2) array([[0, 1, 2], [0, 1, 2]]) >>> repeat_first_axis(a, 4) array([[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) Notes ---- The resulting array will share memory with `array`. If you need to assign to the input or output, you should probably make a copy first. See Also -------- repeat_last_axis """ return as_strided(array, (count,) + array.shape, (0,) + array.strides)
Example #17
Source File: index_tricks.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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 #18
Source File: test_mem_overlap.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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 #19
Source File: test_mem_overlap.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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
Source File: index_tricks.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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 #21
Source File: index_tricks.py From mxnet-lambda with Apache License 2.0 | 5 votes |
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
Source File: stft.py From pyroomacoustics with MIT License | 5 votes |
def _analysis_non_streaming(self, x): """ STFT analysis for non-streaming case in which we expect [(num_frames-1)*hop+num_samples] samples """ ## ----- STRIDED WAY new_strides = (x.strides[0], self.hop * x.strides[0]) new_shape = (self.num_samples, self.num_frames) if not self.mono: for c in range(self.num_channels): y = _as_strided(x[:, c], shape=new_shape, strides=new_strides) y = np.concatenate((np.zeros((self.zf, self.num_frames)), y, np.zeros((self.zb, self.num_frames)))) if self.num_frames == 1: self.X[:, c] = self.dft_frames.analysis(y[:, 0]).T else: self.X[:, :, c] = self.dft_frames.analysis(y).T else: y = _as_strided(x, shape=new_shape, strides=new_strides) y = np.concatenate((np.zeros((self.zf, self.num_frames)), y, np.zeros((self.zb, self.num_frames)))) if self.num_frames == 1: self.X[:] = self.dft_frames.analysis(y[:, 0]).T else: self.X[:] = self.dft_frames.analysis(y).T
Example #23
Source File: penntree.py From TextDetector with GNU General Public License v3.0 | 5 votes |
def __init__(self, which_set, context_len, data_mode, shuffle=True): self.__dict__.update(locals()) del self.self # Load data into self._data (defined in PennTreebank) self._load_data(which_set, context_len, data_mode) self._data = as_strided(self._raw_data, shape=(len(self._raw_data) - context_len, context_len + 1), strides=(self._raw_data.itemsize, self._raw_data.itemsize)) super(PennTreebankNGrams, self).__init__( X=self._data[:, :-1], y=self._data[:, -1:], X_labels=self._max_labels, y_labels=self._max_labels ) if shuffle: warnings.warn("Note that the PennTreebank samples are only " "shuffled when the iterator method is used to " "retrieve them.") self._iter_subset_class = resolve_iterator_class( 'shuffled_sequential' )
Example #24
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
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 #25
Source File: utils.py From geoio with MIT License | 5 votes |
def block_view_image(A, block=(3, 3), strides=(2, 2)): """Provide a 2D tiled block view to a 3d image array. No error checking is made. Therefore meaningful (as implemented) only for blocks strictly compatible with the shape of A.""" # simple shape and strides computations may seem at first strange # unless one is able to recognize the 'tuple additions' involved ;-) shape = (A.shape[0] / block[0], A.shape[1] / block[1]) + block strides = (strides[0] * A.strides[0], strides[1] * A.strides[1]) + A.strides return as_strided(A, shape=shape, strides=strides)
Example #26
Source File: utils.py From geoio with MIT License | 5 votes |
def block_view(A, block=(3,3), strides=(2,2)): """Provide a 2D block view to 2D array. No error checking made. Therefore meaningful (as implemented) only for blocks strictly compatible with the shape of A.""" # simple shape and strides computations may seem at first strange # unless one is able to recognize the 'tuple additions' involved ;-) shape= (A.shape[0]/ block[0], A.shape[1]/ block[1])+ block strides= (strides[0]* A.strides[0], strides[1]* A.strides[1])+ A.strides return as_strided(A, shape= shape, strides= strides)
Example #27
Source File: special_matrices.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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 #28
Source File: test_mem_overlap.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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 #29
Source File: numpy_utils.py From zipline-chinese with Apache License 2.0 | 5 votes |
def repeat_first_axis(array, count): """ Restride `array` to repeat `count` times along the first axis. Parameters ---------- array : np.array The array to restride. count : int Number of times to repeat `array`. Returns ------- result : array Array of shape (count,) + array.shape, composed of `array` repeated `count` times along the first axis. Example ------- >>> from numpy import arange >>> a = arange(3); a array([0, 1, 2]) >>> repeat_first_axis(a, 2) array([[0, 1, 2], [0, 1, 2]]) >>> repeat_first_axis(a, 4) array([[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) Notes ---- The resulting array will share memory with `array`. If you need to assign to the input or output, you should probably make a copy first. See Also -------- repeat_last_axis """ return as_strided(array, (count,) + array.shape, (0,) + array.strides)
Example #30
Source File: test_mem_overlap.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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)