如何在ParaVi的可编程滤波器中使用numpy

2024-10-01 02:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我在paraview中有一个ProgrammableFilter,它有两个输入:mesh1有数据,mesh2没有数据。 此外,我知道从mesh1到mesh2的点的排列。 在过滤器内部,我可以通过

data0=inputs[0].GetPointData().GetArray('data')`

并使用

^{pr2}$

例如。但是如果没有python循环,我如何将这个subData添加到输出中呢?在

为了试验代码,我创建了一个(不是很小)工作示例:

#!/usr/bin/python
from paraview.simple import *
import numpy as np
import vtk
from vtk.util.numpy_support import numpy_to_vtk

#generate an arbitrary source with data
mesh2=Sphere()
mesh2.Center=[0.0, 0.0, 0.0]
mesh2.EndPhi=360
mesh2.EndTheta=360
mesh2.PhiResolution=100
mesh2.Radius=1.0
mesh2.StartPhi=0.0
mesh2.StartTheta=0.0
mesh2.ThetaResolution=100
mesh2.UpdatePipeline()

#add the data
mesh2Vtk=servermanager.Fetch(mesh2)
nPointsSphere=mesh2Vtk.GetNumberOfPoints()
mesh2Data=paraview.vtk.vtkFloatArray()
mesh2Data.SetNumberOfValues(nPointsSphere)
mesh2Data.SetName("mesh2Data")
#TODO: use numpy here?? do this with a ProgrammableFilter ?
data=np.random.rand(nPointsSphere,1)
for k in range(nPointsSphere):
  mesh2Data.SetValue(k, data[k])
mesh2Vtk.GetPointData().AddArray(mesh2Data)

#send back to paraview server
#from https://public.kitware.com/pipermail/paraview/2011-February/020120.html
t=TrivialProducer()
filter= t.GetClientSideObject()
filter.SetOutput(mesh2Vtk)
t.UpdatePipeline()
w=CreateWriter('Sphere_withData.vtp')
w.UpdatePipeline()
Delete(w)

#create mesh1 without data
mesh1=Line()
mesh1.Point1=[0,0,0]
mesh1.Point2=[0,0,1]
mesh1.Resolution=5
mesh1.UpdatePipeline()

progFilter=ProgrammableFilter(mesh1)
progFilter.Input=[mesh1, t]
progFilter.Script="curT=inputs[1].GetPointData().GetArray('mesh2Data')"\
  "\nglobIndices=range(0,6)"\
  "\nsubT=curT[globIndices]"\
  "\nswap=vtk.vtkFloatArray()"\
  "\nswap.SetNumberOfValues(len(globIndices))"\
  "\nswap.SetName('T')"\
  "\n#TODO: how can i avoid this loop, i.e. write output.GetPointData().AddArray(converToVTK(subT))"\
  "\nfor k in range(len(globIndices)):"\
  "\n  swap.SetValue(k,subT[k])"\
  "\noutput.PointData.AddArray(swap)"
progFilter.UpdatePipeline()
w=CreateWriter('Line_withData.vtp')
w.UpdatePipeline()
Delete(w)

我接受了答案,因为它看起来是对的。以下两个脚本甚至显示了问题: 基本脚本'运行.py':

src1='file1.vtu'
r1=XMLUnstructuredGridReader(FileName=src1)

progFilter=ProgrammableFilter(r1)
progFilter.Input=[r1]
with open('script.py','r') as myFile:
  progFilter.Script=myFile.read()
progFilter.UpdatePipeline()
progData=progFilter.GetPointDataInformation()
print progData.GetArray('T2').GetRange()

以及可编程过滤器的脚本:

import vtk
import vtk.numpy_interface.dataset_adapter as dsa
import numpy as np
globIndices=inputs[0].GetPointData().GetArray('T')
subT=np.ones((globIndices.shape[0],1))
subTVtk=dsa.VTKArray(subT)
output.PointData.append(subTVtk, 'T2')

通过这个组合,我得到错误消息:

  • 文件“/usr/lib/python2.7/dist-packages/vtk/numpy_接口/dataset_适配器.py“,第652行,在append中 self.vtkoobject.AddArray(arr)

    TypeError:AddArray参数1:方法需要VTK对象

  • “文件”运行.py“,第15行,英寸

    print progData.GetArray('T2').GetRange()
    

    AttributeError:“NoneType”对象没有属性“GetRange”

茎干似乎是第一个错误的原因。在


Tags: importnumpydatavtkparaviewgetarraygetpointdataaddarray
1条回答
网友
1楼 · 发布于 2024-10-01 02:35:48

下面是一个从Numpy数组创建VTK数据数组的最小示例。你应该能够适应你的目的。在

import numpy as np
import vtk
from vtk.numpy_interface import dataset_adapter as da

np_arr = np.ones(6)
vtk_arr = da.VTKArray(np_arr)
output.PointData.append(vtk_arr, "my data")

相关问题 更多 >