Ticket #1270: WISHplot_3d.py

File WISHplot_3d.py, 7.7 KB (added by Martyn Gigg, 10 years ago)
Line 
1# Creates a mayavi 3D  plot from a WISH Nexus Processed File (ie useable for small machines)
2# It therefore assumes the data is already transformed in q or in lambda
3import matplotlib.pyplot as p
4import numpy as np
5from scipy import *
6from pylab import *
7from math import atan2, degrees
8#from scipy.optimize import leastsq
9#import sys
10#sys.path.append('/opt/lib/python2.4/site-packages/enthought.tvtk-2.0.2.dev_r18171-py2.4-linux-x86_64.egg/enthought/')
11from enthought.mayavi import mlab
12from enthought.tvtk.api import tvtk
13from enthought.mayavi.scripts import mayavi2
14from enthought.mayavi.sources.vtk_data_source import VTKDataSource
15from enthought.mayavi.modules.surface import Surface
16pi=np.pi
17
18def cart2sph(x,y,z):
19  r=(x**2+y**2+z**2)**0.5
20  theta=atan2(y,x)
21  phi=atan2(z,(x**2+y**2)**0.5)
22  return r,theta,phi
23
24def sph2cart(r,theta,phi):
25  x=r*sin(theta)*cos(phi)
26  y=r*sin(theta)*sin(phi)
27  z=r*cos(theta)
28  return x,y,z
29
30def make_3dgrid(x,y,z,s):
31        print "making a 3D grid..."
32        sx=int(np.size(x)/256.0)
33        sy=int(np.size(y)/256.0)
34        sz=int(np.size(z)/256.0)
35        bigx=np.zeros([sx,sy,sz])
36        bigy=np.zeros([sx,sy,sz])
37        bigz=np.zeros([sx,sy,sz])
38        bigs=np.zeros([sx,sy,sz])
39        for i in range(0,sx):
40                bigx[i,:,:]=x[i]
41        for j in range(0,sy):
42                bigy[:,j,:]=y[j]
43        for k in range(0,sz):
44                bigz[:,:,k]=z[k]
45                bigs[k,k,k]=s[k]
46        return bigx,bigy,bigz,bigs
47
48def make_squares(nbpanels,nbtubesperpanel,nbpixelspertube):
49  squares=np.zeros( ( nbpanels*(nbtubesperpanel-1)*(nbpixelspertube-1) ,4 ),'int')
50  spectrum=0
51  for h in range(0,nbpanels):
52    for i in range(0,nbtubesperpanel-1):
53      for j in range(0,nbpixelspertube-1):
54          squares[spectrum,0]=j+nbpixelspertube*(i+h*nbtubesperpanel)
55          squares[spectrum,1]=j+nbpixelspertube*(i+h*nbtubesperpanel)+1
56          squares[spectrum,2]=j+nbpixelspertube*(i+1+h*nbtubesperpanel)+1
57          squares[spectrum,3]=j+nbpixelspertube*(i+1+h*nbtubesperpanel)
58          spectrum+=1
59  return squares
60
61def get_size_wksp(work_handle):
62  return work_handle.getNumberHistograms()
63
64def get_info_source_sample(work_handle):
65  print "getting info from instrument"
66  sample=work_handle.getInstrument().getSample()
67  source=work_handle.getInstrument().getSource()
68  samplePos=sample.getPos()
69  beamPos=samplePos-source.getPos()
70  return sample,samplePos,beamPos
71
72def get_spectrum_x_y(work_handle,spectrum_number):
73  x=work_handle.readX(spectrum_number)[0]
74  y=work_handle.readY(spectrum_number)[0]
75  return x,y
76
77def get_r_twotheta_phi_detector(work_handle,spectrum_number,s,sp,bp):
78  udet=work_handle.getDetector(spectrum_number)
79  r=udet.getDistance(s)
80  twotheta=udet.getTwoTheta(sp,bp)
81  phi=udet.getPhi()
82  return r,twotheta, phi
83
84def create_map_single_nxs(wname):
85  try:
86    h=mantid.getMatrixWorkspace(wname)
87  except:
88    print "Could not find workspace"+wname
89  sizew=get_size_wksp(h)
90  datax=n.zeros(sizew)
91  print datax
92  datay=n.zeros(sizew)
93  dataz=n.zeros(sizew)
94  datas=n.zeros(sizew)
95  print "nb of histograms "+str(sizew)
96  s,sp,bp=get_info_source_sample(h)
97  for spectrum_number in range(0,sizew):
98    x,y=get_spectrum_x_y(h,spectrum_number)
99    r,t,p=get_r_twotheta_phi_detector(h,spectrum_number,s,sp,bp)
100    oneoverx=1.0/x
101    datax[spectrum_number],datay[spectrum_number],dataz[spectrum_number]=sph2cart(oneoverx,t,p)
102    datas[spectrum_number]=y
103  return datax,datay,dataz,datas
104
105def plot_map(x,y,z,s):
106  src=mlab.pipeline.scalar_field(x,y,z,s)
107  mlab.pipeline.image_plane_widget(src,plane_orientation='z_axes',slice_index=10,opacity=0.3)
108  mlab.pipeline.volume(src)
109  return
110 
111
112def make_Laue_map(wname):
113  nbmonitors=5
114  work_handle=mantid.getMatrixWorkspace(wname)
115  wsize=get_size_wksp(work_handle)
116  max=int(1.0*wsize)-nbmonitors
117  s=np.zeros(max)
118  xyz=np.zeros((max,3))
119  for spectrum in range (0,max):
120    s[spectrum]=work_handle.readY(spectrum+nbmonitors)[0]
121    pos=work_handle.getDetector(spectrum+nbmonitors).getPos()
122    xyz[spectrum,0]=pos.getX()
123    xyz[spectrum,1]=pos.getY()
124    xyz[spectrum,2]=pos.getZ()
125  return xyz,s
126
127def create_tvtk_mesh(points,squares,valuestoplot):
128    print "making the polys"
129    mesh = tvtk.PolyData(points=points, polys=squares)
130    print "making the scalar"
131    mesh.point_data.scalars = valuestoplot
132    mesh.point_data.scalars.name = 'IntegratedIntensity'
133    return mesh
134#@mlab.show
135def Laue_plot(mesh,vmin,vmax):
136    print "making the engine"
137    engine = mlab.get_engine()
138    #replacemlab.figure(1) by mlab.figure() and comment malb.clf() to create a new figure each time
139    fig = mlab.figure()
140    #mlab.clf()
141    #bgcolor=(0, 0, 0), fgcolor=(0, 0, 0), figure=mesh.class_name[3:])
142    print "calling VTKDataSource"
143    src = VTKDataSource(data=mesh)
144    print "add source to engine"
145    engine.add_source(src) 
146    print "calling pipeline.surface"
147    mlab.pipeline.surface(src, opacity=1.0,vmin=vmin,vmax=vmax)       
148    #mlab.pipeline.surface(mlab.pipeline.extract_edges(src),
149    #                        color=(0, 0, 0), )
150    a=fig.scene
151    return a
152
153LoadNexusProcessed("w3319.nx5","w3319")
154print "making the map"
155xyz,integratedint=make_Laue_map("w3319")
156print "map made, now making squares"
157sq=make_squares(5,152,128)
158print "squares made, now making tvtk mesh"
159mesh=create_tvtk_mesh(xyz,sq,integratedint)
160print "tvtk mesh made, now making the plot"   
161a=Laue_plot(mesh,0,2000)
162
163
164
165========================================================
166def picker_callback(picker_obj, evt):
167    picker_obj = tvtk.to_tvtk(picker_obj)
168    picked = picker_obj.actors
169    if mesh.actor.actor._vtk_obj in [o._vtk_obj for o in picked]:
170        # m.mlab_source.points is the points array underlying the vtk
171        # dataset. GetPointId return the index in this array.
172        x_, y_ = np.lib.index_tricks.unravel_index(picker_obj.point_id,
173                                                                r.shape)
174        print "Data indices: %i, %i" % (x_, y_)
175        n_x, n_y = r.shape
176        cursor.mlab_source.set(x=np.atleast_1d(x_) - n_x/2., 
177                               y=np.atleast_1d(y_) - n_y/2.)
178        cursor3d.mlab_source.set(x=np.atleast_1d(x[x_, y_]), 
179                                 y=np.atleast_1d(y[x_, y_]),
180                                 z=np.atleast_1d(z[x_, y_]))
181        #x_, y_, z_ = picker_obj.pick_position
182        #cursor3d.mlab_source.set(x=np.atleast_1d(x_),
183        #                         y=np.atleast_1d(y_),
184        #                         z=np.atleast_1d(z_))
185
186a.picker.pointpicker.add_observer('EndPickEvent', picker_callback)
187
188################################################################################
189# Some logic to pick on click but no move
190class MvtPicker(object):
191    mouse_mvt = False
192
193    def __init__(self, picker):
194        self.picker = picker
195
196    def on_button_press(self, obj, evt):
197        self.mouse_mvt = False
198
199    def on_mouse_move(self, obj, evt):
200        self.mouse_mvt = True
201
202    def on_button_release(self, obj, evt):
203        if not self.mouse_mvt:
204            x, y = obj.GetEventPosition()
205            self.picker.pick((x, y, 0), fig.scene.renderer)
206        self.mouse_mvt = False
207       
208
209
210mvt_picker = MvtPicker(a.picker.pointpicker)
211
212a.interactor.add_observer('LeftButtonPressEvent', 
213                                mvt_picker.on_button_press)
214a.interactor.add_observer('MouseMoveEvent', 
215                                mvt_picker.on_mouse_move)
216a.interactor.add_observer('LeftButtonReleaseEvent', 
217                                mvt_picker.on_button_release)
218
219
220mlab.show()
221
222def test_getdetectorinfo(wname,spectrumnumber):
223  h=mantid.getMatrixWorkspace(wname)
224  s,sp,bp=get_info_source_sample(h)
225  r,t,p=get_r_twotheta_phi_detector(h,spectrumnumber,s,sp,bp)
226  print r, degrees(t), degrees(p)
227  return
228
229#test_getdetectorinfo("w3319_1",1000)
230#ewald=create_map_single_nxs("w3319_1")
231#plot_map(ewald)
232
233#x,y,z,s=create_map_single_nxs("w3319")
234#print "data created. Now trying to plot"
235#bigx,bigy,bigz,bigs=make_3dgrid(x,y,z,s)
236#print "grid made"
237#plot_map(bigx,bigy,bigz,bigs)