如何使用pyvtk将numpy数组中的3D矢量字段导出到* .vtk文件?

时间:2019-07-16 22:20:04

标签: python numpy vtk

我正在努力将某些3D矢量数组(numpy数组)从python导出到* .vtk文件,以便以后在ParaView中使用。

我有三个3D MR测速图像,每个100x100x200体素包含x,y和z中的速度分量。我想要的是使用here中的pyvtk-module将此向量字段导出到* .vtk-文件。

不幸的是,我不明白它是如何工作的:-(

到目前为止我尝试过的事情:

ValueError: DataSet (size=100) and PointData (size=3) have different sizes

其中flow ['...']包含矢量分量。我收到以下错误:

def _ReductionDims(x, axis, reduction_indices=None):  # pylint: disable=invalid-name
  """Returns range(0, rank(x)) if reduction_indices is None."""
  # TODO(aselle): Remove this after deprecation
  if reduction_indices is not None:
    if axis is not None:
      raise ValueError("Can't specify both axis' and 'reduction_indices'.")
    axis = reduction_indices
  if axis is not None:
    return axis
  else:
    # Fast path: avoid creating Rank and Range ops if ndims is known.
    rank = common_shapes.rank(x)
    if rank is not None:
      return constant_op.constant(np.arange(rank), dtype=dtypes.int32)
    if (isinstance(x, sparse_tensor.SparseTensor) and
        x.dense_shape.shape.is_fully_defined()):
      rank = x.dense_shape.shape.dims[0].value  # sparse.dense_shape is 1-D.
      return constant_op.constant(np.arange(rank), dtype=dtypes.int32)

    # Otherwise, we rely on Range and Rank to do the right thing at run-time.
    return range(0, array_ops.rank(x))

是的,这是想告诉我什么?好的,猜想类似尺寸不匹配的问题,但是如何正确设置输入?

任何帮助将不胜感激。预先感谢

1 个答案:

答案 0 :(得分:0)

我使用TVTK而不是PyVTK找到了解决问题的合适方法。因此,每个感兴趣的人,可能的解决方法如下:

from tvtk.api import tvtk, write_data
# Unpack velocity information

vx=flow['vx']
vy=flow['vy']
vz=flow['vz']

dim=vx.shape

# Generate the grid
xx,yy,zz=np.mgrid[0:dim[0],0:dim[1],0:dim[2]]
pts = empty(vx.shape + (3,), dtype=int)
pts[..., 0] = xx
pts[..., 1] = yy
pts[..., 2] = zz

vectors = empty(vx.shape + (3,), dtype=float)
vectors[..., 0] = vx
vectors[..., 1] = vy
vectors[..., 2] = vz

# We reorder the points and vectors so this is as per VTK's
# requirement of x first, y next and z last.
pts = pts.transpose(2, 1, 0, 3).copy()
pts.shape = pts.size // 3, 3

vectors = vectors.transpose(2, 1, 0, 3).copy()
vectors.shape = vectors.size // 3, 3

sg = tvtk.StructuredGrid(dimensions=xx.shape, points=pts)

sg.point_data.vectors = vectors
sg.point_data.vectors.name = 'velocity'

write_data(sg, 'vtktest.vtk')

问候

光子