I have a dataset that is giving a different number of stride lengths compared to another dataset of the same shape. The dataset can be downloaded from filebin at:
https://filebin.net/e02dm84v5etjyxoq
while the code that I use is the following, where I want to extract every 5x5 patch from the NxM array:
import numpy as np
from numpy.lib.stride_tricks import as_strided
N = 827
M = 914
gsize = 5
rand_array = np.random.normal(0,1,(N,M))
a = as_strided(rand_array, ((N-gsize+1),(M-gsize+1),gsize,gsize),(rand_array.strides[1], rand_array.strides[1]) + rand_array.strides).reshape(-1, gsize, gsize)
# Using the same code as above, just replacing with the loaded data
tmp = np.load('tmp.npy',allow_pickle=True)
b = as_strided(tmp, ((N-gsize+1),(M-gsize+1),gsize,gsize),(tmp.strides[1], tmp.strides[1]) + tmp.strides).reshape(-1, gsize, gsize)
the results are correct for the array 'a', but I get an empty array for 'b', even when 'np.where(tmp > 0)' clearly produces output for values > 0. Looking more closely at the data reveals:
>>> rand_array.shape
(827,914)
>>> tmp.shape
(827,914)
>>> a.shape
(748930,5,5)
>>> b.shape
(748930,5,5)
>>> rand_array.strides
(7312,8)
>>> tmp.strides
(3656,4)
Therefore, why does the as_strided technique work for the random array but not for the data that I have loaded, where the only difference seems to be for the stride shape, and how can this be fixed to produce the proper results? I can get this to work for a messy-looking loop, but that takes much longer than this method. Also, I currently cannot install skimage for the 'view_as_windows' option, either.