<div dir="ltr">Thanks Cory! That was very helpful. One subtlety - my np.genfromtxt has the names argument set, so that returns a structured numpy array. So I had to do,<div><br></div><div>bfieldVtkArray = ns.numpy_to_vtk( d[ ["Bx", "By", "Bz"] ].view( (np.float,3) ), 1, vtk.VTK_FLOAT )<br></div><div>bfieldVtkArray.SetName("BField")<br></div><div><br></div><div>That seems to work.</div><div><br></div><div>The one-column arrays are a little easier. E.g.,</div><div><div>timeVtkArray  = ns.numpy_to_vtk( d["time"].ravel(), 1, vtk.VTK_FLOAT) </div><div>timeVtkArray.SetName("time")</div></div><div><br></div><div>I have to do the .ravel() or else I get the "non-contiguous" error. </div><div><br></div><div>Avoiding that old-fashioned loops seems to make it quite a bit faster too.</div><div><br></div><div>Thanks again for the help! -- Adam</div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><div style="font-size:12.8000001907349px"><i><font color="#999999">------</font></i></div></div><div><i><font color="#999999"><br></font></i></div><div><b><font color="#3d85c6">Adam L. Lyon</font></b></div><div><i><font color="#999999">Scientist; Associate Division Head for Systems for Scientific Applications</font></i></div><div><span style="font-family:Arial;font-size:9pt"><font color="#999999"><i><br></i></font></span></div><div><font color="#3d85c6">Scientific Computing Division & Muon g-2 Experiment</font></div><div><font color="#999999">Fermi National Accelerator Laboratory</font></div><div><font color="#999999">630 840 5522 office</font></div><div><font color="#999999"><a href="http://www.fnal.gov" target="_blank">www.fnal.gov</a></font></div><div><font color="#999999"><a href="mailto:lyon@fnal.gov" target="_blank">lyon@fnal.gov</a></font></div><div><font color="#999999"><br></font></div><div><font color="#999999">Connect with us!</font><br><font color="#9fc5e8"><span style="font-size:12.8000001907349px"><a href="http://www.fnal.gov/pub/today/" target="_blank">Newsletter</a></span><span style="font-size:12.8000001907349px">  |  <a href="https://www.facebook.com/Fermilab" target="_blank"><span>Facebook</span></a>  |  <a href="https://twitter.com/Fermilab" target="_blank"><span>Twitter</span></a></span></font></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Fri, Jun 19, 2015 at 12:36 PM, Cory Quammen <span dir="ltr"><<a href="mailto:cory.quammen@kitware.com" target="_blank">cory.quammen@kitware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Adam,<br>
<br>
There are some utilities in VTK for converting back and forth from VTK<br>
arrays to numpy arrays. Here is an example of usage from within<br>
pvpython that should work:<br>
<br>
>>> import numpy as np<br>
>>> d = np.genfromtxt('/home/cory/sample.csv', skip_header=1, delimiter=',')<br>
>>> d<br>
array([[ 1.,  2.,  3.],<br>
       [ 4.,  5.,  6.],<br>
       [ 7.,  8.,  9.]])<br>
