我正在尝试使用 Paraview 4.2 从 Python 脚本中的切片中提取数据。我有这样的东西:
from paraview.numpy_support import vtk_to_numpy
from paraview.simple import *
import os
os.environ["DISPLAY"] = ":0"
paraview.simple._DisableFirstRenderCameraReset()
# create a new 'XDMF Reader'
xDMFtemporalFieldsxmf = XDMFReader(FileNames=['<pathtodata>/XDMF.temporalFields.xmf'])
# Properties modified on xDMFtemporalFieldsxmf
xDMFtemporalFieldsxmf.PointArrayStatus = ['DensityProperty-mesh', 'VelocityField']
xDMFtemporalFieldsxmf.CellArrayStatus = []
# create a new 'Slice'
slice1 = Slice(Input=xDMFtemporalFieldsxmf)
# create a new 'Clip'
clip1 = Clip(Input=slice1)
clip1.ClipType = 'Scalar'
clip1.Value = 1200.0
所以我想做的就是从剪辑中提取 VelocityField 数据,最好是每个数据点的坐标位置。
以下是我的一些探索:
>> print clip1
<paraview.servermanager.Clip object at 0x7f1a14e356d0>
>> print clip1.PointData.keys()
[]
>> print clip1.FieldData.keys()
[]
>> proxy = servermanager.Proxy(proxy=clip1.SMProxy)
>> for property in proxy:
>> print property
<paraview.servermanager.Scalar object at 0x7f1a4e346850>
<paraview.servermanager.Slice object at 0x7f1a14e35190>
None
0
0
[None, '']
0
1200.0
>> print paraview.numpy_support.vtk_to_numpy(clip1.SMProxy)
AttributeError Traceback (most recent call last)
<ipython-input-21-d5fe9e4e150c> in <module>()
----> 1 print paraview.numpy_support.vtk_to_numpy(clip1.SMProxy)
/home/luke/Programs/paraview4.2/lib/site-packages/paraview/numpy_support.pyc in vtk_to_numpy(vtk_array)
202
203 """
--> 204 typ = vtk_array.GetDataType()
205 assert typ in get_vtk_to_numpy_typemap().keys(), \
206 "Unsupported array type %s"%typ
AttributeError: GetDataType
>> data = servermanager.Fetch(clip1)
>> print data
vtkUnstructuredGrid (0x6276dc0)
Debug: Off
Modified Time: 37918079
Reference Count: 1
Registered Events: (none)
Information: 0x65f2f40
Data Released: False
Global Release Data: Off
UpdateTime: 0
Field Data:
Debug: Off
Modified Time: 37918063
Reference Count: 1
Registered Events: (none)
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Number Of Points: 0
Number Of Cells: 0
Cell Data:
Debug: Off
Modified Time: 37918077
Reference Count: 1
Registered Events: (none)
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
Scalars: (none)
Vectors: (none)
Normals: (none)
TCoords: (none)
Tensors: (none)
GlobalIds: (none)
PedigreeIds: (none)
EdgeFlag: (none)
Point Data:
Debug: Off
Modified Time: 37918079
Reference Count: 1
Registered Events: (none)
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
Scalars: (none)
Vectors: (none)
Normals: (none)
TCoords: (none)
Tensors: (none)
GlobalIds: (none)
PedigreeIds: (none)
EdgeFlag: (none)
Bounds:
Xmin,Xmax: (1, -1)
Ymin,Ymax: (1, -1)
Zmin,Zmax: (1, -1)
Compute Time: 0
Number Of Points: 0
Point Coordinates: 0
Locator: 0
Number Of Pieces: 1
Piece: -1
Ghost Level: 0
任何进一步的探索似乎总是会引导我回到 FieldDataInformation 或 PointDataInformation 对象 - 而不是实际数据!
以前用于提取点 XYZ 位置的方法是这样的:
data = servermanager.Fetch(clip1)
d2 = data.GetPoints()
xyz = zeros((d2.GetNumberOfPoints(), 3))
for i in range(d2.GetNumberOfPoints()):
xyz[i,:] = data.GetPoint(i)
编辑 - 输出是:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-e34f36729df1> in <module>()
1 data = servermanager.Fetch(clip1)
2 d2 = data.GetPoints()
----> 3 xyz = zeros((d2.GetNumberOfPoints(), 3))
4 for i in range(d2.GetNumberOfPoints()):
5 xyz[i,:] = data.GetPoint(i)
AttributeError: 'NoneType' object has no attribute 'GetNumberOfPoints'
但是,是的,它似乎不再起作用了,我认为通过所有这些 Numpy 集成,会有一些更整洁的东西。
编辑2: Utkarsh 问题的输出:
>> clip1.UpdatePipeline()
>> rawData = servermanager.Fetch(clip1)
>> import vtk.numpy_interface.dataset_adapter as dsa
>> # Wrap the raw data object to access NumPy friendly API
>> data = dsa.WrapDataObject(rawData) # Note I changed this from Utkarsh's "data" to "rawData"
>> print data.Points
>> print data.PointData["VelocityField"]
给出:
None
<vtk.numpy_interface.dataset_adapter.VTKNoneArray object at 0x7f57c4a75390>
使用 previous 方法遇到的错误是什么。它应该有效。更好的方法如下:
# ensure that the clip1 filter is updated.
clip1.UpdatePipeline()
rawData = servermanager.Fetch(clip1)
import vtk.numpy_interface.dataset_adapter as dsa
# Wrap the raw data object to access NumPy friendly API
data = dsa.WrapDataObject(data)
print data.Points
VTKArray([[ -5. , -5. , -9.15493107],
[ -3. , -5. , -9.75046444],
[ -2. , -5. , -9.50859547],
...,
[ 3.04815888, 3. , 10. ],
[ 4.24629259, 3. , 10. ],
[ 4. , 3.25505328, 10. ]], dtype=float32)
# To access the point data array named VelocityField
print data.PointData["VelocityField"]
@Utkarsh 的回答对于 OP 问题非常好。它也适用于其他类型的 Paraview 对象(我亲自测试过)。
然而,我发现这个主题搜索标题:
Extracting data in Python script from Paraview
。
从 Paraview VTK-Objects 中提取所有数据是一项艰巨的任务,特别是在涉及网格属性(节点连接性等)时。我发现的最完整的方法是使用
CreateWriter
方法,并以 ASCII 格式保存完整的 VTK 对象:
writer_vtk = CreateWriter(filename_vtk, paraview_object, FileType='Ascii')
writer_vtk.UpdatePipeline()
这将保存一个包含所有内部 VTK 数据(网格连接信息等)的 ASCII 文件。解析此文件可能很乏味,但所有信息都是可见的。也许所需的数据很容易获取,或者像
meshio
这样的其他库可以重新导入数据进行后处理。
希望这对搜索
Extracting data in Python script from Paraview
的人有帮助:)