对于下面编写的代码,我真的可以使用一些建议。
基本上,我创建了一个二维数组配置文件,其中包含active (1)
和inactive (0)
单元。但是,我得到了断开连接的单元格,它是一个值为1的单元格,但周围是值为0的单元格。但是,我想将此active cell (1)
转换为inactive cell (0)
。我的脚本确实运行了,结果是活动的单元格保持活动状态,尽管被不活动的单元格包围了。我在下面写下了以下代码:
def find_lonely_cells(ibound_array):
col_n = ibound_array.shape[-1]
row_n = ibound_array.shape[0]
# go through the ibound_arr cell by cell
for i in xrange(1, col_n-1, 1):
for j in xrange(1, row_n-1, 1):
cell_up = ibound_array[i,0,j-1]
cell_down = ibound_array[i,0,j+1]
cell_right = ibound_array[i+1,0,j]
cell_left = ibound_array[i-1,0,j]
if ibound_array[i,0,j] == 1:
continue
if i == 0:
if j == 0:
if (cell_right == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
if (row_n -1) > j > 0:
if (cell_up == 0 and cell_right == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
elif i == (col_n - 1):
if j == 0:
if (cell_left == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
if (row_n - 1) > j > 0:
if (cell_up == 0 and cell_left == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
elif j == 0:
if i == 0:
if (cell_right == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
if (col_n -1) > i > 0:
if (cell_left == 0 and cell_right == 0 and cell_down == 0):
ibound_array[i,0,j] = 0
elif j == (row_n - 1):
if i == 0:
if (cell_up == 0 and cell_right == 0):
ibound_array[i,0,j] = 0
if (col_n - 1) > i > 0:
if (cell_up == 0 and cell_left == 0 and cell_right == 0):
ibound_array[i,0,j] = 0
else:
if (col_n - 1) > i > 0 and (row_n-1) > j > 0:
if (cell_up == 0 and cell_right == 0 and cell_down == 0 and cell_left == 0):
ibound_array[i,0,j] = 0
return ibound_array
ibound_arr = find_lonely_cells(ibound_array)
请澄清一下,因为我有一个2D数组,所以0
的值是[i,0,j]
,代表0
行。
数组的大小非常大,因为我现在无法更改其大小; size: (120, 1, 150))
[[[1 0 0 ... 0 0 0]]
[[1 1 1 ... 0 0 0]]
[[1 1 1 ... 0 0 0]]
...
[[0 0 0 ... 0 1 0]]
[[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]]]
尽管如此,我希望我的问题很清楚。如果有人在我的脚本中发现任何错误,请告诉我。我可以就如何改善它并使之发挥作用使用一些建议。
谢谢!
Image of disconnected cells I'm referring to
输入的最小示例:
a = np.array([[1,0,1,1,1,1,1,1],
[0,0,1,1,1,1,1,1],
[1,1,1,1,1,0,0,0],
[1,1,1,1,1,0,1,0],
[1,0,0,0,1,0,0,0],
[1,0,1,0,1,1,1,1]])
a = a.reshape(6,1,8)
也许更好的例子是
h = np.array([[1,0,1,1,1,1,1,1],
[0,0,1,1,1,1,1,1],
[1,1,1,1,1,1,0,1],
[1,1,1,1,1,0,1,0],
[1,0,0,0,1,1,0,1],
[1,0,1,0,1,1,1,1]])
#h = h.reshape(6,1,8)
请注意,位于h [3,6]的one
的左侧,右侧,顶部和底部分别有zeroes
,但相邻的对角线为one
。
答案 0 :(得分:1)
我们可以使用卷积再次开始:
import scipy.signal
import numpy as np
a = np.array([[1,0,1,1,1,1,1,1],
[0,0,1,1,1,1,1,1],
[1,1,1,1,1,0,0,0],
[1,1,1,1,1,0,1,0],
[1,0,0,0,1,0,0,0],
[1,0,1,0,1,1,1,1]])
b = scipy.signal.convolve2d(a, np.ones((3,3)), 'same')
在这里,我们对3x3的1s数组进行了每个点的卷积(即发现共现的次数)。 “相同”的论点没有给我们边界上的空白。因此,邻居数为b:
b
array([[1., 3., 4., 6., 6., 6., 6., 4.],
[3., 6., 7., 9., 8., 7., 6., 4.],
[4., 7., 8., 9., 7., 6., 4., 3.],
[5., 7., 6., 7., 5., 4., 1., 1.],
[4., 6., 4., 6., 5., 6., 4., 3.],
[2., 3., 1., 3., 3., 4., 3., 2.]])
因此,我们可以按b == 1的位置将a子集化,然后重新分配:
a[b == 1] = 0
a
array([[0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 0],
[1, 0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 1, 1, 1, 1]])
使用multid数组也可以,将函数更改为convolve
,然后使用np.ones((3,1,3)):
b = scipy.signal.convolve(a, np.ones((3,1,3)), 'same')
a[b == 1] = 0
答案 1 :(得分:0)
ibound_array[i,0,j] == 0
应该是
ibound_array[i,0,j] = 0
并将return语句移出循环。
编辑: 您还应该将for循环更改为
for i in xrange(1, col_n-1, 1):
for j in xrange(1, row_n-1, 1):
否则,您将摆脱错误。 您还可以检查该单元格实际上是否为1:
if ibound_array[i,0,j] == 0:
continue
Edit2: 您的情况也有错误:
(row_n -1) < j > 0:
(col_n -1) < i > 0:
应该是
(row_n -1) > j > 0:
(col_n -1) > i > 0: