Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate with binary structure over numpy array to get cell sums

In the package scipy there is the function to define a binary structure (such as a taxicab (2,1) or a chessboard (2,2)).

import numpy
from scipy import ndimage
a = numpy.zeros((6,6), dtype=numpy.int) 
a[1:5, 1:5] = 1;a[3,3] = 0 ; a[2,2] = 2
s = ndimage.generate_binary_structure(2,2) # Binary structure
#.... Calculate Sum of 
result_array = numpy.zeros_like(a)

What i want is to iterate over all cells of this array with the given structure s. Then i want to append a function to the current cell value indexed in a empty array (example function sum), which uses the values of all cells in the binary structure.

For example:

array([[0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 0],
    [0, 1, 2, 1, 1, 0],
    [0, 1, 1, 0, 1, 0],
    [0, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0]])

# The array a. The value in cell 1,2 is currently one. Given the structure s and an example function such as sum the value in the resulting array (result_array) becomes 7 (or 6 if the current cell value is excluded).

Someone got an idea?

like image 757
Curlew Avatar asked Feb 07 '26 01:02

Curlew


2 Answers

For the particular case of sums, you could use ndimage.convolve:

In [42]: import numpy as np

In [43]: a = np.zeros((6,6), dtype=np.int) 
a[1:5, 1:5] = 1;
a[3,3] = 0;
a[2,2] = 2

In [48]: s = ndimage.generate_binary_structure(2,2) # Binary structure

In [49]: ndimage.convolve(a,s)
Out[49]: 
array([[1, 2, 3, 3, 2, 1],
       [2, 5, 7, 7, 4, 2],
       [3, 7, 9, 9, 5, 3],
       [3, 7, 9, 9, 5, 3],
       [2, 4, 5, 5, 3, 2],
       [1, 2, 3, 3, 2, 1]])

For the particular case of products, you could use the fact that log(a*b) = log(a)+log(b) to convert the problem back to one involving sums. For example, if we wanted to "product-convolve" b:

b = a[1:-1, 1:-1]
print(b)
# [[1 1 1 1]
#  [1 2 1 1]
#  [1 1 0 1]
#  [1 1 1 1]]

we could compute:

print(np.exp(ndimage.convolve(np.log(b), s, mode = 'constant')))
# [[ 2.  2.  2.  1.]
#  [ 2.  0.  0.  0.]
#  [ 2.  0.  0.  0.]
#  [ 1.  0.  0.  0.]]

The situation becomes more complicated if b includes negative values:

b[0,1] = -1
print(b)
# [[ 1 -1  1  1]
#  [ 1  2  1  1]
#  [ 1  1  0  1]
#  [ 1  1  1  1]]

but not impossible:

logb = np.log(b.astype('complex'))
real, imag = logb.real, logb.imag
print(np.real_if_close(
    np.exp(
        sum(j * ndimage.convolve(x, s, mode = 'constant')
            for x,j in zip((real, imag),(1,1j))))))
# [[-2. -2. -2.  1.]
#  [-2. -0. -0.  0.]
#  [ 2.  0.  0.  0.]
#  [ 1.  0.  0.  0.]]
like image 142
unutbu Avatar answered Feb 08 '26 15:02

unutbu


It's easier if you use a 2-deep wall of zeroes:

In [11]: a0
Out[11]: 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  2.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

In [12]: b0 = zeros_like(a0)

In [13]: for i in range(1,len(a0)-1):
   ....:     for j in range(1,len(a0)-1):
   ....:         b0[i,j] = sum(a0[i-1:i+2, j-1:j+2] * s)

This enables you to multiply the two sub-matrices together and sum, as desired. (You could also do something more elaborate here...)

In [14]: b0
Out[14]: 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  2.,  3.,  3.,  2.,  1.,  0.],
       [ 0.,  2.,  5.,  7.,  7.,  4.,  2.,  0.],
       [ 0.,  3.,  7.,  9.,  9.,  5.,  3.,  0.],
       [ 0.,  3.,  7.,  9.,  9.,  5.,  3.,  0.],
       [ 0.,  2.,  4.,  5.,  5.,  3.,  2.,  0.],
       [ 0.,  1.,  2.,  3.,  3.,  2.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

In [15]: b0[1:len(b0)-1, 1:len(b0)-1]
Out[15]: 
array([[ 1.,  2.,  3.,  3.,  2.,  1.],
       [ 2.,  5.,  7.,  7.,  4.,  2.],
       [ 3.,  7.,  9.,  9.,  5.,  3.],
       [ 3.,  7.,  9.,  9.,  5.,  3.],
       [ 2.,  4.,  5.,  5.,  3.,  2.],
       [ 1.,  2.,  3.,  3.,  2.,  1.]])
like image 45
Andy Hayden Avatar answered Feb 08 '26 15:02

Andy Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!