def textureFitsFile(inFilename, pF, wavelength, cake=True, doPlot=False): runNum = inFilename.split('_')[-1].split('.')[0] print 'Reading ' + inFilename with open(inFilename, mode='rb') as fileobj: rawData = np.fromfile(fileobj, np.float32, num_X * num_Y).astype('float32') z = rawData.reshape((num_X, num_Y)) dMax = 3.1 dMin = 1.5 x1, y1 = pF[0], pF[1] if cake: xd, yd = pF[0] - np.arange(2048), pF[1] - np.arange(2048) xv, yv = np.meshgrid(xd, yd) psi = np.degrees( np.arctan2( -np.sin(pF[3]) * xv + np.cos(pF[2]) * yv, np.cos(pF[3]) * (np.cos(pF[2]) * xv + np.sin(pF[2]) * yv))) #arcWid = np.abs(rhoStart - rhoEnd) / rhoN / 2. arcWid = rhoStep / 2. #for angle in np.linspace(rhoStart, rhoEnd, rhoN): for angle in np.arange(rhoStart, rhoEnd, rhoStep): x2, y2 = x1 + arcRad * np.cos(np.radians(angle)), y1 + arcRad * np.sin( np.radians(angle)) if cake: cond1 = (psi > angle - arcWid) & (psi < angle + arcWid) cakeMat = np.where(cond1) x, y = xv[cakeMat] + pF[0], yv[cakeMat] + pF[1] else: x, y = np.linspace(x1, x2, num), np.linspace(y1, y2, num) dSpac = guessDSpacing(pF, x, y, wavelength) kKeys = np.where((dMin < dSpac) & (dSpac < dMax) & (0 < x) & (x < num_X) & (0 < y) & (y < num_Y)) x = x[kKeys] y = y[kKeys] dSpac = dSpac[kKeys] if len(dSpac) is 0: print 'No measurement for angle of %d.' % angle pass else: datOut = outDir + 'dat/' + runNum + '_' + str( np.round(angle)) + '.dat' if cake: binedges = np.linspace(np.min(dSpac), np.max(dSpac), 256) dg = np.digitize(dSpac, binedges) ind = np.argsort(dg) splits = np.where(np.diff(dg[ind]))[0] sliceInt = np.split(z[cakeMat][kKeys][ind], splits)[1:] zi = np.array([np.maximum(x.mean(), 1) for x in sliceInt]) dSpac = np.array(binedges[:-1]) else: zi = textResample(z, x, y) if np.abs(dSpac.size - zi.size) > 0: print 'Choose a wider rho angle!' tempOut = np.vstack((dSpac, zi)) np.savetxt(datOut, tempOut.T)
def textureFitsFile(inFilename, pF, wavelength, cake=True, doPlot=False): runNum = inFilename.split('_')[-1].split('.')[0] print 'Reading ' + inFilename with open(inFilename, mode='rb') as fileobj: rawData = np.fromfile(fileobj, np.float32, num_X * num_Y).astype('float32') z = rawData.reshape((num_X, num_Y)) dMax = 3.1 dMin = 1.5 x1, y1 = pF[0], pF[1] if cake: xd, yd = pF[0] - np.arange(2048), pF[1] - np.arange(2048) xv, yv = np.meshgrid(xd, yd) psi = np.degrees(np.arctan2(-np.sin(pF[3])*xv + np.cos(pF[2]) * yv, np.cos(pF[3]) * (np.cos(pF[2]) * xv + np.sin(pF[2]) * yv))) #arcWid = np.abs(rhoStart - rhoEnd) / rhoN / 2. arcWid = rhoStep / 2. #for angle in np.linspace(rhoStart, rhoEnd, rhoN): for angle in np.arange(rhoStart, rhoEnd, rhoStep): x2, y2 = x1 + arcRad * np.cos(np.radians(angle)), y1 + arcRad * np.sin(np.radians(angle)) if cake: cond1 = (psi > angle - arcWid) & (psi < angle + arcWid) cakeMat = np.where(cond1) x, y = xv[cakeMat]+pF[0], yv[cakeMat]+pF[1] else: x, y = np.linspace(x1, x2, num), np.linspace(y1, y2, num) dSpac = guessDSpacing(pF, x, y, wavelength) kKeys = np.where((dMin < dSpac) & (dSpac < dMax) & (0 < x) & (x < num_X) & (0 < y) & (y < num_Y)) x = x[kKeys] y = y[kKeys] dSpac=dSpac[kKeys] if len(dSpac) is 0: print 'No measurement for angle of %d.' % angle pass else: datOut = outDir + 'dat/' + runNum + '_' + str(np.round(angle)) + '.dat' if cake: binedges = np.linspace(np.min(dSpac),np.max(dSpac),256) dg = np.digitize(dSpac,binedges) ind = np.argsort(dg) splits = np.where(np.diff(dg[ind]))[0] sliceInt = np.split(z[cakeMat][kKeys][ind],splits)[1:] zi= np.array([np.maximum(x.mean(),1) for x in sliceInt]) dSpac = np.array(binedges[:-1]) else: zi = textResample(z,x,y) if np.abs(dSpac.size - zi.size) > 0: print 'Choose a wider rho angle!' tempOut = np.vstack((dSpac, zi)) np.savetxt(datOut, tempOut.T)
def textureFitsFile(inFilename, pF, wavelength, cake=True, doPlot=False): # ptList = [] xLi = [] yLi = [] areaLi = [] angleList = [] cLi = [] pkLi = [] runNum = inFilename.split('_')[-1].split('.')[0] print 'Reading ' + inFilename with open(inFilename, mode='rb') as fileobj: rawData = np.fromfile(fileobj, np.float32, num_X * num_Y).astype('float32') z = rawData.reshape((num_X, num_Y)) #peakCens = getPeakCens('hcp',[3.23433391, 5.163978], 2) peakCens = np.array([2.78126, 2.4591, 2.585,1.90, 1.60544, 1.2384]) #peakHKL = getHKL('hcp',2) peakHKL = [(1,0,0),(1,0,1),(0,0,2),(1,0,2),(1,1,0),(2,0,2)] peakWids = np.repeat(0.01,len(peakCens)) dMax = np.max(peakCens) + 0.5 x1, y1 = pF[0], pF[1] if doPlot: fig, ax = plt.subplots(1, 1) if cake: xd, yd = pF[0] - np.arange(2048), pF[1] - np.arange(2048) xv, yv = np.meshgrid(xd, yd) psi = np.degrees(np.arctan2(-np.sin(pF[3])*xv + np.cos(pF[2]) * yv, np.cos(pF[3]) * (np.cos(pF[2]) * xv + np.sin(pF[2]) * yv))) arcWid = np.abs(rhoStart - rhoEnd) / rhoN / 2. for angle in np.linspace(rhoStart, rhoEnd, rhoN): x2, y2 = x1 + arcRad * np.cos(np.radians(angle)), y1 + arcRad * np.sin(np.radians(angle)) if cake: cond1 = (psi > angle - arcWid) & (psi < angle + arcWid) cakeMat = np.where(cond1) x, y = xv[cakeMat]+pF[0], yv[cakeMat]+pF[1] else: x, y = np.linspace(x1, x2, num), np.linspace(y1, y2, num) dSpac = guessDSpacing(pF, x, y, wavelength) kKeys = np.where((dSpac < dMax) & (0 < x) & (x < num_X) & (0 < y) & (y < num_Y)) x = x[kKeys] y = y[kKeys] dSpac=dSpac[kKeys] if len(dSpac) is 0: print 'No measurement for angle of %d.' % angle pass else: if cake: binedges = np.linspace(np.min(dSpac),np.max(dSpac),1024) dg = np.digitize(dSpac,binedges) ind = np.argsort(dg) splits = np.where(np.diff(dg[ind]))[0] sliceInt = np.split(z[cakeMat][kKeys][ind],splits)[1:] zi= np.array([x.mean() for x in sliceInt]) dSpac = np.array(binedges[1:]) else: zi = textResample(z,x,y) if doPlot: plt.cla() ax.plot(dSpac[:np.size(zi)],zi,'k.-') ''' BGmm1=np.argmin(np.abs(dSpac-(2.17+0.01*7))) BGmm2=np.argmin(np.abs(dSpac-(2.17-0.01*7))) BGmmRange = range(np.max((np.min((BGmm1,BGmm2)),0)),np.min((np.max((BGmm1,BGmm2)),len(zi)))) BGArea = np.sum(zi[BGmmRange]) ''' for c in [xx for xx in peakCens if xx > np.min(dSpac)]: # mmX = np.argmin(np.abs(dSpac-c)) # print c mm1=np.argmin(np.abs(dSpac-(c+peakWids[peakCens==c][0]*10))) mm2=np.argmin(np.abs(dSpac-(c-peakWids[peakCens==c][0]*10))) mmRange = np.array(range(np.max((np.min((mm1,mm2)),0)),np.min((np.max((mm1,mm2)),len(zi))))) # if np.any(zi[mmRange] <= 0) or np.all(c > (dSpac[mmRange])): if False: pass else: ''' p0 = [np.max(zi[mmRange]) - np.min(zi[mmRange]),dSpac[mmX],peakWids[peakCens==c],np.min(zi[mmRange])] p0 = [np.max(zi[mmRange]) - np.min(zi[mmRange]),dSpac[mm2+np.argmax(zi[mmRange])],peakWids[peakCens==c],np.min(zi[mmRange])] try: coeff, _ = curve_fit(gauss, dSpac[mmRange], zi[mmRange], p0,xtol=1e-2) except: coeff = np.array([0, c, 1e-3, 0]) ''' coeff = np.array([np.sum(zi[mmRange])/np.min(zi[mmRange]),c,1e-3,0]) coeff[0] = np.abs(coeff[0]) coeff[2] = np.abs(coeff[2]) mRan = zi[mmRange].argsort()[:5] bx = np.hstack(([mmRange[0],mmRange[-1]], mmRange[mRan])) by = np.hstack(([zi[mmRange[0]],zi[mmRange[-1]]],zi[mmRange][mRan])) bkfit = np.polyfit(bx,by,0) newArea = np.sum(zi[mmRange]) - np.sum(np.polyval(bkfit,mmRange)) p0 = [np.max(zi[mmRange]) - np.min(zi[mmRange]),dSpac[mm2+np.argmax(zi[mmRange])],peakWids[peakCens==c],np.min(zi[mmRange])] try: coeff, pcov = curve_fit(fakePseudoVoigt, dSpac[mmRange], zi[mmRange]-np.polyval(bkfit,mmRange), p0,xtol=1e-2,method='lm') perr = np.sqrt(np.diag(pcov)) print 'FIRST LINE' print pcov print '' print coeff print perr print '' except: coeff = np.array([0, c, 1e-3, 0]) if doPlot: resca = np.linspace(dSpac[mmRange[0]],dSpac[mmRange[-1]],500) resca2 = np.linspace(mmRange[0],mmRange[-1],500) # pk_fit = gauss(resca, *coeff) # ax.plot(dSpac[mmRange],zi[mmRange],'.') # ax.axvline(dSpac[mmRange[0]]) # ax.axvline(dSpac[mmRange[-1]]) ax.plot(dSpac[bx],by,'ro') ax.plot(resca,fakePseudoVoigt(resca,*coeff)+np.polyval(bkfit,resca2),'-',lw=2) if coeff[2] > 0.1: print coeff # ax.plot(resca,gauss(resca,*p0),'k--') pass else: zi[mmRange] = zi[mmRange] - fakePseudoVoigt(dSpac[mmRange],*coeff) # if doPlot: ax.plot(resca,pk_fit,'-') # areaLi = np.append(areaLi, coeff[0] * coeff[2] * areaScale) areaLi = np.append(areaLi, newArea) if not cake: xLi = np.append(xLi, np.interp(coeff[1],range(np.size(kKeys)),x)) yLi = np.append(yLi, np.interp(coeff[1],range(np.size(kKeys)),y)) angleList = np.append(angleList, angle) cLi = np.append(cLi, coeff[1]) pkLi= np.append(pkLi,np.where(peakCens==c)[0][0]) if (c == 2.585): pass else: np.place(peakCens,peakCens == c,coeff[1]) # np.place(peakWids,peakCens == c,coeff[2]) if doPlot: ax.set_ylim(1,1e3) ax.set_xlim(1.2,3) ax.set_yscale('log') # print '%.2f' % angle ax.plot(dSpac[:np.size(zi)],zi,'r.') #plt.show() figOut = outDir + 'fig/' + runNum + '_' + str(np.round(angle)) + '.png' print figOut plt.savefig(figOut) theta = 90 - 0 * cLi #- np.degrees(np.arcsin(wavelength / (2 * cLi))/2) try: os.makedirs(outDir + runNum) except: pass outputName = inFilename.split('/')[-1].split('.')[0] if cake: outputName += 'cake' for K, n in zip(peakHKL,range(len(peakHKL))): nKeys = np.where(pkLi == n) if len(nKeys[0]) > 5: #print '**\n%d%d%d has ' % K + str(len(nKeys[0])) + ' fits.' outFile = outDir + runNum + '/' + outputName + '.%d%d%d' % K #print 'Output to ' + outFile M = np.vstack((theta[nKeys], angleList[nKeys], areaLi[nKeys])) np.savetxt(outFile,M.T,fmt='%.6f',delimiter=' ') print 'Files output to ' + outFile
def textureFitsFile(inFilename, pF, wavelength, cake=True, doPlot=False): dMax = 3.1 dMin = 1.5 x1, y1 = pF[0], pF[1] c = [2.80, 2.575, 2.46, 1.89, 1.616] pkInd = ['100', '002', '101', '102', '110'] cMult = [1, 1, 1, 1, 1] pkWid = [0.07, 0.035, 0.04, 0.04, 0.035] sliceCount = 512 runNum = inFilename.split('_')[-1].split('.')[0] with open(inFilename, mode='rb') as fileobj: rawData = np.fromfile(fileobj, np.float32, num_X * num_Y).astype('float32') z = rawData.reshape((num_X, num_Y)) if doPlot: fig, ax = plt.subplots(1, 1) for mC, de in zip(c, pkWid): ax.axvline(mC + de, color='k', ls='dotted') ax.axvline(mC - de, color='k', ls='dotted') if cake: xd, yd = pF[0] - np.arange(2048), pF[1] - np.arange(2048) xv, yv = np.meshgrid(xd, yd) psi = np.degrees( np.arctan2( -np.sin(pF[3]) * xv + np.cos(pF[2]) * yv, np.cos(pF[3]) * (np.cos(pF[2]) * xv + np.sin(pF[2]) * yv))) arcWid = rhoStep datBuff = np.zeros((np.arange(rhoStart, rhoEnd, rhoStep).size, 6)) for angle, k in zip(np.arange(rhoStart, rhoEnd, rhoStep), count(0)): x2, y2 = x1 + arcRad * np.cos(np.radians(angle)), y1 + arcRad * np.sin( np.radians(angle)) if cake: cond1 = (psi > angle) & (psi < angle + arcWid) cakeMat = np.where(cond1) x, y = xv[cakeMat] + pF[0], yv[cakeMat] + pF[1] else: x, y = np.linspace(x1, x2, num), np.linspace(y1, y2, num) dSpac = guessDSpacing(pF, x, y, wavelength) kKeys = np.where((dMin < dSpac) & (dSpac < dMax) & (0 < x) & (x < num_X) & (0 < y) & (y < num_Y)) x = x[kKeys] y = y[kKeys] dSpac = dSpac[kKeys] zSlice = z[cakeMat][kKeys] if len(dSpac) is 0: print 'No measurement for angle of %d.' % angle pass else: if cake: sliceStyle = True if sliceStyle: # Split the d-spacing space into sliceCount equally spaced bins and take the mean. # The number of pixels per bin will vary. binedges = np.linspace(np.min(dSpac), np.max(dSpac), sliceCount) dg = np.digitize(dSpac, binedges) zi = np.zeros(binedges.size) for ii in np.intersect1d(dg, np.arange(sliceCount)): ind = np.where(dg == ii) zi[ii] = np.sum(zSlice[ind]) # Fill empty slices with the average of adjacent slices for ii in np.setxor1d(dg, np.arange(sliceCount)): if ii < np.size(zi): zi[ii] = np.mean([zi[ii - 1], zi[ii + 1]]) dSpac = np.array(binedges) else: # Split d-Spacing array into sliceCount equal pixel number segments and take the mean # The number of pixels in each bin is equal. # NB: Throws out last N - sliceCount * k data points ind = np.argsort(dSpac) ll = dSpac.size / sliceCount zi = np.mean(np.reshape( zSlice[ind][:ll * sliceCount], (sliceCount, -1), ), axis=1) dSpac = np.mean(np.reshape( dSpac[ind][:ll * sliceCount], (sliceCount, -1), ), axis=1) else: zi = textResample(z, x, y) if doPlot: ax.plot(dSpac, zi, '.') bgInd = np.where((2.0 - 0.05 < dSpac) & (2.0 + 0.05 > dSpac)) backG = np.trapz(zi[bgInd], dSpac[bgInd]) cbuff = np.zeros(5) for mC, i in zip(c, count(0)): thisPeak = np.where((mC - pkWid[i] < dSpac) & (mC + pkWid[i] > dSpac)) xx = dSpac[thisPeak] yy = zi[thisPeak] boundsX = np.concatenate((xx[:2], xx[-2:])) boundsY = np.concatenate((yy[:2], yy[-2:])) p = np.polyfit(boundsX, boundsY, 2) yy = yy - np.polyval(p, xx) if doPlot: ax.plot(xx, yy, '-') thisArea = np.max(np.trapz(yy, xx), 0) #thisArea = np.mean(zi[thisPeak]) / backG / cMult[i] cbuff[i] = thisArea datBuff[k, 0] = angle + arcWid / 2.0 datBuff[k, 1:] = cbuff outRoot = outDir + 'dat_fits/' + runNum try: os.makedirs(outRoot) except: pass for thisInd, i in zip(pkInd, count(0)): datOut = outRoot + '/' + runNum + '.' + thisInd thisDat = np.zeros((datBuff[:, 0].size, 3)) thisDat[:, 0] = 90 thisDat[:, 1] = datBuff[:, 0] thisDat[:, 2] = datBuff[:, i + 1] np.savetxt(datOut, thisDat, fmt='%.1f %.1f %.5f') if doPlot: try: os.makedirs(outDir + 'fig_fits') except: pass ax.set_xlim(dMin, dMax) ax.set_ylim(0, 100000) figOut = outDir + 'fig_fits/' + runNum + '.png' plt.savefig(figOut) plt.close()
def textureFitsFile(inFilename, pF, wavelength, cake=True, doPlot=False): dMax = 3.1 dMin = 1.5 x1, y1 = pF[0], pF[1] c = [2.80, 2.575, 2.46, 1.89, 1.616] pkInd = ['100', '002', '101', '102', '110'] cMult = [1, 1, 1, 1, 1] pkWid = [0.07, 0.035, 0.04, 0.04, 0.035] sliceCount = 512 runNum = inFilename.split('_')[-1].split('.')[0] with open(inFilename, mode='rb') as fileobj: rawData = np.fromfile(fileobj, np.float32, num_X * num_Y).astype('float32') z = rawData.reshape((num_X, num_Y)) if doPlot: fig, ax = plt.subplots(1, 1) for mC, de in zip(c,pkWid): ax.axvline(mC + de, color='k', ls='dotted') ax.axvline(mC - de, color='k', ls='dotted') if cake: xd, yd = pF[0] - np.arange(2048), pF[1] - np.arange(2048) xv, yv = np.meshgrid(xd, yd) psi = np.degrees( np.arctan2( -np.sin(pF[3]) * xv + np.cos(pF[2]) * yv, np.cos(pF[3]) * (np.cos(pF[2]) * xv + np.sin(pF[2]) * yv) ) ) arcWid = rhoStep datBuff = np.zeros((np.arange(rhoStart, rhoEnd, rhoStep).size, 6)) for angle, k in zip(np.arange(rhoStart, rhoEnd, rhoStep), count(0)): x2, y2 = x1 + arcRad * np.cos(np.radians(angle)), y1 + arcRad * np.sin(np.radians(angle)) if cake: cond1 = (psi > angle) & (psi < angle + arcWid) cakeMat = np.where(cond1) x, y = xv[cakeMat] + pF[0], yv[cakeMat] + pF[1] else: x, y = np.linspace(x1, x2, num), np.linspace(y1, y2, num) dSpac = guessDSpacing(pF, x, y, wavelength) kKeys = np.where((dMin < dSpac) & (dSpac < dMax) & (0 < x) & (x < num_X) & (0 < y) & (y < num_Y)) x = x[kKeys] y = y[kKeys] dSpac = dSpac[kKeys] zSlice = z[cakeMat][kKeys] if len(dSpac) is 0: print 'No measurement for angle of %d.' % angle pass else: if cake: sliceStyle = True if sliceStyle: # Split the d-spacing space into sliceCount equally spaced bins and take the mean. # The number of pixels per bin will vary. binedges = np.linspace(np.min(dSpac), np.max(dSpac), sliceCount) dg = np.digitize(dSpac, binedges) zi = np.zeros(binedges.size) for ii in np.intersect1d(dg, np.arange(sliceCount)): ind = np.where(dg == ii) zi[ii] = np.sum(zSlice[ind]) # Fill empty slices with the average of adjacent slices for ii in np.setxor1d(dg, np.arange(sliceCount)): if ii < np.size(zi): zi[ii] = np.mean([zi[ii - 1], zi[ii + 1]]) dSpac = np.array(binedges) else: # Split d-Spacing array into sliceCount equal pixel number segments and take the mean # The number of pixels in each bin is equal. # NB: Throws out last N - sliceCount * k data points ind = np.argsort(dSpac) ll = dSpac.size / sliceCount zi = np.mean(np.reshape(zSlice[ind][:ll * sliceCount], (sliceCount, -1), ), axis=1) dSpac = np.mean(np.reshape(dSpac[ind][:ll * sliceCount], (sliceCount, -1), ), axis=1) else: zi = textResample(z, x, y) if doPlot: ax.plot(dSpac, zi, '.') bgInd = np.where((2.0 - 0.05 < dSpac) & (2.0 + 0.05 > dSpac)) backG = np.trapz(zi[bgInd],dSpac[bgInd]) cbuff = np.zeros(5) for mC, i in zip(c, count(0)): thisPeak = np.where((mC - pkWid[i] < dSpac) & (mC + pkWid[i] > dSpac)) xx = dSpac[thisPeak] yy = zi[thisPeak] boundsX = np.concatenate((xx[:2],xx[-2:])) boundsY = np.concatenate((yy[:2],yy[-2:])) p = np.polyfit(boundsX, boundsY,2) yy = yy - np.polyval(p,xx) if doPlot: ax.plot(xx, yy, '-') thisArea = np.max(np.trapz(yy,xx),0) #thisArea = np.mean(zi[thisPeak]) / backG / cMult[i] cbuff[i] = thisArea datBuff[k, 0] = angle + arcWid / 2.0 datBuff[k, 1:] = cbuff outRoot = outDir + 'dat_fits/' + runNum try: os.makedirs(outRoot) except: pass for thisInd, i in zip(pkInd, count(0)): datOut = outRoot + '/' + runNum + '.' + thisInd thisDat = np.zeros((datBuff[:, 0].size, 3)) thisDat[:, 0] = 90 thisDat[:, 1] = datBuff[:, 0] thisDat[:, 2] = datBuff[:, i + 1] np.savetxt(datOut, thisDat, fmt='%.1f %.1f %.5f') if doPlot: try: os.makedirs(outDir + 'fig_fits') except: pass ax.set_xlim(dMin,dMax) ax.set_ylim(0, 100000) figOut = outDir + 'fig_fits/' + runNum + '.png' plt.savefig(figOut) plt.close()