[Numpy-discussion] [numpy-discussion] Transform 3d data

Nadav Horesh nadavh at visionsense.com
Tue Oct 19 12:20:24 EDT 2010


Of course there is an (at least one) error:
the line should be:

XYZ =
np.mgrid[lwrbnd[0]:uprbnd[0]:shape[0]*1j,lwrbnd[1]:uprbnd[1]:shape[1]*1j, lwrbnd[2]:uprbnd[2]:shape[2]*1j]


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[0]:uprbnd[0]:shape[0],
> lwrbnd[1]:uprbnd[1]:shape[1], lwrbnd[2]:uprbnd[2]:shape[2]] 
>   vectors = fields.reshape(-1,3) 
>   np.savetxt(np.hstack((XYZ.reshape(3,-1).T, vectors)))
> 
> 
>   Nadav 
>   
>  
> 
> -----Original Message----- 
> From: numpy-discussion-bounces at scipy.org on behalf of Thomas
> Königstein 
> Sent: Tue 19-Oct-10 12:05 
> To: numpy-discussion at scipy.org 
> 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
> file:
> 
> >>> import tables 
> >>> field=tables.openFile("test.h5").root.YeeMagField.read()
> 
> now, the data is organized in "nested arrays"... so, when I have, say,
> 300 
> data points on the x-axis, 200 data points on the y-axis and 100 data
> points 
> 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*
> contain 
> one field component (i,e. into one spatial direction) at 300x200x100
> points 
> in a 3D array.
> 
> Now, I would like to have this data in another format, so I can for
> example 
> save it to a textfile with pylab.savetxt. What I would like are six
> arrays, 
> each 1d, three for the coordinates and three for the field components.
> Since 
> 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
> x,y,z 
> min/max 
>     import pylab as p 
>     xx,yy,zz,ex,ey,ez=list(),list(),list(),list(),list(),list()
> #xx,yy,zz 
> will become the spatial coordinates, ex,ey,ez will become the field 
> components 
>     for xi in range(field.shape[0]): #for each x coordinate... 
>         for yi in range(field.shape[1]): #for each y coordinate... 
>             for zi in range(field.shape[2]): #for each z coordinate...
> 
>  xx.append(lowerBounds[0]+xi*(upperBounds[0]-lowerBounds[0])/float(field.shape[0])) 
> #append this
> 
>  yy.append(lowerBounds[1]+yi*(upperBounds[1]-lowerBounds[1])/float(field.shape[1])) 
> #x, y, z coordinate
> 
>  zz.append(lowerBounds[2]+zi*(upperBounds[2]-lowerBounds[2])/float(field.shape[2])) 
> #to xx, yy, zz .... 
>                 ex.append(field[xi][yi][zi][0]) #and also 
>                 ey.append(field[xi][yi][zi][1]) #add this field
> composition 
>                 ez.append(field[xi][yi][zi][2]) #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
> re-organization, 
> right? This one here is much too slow, obviously. Is there maybe a
> single 
> command for pylab that does this?
> 
> Thanks in advance, cheers
> 
> Thomas
> 
> PS. I'm new to this messaging board, and I was wondering if there is
> a 
> "normal" forum as well? I can't even search through the archives at 
> http://mail.scipy.org/pipermail/numpy-discussion/ :( have there ever
> been 
> discussions/initiatives about porting the mailing list archives for
> example 
> to a phpBB based forum?
> 



More information about the NumPy-Discussion mailing list