使用numpy数组中的索引执行计算和比较

时间:2019-02-18 17:41:21

标签: python arrays python-3.x numpy voxel

我有一个3D体素numpy数组,即每个点的索引与其在3D空间中的位置相对应。

我希望执行许多计算,其中包括知道这些点是否满足各种几何条件。这意味着通过乘以基数将索引转换为向量,然后计算点乘积和叉积,范数等。我正在寻找一种实现此目的的较快方法,因为到目前为止我的工作似乎非常缓慢。

如果我有任意根据 a b c

basis = np.array([[a1, a2, a3],
                  [b1, b2, b3],
                  [c1, c2, c3]])

其中a1,a2,a3是 a 的x,y和z分量,对于 b c 同样。 我可以通过以下方式计算每个体素的笛卡尔坐标p =(x,y,z):

for i in range(vox.shape[0]):
    for j in range(vox.shape[1]):
        for k in range(vox.shape[2]):
            p = np.dot(basis, np.array([i, j, k]))

其中“ vox”是三维像素的3D数组。例如,如果我想计算每个向量与单个其他(笛卡尔)向量(例如q = np.array([qx, qy, qz]))的点积,并在结果满足给定条件(此处大于0.0)时存储该索引,我可以做这样的事情(在与上述相同的循环中):

            if np.dot(p, q) > 0.0:
                desired_vox_indices.append([i, j, k])

问题是,这非常慢。我可以用更pythonic的方式还是通过使用更多的numpy工具来做到这一点?我意识到在这个阶段我什至都不访问vox数组的值。

编辑:基于Divakar的回答尝试交叉生产

# Get q_x (matrix multiplication version of cross product)
q_x = np.array([[0.0, -q[2], q[1]],
                [q[2], 0.0, -q[0]],
                [-q[1], q[0], 0.0]])

# transpose (as per cross product definition) and matrix multiply with basis
u = np.matmul(q_x.T, basis)

# Get open range arrays
m,n,r = vox.shape
I,J,K = np.ogrid[:m,:n,:r]

# writing everything explicitly, since I am unsure how ogrid objects behave
pxq = np.empty(3)
pxq[0] = u[0,0]*I + u[0,1]*J + u[0,2]*K
pxq[1] = u[1,0]*I + u[1,1]*J + u[1,2]*K
pxq[2] = u[2,0]*I + u[2,1]*J + u[2,2]*K

可能与以下相同:

pxq = np.dot(u, np.array([I, J, K]))

但我不确定...

2 个答案:

答案 0 :(得分:2)

我们将使用范围数组来建立求和,以进行缩放,而无需立即生成所有索引。我们将按照与三个嵌套循环相对应的三个步骤进行操作。这个想法与this answer to - Python vectorizing nested for loops中探讨的想法非常相似。这将提高内存效率,因此有望在性能上也不错。然后,我们将求和与阈值0.0进行比较,并使用np.argwhere获得相应的索引。因此,我们将有一个解决方案,像这样-

# Get q scaled version
s = q.dot(basis)

# Get open range arrays and scale and sum-reduce s array
m,n,r = vox.shape
I,J,K = np.ogrid[:m,:n,:r]
sums = s[0]*I + s[1]*J + s[2]*K

# Finally compare the sums against threshold amd get corresponding indices
out = np.argwhere(sums > 0.0)

大型vox数组上的时间-

# Setup
In [371]: np.random.seed(0)
     ...: basis = np.random.randn(3,3)
     ...: vox = np.random.randn(100,100,100)
     ...: q = np.random.randn(3)

# Original soln
In [372]: %%timeit
     ...: desired_vox_indices = []
     ...: for i in range(vox.shape[0]):
     ...:     for j in range(vox.shape[1]):
     ...:         for k in range(vox.shape[2]):
     ...:             p = np.dot(basis, np.array([i, j, k]))            
     ...:             if np.dot(p, q) > 0.0:
     ...:                 desired_vox_indices.append([i, j, k])
1 loop, best of 3: 2.13 s per loop

# @jdehesa's soln
In [373]: %%timeit
     ...: ind = np.indices(vox.shape).reshape(3, -1)
     ...: p = basis.dot(ind)
     ...: d = q.dot(p)
     ...: desired_vox_indices = ind[:, d > 0.0].T
10 loops, best of 3: 35.9 ms per loop

# From this post
In [374]: %%timeit
     ...: s = q.dot(basis)
     ...: m,n,r = vox.shape
     ...: I,J,K = np.ogrid[:m,:n,:r]
     ...: sums = s[0]*I + s[1]*J + s[2]*K
     ...: out = np.argwhere(sums > 0.0)
100 loops, best of 3: 7.56 ms per loop

答案 1 :(得分:1)

您可以按照以下矢量化方式进行操作:

# Array of indices for your voxel data
ind = np.indices(vox.shape).reshape(3, -1)
# Multiply the basis times each coordinate
p = basis @ ind
# Dot product of each result with vector
d = q @ p
# Select coordinates where criteria is met
desired_vox_indices = ind[:, d > 0.0].T