6

I have a multidimensional numpy array where the elements are either True or False values:

import numpy as np 
#just making a toy array grid to show what I want to do 
grid = np.ones((4,4),dtype = 'bool')
grid[0,0]=False 
grid[-1,-1]=False 
#now grid has a few false values but is a 4x4 filled with mostly true values

Now I need to generate another array M, where the value at each site M[i,j] depends on grid[i:i+2,j:j+2] as in

M = np.empty((4x4)) #elements to be filled

#here is the part I want to clean up 
for ii in range(4): 
    for jj in range(4): 

        #details here are unimportant. It's just that M[ii,jj] depends on 
        #multiple elements of grid in some way
        if ii+2<=4 and jj+2<=4:     
            M[ii,jj] = np.all(grid[ii:ii+2,jj:jj+2]==True)
        else: 
            M[ii,jj] = False 

Is there some way to fill the array M using elements from grid without double loops?

0

1 Answer 1

3

Approach #1

Here's one approach with 2D convolution -

from scipy.signal import convolve2d as conv2

out = (conv2(grid,np.ones((2,2),dtype=int),'valid')==4).astype(int)

Sample run -

In [118]: grid
Out[118]: 
array([[False,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True, False]], dtype=bool)

In [119]: (conv2(grid,np.ones((2,2),dtype=int),'valid')==4).astype(int)
Out[119]: 
array([[0, 1, 1],
       [1, 1, 1],
       [1, 1, 0]])

Please note that the last row and last column from the expected output would be all zeros with the initialized output array. This is because of the sliding nature of the code, as it won't have that much of extent along the rows and columns.

Approach #2

Here's another with 2D uniform filter -

from scipy.ndimage.filters import uniform_filter as unif2d

out = unif2d(grid,size=2).astype(int)[1:,1:]

Approach #3

Here's another with 4D slided windowed view -

from skimage.util import view_as_windows as viewW

out = viewW(grid,(2,2)).all(axis=(2,3)).astype(int)

With that all(axis=(2,3)), we are just checking along both the dimensions of every window for all elements to be all True elements.


Runtime test

In [122]: grid = np.random.rand(5000,5000)>0.1

In [123]: %timeit (conv2(grid,np.ones((2,2),dtype=int),'valid')==4).astype(int)
1 loops, best of 3: 520 ms per loop

In [124]: %timeit unif2d(grid,size=2).astype(int)[1:,1:]
1 loops, best of 3: 210 ms per loop

In [125]: %timeit viewW(grid,(2,2)).all(axis=(2,3)).astype(int)
1 loops, best of 3: 614 ms per loop
5
  • Thanks Divakar! I think the slided windowed view makes the most sense to me-- that approach should be useful for other tasks later in this project too. I have to investigate these a little more to understand how they work Apr 18, 2017 at 8:14
  • I understand viewW(grid,(2,2)) pulls out 2x2 blocks from grid. Simplistic questions incoming: What does the .all((2,3)) do? Why (2,3)? Apr 18, 2017 at 8:24
  • @kevinkayaks Added few comments on it.
    – Divakar
    Apr 18, 2017 at 8:26
  • @kevinkayaks The np.all((2, 3)) is just .all(axis=(2, 3)) so along axis 2 and 3.
    – MSeifert
    Apr 18, 2017 at 8:27
  • This would be faster decomposing the 2d convolution into two 1d convolutions - but presumably, that's what uniform_filter does
    – Eric
    Apr 18, 2017 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.