zabop
zabop

Reputation: 7892

How to count neighbouring cells in a numpy array using try-except?

I have a numpy array:

x = np.random.rand(4, 5)

I would like to create an array showing how many neighbouring values are there for each value in the original array. By neighbouring I mean:

example=np.array([[0,1,0,0,0],
                  [1,2,1,0,0],
                  [0,1,0,0,0],
                  [0,0,0,0,0]])

plt.imshow(example)

enter image description here

The value at position [1][1] has 4 neighbours (the yellow square has 4 adjacent green cells).

A solution which works:

x = np.random.rand(4, 5)

mask = 4*np.ones(x.shape, dtype=int)

mask[0][:]=3
mask[-1][:]=3

for each in mask: each[0]=3
for each in mask: each[-1]=3

mask[0][0]=2
mask[0][-1]=2
mask[-1][0]=2
mask[-1][-1]=2

mask becomes:

array([[2, 3, 3, 3, 2],
       [3, 4, 4, 4, 3],
       [3, 4, 4, 4, 3],
       [2, 3, 3, 3, 2]])

Now I try to create the same array with try-except:

x = np.random.rand(4, 5)

numofneighbours=[]

for index, each in enumerate(x):    
    row=[]
    for INDEX, EACH in enumerate(each):

        c=4
        try:ap[index+1][INDEX]
        except:c-=1

        try:ap[index][INDEX+1]
        except:c-=1

        try:ap[index-1][INDEX]
        except:c-=1

        try:ap[index][INDEX-1]   
        except:c-=1

        row.append(c)

    numofneighbours.append(row)
numofneighbours=np.asarray(numofneighbours)

Giving thre resulting numofneighbours array:

array([[4, 4, 4, 4, 3],
       [4, 4, 4, 4, 3],
       [4, 4, 4, 4, 3],
       [3, 3, 3, 3, 2]])

Which is not equal to mask, as I expected it to be.

What am I doing wrong here or how should I use try-except for the purpose described above?

Upvotes: 2

Views: 354

Answers (2)

zabop
zabop

Reputation: 7892

Realised that index-1 and INDEX-1 indicies are still valid when index and INDEX are equal to 0, they just have the value -1, making it a valid array index even though the value they are refering to is not neighbouring the value referred to by index and INDEX. My fix is the following:

x = np.random.rand(4, 5)

numofneighbours=[]

for index, each in enumerate(x):    
    row=[]
    for INDEX, EACH in enumerate(each):

        c=4
        try:x[index+1][INDEX]
        except:c-=1

        try:x[index][INDEX+1]
        except:c-=1

        try:x[index-1][INDEX]
        except:c-=1
        if (index-1)<0: c-=1

        try:x[index][INDEX-1]
        except:c-=1
        if (INDEX-1)<0: c-=1

        row.append(c)

    numofneighbours.append(row)
numofneighbours=np.asarray(numofneighbours)

This gives:

array([[2, 3, 3, 3, 2],
       [3, 4, 4, 4, 3],
       [3, 4, 4, 4, 3],
       [2, 3, 3, 3, 2]])

Upvotes: 2

markuscosinus
markuscosinus

Reputation: 2267

The problem here is that numpy allows negative indexing. a[-1] stands for the last value in a which is why the first numbers in your array are not decreased.

I think the first way you described is cleaner and faster than the try-except method and you should just use that.

Upvotes: 2

Related Questions