首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ParaView的可编程滤波器中使用numpy

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

Stack Overflow用户
提问于 2017-09-09 16:38:30
回答 1查看 2.4K关注 0票数 0

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

代码语言:javascript
复制
data0=inputs[0].GetPointData().GetArray('data')`

获取数组的一部分

代码语言:javascript
复制
subData=data0[0:6]

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

为了对代码进行实验,我创建了一个(不太小)的工作示例:

代码语言:javascript
复制
#!/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)

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

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
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_interface/dataset_adapter.py",第652行,附在self.VTKObject.AddArray(arr)中 TypeError: AddArray参数1:方法需要一个VTK对象
  • 文件"run.py",第15行 打印progData.GetArray('T2').GetRange() AttributeError: NoneType对象没有属性“GetRange”

第一个错误消息似乎是第二个错误的原因。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-26 15:54:42

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

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46132843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档