source: trunk/GSASIIimage.py @ 4102

Last change on this file since 4102 was 4102, checked in by vondreele, 4 years ago

add transfer of flat background to images corrected for 1/dist2
correct error in recalibrate; eliminate duplicate reflections in e.g. silicon from genHKLpeak

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 55.5 KB
Line 
1# -*- coding: utf-8 -*-
2#GSASII image calculations: ellipse fitting & image integration       
3########### SVN repository information ###################
4# $Date: 2019-08-20 18:16:48 +0000 (Tue, 20 Aug 2019) $
5# $Author: vondreele $
6# $Revision: 4102 $
7# $URL: trunk/GSASIIimage.py $
8# $Id: GSASIIimage.py 4102 2019-08-20 18:16:48Z vondreele $
9########### SVN repository information ###################
10'''
11*GSASIIimage: Image calc module*
12================================
13
14Ellipse fitting & image integration
15
16'''
17from __future__ import division, print_function
18import math
19import time
20import numpy as np
21import numpy.linalg as nl
22import numpy.ma as ma
23from scipy.optimize import leastsq
24import scipy.interpolate as scint
25import copy
26import GSASIIpath
27GSASIIpath.SetVersionNumber("$Revision: 4102 $")
28try:
29    import GSASIIplot as G2plt
30except ImportError: # expected in scriptable w/o matplotlib and/or wx
31    pass
32import GSASIIlattice as G2lat
33import GSASIIpwd as G2pwd
34import GSASIIspc as G2spc
35import GSASIImath as G2mth
36import GSASIIfiles as G2fil
37
38# trig functions in degrees
39sind = lambda x: math.sin(x*math.pi/180.)
40asind = lambda x: 180.*math.asin(x)/math.pi
41tand = lambda x: math.tan(x*math.pi/180.)
42atand = lambda x: 180.*math.atan(x)/math.pi
43atan2d = lambda y,x: 180.*math.atan2(y,x)/math.pi
44cosd = lambda x: math.cos(x*math.pi/180.)
45acosd = lambda x: 180.*math.acos(x)/math.pi
46rdsq2d = lambda x,p: round(1.0/math.sqrt(x),p)
47#numpy versions
48npsind = lambda x: np.sin(x*np.pi/180.)
49npasind = lambda x: 180.*np.arcsin(x)/np.pi
50npcosd = lambda x: np.cos(x*np.pi/180.)
51npacosd = lambda x: 180.*np.arccos(x)/np.pi
52nptand = lambda x: np.tan(x*np.pi/180.)
53npatand = lambda x: 180.*np.arctan(x)/np.pi
54npatan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi
55nxs = np.newaxis
56debug = False
57   
58def pointInPolygon(pXY,xy):
59    'Needs a doc string'
60    #pXY - assumed closed 1st & last points are duplicates
61    Inside = False
62    N = len(pXY)
63    p1x,p1y = pXY[0]
64    for i in range(N+1):
65        p2x,p2y = pXY[i%N]
66        if (max(p1y,p2y) >= xy[1] > min(p1y,p2y)) and (xy[0] <= max(p1x,p2x)):
67            if p1y != p2y:
68                xinters = (xy[1]-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
69            if p1x == p2x or xy[0] <= xinters:
70                Inside = not Inside
71        p1x,p1y = p2x,p2y
72    return Inside
73   
74def peneCorr(tth,dep,dist,tilt=0.,azm=0.):
75    'Needs a doc string'
76#    return dep*(1.-npcosd(abs(tilt*npsind(azm))-tth*npcosd(azm)))  #something wrong here
77    return dep*(1.-npcosd(tth))*dist**2/1000.         #best one
78#    return dep*npsind(tth)             #not as good as 1-cos2Q
79       
80def makeMat(Angle,Axis):
81    '''Make rotation matrix from Angle and Axis
82
83    :param float Angle: in degrees
84    :param int Axis: 0 for rotation about x, 1 for about y, etc.
85    '''
86    cs = npcosd(Angle)
87    ss = npsind(Angle)
88    M = np.array(([1.,0.,0.],[0.,cs,-ss],[0.,ss,cs]),dtype=np.float32)
89    return np.roll(np.roll(M,Axis,axis=0),Axis,axis=1)
90                   
91def FitEllipse(xy):
92   
93    def ellipse_center(p):
94        ''' gives ellipse center coordinates
95        '''
96        b,c,d,f,a = p[1]/2., p[2], p[3]/2., p[4]/2., p[0]
97        num = b*b-a*c
98        x0=(c*d-b*f)/num
99        y0=(a*f-b*d)/num
100        return np.array([x0,y0])
101   
102    def ellipse_angle_of_rotation( p ):
103        ''' gives rotation of ellipse major axis from x-axis
104        range will be -90 to 90 deg
105        '''
106        b,c,a = p[1]/2., p[2], p[0]
107        return 0.5*npatand(2*b/(a-c))
108   
109    def ellipse_axis_length( p ):
110        ''' gives ellipse radii in [minor,major] order
111        '''
112        b,c,d,f,g,a = p[1]/2., p[2], p[3]/2., p[4]/2, p[5], p[0]
113        up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
114        down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
115        down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
116        res1=np.sqrt(up/down1)
117        res2=np.sqrt(up/down2)
118        return np.array([ res2,res1])
119   
120    xy = np.array(xy)
121    x = np.asarray(xy.T[0])[:,np.newaxis]
122    y = np.asarray(xy.T[1])[:,np.newaxis]
123    D =  np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
124    S = np.dot(D.T,D)
125    C = np.zeros([6,6])
126    C[0,2] = C[2,0] = 2; C[1,1] = -1
127    E, V =  nl.eig(np.dot(nl.inv(S), C))
128    n = np.argmax(np.abs(E))
129    a = V[:,n]
130    cent = ellipse_center(a)
131    phi = ellipse_angle_of_rotation(a)
132    radii = ellipse_axis_length(a)
133    phi += 90.
134    if radii[0] > radii[1]:
135        radii = [radii[1],radii[0]]
136        phi -= 90.
137    return cent,phi,radii
138
139def FitDetector(rings,varyList,parmDict,Print=True,covar=False):
140    '''Fit detector calibration paremeters
141
142    :param np.array rings: vector of ring positions
143    :param list varyList: calibration parameters to be refined
144    :param dict parmDict: all calibration parameters
145    :param bool Print: set to True (default) to print the results
146    :param bool covar: set to True to return the covariance matrix (default is False)
147    :returns: [chisq,vals,sigList] unless covar is True, then
148        [chisq,vals,sigList,coVarMatrix] is returned
149    '''
150       
151    def CalibPrint(ValSig,chisq,Npts):
152        print ('Image Parameters: chi**2: %12.3g, Np: %d'%(chisq,Npts))
153        ptlbls = 'names :'
154        ptstr =  'values:'
155        sigstr = 'esds  :'
156        for name,value,sig in ValSig:
157            ptlbls += "%s" % (name.rjust(12))
158            if name == 'phi':
159                ptstr += Fmt[name] % (value%360.)
160            else:
161                ptstr += Fmt[name] % (value)
162            if sig:
163                sigstr += Fmt[name] % (sig)
164            else:
165                sigstr += 12*' '
166        print (ptlbls)
167        print (ptstr)
168        print (sigstr)       
169       
170    def ellipseCalcD(B,xyd,varyList,parmDict):
171       
172        x,y,dsp = xyd
173        varyDict = dict(zip(varyList,B))
174        parms = {}
175        for parm in parmDict:
176            if parm in varyList:
177                parms[parm] = varyDict[parm]
178            else:
179                parms[parm] = parmDict[parm]
180        phi = parms['phi']-90.               #get rotation of major axis from tilt axis
181        tth = 2.0*npasind(parms['wave']/(2.*dsp))
182        phi0 = npatan2d(y-parms['det-Y'],x-parms['det-X'])
183        dxy = peneCorr(tth,parms['dep'],parms['dist'],parms['tilt'],phi0)
184        stth = npsind(tth)
185        cosb = npcosd(parms['tilt'])
186        tanb = nptand(parms['tilt'])       
187        tbm = nptand((tth-parms['tilt'])/2.)
188        tbp = nptand((tth+parms['tilt'])/2.)
189        d = parms['dist']+dxy
190        fplus = d*tanb*stth/(cosb+stth)
191        fminus = d*tanb*stth/(cosb-stth)
192        vplus = d*(tanb+(1+tbm)/(1-tbm))*stth/(cosb+stth)
193        vminus = d*(tanb+(1-tbp)/(1+tbp))*stth/(cosb-stth)
194        R0 = np.sqrt((vplus+vminus)**2-(fplus+fminus)**2)/2.      #+minor axis
195        R1 = (vplus+vminus)/2.                                    #major axis
196        zdis = (fplus-fminus)/2.
197        Robs = np.sqrt((x-parms['det-X'])**2+(y-parms['det-Y'])**2)
198        rsqplus = R0**2+R1**2
199        rsqminus = R0**2-R1**2
200        R = rsqminus*npcosd(2.*phi0-2.*phi)+rsqplus
201        Q = np.sqrt(2.)*R0*R1*np.sqrt(R-2.*zdis**2*npsind(phi0-phi)**2)
202        P = 2.*R0**2*zdis*npcosd(phi0-phi)
203        Rcalc = (P+Q)/R
204        M = (Robs-Rcalc)*10.        #why 10? does make "chi**2" more reasonable
205        return M
206       
207    names = ['dist','det-X','det-Y','tilt','phi','dep','wave']
208    fmt = ['%12.3f','%12.3f','%12.3f','%12.3f','%12.3f','%12.4f','%12.6f']
209    Fmt = dict(zip(names,fmt))
210    p0 = [parmDict[key] for key in varyList]
211    result = leastsq(ellipseCalcD,p0,args=(rings.T,varyList,parmDict),full_output=True,ftol=1.e-8)
212    chisq = np.sum(result[2]['fvec']**2)/(rings.shape[0]-len(p0))   #reduced chi^2 = M/(Nobs-Nvar)
213    parmDict.update(zip(varyList,result[0]))
214    vals = list(result[0])
215    sig = list(np.sqrt(chisq*np.diag(result[1])))
216    sigList = np.zeros(7)
217    for i,name in enumerate(varyList):
218        sigList[i] = sig[varyList.index(name)]
219    ValSig = zip(varyList,vals,sig)
220    if Print:
221        CalibPrint(ValSig,chisq,rings.shape[0])
222    if covar:
223        return [chisq,vals,sigList,result[1]]
224    else:
225        return [chisq,vals,sigList]
226
227def ImageLocalMax(image,w,Xpix,Ypix):
228    'Needs a doc string'
229    w2 = w*2
230    sizey,sizex = image.shape
231    xpix = int(Xpix)            #get reference corner of pixel chosen
232    ypix = int(Ypix)
233    if not w:
234        ZMax = np.sum(image[ypix-2:ypix+2,xpix-2:xpix+2])
235        return xpix,ypix,ZMax,0.0001
236    if (w2 < xpix < sizex-w2) and (w2 < ypix < sizey-w2) and image[ypix,xpix]:
237        ZMax = image[ypix-w:ypix+w,xpix-w:xpix+w]
238        Zmax = np.argmax(ZMax)
239        ZMin = image[ypix-w2:ypix+w2,xpix-w2:xpix+w2]
240        Zmin = np.argmin(ZMin)
241        xpix += Zmax%w2-w
242        ypix += Zmax//w2-w
243        return xpix,ypix,np.ravel(ZMax)[Zmax],max(0.0001,np.ravel(ZMin)[Zmin])   #avoid neg/zero minimum
244    else:
245        return 0,0,0,0     
246   
247def makeRing(dsp,ellipse,pix,reject,scalex,scaley,image,mul=1):
248    'Needs a doc string'
249    def ellipseC():
250        'compute estimate of ellipse circumference'
251        if radii[0] < 0:        #hyperbola
252#            theta = npacosd(1./np.sqrt(1.+(radii[0]/radii[1])**2))
253#            print (theta)
254            return 0
255        apb = radii[1]+radii[0]
256        amb = radii[1]-radii[0]
257        return np.pi*apb*(1+3*(amb/apb)**2/(10+np.sqrt(4-3*(amb/apb)**2)))
258       
259    cent,phi,radii = ellipse
260    cphi = cosd(phi-90.)        #convert to major axis rotation
261    sphi = sind(phi-90.)
262    ring = []
263    C = int(ellipseC())*mul         #ring circumference in mm
264    azm = []
265    for i in range(0,C,1):      #step around ring in 1mm increments
266        a = 360.*i/C
267        x = radii[1]*cosd(a-phi+90.)        #major axis
268        y = radii[0]*sind(a-phi+90.)
269        X = (cphi*x-sphi*y+cent[0])*scalex      #convert mm to pixels
270        Y = (sphi*x+cphi*y+cent[1])*scaley
271        X,Y,I,J = ImageLocalMax(image,pix,X,Y)
272        if I and J and float(I)/J > reject:
273            X += .5                             #set to center of pixel
274            Y += .5
275            X /= scalex                         #convert back to mm
276            Y /= scaley
277            if [X,Y,dsp] not in ring:           #no duplicates!
278                ring.append([X,Y,dsp])
279                azm.append(a)
280    if len(ring) < 10:
281        ring = []
282        azm = []
283    return ring,azm
284   
285def GetEllipse2(tth,dxy,dist,cent,tilt,phi):
286    '''uses Dandelin spheres to find ellipse or hyperbola parameters from detector geometry
287    on output
288    radii[0] (b-minor axis) set < 0. for hyperbola
289   
290    '''
291    radii = [0,0]
292    stth = sind(tth)
293    cosb = cosd(tilt)
294    tanb = tand(tilt)
295    tbm = tand((tth-tilt)/2.)
296    tbp = tand((tth+tilt)/2.)
297    sinb = sind(tilt)
298    d = dist+dxy
299    if tth+abs(tilt) < 90.:      #ellipse
300        fplus = d*tanb*stth/(cosb+stth)
301        fminus = d*tanb*stth/(cosb-stth)
302        vplus = d*(tanb+(1+tbm)/(1-tbm))*stth/(cosb+stth)
303        vminus = d*(tanb+(1-tbp)/(1+tbp))*stth/(cosb-stth)
304        radii[0] = np.sqrt((vplus+vminus)**2-(fplus+fminus)**2)/2.      #+minor axis
305        radii[1] = (vplus+vminus)/2.                                    #major axis
306        zdis = (fplus-fminus)/2.
307    else:   #hyperbola!
308        f = d*abs(tanb)*stth/(cosb+stth)
309        v = d*(abs(tanb)+tand(tth-abs(tilt)))
310        delt = d*stth*(1.+stth*cosb)/(abs(sinb)*cosb*(stth+cosb))
311        eps = (v-f)/(delt-v)
312        radii[0] = -eps*(delt-f)/np.sqrt(eps**2-1.)                     #-minor axis
313        radii[1] = eps*(delt-f)/(eps**2-1.)                             #major axis
314        if tilt > 0:
315            zdis = f+radii[1]*eps
316        else:
317            zdis = -f
318#NB: zdis is || to major axis & phi is rotation of minor axis
319#thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)]
320    elcent = [cent[0]+zdis*sind(phi),cent[1]-zdis*cosd(phi)]
321    return elcent,phi,radii
322   
323def GetEllipse(dsp,data):
324    '''uses Dandelin spheres to find ellipse or hyperbola parameters from detector geometry
325    as given in image controls dictionary (data) and a d-spacing (dsp)
326    '''
327    cent = data['center']
328    tilt = data['tilt']
329    phi = data['rotation']
330    dep = data.get('DetDepth',0.0)
331    tth = 2.0*asind(data['wavelength']/(2.*dsp))
332    dist = data['distance']
333    dxy = peneCorr(tth,dep,dist,tilt)
334    return GetEllipse2(tth,dxy,dist,cent,tilt,phi)
335       
336def GetDetectorXY(dsp,azm,data):
337    '''Get detector x,y position from d-spacing (dsp), azimuth (azm,deg)
338    & image controls dictionary (data)
339    it seems to be only used in plotting
340    '''   
341    elcent,phi,radii = GetEllipse(dsp,data)
342    phi = data['rotation']-90.          #to give rotation of major axis
343    tilt = data['tilt']
344    dist = data['distance']
345    cent = data['center']
346    tth = 2.0*asind(data['wavelength']/(2.*dsp))
347    stth = sind(tth)
348    cosb = cosd(tilt)
349    if radii[0] > 0.:
350        sinb = sind(tilt)
351        tanb = tand(tilt)
352        fplus = dist*tanb*stth/(cosb+stth)
353        fminus = dist*tanb*stth/(cosb-stth)
354        zdis = (fplus-fminus)/2.
355        rsqplus = radii[0]**2+radii[1]**2
356        rsqminus = radii[0]**2-radii[1]**2
357        R = rsqminus*cosd(2.*azm-2.*phi)+rsqplus
358        Q = np.sqrt(2.)*radii[0]*radii[1]*np.sqrt(R-2.*zdis**2*sind(azm-phi)**2)
359        P = 2.*radii[0]**2*zdis*cosd(azm-phi)
360        radius = (P+Q)/R
361        xy = np.array([radius*cosd(azm),radius*sind(azm)])
362        xy += cent
363    else:   #hyperbola - both branches (one is way off screen!)
364        sinb = abs(sind(tilt))
365        tanb = abs(tand(tilt))
366        f = dist*tanb*stth/(cosb+stth)
367        v = dist*(tanb+tand(tth-abs(tilt)))
368        delt = dist*stth*(1+stth*cosb)/(sinb*cosb*(stth+cosb))
369        ecc = (v-f)/(delt-v)
370        R = radii[1]*(ecc**2-1)/(1-ecc*cosd(azm))
371        if tilt > 0.:
372            offset = 2.*radii[1]*ecc+f      #select other branch
373            xy = [-R*cosd(azm)-offset,-R*sind(azm)]
374        else:
375            offset = -f
376            xy = [-R*cosd(azm)-offset,R*sind(azm)]
377        xy = -np.array([xy[0]*cosd(phi)+xy[1]*sind(phi),xy[0]*sind(phi)-xy[1]*cosd(phi)])
378        xy += cent
379    return xy
380   
381def GetDetXYfromThAzm(Th,Azm,data):
382    '''Computes a detector position from a 2theta angle and an azimultal
383    angle (both in degrees) - apparently not used!
384    '''
385    dsp = data['wavelength']/(2.0*npsind(Th))   
386    return GetDetectorXY(dsp,Azm,data)
387                   
388def GetTthAzmDsp(x,y,data): #expensive
389    '''Computes a 2theta, etc. from a detector position and calibration constants - checked
390    OK for ellipses & hyperbola.
391
392    :returns: np.array(tth,azm,G,dsp) where tth is 2theta, azm is the azimutal angle,
393       G is ? and dsp is the d-space
394    '''
395    wave = data['wavelength']
396    cent = data['center']
397    tilt = data['tilt']
398    dist = data['distance']/cosd(tilt)
399    x0 = dist*tand(tilt)
400    phi = data['rotation']
401    dep = data.get('DetDepth',0.)
402    azmthoff = data['azmthOff']
403    dx = np.array(x-cent[0],dtype=np.float32)
404    dy = np.array(y-cent[1],dtype=np.float32)
405    D = ((dx-x0)**2+dy**2+dist**2)      #sample to pixel distance
406    X = np.array(([dx,dy,np.zeros_like(dx)]),dtype=np.float32).T
407    X = np.dot(X,makeMat(phi,2))
408    Z = np.dot(X,makeMat(tilt,0)).T[2]
409    tth = npatand(np.sqrt(dx**2+dy**2-Z**2)/(dist-Z))
410    dxy = peneCorr(tth,dep,dist,tilt,npatan2d(dy,dx))
411    DX = dist-Z+dxy
412    DY = np.sqrt(dx**2+dy**2-Z**2)
413    tth = npatan2d(DY,DX) 
414    dsp = wave/(2.*npsind(tth/2.))
415    azm = (npatan2d(dy,dx)+azmthoff+720.)%360.
416    G = D/dist**2       #for geometric correction = 1/cos(2theta)^2 if tilt=0.
417    return np.array([tth,azm,G,dsp])
418   
419def GetTth(x,y,data):
420    'Give 2-theta value for detector x,y position; calibration info in data'
421    return GetTthAzmDsp(x,y,data)[0]
422   
423def GetTthAzm(x,y,data):
424    'Give 2-theta, azimuth values for detector x,y position; calibration info in data'
425    return GetTthAzmDsp(x,y,data)[0:2]
426   
427def GetTthAzmG(x,y,data):
428    '''Give 2-theta, azimuth & geometric corr. values for detector x,y position;
429     calibration info in data - only used in integration
430    '''
431    'Needs a doc string - checked OK for ellipses & hyperbola'
432    tilt = data['tilt']
433    dist = data['distance']/npcosd(tilt)
434    x0 = data['distance']*nptand(tilt)
435    MN = -np.inner(makeMat(data['rotation'],2),makeMat(tilt,0))
436    distsq = data['distance']**2
437    dx = x-data['center'][0]
438    dy = y-data['center'][1]
439    G = ((dx-x0)**2+dy**2+distsq)/distsq       #for geometric correction = 1/cos(2theta)^2 if tilt=0.
440    Z = np.dot(np.dstack([dx.T,dy.T,np.zeros_like(dx.T)]),MN).T[2]
441    xyZ = dx**2+dy**2-Z**2   
442    tth = npatand(np.sqrt(xyZ)/(dist-Z))
443    dxy = peneCorr(tth,data['DetDepth'],dist,tilt,npatan2d(dy,dx))
444    tth = npatan2d(np.sqrt(xyZ),dist-Z+dxy) 
445    azm = (npatan2d(dy,dx)+data['azmthOff']+720.)%360.
446    return tth,azm,G
447
448def GetDsp(x,y,data):
449    'Give d-spacing value for detector x,y position; calibration info in data'
450    return GetTthAzmDsp(x,y,data)[3]
451       
452def GetAzm(x,y,data):
453    'Give azimuth value for detector x,y position; calibration info in data'
454    return GetTthAzmDsp(x,y,data)[1]
455   
456def meanAzm(a,b):
457    AZM = lambda a,b: npacosd(0.5*(npsind(2.*b)-npsind(2.*a))/(np.pi*(b-a)/180.))/2.
458    azm = AZM(a,b)
459#    quad = int((a+b)/180.)
460#    if quad == 1:
461#        azm = 180.-azm
462#    elif quad == 2:
463#        azm += 180.
464#    elif quad == 3:
465#        azm = 360-azm
466    return azm     
467       
468def ImageCompress(image,scale):
469    ''' Reduces size of image by selecting every n'th point
470    param: image array: original image
471    param: scale int: intervsl between selected points
472    returns: array: reduced size image
473    '''
474    if scale == 1:
475        return image
476    else:
477        return image[::scale,::scale]
478       
479def checkEllipse(Zsum,distSum,xSum,ySum,dist,x,y):
480    'Needs a doc string'
481    avg = np.array([distSum/Zsum,xSum/Zsum,ySum/Zsum])
482    curr = np.array([dist,x,y])
483    return abs(avg-curr)/avg < .02
484
485def GetLineScan(image,data):
486    Nx,Ny = data['size']
487    pixelSize = data['pixelSize']
488    scalex = 1000./pixelSize[0]         #microns --> 1/mm
489    scaley = 1000./pixelSize[1]
490    wave = data['wavelength']
491    numChans = data['outChannels']
492    LUtth = np.array(data['IOtth'],dtype=np.float)
493    azm = data['linescan'][1]-data['azmthOff']
494    Tx = np.array([tth for tth in np.linspace(LUtth[0],LUtth[1],numChans+1)])
495    Ty = np.zeros_like(Tx)
496    dsp = wave/(2.0*npsind(Tx/2.0))
497    xy = np.array([GetDetectorXY(d,azm,data) for d in dsp]).T
498    xy[1] *= scalex
499    xy[0] *= scaley
500    xy = np.array(xy,dtype=int)
501    Xpix = ma.masked_outside(xy[1],0,Ny-1)
502    Ypix = ma.masked_outside(xy[0],0,Nx-1)
503    xpix = Xpix[~(Xpix.mask+Ypix.mask)].compressed()
504    ypix = Ypix[~(Xpix.mask+Ypix.mask)].compressed()
505    Ty = image[xpix,ypix]
506    Tx = ma.array(Tx,mask=Xpix.mask+Ypix.mask).compressed()
507    return [Tx,Ty]
508
509def EdgeFinder(image,data):
510    '''this makes list of all x,y where I>edgeMin suitable for an ellipse search?
511    Not currently used but might be useful in future?
512    '''
513    import numpy.ma as ma
514    Nx,Ny = data['size']
515    pixelSize = data['pixelSize']
516    edgemin = data['edgemin']
517    scalex = pixelSize[0]/1000.
518    scaley = pixelSize[1]/1000.   
519    tay,tax = np.mgrid[0:Nx,0:Ny]
520    tax = np.asfarray(tax*scalex,dtype=np.float32)
521    tay = np.asfarray(tay*scaley,dtype=np.float32)
522    tam = ma.getmask(ma.masked_less(image.flatten(),edgemin))
523    tax = ma.compressed(ma.array(tax.flatten(),mask=tam))
524    tay = ma.compressed(ma.array(tay.flatten(),mask=tam))
525    return zip(tax,tay)
526   
527def MakeFrameMask(data,frame):
528    import polymask as pm
529    pixelSize = data['pixelSize']
530    scalex = pixelSize[0]/1000.
531    scaley = pixelSize[1]/1000.
532    blkSize = 512
533    Nx,Ny = data['size']
534    nXBlks = (Nx-1)//blkSize+1
535    nYBlks = (Ny-1)//blkSize+1
536    tam = ma.make_mask_none(data['size'])
537    for iBlk in range(nXBlks):
538        iBeg = iBlk*blkSize
539        iFin = min(iBeg+blkSize,Nx)
540        for jBlk in range(nYBlks):
541            jBeg = jBlk*blkSize
542            jFin = min(jBeg+blkSize,Ny)               
543            nI = iFin-iBeg
544            nJ = jFin-jBeg
545            tax,tay = np.mgrid[iBeg+0.5:iFin+.5,jBeg+.5:jFin+.5]         #bin centers not corners
546            tax = np.asfarray(tax*scalex,dtype=np.float32)
547            tay = np.asfarray(tay*scaley,dtype=np.float32)
548            tamp = ma.make_mask_none((1024*1024))
549            tamp = ma.make_mask(pm.polymask(nI*nJ,tax.flatten(),
550                tay.flatten(),len(frame),frame,tamp)[:nI*nJ])^True  #switch to exclude around frame
551            if tamp.shape:
552                tamp = np.reshape(tamp[:nI*nJ],(nI,nJ))
553                tam[iBeg:iFin,jBeg:jFin] = ma.mask_or(tamp[0:nI,0:nJ],tam[iBeg:iFin,jBeg:jFin])
554            else:
555                tam[iBeg:iFin,jBeg:jFin] = True
556    return tam.T
557   
558def ImageRecalibrate(G2frame,ImageZ,data,masks):
559    '''Called to repeat the calibration on an image, usually called after
560    calibration is done initially to improve the fit.
561
562    :param G2frame: The top-level GSAS-II frame or None, to skip plotting
563    :param np.Array ImageZ: the image to calibrate
564    :param dict data: the Controls dict for the image
565    :param dict masks: a dict with masks
566    :returns: a list containing vals,varyList,sigList,parmDict,covar
567    '''
568    import ImageCalibrants as calFile
569    G2fil.G2Print ('Image recalibration:')
570    time0 = time.time()
571    pixelSize = data['pixelSize']
572    scalex = 1000./pixelSize[0]
573    scaley = 1000./pixelSize[1]
574    pixLimit = data['pixLimit']
575    cutoff = data['cutoff']
576    data['rings'] = []
577    data['ellipses'] = []
578    if data['DetDepth'] > 0.5:          #patch - redefine DetDepth
579        data['DetDepth'] /= data['distance']
580    if not data['calibrant']:
581        G2fil.G2Print ('warning: no calibration material selected')
582        return []   
583    skip = data['calibskip']
584    dmin = data['calibdmin']
585    if data['calibrant'] not in calFile.Calibrants:
586        G2fil.G2Print('Warning: %s not in local copy of image calibrants file'%data['calibrant'])
587        return []
588    calibrant = calFile.Calibrants[data['calibrant']]
589    Bravais,SGs,Cells = calibrant[:3]
590    HKL = []
591    for bravais,sg,cell in zip(Bravais,SGs,Cells):
592        A = G2lat.cell2A(cell)
593        if sg:
594            SGData = G2spc.SpcGroup(sg)[1]
595            hkl = G2pwd.getHKLpeak(dmin,SGData,A,Inst=None,nodup=True)
596            HKL += list(hkl)
597        else:
598            hkl = G2lat.GenHBravais(dmin,bravais,A)
599            HKL += list(hkl)
600    if len(calibrant) > 5:
601        absent = calibrant[5]
602    else:
603        absent = ()
604    HKL = G2lat.sortHKLd(HKL,True,False)
605    varyList = [item for item in data['varyList'] if data['varyList'][item]]
606    parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1],
607        'setdist':data.get('setdist',data['distance']),
608        'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']}
609    Found = False
610    wave = data['wavelength']
611    frame = masks['Frames']
612    tam = ma.make_mask_none(ImageZ.shape)
613    if frame:
614        tam = ma.mask_or(tam,MakeFrameMask(data,frame))
615    for iH,H in enumerate(HKL):
616        if debug:   print (H) 
617        dsp = H[3]
618        tth = 2.0*asind(wave/(2.*dsp))
619        if tth+abs(data['tilt']) > 90.:
620            G2fil.G2Print ('next line is a hyperbola - search stopped')
621            break
622        ellipse = GetEllipse(dsp,data)
623        if iH not in absent and iH >= skip:
624            Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,ma.array(ImageZ,mask=tam))[0]
625        else:
626            Ring = makeRing(dsp,ellipse,pixLimit,1000.0,scalex,scaley,ma.array(ImageZ,mask=tam))[0]
627        if Ring:
628            if iH not in absent and iH >= skip:
629                data['rings'].append(np.array(Ring))
630            data['ellipses'].append(copy.deepcopy(ellipse+('r',)))
631            Found = True
632        elif not Found:         #skipping inner rings, keep looking until ring found
633            continue
634        else:                   #no more rings beyond edge of detector
635            data['ellipses'].append([])
636            continue
637    if not data['rings']:
638        G2fil.G2Print ('no rings found; try lower Min ring I/Ib',mode='warn')
639        return []   
640       
641    rings = np.concatenate((data['rings']),axis=0)
642    [chisq,vals,sigList,covar] = FitDetector(rings,varyList,parmDict,True,True)
643    data['wavelength'] = parmDict['wave']
644    data['distance'] = parmDict['dist']
645    data['center'] = [parmDict['det-X'],parmDict['det-Y']]
646    data['rotation'] = np.mod(parmDict['phi'],360.0)
647    data['tilt'] = parmDict['tilt']
648    data['DetDepth'] = parmDict['dep']
649    data['chisq'] = chisq
650    N = len(data['ellipses'])
651    data['ellipses'] = []           #clear away individual ellipse fits
652    for H in HKL[:N]:
653        ellipse = GetEllipse(H[3],data)
654        data['ellipses'].append(copy.deepcopy(ellipse+('b',)))   
655    G2fil.G2Print ('calibration time = %.3f'%(time.time()-time0))
656    if G2frame:
657        G2plt.PlotImage(G2frame,newImage=True)       
658    return [vals,varyList,sigList,parmDict,covar]
659
660def ImageCalibrate(G2frame,data):
661    '''Called to perform an initial image calibration after points have been
662    selected for the inner ring.
663    '''
664    import ImageCalibrants as calFile
665    G2fil.G2Print ('Image calibration:')
666    time0 = time.time()
667    ring = data['ring']
668    pixelSize = data['pixelSize']
669    scalex = 1000./pixelSize[0]
670    scaley = 1000./pixelSize[1]
671    pixLimit = data['pixLimit']
672    cutoff = data['cutoff']
673    varyDict = data['varyList']
674    if varyDict['dist'] and varyDict['wave']:
675        G2fil.G2Print ('ERROR - you can not simultaneously calibrate distance and wavelength')
676        return False
677    if len(ring) < 5:
678        G2fil.G2Print ('ERROR - not enough inner ring points for ellipse')
679        return False
680       
681    #fit start points on inner ring
682    data['ellipses'] = []
683    data['rings'] = []
684    outE = FitEllipse(ring)
685    fmt  = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f'
686    fmt2 = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f, chi**2: %.3f, Np: %d'
687    if outE:
688        G2fil.G2Print (fmt%('start ellipse: ',outE[0][0],outE[0][1],outE[1],outE[2][0],outE[2][1]))
689        ellipse = outE
690    else:
691        return False
692       
693    #setup 360 points on that ring for "good" fit
694    data['ellipses'].append(ellipse[:]+('g',))
695    Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,G2frame.ImageZ)[0]
696    if Ring:
697        ellipse = FitEllipse(Ring)
698        Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,G2frame.ImageZ)[0]    #do again
699        ellipse = FitEllipse(Ring)
700    else:
701        G2fil.G2Print ('1st ring not sufficiently complete to proceed',mode='warn')
702        return False
703    if debug:
704        G2fil.G2Print (fmt2%('inner ring:    ',ellipse[0][0],ellipse[0][1],ellipse[1],
705            ellipse[2][0],ellipse[2][1],0.,len(Ring)))     #cent,phi,radii
706    data['ellipses'].append(ellipse[:]+('r',))
707    data['rings'].append(np.array(Ring))
708    G2plt.PlotImage(G2frame,newImage=True)
709   
710#setup for calibration
711    data['rings'] = []
712    if not data['calibrant']:
713        G2fil.G2Print ('Warning: no calibration material selected')
714        return True
715   
716    skip = data['calibskip']
717    dmin = data['calibdmin']
718#generate reflection set
719    calibrant = calFile.Calibrants[data['calibrant']]
720    Bravais,SGs,Cells = calibrant[:3]
721    HKL = []
722    for bravais,sg,cell in zip(Bravais,SGs,Cells):
723        A = G2lat.cell2A(cell)
724        if sg:
725            SGData = G2spc.SpcGroup(sg)[1]
726            hkl = G2pwd.getHKLpeak(dmin,SGData,A,Inst=None,nodup=True)
727            #G2fil.G2Print(hkl)
728            HKL += list(hkl)
729        else:
730            hkl = G2lat.GenHBravais(dmin,bravais,A)
731            HKL += list(hkl)
732    HKL = G2lat.sortHKLd(HKL,True,False)[skip:]
733#set up 1st ring
734    elcent,phi,radii = ellipse              #from fit of 1st ring
735    dsp = HKL[0][3]
736    G2fil.G2Print ('1st ring: try %.4f'%(dsp))
737    if varyDict['dist']:
738        wave = data['wavelength']
739        tth = 2.0*asind(wave/(2.*dsp))
740    else:   #varyDict['wave']!
741        dist = data['distance']
742        tth = npatan2d(radii[0],dist)
743        data['wavelength'] = wave =  2.0*dsp*sind(tth/2.0)
744    Ring0 = makeRing(dsp,ellipse,3,cutoff,scalex,scaley,G2frame.ImageZ)[0]
745    ttth = nptand(tth)
746    ctth = npcosd(tth)
747#1st estimate of tilt; assume ellipse - don't know sign though
748    if varyDict['tilt']:
749        tilt = npasind(np.sqrt(max(0.,1.-(radii[0]/radii[1])**2))*ctth)
750        if not tilt:
751            G2fil.G2Print ('WARNING - selected ring was fitted as a circle')
752            G2fil.G2Print (' - if detector was tilted we suggest you skip this ring - WARNING')
753    else:
754        tilt = data['tilt']
755#1st estimate of dist: sample to detector normal to plane
756    if varyDict['dist']:
757        data['distance'] = dist = radii[0]**2/(ttth*radii[1])
758    else:
759        dist = data['distance']
760    if varyDict['tilt']:
761#ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt
762        zdisp = radii[1]*ttth*tand(tilt)
763        zdism = radii[1]*ttth*tand(-tilt)
764#cone axis position; 2 choices. Which is right?     
765#NB: zdisp is || to major axis & phi is rotation of minor axis
766#thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)]
767        centp = [elcent[0]+zdisp*sind(phi),elcent[1]-zdisp*cosd(phi)]
768        centm = [elcent[0]+zdism*sind(phi),elcent[1]-zdism*cosd(phi)]
769#check get same ellipse parms either way
770#now do next ring; estimate either way & do a FitDetector each way; best fit is correct one
771        fail = True
772        i2 = 1
773        while fail:
774            dsp = HKL[i2][3]
775            G2fil.G2Print ('2nd ring: try %.4f'%(dsp))
776            tth = 2.0*asind(wave/(2.*dsp))
777            ellipsep = GetEllipse2(tth,0.,dist,centp,tilt,phi)
778            G2fil.G2Print (fmt%('plus ellipse :',ellipsep[0][0],ellipsep[0][1],ellipsep[1],ellipsep[2][0],ellipsep[2][1]))
779            Ringp = makeRing(dsp,ellipsep,3,cutoff,scalex,scaley,G2frame.ImageZ)[0]
780            parmDict = {'dist':dist,'det-X':centp[0],'det-Y':centp[1],
781                'tilt':tilt,'phi':phi,'wave':wave,'dep':0.0}       
782            varyList = [item for item in varyDict if varyDict[item]]
783            if len(Ringp) > 10:
784                chip = FitDetector(np.array(Ring0+Ringp),varyList,parmDict,True)[0]
785                tiltp = parmDict['tilt']
786                phip = parmDict['phi']
787                centp = [parmDict['det-X'],parmDict['det-Y']]
788                fail = False
789            else:
790                chip = 1e6
791            ellipsem = GetEllipse2(tth,0.,dist,centm,-tilt,phi)
792            G2fil.G2Print (fmt%('minus ellipse:',ellipsem[0][0],ellipsem[0][1],ellipsem[1],ellipsem[2][0],ellipsem[2][1]))
793            Ringm = makeRing(dsp,ellipsem,3,cutoff,scalex,scaley,G2frame.ImageZ)[0]
794            if len(Ringm) > 10:
795                parmDict['tilt'] *= -1
796                chim = FitDetector(np.array(Ring0+Ringm),varyList,parmDict,True)[0]
797                tiltm = parmDict['tilt']
798                phim = parmDict['phi']
799                centm = [parmDict['det-X'],parmDict['det-Y']]
800                fail = False
801            else:
802                chim = 1e6
803            if fail:
804                i2 += 1
805        if chip < chim:
806            data['tilt'] = tiltp
807            data['center'] = centp
808            data['rotation'] = phip
809        else:
810            data['tilt'] = tiltm
811            data['center'] = centm
812            data['rotation'] = phim
813        data['ellipses'].append(ellipsep[:]+('b',))
814        data['rings'].append(np.array(Ringp))
815        data['ellipses'].append(ellipsem[:]+('r',))
816        data['rings'].append(np.array(Ringm))
817        G2plt.PlotImage(G2frame,newImage=True)
818    if data['DetDepth'] > 0.5:          #patch - redefine DetDepth
819        data['DetDepth'] /= data['distance']
820    parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1],
821        'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']}
822    varyList = [item for item in varyDict if varyDict[item]]
823    data['rings'] = []
824    data['ellipses'] = []
825    for i,H in enumerate(HKL):
826        dsp = H[3]
827        tth = 2.0*asind(wave/(2.*dsp))
828        if tth+abs(data['tilt']) > 90.:
829            G2fil.G2Print ('next line is a hyperbola - search stopped')
830            break
831        if debug:   print ('HKLD:'+str(H[:4])+'2-theta: %.4f'%(tth))
832        elcent,phi,radii = ellipse = GetEllipse(dsp,data)
833        data['ellipses'].append(copy.deepcopy(ellipse+('g',)))
834        if debug:   print (fmt%('predicted ellipse:',elcent[0],elcent[1],phi,radii[0],radii[1]))
835        Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,G2frame.ImageZ)[0]
836        if Ring:
837            data['rings'].append(np.array(Ring))
838            rings = np.concatenate((data['rings']),axis=0)
839            if i:
840                chisq = FitDetector(rings,varyList,parmDict,False)[0]
841                data['distance'] = parmDict['dist']
842                data['center'] = [parmDict['det-X'],parmDict['det-Y']]
843                data['rotation'] = parmDict['phi']
844                data['tilt'] = parmDict['tilt']
845                data['DetDepth'] = parmDict['dep']
846                data['chisq'] = chisq
847                elcent,phi,radii = ellipse = GetEllipse(dsp,data)
848                if debug:   print (fmt2%('fitted ellipse:   ',elcent[0],elcent[1],phi,radii[0],radii[1],chisq,len(rings)))
849            data['ellipses'].append(copy.deepcopy(ellipse+('r',)))
850#            G2plt.PlotImage(G2frame,newImage=True)
851        else:
852            if debug:   print ('insufficient number of points in this ellipse to fit')
853#            break
854    G2plt.PlotImage(G2frame,newImage=True)
855    fullSize = len(G2frame.ImageZ)/scalex
856    if 2*radii[1] < .9*fullSize:
857        G2fil.G2Print ('Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib')
858    N = len(data['ellipses'])
859    if N > 2:
860        FitDetector(rings,varyList,parmDict)[0]
861        data['wavelength'] = parmDict['wave']
862        data['distance'] = parmDict['dist']
863        data['center'] = [parmDict['det-X'],parmDict['det-Y']]
864        data['rotation'] = parmDict['phi']
865        data['tilt'] = parmDict['tilt']
866        data['DetDepth'] = parmDict['dep']
867    for H in HKL[:N]:
868        ellipse = GetEllipse(H[3],data)
869        data['ellipses'].append(copy.deepcopy(ellipse+('b',)))
870    G2fil.G2Print ('calibration time = %.3f'%(time.time()-time0))
871    G2plt.PlotImage(G2frame,newImage=True)       
872    return True
873   
874def Make2ThetaAzimuthMap(data,iLim,jLim): #most expensive part of integration!
875    'Needs a doc string'
876    #transforms 2D image from x,y space to 2-theta,azimuth space based on detector orientation
877    pixelSize = data['pixelSize']
878    scalex = pixelSize[0]/1000.
879    scaley = pixelSize[1]/1000.
880   
881    tay,tax = np.mgrid[iLim[0]+0.5:iLim[1]+.5,jLim[0]+.5:jLim[1]+.5]         #bin centers not corners
882    tax = np.asfarray(tax*scalex,dtype=np.float32).flatten()
883    tay = np.asfarray(tay*scaley,dtype=np.float32).flatten()
884    nI = iLim[1]-iLim[0]
885    nJ = jLim[1]-jLim[0]
886    TA = np.array(GetTthAzmG(np.reshape(tax,(nI,nJ)),np.reshape(tay,(nI,nJ)),data))     #includes geom. corr. as dist**2/d0**2 - most expensive step
887    TA[1] = np.where(TA[1]<0,TA[1]+360,TA[1])
888    return TA           #2-theta, azimuth & geom. corr. arrays
889
890def MakeMaskMap(data,masks,iLim,jLim,tamp):
891    import polymask as pm
892    pixelSize = data['pixelSize']
893    scalex = pixelSize[0]/1000.
894    scaley = pixelSize[1]/1000.
895   
896    tay,tax = np.mgrid[iLim[0]+0.5:iLim[1]+.5,jLim[0]+.5:jLim[1]+.5]         #bin centers not corners
897    tax = np.asfarray(tax*scalex,dtype=np.float32).flatten()
898    tay = np.asfarray(tay*scaley,dtype=np.float32).flatten()
899    nI = iLim[1]-iLim[0]
900    nJ = jLim[1]-jLim[0]
901    #make position masks here
902    frame = masks['Frames']
903    tam = ma.make_mask_none((nI*nJ))
904    if frame:
905        tam = ma.mask_or(tam,ma.make_mask(pm.polymask(nI*nJ,tax,
906            tay,len(frame),frame,tamp)[:nI*nJ])^True)
907    polygons = masks['Polygons']
908    for polygon in polygons:
909        if polygon:
910            tam = ma.mask_or(tam,ma.make_mask(pm.polymask(nI*nJ,tax,
911                tay,len(polygon),polygon,tamp)[:nI*nJ]))
912    for X,Y,rsq in masks['Points'].T:
913        tam = ma.mask_or(tam,ma.getmask(ma.masked_less((tax-X)**2+(tay-Y)**2,rsq)))
914    if tam.shape: 
915        tam = np.reshape(tam,(nI,nJ))
916    else:
917        tam = ma.make_mask_none((nI,nJ))
918    for xline in masks.get('Xlines',[]):    #a y pixel position
919        if iLim[0] <= xline <= iLim[1]:
920            tam[xline-iLim[0],:] = True
921    for yline in masks.get('Ylines',[]):    #a x pixel position
922        if jLim[0] <= yline <= jLim[1]:
923            tam[:,yline-jLim[0]] = True           
924    return tam           #position mask
925
926def Fill2ThetaAzimuthMap(masks,TA,tam,image):
927    'Needs a doc string'
928    Zlim = masks['Thresholds'][1]
929    rings = masks['Rings']
930    arcs = masks['Arcs']
931    TA = np.dstack((ma.getdata(TA[1]),ma.getdata(TA[0]),ma.getdata(TA[2])))    #azimuth, 2-theta, dist
932    tax,tay,tad = np.dsplit(TA,3)    #azimuth, 2-theta, dist**2/d0**2
933    for tth,thick in rings:
934        tam = ma.mask_or(tam.flatten(),ma.getmask(ma.masked_inside(tay.flatten(),max(0.01,tth-thick/2.),tth+thick/2.)))
935    for tth,azm,thick in arcs:
936        tamt = ma.getmask(ma.masked_inside(tay.flatten(),max(0.01,tth-thick/2.),tth+thick/2.))
937        tama = ma.getmask(ma.masked_inside(tax.flatten(),azm[0],azm[1]))
938        tam = ma.mask_or(tam.flatten(),tamt*tama)
939    taz = ma.masked_outside(image.flatten(),int(Zlim[0]),Zlim[1])
940    tabs = np.ones_like(taz)
941    tam = ma.mask_or(tam.flatten(),ma.getmask(taz))
942    tax = ma.compressed(ma.array(tax.flatten(),mask=tam))   #azimuth
943    tay = ma.compressed(ma.array(tay.flatten(),mask=tam))   #2-theta
944    taz = ma.compressed(ma.array(taz.flatten(),mask=tam))   #intensity
945    tad = ma.compressed(ma.array(tad.flatten(),mask=tam))   #dist**2/d0**2
946    tabs = ma.compressed(ma.array(tabs.flatten(),mask=tam)) #ones - later used for absorption corr.
947    return tax,tay,taz,tad,tabs
948
949def MakeUseTA(data,blkSize=128):
950    Nx,Ny = data['size']
951    nXBlks = (Nx-1)//blkSize+1
952    nYBlks = (Ny-1)//blkSize+1
953    useTA = []
954    for iBlk in range(nYBlks):
955        iBeg = iBlk*blkSize
956        iFin = min(iBeg+blkSize,Ny)
957        useTAj = []
958        for jBlk in range(nXBlks):
959            jBeg = jBlk*blkSize
960            jFin = min(jBeg+blkSize,Nx)
961            TA = Make2ThetaAzimuthMap(data,(iBeg,iFin),(jBeg,jFin))          #2-theta & azimuth arrays & create position mask
962            useTAj.append(TA)
963        useTA.append(useTAj)
964    return useTA
965
966def MakeUseMask(data,masks,blkSize=128):
967    Masks = copy.deepcopy(masks)
968    Masks['Points'] = np.array(Masks['Points']).T           #get spots as X,Y,R arrays
969    if np.any(masks['Points']):
970        Masks['Points'][2] = np.square(Masks['Points'][2]/2.)
971    Nx,Ny = data['size']
972    nXBlks = (Nx-1)//blkSize+1
973    nYBlks = (Ny-1)//blkSize+1
974    useMask = []
975    tamp = ma.make_mask_none((1024*1024))       #NB: this array size used in the fortran histogram2d
976    for iBlk in range(nYBlks):
977        iBeg = iBlk*blkSize
978        iFin = min(iBeg+blkSize,Ny)
979        useMaskj = []
980        for jBlk in range(nXBlks):
981            jBeg = jBlk*blkSize
982            jFin = min(jBeg+blkSize,Nx)
983            mask = MakeMaskMap(data,Masks,(iBeg,iFin),(jBeg,jFin),tamp)          #2-theta & azimuth arrays & create position mask
984            useMaskj.append(mask)
985        useMask.append(useMaskj)
986    return useMask
987
988def ImageIntegrate(image,data,masks,blkSize=128,returnN=False,useTA=None,useMask=None):
989    'Integrate an image; called from OnIntegrateAll and OnIntegrate in G2imgGUI'    #for q, log(q) bins need data['binType']
990    import histogram2d as h2d
991    G2fil.G2Print ('Begin image integration; image range: %d %d'%(np.min(image),np.max(image)))
992    CancelPressed = False
993    LUtth = np.array(data['IOtth'])
994    LRazm = np.array(data['LRazimuth'],dtype=np.float64)
995    numAzms = data['outAzimuths']
996    numChans = (data['outChannels']//4)*4
997    Dazm = (LRazm[1]-LRazm[0])/numAzms
998    if '2-theta' in data.get('binType','2-theta'):
999        lutth = LUtth               
1000    elif 'log(q)' in data['binType']:
1001        lutth = np.log(4.*np.pi*npsind(LUtth/2.)/data['wavelength'])
1002    elif 'q' == data['binType'].lower():
1003        lutth = 4.*np.pi*npsind(LUtth/2.)/data['wavelength']
1004    dtth = (lutth[1]-lutth[0])/numChans
1005    muT = data.get('SampleAbs',[0.0,''])[0]
1006    if data['DetDepth'] > 0.5:          #patch - redefine DetDepth
1007        data['DetDepth'] /= data['distance']
1008    if 'SASD' in data['type']:
1009        muT = -np.log(muT)/2.       #Transmission to 1/2 thickness muT
1010    Masks = copy.deepcopy(masks)
1011    Masks['Points'] = np.array(Masks['Points']).T           #get spots as X,Y,R arrays
1012    if np.any(masks['Points']):
1013        Masks['Points'][2] = np.square(Masks['Points'][2]/2.)
1014    NST = np.zeros(shape=(numAzms,numChans),order='F',dtype=np.float32)
1015    H0 = np.zeros(shape=(numAzms,numChans),order='F',dtype=np.float32)
1016    H2 = np.linspace(lutth[0],lutth[1],numChans+1)
1017    Nx,Ny = data['size']
1018    nXBlks = (Nx-1)//blkSize+1
1019    nYBlks = (Ny-1)//blkSize+1
1020    tbeg = time.time()
1021    times = [0,0,0,0,0]
1022    tamp = ma.make_mask_none((1024*1024))       #NB: this array size used in the fortran histogram2d
1023    for iBlk in range(nYBlks):
1024        iBeg = iBlk*blkSize
1025        iFin = min(iBeg+blkSize,Ny)
1026        for jBlk in range(nXBlks):
1027            jBeg = jBlk*blkSize
1028            jFin = min(jBeg+blkSize,Nx)
1029            # next is most expensive step!
1030            t0 = time.time()
1031            if useTA:
1032                TA = useTA[iBlk][jBlk]
1033            else:
1034                TA = Make2ThetaAzimuthMap(data,(iBeg,iFin),(jBeg,jFin))           #2-theta & azimuth arrays & create position mask
1035            times[1] += time.time()-t0
1036            t0 = time.time()
1037            if useMask:
1038                tam = useMask[iBlk][jBlk]
1039            else:
1040                tam = MakeMaskMap(data,Masks,(iBeg,iFin),(jBeg,jFin),tamp)
1041            Block = image[iBeg:iFin,jBeg:jFin]
1042            tax,tay,taz,tad,tabs = Fill2ThetaAzimuthMap(Masks,TA,tam,Block)    #and apply masks
1043            pol = G2pwd.Polarization(data['PolaVal'][0],tay,tax-90.)[0]         #for pixel pola correction
1044            times[0] += time.time()-t0
1045            t0 = time.time()
1046            tax = np.where(tax > LRazm[1],tax-360.,tax)                 #put azm inside limits if possible
1047            tax = np.where(tax < LRazm[0],tax+360.,tax)
1048            if data.get('SampleAbs',[0.0,''])[1]:
1049                if 'Cylind' in data['SampleShape']:
1050                    muR = muT*(1.+npsind(tax)**2/2.)/(npcosd(tay))      #adjust for additional thickness off sample normal
1051                    tabs = G2pwd.Absorb(data['SampleShape'],muR,tay)
1052                elif 'Fixed' in data['SampleShape']:    #assumes flat plate sample normal to beam
1053                    tabs = G2pwd.Absorb('Fixed',muT,tay)
1054            if 'log(q)' in data.get('binType',''):
1055                tay = np.log(4.*np.pi*npsind(tay/2.)/data['wavelength'])
1056            elif 'q' == data.get('binType','').lower():
1057                tay = 4.*np.pi*npsind(tay/2.)/data['wavelength']
1058            times[2] += time.time()-t0
1059            t0 = time.time()
1060            taz = np.array((taz*tad/tabs),dtype='float32')/pol
1061            if any([tax.shape[0],tay.shape[0],taz.shape[0]]):
1062                NST,H0 = h2d.histogram2d(len(tax),tax,tay,taz,
1063                    numAzms,numChans,LRazm,lutth,Dazm,dtth,NST,H0)
1064            times[3] += time.time()-t0
1065    G2fil.G2Print('End integration loops')
1066    t0 = time.time()
1067    #prepare masked arrays of bins with pixels for interpolation setup
1068    H2msk = [ma.array(H2[:-1],mask=np.logical_not(nst)) for nst in NST]
1069    H0msk = [ma.array(np.divide(h0,nst),mask=np.logical_not(nst)) for nst,h0 in zip(NST,H0)]
1070    #make linear interpolators; outside limits give NaN
1071    H0int = [scint.interp1d(h2msk.compressed(),h0msk.compressed(),bounds_error=False) for h0msk,h2msk in zip(H0msk,H2msk)]
1072    #do interpolation on all points - fills in the empty bins; leaves others the same
1073    H0 = np.array([h0int(H2[:-1]) for h0int in H0int])
1074    H0 = np.nan_to_num(H0)
1075    if 'log(q)' in data.get('binType',''):
1076        H2 = 2.*npasind(np.exp(H2)*data['wavelength']/(4.*np.pi))
1077    elif 'q' == data.get('binType','').lower():
1078        H2 = 2.*npasind(H2*data['wavelength']/(4.*np.pi))
1079    if Dazm:       
1080        H1 = np.array([azm for azm in np.linspace(LRazm[0],LRazm[1],numAzms+1)])
1081    else:
1082        H1 = LRazm
1083    if 'SASD' not in data['type']:
1084        H0 *= np.array(G2pwd.Polarization(data['PolaVal'][0],H2[:-1],0.)[0])
1085    H0 /= npcosd(H2[:-1])           #**2? I don't think so, **1 is right for powders
1086    if 'SASD' in data['type']:
1087        H0 /= npcosd(H2[:-1])           #one more for small angle scattering data?
1088    if data['Oblique'][1]:
1089        H0 /= G2pwd.Oblique(data['Oblique'][0],H2[:-1])
1090    times[4] += time.time()-t0
1091    G2fil.G2Print ('Step times: \n apply masks  %8.3fs xy->th,azm   %8.3fs fill map     %8.3fs \
1092        \n binning      %8.3fs cleanup      %8.3fs'%(times[0],times[1],times[2],times[3],times[4]))
1093    G2fil.G2Print ("Elapsed time:","%8.3fs"%(time.time()-tbeg))
1094    G2fil.G2Print ('Integration complete')
1095    if returnN:     #As requested by Steven Weigand
1096        return H0,H1,H2,NST,CancelPressed
1097    else:
1098        return H0,H1,H2,CancelPressed
1099   
1100def MakeStrStaRing(ring,Image,Controls):
1101    ellipse = GetEllipse(ring['Dset'],Controls)
1102    pixSize = Controls['pixelSize']
1103    scalex = 1000./pixSize[0]
1104    scaley = 1000./pixSize[1]
1105    Ring = np.array(makeRing(ring['Dset'],ellipse,ring['pixLimit'],ring['cutoff'],scalex,scaley,Image)[0]).T   #returns x,y,dsp for each point in ring
1106    if len(Ring):
1107        ring['ImxyObs'] = copy.copy(Ring[:2])
1108        TA = GetTthAzm(Ring[0],Ring[1],Controls)       #convert x,y to tth,azm
1109        TA[0] = Controls['wavelength']/(2.*npsind(TA[0]/2.))      #convert 2th to d
1110        ring['ImtaObs'] = TA
1111        ring['ImtaCalc'] = np.zeros_like(ring['ImtaObs'])
1112        Ring[0] = TA[0]
1113        Ring[1] = TA[1]
1114        return Ring,ring
1115    else:
1116        ring['ImxyObs'] = [[],[]]
1117        ring['ImtaObs'] = [[],[]]
1118        ring['ImtaCalc'] = [[],[]]
1119        return [],[]    #bad ring; no points found
1120   
1121def FitStrSta(Image,StrSta,Controls):
1122    'Needs a doc string'
1123   
1124    StaControls = copy.deepcopy(Controls)
1125    phi = StrSta['Sample phi']
1126    wave = Controls['wavelength']
1127    pixelSize = Controls['pixelSize']
1128    scalex = 1000./pixelSize[0]
1129    scaley = 1000./pixelSize[1]
1130    StaType = StrSta['Type']
1131    StaControls['distance'] += StrSta['Sample z']*cosd(phi)
1132
1133    for ring in StrSta['d-zero']:       #get observed x,y,d points for the d-zeros
1134        dset = ring['Dset']
1135        Ring,R = MakeStrStaRing(ring,Image,StaControls)
1136        if len(Ring):
1137            ring.update(R)
1138            p0 = ring['Emat']
1139            val,esd,covMat = FitStrain(Ring,p0,dset,wave,phi,StaType)
1140            ring['Emat'] = val
1141            ring['Esig'] = esd
1142            ellipse = FitEllipse(R['ImxyObs'].T)
1143            ringxy,ringazm = makeRing(ring['Dcalc'],ellipse,0,0.,scalex,scaley,Image)
1144            ring['ImxyCalc'] = np.array(ringxy).T[:2]
1145            ringint = np.array([float(Image[int(x*scalex),int(y*scaley)]) for y,x in np.array(ringxy)[:,:2]])
1146            ringint /= np.mean(ringint)
1147            ring['Ivar'] = np.var(ringint)
1148            ring['covMat'] = covMat
1149            G2fil.G2Print ('Variance in normalized ring intensity: %.3f'%(ring['Ivar']))
1150    CalcStrSta(StrSta,Controls)
1151   
1152def IntStrSta(Image,StrSta,Controls):
1153    StaControls = copy.deepcopy(Controls)
1154    pixelSize = Controls['pixelSize']
1155    scalex = 1000./pixelSize[0]
1156    scaley = 1000./pixelSize[1]
1157    phi = StrSta['Sample phi']
1158    StaControls['distance'] += StrSta['Sample z']*cosd(phi)
1159    RingsAI = []
1160    for ring in StrSta['d-zero']:       #get observed x,y,d points for the d-zeros
1161        Ring,R = MakeStrStaRing(ring,Image,StaControls)
1162        if len(Ring):
1163            ellipse = FitEllipse(R['ImxyObs'].T)
1164            ringxy,ringazm = makeRing(ring['Dcalc'],ellipse,0,0.,scalex,scaley,Image,5)
1165            ring['ImxyCalc'] = np.array(ringxy).T[:2]
1166            ringint = np.array([float(Image[int(x*scalex),int(y*scaley)]) for y,x in np.array(ringxy)[:,:2]])
1167            ringint /= np.mean(ringint)
1168            G2fil.G2Print (' %s %.3f %s %.3f %s %d'%('d-spacing',ring['Dcalc'],'sig(MRD):',np.sqrt(np.var(ringint)),'# points:',len(ringint)))
1169            RingsAI.append(np.array(zip(ringazm,ringint)).T)
1170    return RingsAI
1171   
1172def CalcStrSta(StrSta,Controls):
1173
1174    wave = Controls['wavelength']
1175    phi = StrSta['Sample phi']
1176    StaType = StrSta['Type']
1177    for ring in StrSta['d-zero']:
1178        Eij = ring['Emat']
1179        E = [[Eij[0],Eij[1],0],[Eij[1],Eij[2],0],[0,0,0]]
1180        th,azm = ring['ImtaObs']
1181        th0 = np.ones_like(azm)*npasind(wave/(2.*ring['Dset']))
1182        V = -np.sum(np.sum(E*calcFij(90.,phi,azm,th0).T/1.e6,axis=2),axis=1)
1183        if StaType == 'True':
1184            ring['ImtaCalc'] = np.array([np.exp(V)*ring['Dset'],azm])
1185        else:
1186            ring['ImtaCalc'] = np.array([(V+1.)*ring['Dset'],azm])
1187        dmin = np.min(ring['ImtaCalc'][0])
1188        dmax = np.max(ring['ImtaCalc'][0])
1189        if ring.get('fixDset',True):
1190            if abs(Eij[0]) < abs(Eij[2]):         #tension
1191                ring['Dcalc'] = dmin+(dmax-dmin)/4.
1192            else:                       #compression
1193                ring['Dcalc'] = dmin+3.*(dmax-dmin)/4.
1194        else:
1195            ring['Dcalc'] = np.mean(ring['ImtaCalc'][0])
1196
1197def calcFij(omg,phi,azm,th):
1198    '''    Uses parameters as defined by Bob He & Kingsley Smith, Adv. in X-Ray Anal. 41, 501 (1997)
1199
1200    :param omg: his omega = sample omega rotation; 0 when incident beam || sample surface,
1201        90 when perp. to sample surface
1202    :param phi: his phi = sample phi rotation; usually = 0, axis rotates with omg.
1203    :param azm: his chi = azimuth around incident beam
1204    :param th:  his theta = theta
1205    '''
1206    a = npsind(th)*npcosd(omg)+npsind(azm)*npcosd(th)*npsind(omg)
1207    b = -npcosd(azm)*npcosd(th)
1208    c = npsind(th)*npsind(omg)-npsind(azm)*npcosd(th)*npcosd(omg)
1209    d = a*npsind(phi)+b*npcosd(phi)
1210    e = a*npcosd(phi)-b*npsind(phi)
1211    Fij = np.array([
1212        [d**2,d*e,c*d],
1213        [d*e,e**2,c*e],
1214        [c*d,c*e,c**2]])
1215    return -Fij
1216
1217def FitStrain(rings,p0,dset,wave,phi,StaType):
1218    'Needs a doc string'
1219    def StrainPrint(ValSig,dset):
1220        print ('Strain tensor for Dset: %.6f'%(dset))
1221        ptlbls = 'names :'
1222        ptstr =  'values:'
1223        sigstr = 'esds  :'
1224        for name,fmt,value,sig in ValSig:
1225            ptlbls += "%s" % (name.rjust(12))
1226            ptstr += fmt % (value)
1227            if sig:
1228                sigstr += fmt % (sig)
1229            else:
1230                sigstr += 12*' '
1231        print (ptlbls)
1232        print (ptstr)
1233        print (sigstr)
1234       
1235    def strainCalc(p,xyd,dset,wave,phi,StaType):
1236        E = np.array([[p[0],p[1],0],[p[1],p[2],0],[0,0,0]])
1237        dspo,azm,dsp = xyd
1238        th = npasind(wave/(2.0*dspo))
1239        V = -np.sum(np.sum(E*calcFij(90.,phi,azm,th).T/1.e6,axis=2),axis=1)
1240        if StaType == 'True':
1241            dspc = dset*np.exp(V)
1242        else:
1243            dspc = dset*(V+1.)
1244        return dspo-dspc
1245       
1246    names = ['e11','e12','e22']
1247    fmt = ['%12.2f','%12.2f','%12.2f']
1248    result = leastsq(strainCalc,p0,args=(rings,dset,wave,phi,StaType),full_output=True)
1249    vals = list(result[0])
1250    chisq = np.sum(result[2]['fvec']**2)/(rings.shape[1]-3)     #reduced chi^2 = M/(Nobs-Nvar)
1251    covM = result[1]
1252    covMatrix = covM*chisq
1253    sig = list(np.sqrt(chisq*np.diag(result[1])))
1254    ValSig = zip(names,fmt,vals,sig)
1255    StrainPrint(ValSig,dset)
1256    return vals,sig,covMatrix
1257
1258def FitImageSpots(Image,ImMax,ind,pixSize,nxy,spotSize=1.0):
1259   
1260    def calcMean(nxy,pixSize,img):
1261        gdx,gdy = np.mgrid[0:nxy,0:nxy]
1262        gdx = ma.array((gdx-nxy//2)*pixSize[0]/1000.,mask=~ma.getmaskarray(ImBox))
1263        gdy = ma.array((gdy-nxy//2)*pixSize[1]/1000.,mask=~ma.getmaskarray(ImBox))
1264        posx = ma.sum(gdx)/ma.count(gdx)
1265        posy = ma.sum(gdy)/ma.count(gdy)
1266        return posx,posy
1267   
1268    def calcPeak(values,nxy,pixSize,img):
1269        back,mag,px,py,sig = values
1270        peak = np.zeros([nxy,nxy])+back
1271        nor = 1./(2.*np.pi*sig**2)
1272        gdx,gdy = np.mgrid[0:nxy,0:nxy]
1273        gdx = (gdx-nxy//2)*pixSize[0]/1000.
1274        gdy = (gdy-nxy//2)*pixSize[1]/1000.
1275        arg = (gdx-px)**2+(gdy-py)**2       
1276        peak += mag*nor*np.exp(-arg/(2.*sig**2))
1277        return ma.compressed(img-peak)/np.sqrt(ma.compressed(img))
1278   
1279    def calc2Peak(values,nxy,pixSize,img):
1280        back,mag,px,py,sigx,sigy,rho = values
1281        peak = np.zeros([nxy,nxy])+back
1282        nor = 1./(2.*np.pi*sigx*sigy*np.sqrt(1.-rho**2))
1283        gdx,gdy = np.mgrid[0:nxy,0:nxy]
1284        gdx = (gdx-nxy//2)*pixSize[0]/1000.
1285        gdy = (gdy-nxy//2)*pixSize[1]/1000.
1286        argnor = -1./(2.*(1.-rho**2))
1287        arg = (gdx-px)**2/sigx**2+(gdy-py)**2/sigy**2-2.*rho*(gdx-px)*(gdy-py)/(sigx*sigy)       
1288        peak += mag*nor*np.exp(argnor*arg)
1289        return ma.compressed(img-peak)/np.sqrt(ma.compressed(img))       
1290   
1291    nxy2 = nxy//2
1292    ImBox = Image[ind[1]-nxy2:ind[1]+nxy2+1,ind[0]-nxy2:ind[0]+nxy2+1]
1293    back = np.min(ImBox)
1294    mag = np.sum(ImBox-back)
1295    vals = [back,mag,0.,0.,0.2,0.2,0.]
1296    ImBox = ma.array(ImBox,dtype=float,mask=ImBox>0.75*ImMax)
1297    px = (ind[0]+.5)*pixSize[0]/1000.
1298    py = (ind[1]+.5)*pixSize[1]/1000.
1299    if ma.any(ma.getmaskarray(ImBox)):
1300        vals = calcMean(nxy,pixSize,ImBox)
1301        posx,posy = [px+vals[0],py+vals[1]]
1302        return [posx,posy,spotSize]
1303    else:
1304        result = leastsq(calc2Peak,vals,args=(nxy,pixSize,ImBox),full_output=True)
1305        vals = result[0]
1306        ratio = vals[4]/vals[5]
1307        if 0.5 < ratio < 2.0 and vals[2] < 2. and vals[3] < 2.:
1308            posx,posy = [px+vals[2],py+vals[3]]
1309            return [posx,posy,min(6.*vals[4],spotSize)]
1310        else:
1311            return None
1312   
1313def AutoSpotMasks(Image,Masks,Controls):
1314   
1315    G2fil.G2Print ('auto spot search')
1316    nxy = 15
1317    rollImage = lambda rho,roll: np.roll(np.roll(rho,roll[0],axis=0),roll[1],axis=1)
1318    pixelSize = Controls['pixelSize']
1319    spotMask = ma.array(Image,mask=(Image<np.mean(Image)))
1320    indices = (-1,0,1)
1321    rolls = np.array([[ix,iy] for ix in indices for iy in indices])
1322    time0 = time.time()
1323    for roll in rolls:
1324        if np.any(roll):        #avoid [0,0]
1325            spotMask = ma.array(spotMask,mask=(spotMask-rollImage(Image,roll)<0.),dtype=float)
1326    mags = spotMask[spotMask.nonzero()]
1327    indx = np.transpose(spotMask.nonzero())
1328    size1 = mags.shape[0]
1329    magind = [[indx[0][0],indx[0][1],mags[0]],]
1330    for ind,mag in list(zip(indx,mags))[1:]:        #remove duplicates
1331#            ind[0],ind[1],I,J = ImageLocalMax(Image,nxy,ind[0],ind[1])
1332        if (magind[-1][0]-ind[0])**2+(magind[-1][1]-ind[1])**2 > 16:
1333            magind.append([ind[0],ind[1],Image[ind[0],ind[1]]]) 
1334    magind = np.array(magind).T
1335    indx = np.array(magind[0:2],dtype=np.int32)
1336    mags = magind[2]
1337    size2 = mags.shape[0]
1338    G2fil.G2Print ('Initial search done: %d -->%d %.2fs'%(size1,size2,time.time()-time0))
1339    nx,ny = Image.shape
1340    ImMax = np.max(Image)
1341    peaks = []
1342    nxy2 = nxy//2
1343    mult = 0.001
1344    num = 1e6
1345    while num>500:
1346        mult += .0001           
1347        minM = mult*np.max(mags)
1348        num = ma.count(ma.array(mags,mask=mags<=minM))
1349        G2fil.G2Print('try',mult,minM,num)
1350    minM = mult*np.max(mags)
1351    G2fil.G2Print ('Find biggest spots:',mult,num,minM)
1352    for i,mag in enumerate(mags):
1353        if mag > minM:
1354            if (nxy2 < indx[0][i] < nx-nxy2-1) and (nxy2 < indx[1][i] < ny-nxy2-1):
1355#                    G2fil.G2Print ('try:%d %d %d %.2f'%(i,indx[0][i],indx[1][i],mags[i]))
1356                peak = FitImageSpots(Image,ImMax,[indx[1][i],indx[0][i]],pixelSize,nxy)
1357                if peak and not any(np.isnan(np.array(peak))):
1358                    peaks.append(peak)
1359#                    G2fil.G2Print (' Spot found: %s'%str(peak))
1360    peaks = G2mth.sortArray(G2mth.sortArray(peaks,1),0)
1361    Peaks = [peaks[0],]
1362    for peak in peaks[1:]:
1363        if GetDsp(peak[0],peak[1],Controls) >= 1.:      #toss non-diamond low angle spots
1364            continue
1365        if (peak[0]-Peaks[-1][0])**2+(peak[1]-Peaks[-1][1])**2 > peak[2]*Peaks[-1][2] :
1366            Peaks.append(peak)
1367#            G2fil.G2Print (' Spot found: %s'%str(peak))
1368    G2fil.G2Print ('Spots found: %d time %.2fs'%(len(Peaks),time.time()-time0))
1369    Masks['Points'] = Peaks
1370    return None
Note: See TracBrowser for help on using the repository browser.