python - Separable filter on numpy array -
say have numpy array a, , want create new array, b such b[i, j] function of, say:
a[i-1, j-1], a[i-1, j ], a[i-1, j+1], a[i , j-1], a[i , j ], a[i , j+1], a[i+1, j-1], a[i+1, j ], a[i+1, j+1] what fastest way this?
as separable filter, there way run in multiple threads? (not processes, because have copy data back)
or writing c code bypass gil mandatory?
partial solutions (like assuming function linear) welcome too.
an idealized numpy way of working sliding window construct 4d array
c.shape = (n,m,3,3) where
c[i,j,:,:] = np.array([a[i-1, j-1], a[i-1, j ], a[i-1, j+1], a[i , j-1], a[i , j ], a[i , j+1], a[i+1, j-1], a[i+1, j ], a[i+1, j+1]]) and write function sort of reduction on last 2 dimensions. sum or mean typical, e.g.
b = c.sum(axis=(2,3)) other questions show how use np.lib.stride_tricks.as_strided construct such array. 3x3 subarray, might fast like
c = np.zeros((n,m,3,3)) c[:,:,0,0] = a[:-1,:-1] etc. (or use hstack , vstack same effect).
but nice thing (or maybe not nice) strided approach doesn't involve copy data of a - view.
as splitting job pieces, can imagine using slices of c (on 1st 2 dimensions), e.g.
c[0:100,0:100,:,:].sum(axis=(2,3))
Comments
Post a Comment