if verbosity>9: info=[]; info.append({'desc': 'DEBUG: rebinned image', 'yperchan':ybin, 'xperchan': dq, 'xunits':'1/A', 'xlabel':'q', 'xoffset':q[0]}); info.append({'desc': 'DEBUG: rebinned image (reversed q-axis)', 'yperchan':ybin, 'xperchan': -dq, 'xunits':'1/A', 'xlabel':'-q', 'xoffset':q[-1]}); fig = WQStackBrowser([ret,ret],info,aspect='auto'); plt.show(); # 5. save undistorted w-q map # 5.1 save as TIFF (readable by DM3, ImageJ) tiffile = '%s-rebinned%dx%d.tif'%(filename.split('/')[-1].split('.tif')[0], ret.shape[0],ret.shape[1]); print 'write to file %s'%tiffile; tiff.imsave(tiffile,ret.astype(np.float32)); # 5.2 pickle array and history for extract_spectrum pklfile = tiffile.replace('.tif','.pkl'); print 'write to file %s'%pklfile; FILE=open(pklfile,'w'); data={'yqmap':ret, 'qaxis':q, 'qdisp':qcal, 'rawfile':filename,'descr':descr, 'history':history}; pickle.Pickler(FILE).dump(data); FILE.close();
# spectrum spectra[s, Ny] = E[s] # energy offset # DEBUG if s == 0: plt.imshow(image, vmin=0, vmax=1000) plt.figure() plt.plot(xline, label='projected Tiff') plt.plot(gauss(range(Nx), *param), label='Gauss fit') window = np.zeros(Nx) window[xmin:xmax] = xline.max() plt.plot(window, label='selected region') plt.legend() plt.show(block=False) # test if crop region has been too small if x0.max() - x0.min() > dx.min(): print 'WARNING: atomatic crop region might be too small' # save spectra to TIFF print "SAVE EELS spectra to file '%s'" % outfile tiff.imsave(outfile, spectra.astype(np.float32)) # plot all spectra plt.figure() for i, s in enumerate(spectra): plt.plot(s / float(s[0:Ny].max()) - i * 0.1, 'k', linewidth=1) plt.show()
# crop image and extract EELS xmin = max(x0[s]-dx[s],0); xmax = min(x0[s]+dx[s], Ny); spectra[s,0:Ny] = np.sum(image[:,xmin:xmax],axis=1); # spectrum spectra[s,Ny] = E[s]; # energy offset # DEBUG if s==0: plt.imshow(image,vmin=0,vmax=1000); plt.figure() plt.plot(xline,label='projected Tiff'); plt.plot(gauss(range(Nx),*param),label='Gauss fit'); window=np.zeros(Nx); window[xmin:xmax]=xline.max(); plt.plot(window,label='selected region'); plt.legend(); plt.show(block=False); # test if crop region has been too small if x0.max()-x0.min() > dx.min(): print 'WARNING: atomatic crop region might be too small' # save spectra to TIFF print "SAVE EELS spectra to file '%s'"%outfile; tiff.imsave(outfile,spectra.astype(np.float32)); # plot all spectra plt.figure() for i,s in enumerate(spectra): plt.plot(s/float(s[0:Ny].max()) -i*0.1,'k',linewidth=1); plt.show()
EQmap.append(spectrum); EQmap=np.asarray(EQmap).T; # first index E, second q ehistory+="\nRebinning for Emin=%8.5f, Emax=%8.5f, dE=%8.5f"%(E.min(), E.max(),dE); # DEBUG: plot calibrated Eqmap if verbosity>9: info = {'desc': 'DEBUG: rebinned WQmap', 'xperchan':dE, 'ylabel':'E', 'yunits':'eV', 'yperchan':dqx,'ylabel':'q', 'yunits':'1/A', 'yoffset' :qaxis[0]}; WQB = WQBrowser(EQmap.T,info,aspect='auto'); plt.show(); # 5. save E-q map as readable tif outfile = '%s-calibrated.tif'%(filename.split('/')[-1].split('.pkl')[0]); print 'write to file %s'%outfile; tiff.imsave(outfile,EQmap.astype(np.float32)); # 6. save energy-loss function for iq, q in enumerate(qaxis): if qmin > q or q > qmax: continue E0_offset,_ = e2y.inverse(y0_fit(iq),y0_fit(iq)); # keV, beam energy offset # calculate aperture correction function for given q (rectangular aperture) if apply_APC==True: aperture = TEM_wqslit(q*conv.bohr,dqx*conv.bohr,dqy*conv.bohr,E0); APC = [aperture.get_APC(_E) for _E in E]; else: APC = 1; elf = EQmap[:,iq]/APC # write file containing energy-loss function: # required parameters param = { 'title' : title + ', ELF',
'desc': 'DEBUG: rebinned WQmap', 'xperchan': dE, 'xlabel': 'E', 'xunits': 'eV', 'yperchan': dqx, 'ylabel': 'q', 'yunits': '1/A', 'yoffset': qaxis[0] } WQB = WQBrowser(EQmap.T, info, aspect='auto') plt.show() # 5. save E-q map as readable tif outfile = '%s-calibrated.tif' % (filename.split('/')[-1].split('.pkl')[0]) print 'write to file %s' % outfile tiff.imsave(outfile, EQmap.astype(np.float32)) # 6. save energy-loss function for iq, q in enumerate(qaxis): if qmin > q or q > qmax: continue # calibrate offset in energy axis (so far E=0eV at x=0px) E_ZLP, _ = e2y.inverse(y0_fit(q), y0_fit(q)) Ecorr = E + E0_offset - E_ZLP # calculate aperture correction function for given q (rectangular aperture) # note: APC for negative energies is well defined (energy gain) if apply_APC == True: aperture = TEM_wqslit(q * conv.bohr, dqx * conv.bohr, dqy * conv.bohr, E0) APC = [aperture.get_APC(_E) for _E in Ecorr] else:
import glob; import numpy as np import scipy.signal as sig from TEMareels.tools.img_filter import binning from TEMareels.tools import tvips import TEMareels.tools.tifffile as tiff # define infile name and binning along y-direction (E-axis) pattern = "/path/to/q_calibration/img%d.tif"; files = [pattern%i for i in range(344,345)] ybin = 64; for filename in files: image = tvips.load_TVIPS(filename) # img[iy,ix] #image = tiff.imread(filename).astype(np.float64); if ~np.allclose(image.shape, (4096,4096)): raise IOError("Unexpected image size in file '%s'"%filename); medimg= sig.medfilt2d(image,kernel_size=3); # filtering binimg= binning(medimg,ybin); # binning along y #binimg = binning(binimg.T,32).T; # binning along x outfile = filename.split(".tif")[0]+"_filt_bin%d.tif"%ybin; tiff.imsave(outfile,binimg.astype(np.float32));
""" # use TEMareels package specified in _set_pkgdir (overrides PYTHONPATH) import _set_pkgdir import glob import numpy as np import scipy.signal as sig from TEMareels.tools.img_filter import binning from TEMareels.tools import tvips import TEMareels.tools.tifffile as tiff # define infile name and binning along y-direction (E-axis) pattern = "/path/to/q_calibration/img%d.tif" files = [pattern % i for i in range(344, 345)] ybin = 64 for filename in files: image = tvips.load_TVIPS(filename) # img[iy,ix] #image = tiff.imread(filename).astype(np.float64); if ~np.allclose(image.shape, (4096, 4096)): raise IOError("Unexpected image size in file '%s'" % filename) medimg = sig.medfilt2d(image, kernel_size=3) # filtering binimg = binning(medimg, ybin) # binning along y #binimg = binning(binimg.T,32).T; # binning along x outfile = filename.split(".tif")[0] + "_filt_bin%d.tif" % ybin tiff.imsave(outfile, binimg.astype(np.float32))
import glob; import numpy as np import scipy.signal as sig from TEMareels.tools.img_filter import binning from TEMareels.tools import tvips import TEMareels.tools.tifffile as tiff # define infile name and binning along y-direction (E-axis) pattern = "/path/to/q_calibration/img%d.tif"; files = [pattern%i for i in range(344,345)] ybin = 64; for filename in files: image = tvips.load_TVIPS(filename) # img[iy,ix] #image = tiff.imread(filename).astype(np.float64); if ~np.allclose(image.shape, (4096,4096)): raise IOError("Unexpected image size in file '%s'"%filename); medimg= sig.medfilt2d(image,kernel_size=3); # filtering binimg= binning(medimg,ybin); # binning along y #binimg = binning(binimg.T,32).T; # binning along x outfile = filename.split(".tif")[0]+"_filt_bin%d.tif"%ybin; tiff.imsave(outfile,image.astype(np.float32));