重塑3D numpy矩阵中的张量

时间:2018-12-02 18:05:16

标签: arrays python-3.x numpy

我实质上是在尝试完成this,然后再完成this,但要使用3D矩阵,例如(128,128,60,6)。第四维是一个数组矢量,表示该体素处的扩散数组,例如:

d[30,30,30,:] = [dxx, dxy, dxz, dyy, dyz, dzz] = D_array

其中dxx等是特定方向的扩散。 D_array也可以看作是三角矩阵(因为dxy == dyx等)。因此,我可以使用其他两个答案将D_array转换为D_square,例如

D_square = [[dxx, dxy, dxz], [dyx, dyy, dyz],[dzx, dzy, dzz]]

但是,我似乎无法弄清楚下一步-如何将D_array到D_square的单位转换应用于整个3D体积。

这是在单个张量上工作的代码段:

#this solves an linear eq. that provides us with diffusion arrays at each voxel in a 3D space
D = np.einsum('ijkt,tl->ijkl',X,bi_plus)

#our issue at this point is we have a vector that represents a triangular matrix.
# first make a tri matx from the vector, testing on unit tensor first
D_tri = np.zeros((3,3))
D_array = D[30][30][30]
D_tri[np.triu_indices(3)] = D_array
# then getting the full sqr matrix
D_square = D_tri.T + D_tri
np.fill_diagonal(D_square, np.diag(D_tri))

那么,将扩散张量的单位转换一次转换为整个3D体积的方法会是什么?

2 个答案:

答案 0 :(得分:2)

方法1

这里是一个使用row, col中的triu_indices索引沿最后两个轴索引到初始化的输出数组中的

def squareformnd_rowcol_integer(ar, n=3):
    out_shp = ar.shape[:-1] + (n,n)
    out = np.empty(out_shp, dtype=ar.dtype)

    row,col = np.triu_indices(n)

    # Get a "rolled-axis" view with which the last two axes come to the front
    # so that we could index into them just like for a 2D case
    out_rolledaxes_view = out.transpose(np.roll(range(out.ndim),2,0))    

    # Assign permuted version of input array into rolled output version
    arT = np.moveaxis(ar,-1,0)
    out_rolledaxes_view[row,col] = arT
    out_rolledaxes_view[col,row] = arT
    return out

方法2

将最后两个轴的另一个合并为一个,然后使用线性索引进行索引-

def squareformnd_linear_integer(ar, n=3):
    out_shp = ar.shape[:-1] + (n,n)
    out = np.empty(out_shp, dtype=ar.dtype)

    row,col = np.triu_indices(n)
    idx0 = row*n+col
    idx1 = col*n+row

    ar2D = ar.reshape(-1,ar.shape[-1])
    out.reshape(-1,n**2)[:,idx0] = ar2D
    out.reshape(-1,n**2)[:,idx1] = ar2D
    return out

方法3

最后总共使用了masking的新方法,并且在性能上应该更好,因为大多数基于masking的方法都是在索引时-

def squareformnd_masking(ar, n=3):
    out = np.empty((n,n)+ar.shape[:-1] , dtype=ar.dtype)

    r = np.arange(n)
    m = r[:,None]<=r

    arT = np.moveaxis(ar,-1,0)
    out[m] = arT
    out.swapaxes(0,1)[m] = arT
    new_axes = range(out.ndim)[2:] + [0,1]
    return out.transpose(new_axes)

(128,128,60,6)形随机数组上的时间-

In [635]: ar = np.random.rand(128,128,60,6)

In [636]: %timeit squareformnd_linear_integer(ar, n=3)
     ...: %timeit squareformnd_rowcol_integer(ar, n=3)
     ...: %timeit squareformnd_masking(ar, n=3)
10 loops, best of 3: 103 ms per loop
10 loops, best of 3: 103 ms per loop
10 loops, best of 3: 53.6 ms per loop

答案 1 :(得分:1)

一种矢量化的方法:

# Gets the triangle matrix
d_tensor = np.zeros(128, 128, 60, 3, 3)
triu_idx = np.triu_indices(3)
d_tensor[:, :, :, triu_idx[0], triu_idx[1]] = d
# Make it symmetric
diagonal = np.zeros(128, 128, 60, 3, 3)
idx = np.arange(3)
diagonal[:, :, :, idx, idx] = d_tensor[:, :, :, idx, idx]
d_tensor = np.transpose(d_tensor, (0, 1, 2, 4, 3)) + d_tensor - diagonal