[Paraview] What is the "numpy way"?

Adam Lyon lyon at fnal.gov
Fri Jun 19 12:26:39 EDT 2015


Hi all, I've been debugging a simulation of particle physics experiment --
I usually have the program make a csv file of some data to visualize (for
example, x,y,z,time,Bx,By,Bz) where, e.g., Bx is a component of a magnetic
field. I can read that file straight into ParaView with its text file
reader, but I find it more convenient to run the csv file through a python
script that makes a vtp file and I load that into ParaView. These files
tend to be big, and having a compressed binary vtp file means that ParaView
can read it very quickly (e.g. I parse csv file once with my script instead
of loading it into ParaView to parse each time I open it).

The examples in the documentation show how to use the vtk-numpy interface
to make a vector of points and "dump" them into a vtkPoints object. For
example,

d = np.genfromtxt(fname, ...)  # more arguments not shown for brevity

coord = algs.make_vector(d["x"], d["y"], d["z"])
pts = vtk.vtkPoints()
pts.SetData(dsa.numpyTovtkDataArray(coord, "Points"))

That looks very nice and must be very efficient.

I don't see an obvious way to do the same thing with point or cell data.
So, for example, I have...

numPts = pts.GetNumberOfPoints()
bfield = vtk.vtkFloatArray()
bfield.SetNumberOfComponents(3)
bfield.SetNumberOfTuples(numPts)
bfield.SetName("BField")

for a in xrange(numPts):
   bfield.InsertTuple(a, [ d["Bx"][a], d["By"][a], d["Bz"][a] ])

polyData.GetPointData().AddArray(bfield)  # polyData is a vtkPolyData object

That loop and explicitly decomposing the "d" array to get at the magnetic
field components looks "old fashioned" to me.

What would be the "numpy way" to make that work? Thanks! -- Adam

*------*

*Adam L. Lyon*
*Scientist; Associate Division Head for Systems for Scientific Applications*

Scientific Computing Division & Muon g-2 Experiment
Fermi National Accelerator Laboratory
630 840 5522 office
www.fnal.gov
lyon at fnal.gov

Connect with us!
Newsletter <http://www.fnal.gov/pub/today/>  |  Facebook
<https://www.facebook.com/Fermilab>  |  Twitter
<https://twitter.com/Fermilab>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20150619/b9f34e15/attachment.html>


More information about the ParaView mailing list