[Paraview] What is the "numpy way"?
Cory Quammen
cory.quammen at kitware.com
Fri Jun 19 13:36:16 EDT 2015
Hi Adam,
There are some utilities in VTK for converting back and forth from VTK
arrays to numpy arrays. Here is an example of usage from within
pvpython that should work:
>>> import numpy as np
>>> d = np.genfromtxt('/home/cory/sample.csv', skip_header=1, delimiter=',')
>>> d
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
>>> import vtk
>>> import vtk.util.numpy_support as ns
>>> vtk_array = ns.numpy_to_vtk(d, 1, vtk.VTK_FLOAT)
>>> vtk_array
(vtkFloatArray)0x7f76a9b92410
>>> vtk_array.GetNumberOfComponents()
3
>>> vtk_array.GetNumberOfTuples()
3L
>>> vtk_array.GetTuple(0)
(1.0, 2.0, 3.0)
vtk_array is a vtkDoubleArray holding the values in the numpy array d.
The 1 in the second argument means that the data should be deep copied
into the VTK array - this is usually what you want unless you hold a
reference to the numpy array d somewhere else in your code.
I hope that helps.
Best regards,
Cory
On Fri, Jun 19, 2015 at 12:26 PM, Adam Lyon <lyon at fnal.gov> wrote:
> 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 | Facebook | Twitter
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
>
> Search the list archives at: http://markmail.org/search/?q=ParaView
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/paraview
>
--
Cory Quammen
R&D Engineer
Kitware, Inc.
More information about the ParaView
mailing list