[Numpy-discussion] [numpy-discussion] Transform 3d data
Tue Oct 19 11:20:24 CDT 2010
Of course there is an (at least one) error:
the line should be:
On Tue, 2010-10-19 at 14:10 +0200, Nadav Horesh wrote:
> You can aid mgrid, riughy as the follows (I may have mistakes, but the
> direction should be clear):
> def transform_3d_data_(field,lwrbnd,uprbnd):
> shape = field.shape
> XYZ = np.mgrid[lwrbnd:uprbnd:shape,
> lwrbnd:uprbnd:shape, lwrbnd:uprbnd:shape]
> vectors = fields.reshape(-1,3)
> np.savetxt(np.hstack((XYZ.reshape(3,-1).T, vectors)))
> -----Original Message-----
> From: firstname.lastname@example.org on behalf of Thomas
> Sent: Tue 19-Oct-10 12:05
> To: email@example.com
> Subject: [Numpy-discussion] [numpy-discussion] Transform 3d data
> Hello everyone,
> I have the following problem:
> I acquire a (evenly spaced) 3d field of 3d vectors from a HDF5 data
> >>> import tables
> >>> field=tables.openFile("test.h5").root.YeeMagField.read()
> now, the data is organized in "nested arrays"... so, when I have, say,
> data points on the x-axis, 200 data points on the y-axis and 100 data
> on the z-axis, I get an array with the shape
> >>> field.shape
> >>> (300, 200, 100, 3)
> When I now want to see a 3D arrow-plot of this field, I use:
> >>> from enthought.mayavi import mlab as m
> >>> x,y,z=field.transpose()
> >>> m.quiver3d(x,y,z)
> and this works just fine. Here, the arrays (x and y and z) *each*
> one field component (i,e. into one spatial direction) at 300x200x100
> in a 3D array.
> Now, I would like to have this data in another format, so I can for
> save it to a textfile with pylab.savetxt. What I would like are six
> each 1d, three for the coordinates and three for the field components.
> I didn't know any better, I wrote the following procedure:
> def transform_3d_data_(field,lowerBounds,upperBounds): #field is the
> same as
> above, lowerBounds and upperBounds each contain three values for
> import pylab as p
> will become the spatial coordinates, ex,ey,ez will become the field
> for xi in range(field.shape): #for each x coordinate...
> for yi in range(field.shape): #for each y coordinate...
> for zi in range(field.shape): #for each z coordinate...
> #append this
> #x, y, z coordinate
> #to xx, yy, zz ....
> ex.append(field[xi][yi][zi]) #and also
> ey.append(field[xi][yi][zi]) #add this field
> ez.append(field[xi][yi][zi]) #to ex, ey, ez.
> xx,yy,zz,ex,ey,ez=[p.array(_) for _ in [xx,yy,zz,ex,ey,ez]]
> return xx,yy,zz,ex,ey,ez
> , so I get the desired six 1D-arrays xx,yy,zz for the coordinates and
> ex,ey,ez for the field components. It works.
> Now my question: there has to be a better way to get this
> right? This one here is much too slow, obviously. Is there maybe a
> command for pylab that does this?
> Thanks in advance, cheers
> PS. I'm new to this messaging board, and I was wondering if there is
> "normal" forum as well? I can't even search through the archives at
> http://mail.scipy.org/pipermail/numpy-discussion/ :( have there ever
> discussions/initiatives about porting the mailing list archives for
> to a phpBB based forum?
More information about the NumPy-Discussion