def ImageCalibrate(self, data): 'Needs a doc string' import copy import ImageCalibrants as calFile print 'Image calibration:' time0 = time.time() ring = data['ring'] pixelSize = data['pixelSize'] scalex = 1000. / pixelSize[0] scaley = 1000. / pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] varyDict = data['varyList'] if varyDict['dist'] and varyDict['wave']: print 'ERROR - you can not simultaneously calibrate distance and wavelength' return False if len(ring) < 5: print 'ERROR - not enough inner ring points for ellipse' return False #fit start points on inner ring data['ellipses'] = [] data['rings'] = [] outE = FitEllipse(ring) fmt = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f' fmt2 = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f, chi**2: %.3f, Np: %d' if outE: print fmt % ('start ellipse: ', outE[0][0], outE[0][1], outE[1], outE[2][0], outE[2][1]) ellipse = outE else: return False #setup 360 points on that ring for "good" fit data['ellipses'].append(ellipse[:] + ('g', )) Ring = makeRing(1.0, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) if Ring: ellipse = FitEllipse(Ring) Ring = makeRing(1.0, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) #do again ellipse = FitEllipse(Ring) else: print '1st ring not sufficiently complete to proceed' return False if debug: print fmt2 % ('inner ring: ', ellipse[0][0], ellipse[0][1], ellipse[1], ellipse[2][0], ellipse[2][1], 0., len(Ring) ) #cent,phi,radii data['ellipses'].append(ellipse[:] + ('r', )) data['rings'].append(np.array(Ring)) G2plt.PlotImage(self, newImage=True) #setup for calibration data['rings'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] #generate reflection set Bravais, SGs, Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais, sg, cell in zip(Bravais, SGs, Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin, SGData, A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin, bravais, A) HKL += hkl HKL = G2lat.sortHKLd(HKL, True, False)[skip:] #set up 1st ring elcent, phi, radii = ellipse #from fit of 1st ring dsp = HKL[0][3] print '1st ring: try %.4f' % (dsp) if varyDict['dist']: wave = data['wavelength'] tth = 2.0 * asind(wave / (2. * dsp)) else: #varyDict['wave']! dist = data['distance'] tth = npatan2d(radii[0], dist) data['wavelength'] = wave = 2.0 * dsp * sind(tth / 2.0) Ring0 = makeRing(dsp, ellipse, 3, cutoff, scalex, scaley, self.ImageZ) ttth = nptand(tth) stth = npsind(tth) ctth = npcosd(tth) #1st estimate of tilt; assume ellipse - don't know sign though if varyDict['tilt']: tilt = npasind(np.sqrt(max(0., 1. - (radii[0] / radii[1])**2)) * ctth) if not tilt: print 'WARNING - selected ring was fitted as a circle' print ' - if detector was tilted we suggest you skip this ring - WARNING' else: tilt = data['tilt'] #1st estimate of dist: sample to detector normal to plane if varyDict['dist']: data['distance'] = dist = radii[0]**2 / (ttth * radii[1]) else: dist = data['distance'] if varyDict['tilt']: #ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt zdisp = radii[1] * ttth * tand(tilt) zdism = radii[1] * ttth * tand(-tilt) #cone axis position; 2 choices. Which is right? #NB: zdisp is || to major axis & phi is rotation of minor axis #thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)] centp = [elcent[0] + zdisp * sind(phi), elcent[1] - zdisp * cosd(phi)] centm = [elcent[0] + zdism * sind(phi), elcent[1] - zdism * cosd(phi)] #check get same ellipse parms either way #now do next ring; estimate either way & do a FitDetector each way; best fit is correct one fail = True i2 = 1 while fail: dsp = HKL[i2][3] print '2nd ring: try %.4f' % (dsp) tth = 2.0 * asind(wave / (2. * dsp)) ellipsep = GetEllipse2(tth, 0., dist, centp, tilt, phi) print fmt % ('plus ellipse :', ellipsep[0][0], ellipsep[0][1], ellipsep[1], ellipsep[2][0], ellipsep[2][1]) Ringp = makeRing(dsp, ellipsep, 3, cutoff, scalex, scaley, self.ImageZ) parmDict = { 'dist': dist, 'det-X': centp[0], 'det-Y': centp[1], 'tilt': tilt, 'phi': phi, 'wave': wave, 'dep': 0.0 } varyList = [item for item in varyDict if varyDict[item]] if len(Ringp) > 10: chip = FitDetector(np.array(Ring0 + Ringp), varyList, parmDict, True) tiltp = parmDict['tilt'] phip = parmDict['phi'] centp = [parmDict['det-X'], parmDict['det-Y']] fail = False else: chip = 1e6 ellipsem = GetEllipse2(tth, 0., dist, centm, -tilt, phi) print fmt % ('minus ellipse:', ellipsem[0][0], ellipsem[0][1], ellipsem[1], ellipsem[2][0], ellipsem[2][1]) Ringm = makeRing(dsp, ellipsem, 3, cutoff, scalex, scaley, self.ImageZ) if len(Ringm) > 10: parmDict['tilt'] *= -1 chim = FitDetector(np.array(Ring0 + Ringm), varyList, parmDict, True) tiltm = parmDict['tilt'] phim = parmDict['phi'] centm = [parmDict['det-X'], parmDict['det-Y']] fail = False else: chim = 1e6 if fail: i2 += 1 if chip < chim: data['tilt'] = tiltp data['center'] = centp data['rotation'] = phip else: data['tilt'] = tiltm data['center'] = centm data['rotation'] = phim data['ellipses'].append(ellipsep[:] + ('b', )) data['rings'].append(np.array(Ringp)) data['ellipses'].append(ellipsem[:] + ('r', )) data['rings'].append(np.array(Ringm)) G2plt.PlotImage(self, newImage=True) parmDict = { 'dist': data['distance'], 'det-X': data['center'][0], 'det-Y': data['center'][1], 'tilt': data['tilt'], 'phi': data['rotation'], 'wave': data['wavelength'], 'dep': data['DetDepth'] } varyList = [item for item in varyDict if varyDict[item]] data['rings'] = [] data['ellipses'] = [] for i, H in enumerate(HKL): dsp = H[3] tth = 2.0 * asind(wave / (2. * dsp)) if tth + abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break if debug: print 'HKLD:', H[:4], '2-theta: %.4f' % (tth) elcent, phi, radii = ellipse = GetEllipse(dsp, data) data['ellipses'].append(copy.deepcopy(ellipse + ('g', ))) if debug: print fmt % ('predicted ellipse:', elcent[0], elcent[1], phi, radii[0], radii[1]) Ring = makeRing(dsp, ellipse, pixLimit, cutoff, scalex, scaley, self.ImageZ) if Ring: data['rings'].append(np.array(Ring)) rings = np.concatenate((data['rings']), axis=0) if i: chisq = FitDetector(rings, varyList, parmDict, False) data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq elcent, phi, radii = ellipse = GetEllipse(dsp, data) if debug: print fmt2 % ('fitted ellipse: ', elcent[0], elcent[1], phi, radii[0], radii[1], chisq, len(rings)) data['ellipses'].append(copy.deepcopy(ellipse + ('r', ))) # G2plt.PlotImage(self,newImage=True) else: if debug: print 'insufficient number of points in this ellipse to fit' # break G2plt.PlotImage(self, newImage=True) fullSize = len(self.ImageZ) / scalex if 2 * radii[1] < .9 * fullSize: print 'Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib' N = len(data['ellipses']) if N > 2: FitDetector(rings, varyList, parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] for H in HKL[:N]: ellipse = GetEllipse(H[3], data) data['ellipses'].append(copy.deepcopy(ellipse + ('b', ))) print 'calibration time = ', time.time() - time0 G2plt.PlotImage(self, newImage=True) return True
def ImageCalibrate(self,data): 'Needs a doc string' import copy import ImageCalibrants as calFile print 'Image calibration:' time0 = time.time() ring = data['ring'] pixelSize = data['pixelSize'] scalex = 1000./pixelSize[0] scaley = 1000./pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] varyDict = data['varyList'] if varyDict['dist'] and varyDict['wave']: print 'ERROR - you can not simultaneously calibrate distance and wavelength' return False if len(ring) < 5: print 'ERROR - not enough inner ring points for ellipse' return False #fit start points on inner ring data['ellipses'] = [] data['rings'] = [] outE = FitEllipse(ring) fmt = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f' fmt2 = '%s X: %.3f, Y: %.3f, phi: %.3f, R1: %.3f, R2: %.3f, chi**2: %.3f, Np: %d' if outE: print fmt%('start ellipse: ',outE[0][0],outE[0][1],outE[1],outE[2][0],outE[2][1]) ellipse = outE else: return False #setup 360 points on that ring for "good" fit data['ellipses'].append(ellipse[:]+('g',)) Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) if Ring: ellipse = FitEllipse(Ring) Ring = makeRing(1.0,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) #do again ellipse = FitEllipse(Ring) else: print '1st ring not sufficiently complete to proceed' return False if debug: print fmt2%('inner ring: ',ellipse[0][0],ellipse[0][1],ellipse[1], ellipse[2][0],ellipse[2][1],0.,len(Ring)) #cent,phi,radii data['ellipses'].append(ellipse[:]+('r',)) data['rings'].append(np.array(Ring)) G2plt.PlotImage(self,newImage=True) #setup for calibration data['rings'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] #generate reflection set Bravais,SGs,Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais,sg,cell in zip(Bravais,SGs,Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin,SGData,A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin,bravais,A) HKL += hkl HKL = G2lat.sortHKLd(HKL,True,False)[skip:] #set up 1st ring elcent,phi,radii = ellipse #from fit of 1st ring dsp = HKL[0][3] print '1st ring: try %.4f'%(dsp) if varyDict['dist']: wave = data['wavelength'] tth = 2.0*asind(wave/(2.*dsp)) else: #varyDict['wave']! dist = data['distance'] tth = npatan2d(radii[0],dist) data['wavelength'] = wave = 2.0*dsp*sind(tth/2.0) Ring0 = makeRing(dsp,ellipse,3,cutoff,scalex,scaley,self.ImageZ) ttth = nptand(tth) stth = npsind(tth) ctth = npcosd(tth) #1st estimate of tilt; assume ellipse - don't know sign though if varyDict['tilt']: tilt = npasind(np.sqrt(max(0.,1.-(radii[0]/radii[1])**2))*ctth) if not tilt: print 'WARNING - selected ring was fitted as a circle' print ' - if detector was tilted we suggest you skip this ring - WARNING' else: tilt = data['tilt'] #1st estimate of dist: sample to detector normal to plane if varyDict['dist']: data['distance'] = dist = radii[0]**2/(ttth*radii[1]) else: dist = data['distance'] if varyDict['tilt']: #ellipse to cone axis (x-ray beam); 2 choices depending on sign of tilt zdisp = radii[1]*ttth*tand(tilt) zdism = radii[1]*ttth*tand(-tilt) #cone axis position; 2 choices. Which is right? #NB: zdisp is || to major axis & phi is rotation of minor axis #thus shift from beam to ellipse center is [Z*sin(phi),-Z*cos(phi)] centp = [elcent[0]+zdisp*sind(phi),elcent[1]-zdisp*cosd(phi)] centm = [elcent[0]+zdism*sind(phi),elcent[1]-zdism*cosd(phi)] #check get same ellipse parms either way #now do next ring; estimate either way & do a FitDetector each way; best fit is correct one fail = True i2 = 1 while fail: dsp = HKL[i2][3] print '2nd ring: try %.4f'%(dsp) tth = 2.0*asind(wave/(2.*dsp)) ellipsep = GetEllipse2(tth,0.,dist,centp,tilt,phi) print fmt%('plus ellipse :',ellipsep[0][0],ellipsep[0][1],ellipsep[1],ellipsep[2][0],ellipsep[2][1]) Ringp = makeRing(dsp,ellipsep,3,cutoff,scalex,scaley,self.ImageZ) parmDict = {'dist':dist,'det-X':centp[0],'det-Y':centp[1], 'tilt':tilt,'phi':phi,'wave':wave,'dep':0.0} varyList = [item for item in varyDict if varyDict[item]] if len(Ringp) > 10: chip = FitDetector(np.array(Ring0+Ringp),varyList,parmDict,True) tiltp = parmDict['tilt'] phip = parmDict['phi'] centp = [parmDict['det-X'],parmDict['det-Y']] fail = False else: chip = 1e6 ellipsem = GetEllipse2(tth,0.,dist,centm,-tilt,phi) print fmt%('minus ellipse:',ellipsem[0][0],ellipsem[0][1],ellipsem[1],ellipsem[2][0],ellipsem[2][1]) Ringm = makeRing(dsp,ellipsem,3,cutoff,scalex,scaley,self.ImageZ) if len(Ringm) > 10: parmDict['tilt'] *= -1 chim = FitDetector(np.array(Ring0+Ringm),varyList,parmDict,True) tiltm = parmDict['tilt'] phim = parmDict['phi'] centm = [parmDict['det-X'],parmDict['det-Y']] fail = False else: chim = 1e6 if fail: i2 += 1 if chip < chim: data['tilt'] = tiltp data['center'] = centp data['rotation'] = phip else: data['tilt'] = tiltm data['center'] = centm data['rotation'] = phim data['ellipses'].append(ellipsep[:]+('b',)) data['rings'].append(np.array(Ringp)) data['ellipses'].append(ellipsem[:]+('r',)) data['rings'].append(np.array(Ringm)) G2plt.PlotImage(self,newImage=True) parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1], 'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']} varyList = [item for item in varyDict if varyDict[item]] data['rings'] = [] data['ellipses'] = [] for i,H in enumerate(HKL): dsp = H[3] tth = 2.0*asind(wave/(2.*dsp)) if tth+abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break if debug: print 'HKLD:',H[:4],'2-theta: %.4f'%(tth) elcent,phi,radii = ellipse = GetEllipse(dsp,data) data['ellipses'].append(copy.deepcopy(ellipse+('g',))) if debug: print fmt%('predicted ellipse:',elcent[0],elcent[1],phi,radii[0],radii[1]) Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,self.ImageZ) if Ring: data['rings'].append(np.array(Ring)) rings = np.concatenate((data['rings']),axis=0) if i: chisq = FitDetector(rings,varyList,parmDict,False) data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq elcent,phi,radii = ellipse = GetEllipse(dsp,data) if debug: print fmt2%('fitted ellipse: ',elcent[0],elcent[1],phi,radii[0],radii[1],chisq,len(rings)) data['ellipses'].append(copy.deepcopy(ellipse+('r',))) # G2plt.PlotImage(self,newImage=True) else: if debug: print 'insufficient number of points in this ellipse to fit' # break G2plt.PlotImage(self,newImage=True) fullSize = len(self.ImageZ)/scalex if 2*radii[1] < .9*fullSize: print 'Are all usable rings (>25% visible) used? Try reducing Min ring I/Ib' N = len(data['ellipses']) if N > 2: FitDetector(rings,varyList,parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = parmDict['phi'] data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] for H in HKL[:N]: ellipse = GetEllipse(H[3],data) data['ellipses'].append(copy.deepcopy(ellipse+('b',))) print 'calibration time = ',time.time()-time0 G2plt.PlotImage(self,newImage=True) return True
def ImageRecalibrate(self, data, masks): 'Needs a doc string' import ImageCalibrants as calFile print 'Image recalibration:' time0 = time.time() pixelSize = data['pixelSize'] scalex = 1000. / pixelSize[0] scaley = 1000. / pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] data['rings'] = [] data['ellipses'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] Bravais, SGs, Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais, sg, cell in zip(Bravais, SGs, Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin, SGData, A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin, bravais, A) HKL += hkl HKL = G2lat.sortHKLd(HKL, True, False) varyList = [item for item in data['varyList'] if data['varyList'][item]] parmDict = { 'dist': data['distance'], 'det-X': data['center'][0], 'det-Y': data['center'][1], 'tilt': data['tilt'], 'phi': data['rotation'], 'wave': data['wavelength'], 'dep': data['DetDepth'] } Found = False wave = data['wavelength'] frame = masks['Frames'] tam = ma.make_mask_none(self.ImageZ.shape) if frame: tam = ma.mask_or(tam, MakeFrameMask(data, frame)) for iH, H in enumerate(HKL): if debug: print H dsp = H[3] tth = 2.0 * asind(wave / (2. * dsp)) if tth + abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break ellipse = GetEllipse(dsp, data) Ring = makeRing(dsp, ellipse, pixLimit, cutoff, scalex, scaley, ma.array(self.ImageZ, mask=tam)) if Ring: if iH >= skip: data['rings'].append(np.array(Ring)) data['ellipses'].append(copy.deepcopy(ellipse + ('r', ))) Found = True elif not Found: #skipping inner rings, keep looking until ring found continue else: #no more rings beyond edge of detector data['ellipses'].append([]) continue # break rings = np.concatenate((data['rings']), axis=0) chisq = FitDetector(rings, varyList, parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'], parmDict['det-Y']] data['rotation'] = np.mod(parmDict['phi'], 360.0) data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq N = len(data['ellipses']) data['ellipses'] = [] #clear away individual ellipse fits for H in HKL[:N]: ellipse = GetEllipse(H[3], data) data['ellipses'].append(copy.deepcopy(ellipse + ('b', ))) print 'calibration time = ', time.time() - time0 G2plt.PlotImage(self, newImage=True) return True
def ImageRecalibrate(self,data,masks): 'Needs a doc string' import ImageCalibrants as calFile print 'Image recalibration:' time0 = time.time() pixelSize = data['pixelSize'] scalex = 1000./pixelSize[0] scaley = 1000./pixelSize[1] pixLimit = data['pixLimit'] cutoff = data['cutoff'] data['rings'] = [] data['ellipses'] = [] if not data['calibrant']: print 'no calibration material selected' return True skip = data['calibskip'] dmin = data['calibdmin'] Bravais,SGs,Cells = calFile.Calibrants[data['calibrant']][:3] HKL = [] for bravais,sg,cell in zip(Bravais,SGs,Cells): A = G2lat.cell2A(cell) if sg: SGData = G2spc.SpcGroup(sg)[1] hkl = G2pwd.getHKLpeak(dmin,SGData,A) HKL += hkl else: hkl = G2lat.GenHBravais(dmin,bravais,A) HKL += hkl HKL = G2lat.sortHKLd(HKL,True,False) varyList = [item for item in data['varyList'] if data['varyList'][item]] parmDict = {'dist':data['distance'],'det-X':data['center'][0],'det-Y':data['center'][1], 'tilt':data['tilt'],'phi':data['rotation'],'wave':data['wavelength'],'dep':data['DetDepth']} Found = False wave = data['wavelength'] frame = masks['Frames'] tam = ma.make_mask_none(self.ImageZ.shape) if frame: tam = ma.mask_or(tam,MakeFrameMask(data,frame)) for iH,H in enumerate(HKL): if debug: print H dsp = H[3] tth = 2.0*asind(wave/(2.*dsp)) if tth+abs(data['tilt']) > 90.: print 'next line is a hyperbola - search stopped' break ellipse = GetEllipse(dsp,data) Ring = makeRing(dsp,ellipse,pixLimit,cutoff,scalex,scaley,ma.array(self.ImageZ,mask=tam)) if Ring: if iH >= skip: data['rings'].append(np.array(Ring)) data['ellipses'].append(copy.deepcopy(ellipse+('r',))) Found = True elif not Found: #skipping inner rings, keep looking until ring found continue else: #no more rings beyond edge of detector data['ellipses'].append([]) continue # break rings = np.concatenate((data['rings']),axis=0) chisq = FitDetector(rings,varyList,parmDict) data['wavelength'] = parmDict['wave'] data['distance'] = parmDict['dist'] data['center'] = [parmDict['det-X'],parmDict['det-Y']] data['rotation'] = np.mod(parmDict['phi'],360.0) data['tilt'] = parmDict['tilt'] data['DetDepth'] = parmDict['dep'] data['chisq'] = chisq N = len(data['ellipses']) data['ellipses'] = [] #clear away individual ellipse fits for H in HKL[:N]: ellipse = GetEllipse(H[3],data) data['ellipses'].append(copy.deepcopy(ellipse+('b',))) print 'calibration time = ',time.time()-time0 G2plt.PlotImage(self,newImage=True) return True