Changeset 1653


Ignore:
Timestamp:
Feb 13, 2015 9:25:22 AM (7 years ago)
Author:
vondreele
Message:

allow all image controls to be varied in calibration and recalibration
stop debug printing in image calibration/recalibration

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIimage.py

    r1644 r1653  
    4949npatand = lambda x: 180.*np.arctan(x)/np.pi
    5050npatan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi
     51   
     52debug = False
    5153   
    5254def pointInPolygon(pXY,xy):
     
    139141        ptstr =  'values:'
    140142        sigstr = 'esds  :'
    141         for name,fmt,value,sig in ValSig:
     143        for name,value,sig in ValSig:
    142144            ptlbls += "%s" % (name.rjust(12))
    143145            if name == 'phi':
    144                 ptstr += fmt % (value%360.)
     146                ptstr += Fmt[name] % (value%360.)
    145147            else:
    146                 ptstr += fmt % (value)
     148                ptstr += Fmt[name] % (value)
    147149            if sig:
    148                 sigstr += fmt % (sig)
     150                sigstr += Fmt[name] % (sig)
    149151            else:
    150152                sigstr += 12*' '
     
    193195       
    194196    names = ['dist','det-X','det-Y','tilt','phi','dep','wave']
    195     fmt = ['%12.3f','%12.3f','%12.3f','%12.3f','%12.3f','%12.3f','%12.5f']
     197    fmt = ['%12.3f','%12.3f','%12.3f','%12.3f','%12.3f','%12.2f','%12.6f']
     198    Fmt = dict(zip(names,fmt))
    196199    p0 = [parmDict[key] for key in varyList]
    197200    result = leastsq(ellipseCalcD,p0,args=(rings.T,varyList,parmDict),full_output=True,ftol=1.e-8)
     
    203206    for i,name in enumerate(varyList):
    204207        sigList[i] = sig[varyList.index(name)]
    205     ValSig = zip(varyList,fmt,vals,sig)
     208    ValSig = zip(varyList,vals,sig)
    206209    if Print:
    207210        CalibPrint(ValSig,chisq,rings.shape[0])
     
    526529        tam = ma.mask_or(tam,MakeFrameMask(data,frame))
    527530    for iH,H in enumerate(HKL):
    528         print H
     531        if debug:   print H
    529532        dsp = H[3]
    530533        tth = 2.0*asind(wave/(2.*dsp))
     
    547550    rings = np.concatenate((data['rings']),axis=0)
    548551    chisq = FitDetector(rings,varyList,parmDict)
     552    data['wavelength'] = parmDict['wave']
    549553    data['distance'] = parmDict['dist']
    550554    data['center'] = [parmDict['det-X'],parmDict['det-Y']]
     
    557561    for H in HKL[:N]:
    558562        ellipse = GetEllipse(H[3],data)
    559         data['ellipses'].append(copy.deepcopy(ellipse+('b',)))
    560    
     563        data['ellipses'].append(copy.deepcopy(ellipse+('b',)))   
    561564    print 'calibration time = ',time.time()-time0
    562565    G2plt.PlotImage(self,newImage=True)       
     
    575578    pixLimit = data['pixLimit']
    576579    cutoff = data['cutoff']
     580    varyDict = data['varyList']
     581    if varyDict['dist'] and varyDict['wave']:
     582        print 'ERROR - you can not simultaneously calibrate distance and wavelength'
     583        return False
    577584    if len(ring) < 5:
    578         print 'not enough inner ring points for ellipse'
     585        print 'ERROR - not enough inner ring points for ellipse'
    579586        return False
    580587       
     
    601608        print '1st ring not sufficiently complete to proceed'
    602609        return False
    603     print fmt2%('inner ring:    ',ellipse[0][0],ellipse[0][1],ellipse[1],ellipse[2][0],ellipse[2][1],0.,len(Ring))     #cent,phi,radii
     610    if debug:
     611        print fmt2%('inner ring:    ',ellipse[0][0],ellipse[0][1],ellipse[1],
     612            ellipse[2][0],ellipse[2][1],0.,len(Ring))     #cent,phi,radii
    604613    data['ellipses'].append(ellipse[:]+('r',))
    605614    data['rings'].append(np.array(Ring))
     
    638647    ctth = npcosd(tth)
    639648#1st estimate of tilt; assume ellipse - don't know sign though
    640     tilt = npasind(np.sqrt(max(0.,1.-(radii[0]/radii[1])**2))*ctth)
    641     if not tilt:
    642         print 'WARNING - selected ring was fitted as a circle'
    643         print ' - if detector was tilted we suggest you skip this ring - WARNING'
     649    if varyDict['tilt']:
     650        tilt = npasind(np.sqrt(max(0.,1.-(radii[0]/radii[1])**2))*ctth)
     651        if not tilt:
     652            print 'WARNING - selected ring was fitted as a circle'
     653            print ' - if detector was tilted we suggest you skip this ring - WARNING'
     654    else:
     655        tilt = data['tilt']
    644656#1st estimate of dist: sample to detector normal to plane
    645     data['distance'] = dist = radii[0]**2/(ttth*radii[1])
     657    if varyDict['dist']:
     658        data['distance'] = dist = radii[0]**2/(ttth*radii[1])
     659    else:
     660        dist = data['distance']
     661    if varyDict['tilt']:
    646662#ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt
    647     zdisp = radii[1]*ttth*tand(tilt)
    648     zdism = radii[1]*ttth*tand(-tilt)
     663        zdisp = radii[1]*ttth*tand(tilt)
     664        zdism = radii[1]*ttth*tand(-tilt)
    649665#cone axis position; 2 choices. Which is right?     
    650666#NB: zdisp is || to major axis & phi is rotation of minor axis
    651667#thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)]
    652     centp = [elcent[0]+zdisp*sind(phi),elcent[1]-zdisp*cosd(phi)]
    653     centm = [elcent[0]+zdism*sind(phi),elcent[1]-zdism*cosd(phi)]
     668        centp = [elcent[0]+zdisp*sind(phi),elcent[1]-zdisp*cosd(phi)]
     669        centm = [elcent[0]+zdism*sind(phi),elcent[1]-zdism*cosd(phi)]
    654670#check get same ellipse parms either way
    655671#now do next ring; estimate either way & do a FitDetector each way; best fit is correct one
    656     fail = True
    657     i2 = 1
    658     while fail:
    659         dsp = HKL[i2][3]
    660         print '2nd ring: try %.4f'%(dsp)
    661         tth = 2.0*asind(wave/(2.*dsp))
    662         ellipsep = GetEllipse2(tth,0.,dist,centp,tilt,phi)
    663         print fmt%('plus ellipse :',ellipsep[0][0],ellipsep[0][1],ellipsep[1],ellipsep[2][0],ellipsep[2][1])
    664         Ringp = makeRing(dsp,ellipsep,3,cutoff,scalex,scaley,self.ImageZ)
    665         parmDict = {'dist':dist,'det-X':centp[0],'det-Y':centp[1],
    666             'tilt':tilt,'phi':phi,'wave':wave,'dep':0.0}       
    667         varyList = [item for item in data['varyList'] if data['varyList'][item]]
    668         if len(Ringp) > 10:
    669             chip = FitDetector(np.array(Ring0+Ringp),varyList,parmDict,True)
    670             tiltp = parmDict['tilt']
    671             phip = parmDict['phi']
    672             centp = [parmDict['det-X'],parmDict['det-Y']]
    673             fail = False
     672        fail = True
     673        i2 = 1
     674        while fail:
     675            dsp = HKL[i2][3]
     676            print '2nd ring: try %.4f'%(dsp)
     677            tth = 2.0*asind(wave/(2.*dsp))
     678            ellipsep = GetEllipse2(tth,0.,dist,centp,tilt,phi)
     679            print fmt%('plus ellipse :',ellipsep[0][0],ellipsep[0][1],ellipsep[1],ellipsep[2][0],ellipsep[2][1])
     680            Ringp = makeRing(dsp,ellipsep,3,cutoff,scalex,scaley,self.ImageZ)
     681            parmDict = {'dist':dist,'det-X':centp[0],'det-Y':centp[1],
     682                'tilt':tilt,'phi':phi,'wave':wave,'dep':0.0}       
     683            varyList = [item for item in varyDict if varyDict[item]]
     684            if len(Ringp) > 10:
     685                chip = FitDetector(np.array(Ring0+Ringp),varyList,parmDict,True)
     686                tiltp = parmDict['tilt']
     687                phip = parmDict['phi']
     688                centp = [parmDict['det-X'],parmDict['det-Y']]
     689                fail = False
     690            else:
     691                chip = 1e6
     692            ellipsem = GetEllipse2(tth,0.,dist,centm,-tilt,phi)
     693            print fmt%('minus ellipse:',ellipsem[0][0],ellipsem[0][1],ellipsem[1],ellipsem[2][0],ellipsem[2][1])
     694            Ringm = makeRing(dsp,ellipsem,3,cutoff,scalex,scaley,self.ImageZ)
     695            if len(Ringm) > 10:
     696                parmDict['tilt'] *= -1
     697                chim = FitDetector(np.array(Ring0+Ringm),varyList,parmDict,True)
     698                tiltm = parmDict['tilt']
     699                phim = parmDict['phi']
     700                centm = [parmDict['det-X'],parmDict['det-Y']]
     701                fail = False
     702            else:
     703                chim = 1e6
     704            if fail:
     705                i2 += 1
     706        if chip < chim:
     707            data['tilt'] = tiltp
     708            data['center'] = centp
     709            data['rotation'] = phip
    674710        else:
    675             chip = 1e6
    676         ellipsem = GetEllipse2(tth,0.,dist,centm,-tilt,phi)
    677         print fmt%('minus ellipse:',ellipsem[0][0],ellipsem[0][1],ellipsem[1],ellipsem[2][0],ellipsem[2][1])
    678         Ringm = makeRing(dsp,ellipsem,3,cutoff,scalex,scaley,self.ImageZ)
    679         if len(Ringm) > 10:
    680             parmDict['tilt'] *= -1
    681             chim = FitDetector(np.array(Ring0+Ringm),varyList,parmDict,True)
    682             tiltm = parmDict['tilt']
    683             phim = parmDict['phi']
    684             centm = [parmDict['det-X'],parmDict['det-Y']]
    685             fail = False
    686         else:
    687             chim = 1e6
    688         if fail:
    689             i2 += 1
    690     if chip < chim:
    691         data['tilt'] = tiltp
    692         data['center'] = centp
    693         data['rotation'] = phip
    694     else:
    695         data['tilt'] = tiltm
    696         data['center'] = centm
    697         data['rotation'] = phim
    698     data['ellipses'].append(ellipsep[:]+('b',))
    699     data['rings'].append(np.array(Ringp))
    700     data['ellipses'].append(ellipsem[:]+('r',))
    701     data['rings'].append(np.array(Ringm))
    702     G2plt.PlotImage(self,newImage=True)
     711            data['tilt'] = tiltm
     712            data['center'] = centm
     713            data['rotation'] = phim
     714        data['ellipses'].append(ellipsep[:]+('b',))
     715        data['rings'].append(np.array(Ringp))
     716        data['ellipses'].append(ellipsem[:]+('r',))
     717        data['rings'].append(np.array(Ringm))
     718        G2plt.PlotImage(self,newImage=True)
    703719    parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1],
    704720        'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']}
    705     varyList = [item for item in data['varyList'] if data['varyList'][item]]
     721    varyList = [item for item in varyDict if varyDict[item]]
    706722    data['rings'] = []
    707723    data['ellipses'] = []
     
    712728            print 'next line is a hyperbola - search stopped'
    713729            break
    714         print 'HKLD:',H[:4],'2-theta: %.4f'%(tth)
     730        if debug:   print 'HKLD:',H[:4],'2-theta: %.4f'%(tth)
    715731        elcent,phi,radii = ellipse = GetEllipse(dsp,data)
    716732        data['ellipses'].append(copy.deepcopy(ellipse+('g',)))
    717         print fmt%('predicted ellipse:',elcent[0],elcent[1],phi,radii[0],radii[1])
     733        if debug:   print fmt%('predicted ellipse:',elcent[0],elcent[1],phi,radii[0],radii[1])
    718734        Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ)
    719735        if Ring:
     
    729745                data['chisq'] = chisq
    730746                elcent,phi,radii = ellipse = GetEllipse(dsp,data)
    731                 print fmt2%('fitted ellipse:   ',elcent[0],elcent[1],phi,radii[0],radii[1],chisq,len(rings))
     747                if debug:   print fmt2%('fitted ellipse:   ',elcent[0],elcent[1],phi,radii[0],radii[1],chisq,len(rings))
    732748            data['ellipses'].append(copy.deepcopy(ellipse+('r',)))
    733749#            G2plt.PlotImage(self,newImage=True)
    734750        else:
    735             print 'insufficient number of points in this ellipse to fit'
     751            if debug:   print 'insufficient number of points in this ellipse to fit'
    736752#            break
    737753    G2plt.PlotImage(self,newImage=True)
     
    742758    if N > 2:
    743759        FitDetector(rings,varyList,parmDict)
     760        data['wavelength'] = parmDict['wave']
    744761        data['distance'] = parmDict['dist']
    745762        data['center'] = [parmDict['det-X'],parmDict['det-Y']]
  • trunk/GSASIIimgGUI.py

    r1644 r1653  
    5656        Obj.SetValue('%.3f'%(data['pixelSize'][id]))
    5757        G2plt.PlotExposedImage(G2frame,newPlot=True,event=event)
    58        
    59        
    6058       
    6159    if G2frame.dataDisplay:
     
    470468    def CalibCoeffSizer():
    471469       
    472         def OnWavelength(event):
    473             try:
    474                 wave = float(waveSel.GetValue())
    475                 if wave < .01:
     470        def OnCalRef(event):
     471            Obj = event.GetEventObject()
     472            name = Indx[Obj]
     473            data['varyList'][name] = Obj.GetValue()
     474           
     475        def OnCalVal(event):
     476            Obj = event.GetEventObject()
     477            name = Indx[Obj]
     478            try:
     479                value = float(Obj.GetValue())
     480                if name == 'wave' and value < 0.01:
    476481                    raise ValueError
    477                 data['wavelength'] = wave
    478             except ValueError:
    479                 pass
    480             waveSel.SetValue("%7.5f" % (data['wavelength']))          #reset in case of error
    481            
    482         def OnDetDepthRef(event):
    483             data['varyList']['dep'] = penSel.GetValue()
    484            
    485         def OnDetDepth(event):
    486             try:
    487                 data['DetDepth'] = float(penVal.GetValue())
    488             except ValueError:
    489                 pass
    490             penVal.SetValue("%6.3f" % (data['DetDepth']))          #reset in case of error                     
     482            except ValueError:
     483                value = Parms[name][2]
     484            if name == 'dist':
     485                data['distance'] = value
     486            elif name == 'det-X':
     487                data['center'][0] = value
     488            elif name == 'det-Y':
     489                data['center'][1] = value
     490            elif name == 'tilt':
     491                data['tilt'] = value
     492            elif name == 'phi':
     493                data['rotation'] = value
     494            elif name == 'wave':
     495                data['wavelength'] = value
     496            elif name == 'dep':
     497                data['DetDepth'] = value                               
     498            Parms[name][2] = value
     499            Obj.SetValue(Parms[name][1]%(value))
    491500           
    492501        calibSizer = wx.FlexGridSizer(0,2,5,5)
     
    494503        calibSizer.Add((5,0),0)
    495504        cent = data['center']
    496         for axis,cnt in zip(['X','Y'],cent):
    497             calibSizer.Add(wx.StaticText(parent=G2frame.dataDisplay,label=' Beam center '+axis),0,WACV)
    498             centText = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%7.2f" % (cnt)),style=wx.TE_READONLY)
    499             centText.SetBackgroundColour(VERY_LIGHT_GREY)
    500             calibSizer.Add(centText,0,WACV)       
    501         calibSizer.Add(wx.StaticText(parent=G2frame.dataDisplay,label=' Wavelength'),0,WACV)
    502         waveSel = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%7.5f" % (data['wavelength'])),
    503             style=wx.TE_PROCESS_ENTER)
    504         waveSel.Bind(wx.EVT_TEXT_ENTER,OnWavelength)
    505         waveSel.Bind(wx.EVT_KILL_FOCUS,OnWavelength)
    506         calibSizer.Add(waveSel,0,WACV)             
    507         calibSizer.Add(wx.StaticText(parent=G2frame.dataDisplay,label=' Distance'),0,WACV)
    508 #        distVary = wx.CheckBox(G2frame.dataDisplay,label=' Distance')
    509         distSel = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%8.2f"%(data['distance'])),style=wx.TE_READONLY)
    510         distSel.SetBackgroundColour(VERY_LIGHT_GREY)
    511         calibSizer.Add(distSel,0,WACV)
    512         calibSizer.Add(wx.StaticText(parent=G2frame.dataDisplay,label=' Tilt angle'),0,WACV)
    513         tiltSel = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%9.3f"%(data['tilt'])),style=wx.TE_READONLY)
    514         tiltSel.SetBackgroundColour(VERY_LIGHT_GREY)
    515         calibSizer.Add(tiltSel,0,WACV)
    516         calibSizer.Add(wx.StaticText(parent=G2frame.dataDisplay,label=' Tilt rotation'),0,WACV)
    517         rotSel = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%9.3f"%(data['rotation'])),style=wx.TE_READONLY)
    518         rotSel.SetBackgroundColour(VERY_LIGHT_GREY)
    519         calibSizer.Add(rotSel,0,WACV)
     505        Names = ['det-X','det-Y','wave','dist','tilt','phi']
    520506        if 'PWDR' in data['type']:
    521             penSel = wx.CheckBox(parent=G2frame.dataDisplay,label='Penetration?')
    522             calibSizer.Add(penSel,0,WACV)
    523             penSel.Bind(wx.EVT_CHECKBOX, OnDetDepthRef)
    524             penSel.SetValue(data['varyList']['dep'])
    525             penVal = wx.TextCtrl(parent=G2frame.dataDisplay,value=("%6.5f" % (data['DetDepth'])),
    526                 style=wx.TE_PROCESS_ENTER)
    527             penVal.Bind(wx.EVT_TEXT_ENTER,OnDetDepth)
    528             penVal.Bind(wx.EVT_KILL_FOCUS,OnDetDepth)
    529             calibSizer.Add(penVal,0,WACV)             
    530        
     507            Names.append('dep')
     508        Parms = {'dist':['Distance','%.3f',data['distance']],'det-X':['Beam center X','%.3f',data['center'][0]],
     509            'det-Y':['Beam center X','%.3f',data['center'][1]],'tilt':['Tilt angle','%.3f',data['tilt']],
     510            'phi':['Tilt rotation','%.2f',data['rotation']],'dep':['Penetration','%.2f',data['DetDepth']],
     511            'wave':['Wavelength','%.6f',data['wavelength']]}
     512        Indx = {}
     513        for name in Names:
     514            calSel = wx.CheckBox(parent=G2frame.dataDisplay,label=Parms[name][0])
     515            calibSizer.Add(calSel,0,WACV)
     516            calSel.Bind(wx.EVT_CHECKBOX, OnCalRef)
     517            calSel.SetValue(data['varyList'][name])
     518            Indx[calSel] = name
     519            calVal = wx.TextCtrl(G2frame.dataDisplay,value=(Parms[name][1]%(Parms[name][2])),style=wx.TE_PROCESS_ENTER)
     520            calVal.Bind(wx.EVT_TEXT_ENTER,OnCalVal)
     521            calVal.Bind(wx.EVT_KILL_FOCUS,OnCalVal)
     522            Indx[calVal] = name
     523            calibSizer.Add(calVal,0,WACV)
    531524        return calibSizer
    532525   
Note: See TracChangeset for help on using the changeset viewer.