diff --git a/alignment.py b/alignment.py new file mode 120000 index 0000000..bb969dc --- /dev/null +++ b/alignment.py @@ -0,0 +1 @@ +dispersiveXanes_alignment.py \ No newline at end of file diff --git a/dispersiveXanes_utils.py b/dispersiveXanes_utils.py index e2ace07..10467d7 100644 --- a/dispersiveXanes_utils.py +++ b/dispersiveXanes_utils.py @@ -68,77 +68,3 @@ def medianRatio(p1,p2,threshold=0.03): return np.ma.average(ratio,axis=0,weights=p1) -# /--------------------\ -# | | -# | PLOTS & CO. | -# | | -# \--------------------/ - -def plotShot(im1,im2,transf1=None,transf2=None,fig=None,ax=None,res=None,E=defaultE,save=None): - if transf1 is not None: im1 = transf1.transformImage(im1) - if transf2 is not None: im2 = transf2.transformImage(im2) - if fig is None and ax is None: - fig = plt.subplots(2,3,figsize=[7,5],sharex=True)[0] - ax = fig.axes - elif fig is not None: - ax = fig.axes - if E is None: E=np.arange(im1.shape[1]) - n = im1.shape[0] - ax[0].imshow(im1,extent=(E[0],E[-1],0,n),**kw_2dplot) - ax[1].imshow(im2,extent=(E[0],E[-1],0,n),**kw_2dplot) - ax[2].imshow(im1-im2,extent=(E[0],E[-1],0,n),**kw_2dplot) - if res is None: - p1 = np.nansum(im1,axis=0) - p2 = np.nansum(im2,axis=0) - pr = p2/p1 - else: - p1 = res.p1; p2 = res.p2; pr = res.ratio - ax[3].plot(E,p1,lw=3) - ax[4].plot(E,p1,lw=1) - ax[4].plot(E,p2,lw=3) - idx = (p1>p1.max()/10.) - ax[5].plot(E[idx],pr[idx]) - if res is not None: - ax[5].set_title("FOM: %.2f"%res.fom) - else: - ax[5].set_title("FOM: %.2f"% calcFOM(p1,p2,pr)) - if (save is not None) and (save is not False): plt.savefig(save,transparent=True,dpi=500) - return fig - -def plotRatios(r,shot='random',fig=None,E=defaultE,save=None): - if fig is None: fig = plt.subplots(2,1,sharex=True)[0] - ax = fig.axes - n = r.shape[0] - i = ax[0].imshow(r,extent=(E[0],E[-1],0,n),**kw_2dplot) - i.set_clim(0,1.2) - if shot == 'random' : shot = np.random.random_integers(0,n-1) - ax[1].plot(E,r[shot],label="Shot n %d"%shot) - ax[1].plot(E,np.nanmedian(r[:10],axis=0),label="median 10 shots") - ax[1].plot(E,np.nanmedian(r,axis=0),label="all shots") - ax[1].legend() - ax[1].set_ylim(0,1.5) - ax[1].set_xlabel("Energy") - ax[1].set_ylabel("Transmission") - ax[0].set_ylabel("Shot num") - if (save is not None) and (save is not False): plt.savefig(save,transparent=True,dpi=500) - -def plotSingleShots(r,nShots=10,fig=None,E=defaultE,save=None,ErangeForStd=(7090,7150)): - if fig is None: fig = plt.subplots(2,1,sharex=True)[0] - ax = fig.axes - for i in range(nShots): - ax[0].plot(E,r[i]+i) - ax[0].set_ylim(0,nShots+0.5) - av = (1,3,10,30,100) - good = np.nanmedian(r,0) - for i,a in enumerate(av): - m = np.nanmedian(r[:a],0) - idx = (E>ErangeForStd[0]) & (E0: + idx = p1_sum>np.percentile(p1_sum,filterShot*100) + p1 = p1[idx] + p2 = p2[idx] + abs = abs[idx] p1_av = np.nanmean(p1,axis=0) p2_av = np.nanmean(p2,axis=0) + # somehow nanmedian screws up when array is too big ... so using nanmean abs_av = np.nanmean(abs,axis=0) p1 = p1[shots]; p2=p2[shots]; abs = abs[shots] if smoothWidth > 0: abs = smoothSpectra(E,abs,res=smoothWidth) @@ -204,17 +218,26 @@ def showAbs(run=82,shots=slice(5),normalization="auto",shifty=1,xlim=(7080,7180) color = gradual_colors[ishot%len(gradual_colors)] ax[0].axhline(ishot*shifty,ls='--',color=color) ax[1].axhline(ishot*shifty,ls='--',color=color) + if showAvOverAll: + if ishot == 0: ax[0].fill_between(E,ishot*shifty,p1_av/normalization+ishot*shifty,color=color_av_all,alpha=0.6) + ax[1].plot(E,abs_av+ishot*shifty,color=color_av_all,lw=2,zorder=20) if showAv: - if ishot == 0: ax[0].fill_between(E,ishot*shifty,p1_av/normalization+ishot*shifty,color='#d95f0e',alpha=0.6) - ax[1].plot(E,abs_av+ishot*shifty,color='#d95f0e',lw=2,zorder=20) + ax[1].plot(E,np.nanmedian(abs,0)+ishot*shifty,color=color_av,lw=2,zorder=10) ax[0].plot(E,s1_norm+ishot*shifty,ls = '-' ,color='0.8',lw=2) ax[0].plot(E,s2_norm+ishot*shifty,ls = '-' ,color='0.3',lw=2) - ax[1].plot(E,a+ishot*shifty,color=color,lw=2) + ax[1].plot(E,a+ishot*shifty,color=color_ss,lw=2) ax[0].set_xlim(*xlim) - ax[0].set_title("Run %d"%run) + ax[0].set_title("Run %s"%str(run)) ax[1].set_ylabel("Sample Absorption") ax[0].set_ylabel("Normalized Spectra") ax[0].set_ylim(0,shifty*(p1.shape[0])) + print("STD of (average over shown shots) - (average over all): %.3f"%np.nanstd(np.nanmedian(abs,0)-abs_av)) + ax[1].set_title("STD of (average_{shown}) - (average_{all}): %.3f"%np.nanstd(np.nanmedian(abs,0)-abs_av)) + + +def showAbsWithSweep(run=(155,156),first=0,period=150,nSpectra=10,**kwards): + shots = slice(first,first+period,int(period/nSpectra)) + showAbs(run=run,shots=shots,**kwards) def smoothSpectra(E,abs_spectra,res=0.5): from scipy import integrate