Count cells of adjacent numpy regions
<p>I'm looking to solve the following problem. I have a numpy array which is labeled to regions from 1 to n. Let's say this is the array:</p>

<pre><code>x = np.array([[1, 1, 1, 4], [1, 1, 2, 4], [1, 2, 2, 4], [5, 5, 3, 4]], np.int32)

array([[1, 1, 1, 4],
       [1, 1, 2, 4],
       [1, 2, 2, 4],
       [5, 5, 3, 4]])
</code></pre>

<p>A region are the combined cells in the numpy array with a unique value. So in this example x has 5 regions; region 1 which consists of 5 cells, region 2 which consists of 3 cells etc.
Now, I determine the adjacent regions of each region with the following lines of code:</p>

<pre><code>n = x.max()
tmp = np.zeros((n+1, n+1), bool)

# check the vertical adjacency
a, b = x[:-1, :], x[1:, :]
tmp[a[a!=b], b[a!=b]] = True

# check the horizontal adjacency
a, b = x[:, :-1], x[:, 1:]
tmp[a[a!=b], b[a!=b]] = True

# register adjacency in both directions (up, down) and (left,right)
result = (tmp | tmp.T)

result = result.astype(int)
np.column_stack(np.nonzero(result))
resultlist = [np.flatnonzero(row) for row in result[1:]]
</code></pre>

<p>Which gives me a list of each region with its adjacent regions:</p>

<pre><code>[array([2, 4, 5], dtype=int64),
 array([1, 3, 4, 5], dtype=int64),
 array([2, 4, 5], dtype=int64),
 array([1, 2, 3], dtype=int64),
 array([1, 2, 3], dtype=int64)]
</code></pre>

<p>Which works really well. However, I would like to count the amount of cells of each adjacent region and return this output. So, for region 2, in this example would mean a total of 7 adjacent regions (three 1s, two 4s, one 3 and one 5). Therefore:</p>

<ul>
<li>2 is surrounded for 43% by 1</li>
<li>2 is surround for 14 % by 5</li>
<li>2 is surrounded for 14% by 3</li>
<li>2 is surrounded for 29% by 4</li>
</ul>

<p>How could I best adjust the above code to include the amount of cells for each adjacent region? 
Many thanks guys!</p>

<p>Here is a vectorized solution using the <a href="https://github.com/EelcoHoogendoorn/Numpy_arraysetops_EP" rel="nofollow">numpy_indexed</a> package (note; it isn't vectorized over the regions, but it is vectorized over the pixels, which is the useful thing to do assuming n_pixels >> n_regions):</p>

<pre><code>neighbors = np.concatenate([x[:, :-1].flatten(), x[:, +1:].flatten(), x[+1:, :].flatten(), x[:-1, :].flatten()])
centers   = np.concatenate([x[:, +1:].flatten(), x[:, :-1].flatten(), x[:-1, :].flatten(), x[+1:, :].flatten()])
valid = neighbors != centers

import numpy_indexed as npi
regions, neighbors_per_regions = npi.group_by(centers[valid], neighbors[valid])
for region, neighbors_per_region in zip(regions, neighbors_per_regions):
    print(region)
    unique_neighbors, neighbor_counts = npi.count(neighbors_per_region)
    print(unique_neighbors, neighbor_counts / neighbor_counts.sum() * 100)
</code></pre>

<p>Or for a solution which is fully vectorized over both pixels and regions:</p>

<pre><code>(neighbors, centers), counts  = npi.count((neighbors[valid], centers[valid]))
region_group = group_by(centers)
regions, neighbors_per_region = region_group.sum(counts)
fractions = counts / neighbors_per_region[region_group.inverse]
for q in zip(centers, neighbors, fractions): print(q)
</code></pre>

python arrays numpy