>>> import vtk<br>
>>> import vtk.util.numpy_support as ns<br>
>>> vtk_array = ns.numpy_to_vtk(d, 1, vtk.VTK_FLOAT)<br>
>>> vtk_array<br>
(vtkFloatArray)0x7f76a9b92410<br>
>>> vtk_array.GetNumberOfComponents()<br>
3<br>
>>> vtk_array.GetNumberOfTuples()<br>
3L<br>
>>> vtk_array.GetTuple(0)<br>
(1.0, 2.0, 3.0)<br>
<br>
vtk_array is a vtkDoubleArray holding the values in the numpy array d.<br>
The 1 in the second argument means that the data should be deep copied<br>
into the VTK array - this is usually what you want unless you hold a<br>
reference to the numpy array d somewhere else in your code.<br>
<br>
I hope that helps.<br>
<br>
Best regards,<br>
Cory<br>
<div><div class="h5"><br>
On Fri, Jun 19, 2015 at 12:26 PM, Adam Lyon <<a href="mailto:lyon@fnal.gov">lyon@fnal.gov</a>> wrote:<br>
> Hi all, I've been debugging a simulation of particle physics experiment -- I<br>
> usually have the program make a csv file of some data to visualize (for<br>
> example, x,y,z,time,Bx,By,Bz) where, e.g., Bx is a component of a magnetic<br>
> field. I can read that file straight into ParaView with its text file<br>
> reader, but I find it more convenient to run the csv file through a python<br>
> script that makes a vtp file and I load that into ParaView. These files tend<br>
> to be big, and having a compressed binary vtp file means that ParaView can<br>
> read it very quickly (e.g. I parse csv file once with my script instead of<br>
> loading it into ParaView to parse each time I open it).<br>
><br>
> The examples in the documentation show how to use the vtk-numpy interface to<br>
> make a vector of points and "dump" them into a vtkPoints object. For<br>
> example,<br>
><br>
> d = np.genfromtxt(fname, ...)  # more arguments not shown for brevity<br>
><br>
> coord = algs.make_vector(d["x"], d["y"], d["z"])<br>
> pts = vtk.vtkPoints()<br>
> pts.SetData(dsa.numpyTovtkDataArray(coord, "Points"))<br>
><br>
> That looks very nice and must be very efficient.<br>
><br>
> I don't see an obvious way to do the same thing with point or cell data. So,<br>
> for example, I have...<br>
><br>
> numPts = pts.GetNumberOfPoints()<br>
> bfield = vtk.vtkFloatArray()<br>
> bfield.SetNumberOfComponents(3)<br>
> bfield.SetNumberOfTuples(numPts)<br>
> bfield.SetName("BField")<br>
><br>
> for a in xrange(numPts):<br>
>    bfield.InsertTuple(a, [ d["Bx"][a], d["By"][a], d["Bz"][a] ])<br>
><br>
> polyData.GetPointData().AddArray(bfield)  # polyData is a vtkPolyData object<br>
><br>
> That loop and explicitly decomposing the "d" array to get at the magnetic<br>
> field components looks "old fashioned" to me.<br>
><br>
> What would be the "numpy way" to make that work? Thanks! -- Adam<br>
><br>
> ------<br>
><br>
> Adam L. Lyon<br>
> Scientist; Associate Division Head for Systems for Scientific Applications<br>
><br>
> Scientific Computing Division & Muon g-2 Experiment<br>
> Fermi National Accelerator Laboratory<br>
> <a href="tel:630%20840%205522" value="+16308405522">630 840 5522</a> office<br>
> <a href="http://www.fnal.gov" rel="noreferrer" target="_blank">www.fnal.gov</a><br>
> <a href="mailto:lyon@fnal.gov">lyon@fnal.gov</a><br>
><br>
> Connect with us!<br>
> Newsletter  |  Facebook  |  Twitter<br>
><br>
</div></div>> _______________________________________________<br>
> Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
><br>
> Visit other Kitware open-source projects at<br>
> <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
><br>
> Please keep messages on-topic and check the ParaView Wiki at:<br>
> <a href="http://paraview.org/Wiki/ParaView" rel="noreferrer" target="_blank">http://paraview.org/Wiki/ParaView</a><br>
><br>
> Search the list archives at: <a href="http://markmail.org/search/?q=ParaView" rel="noreferrer" target="_blank">http://markmail.org/search/?q=ParaView</a><br>
><br>
> Follow this link to subscribe/unsubscribe:<br>
> <a href="http://public.kitware.com/mailman/listinfo/paraview" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/paraview</a><br>
><br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Cory Quammen<br>
R&D Engineer<br>
Kitware, Inc.<br>
</font></span></blockquote></div><br></div>