during marion visit
This commit is contained in:
parent
0df7c7b2f1
commit
487d8dae3d
523
HOW-TO.ipynb
523
HOW-TO.ipynb
File diff suppressed because one or more lines are too long
|
@ -19,9 +19,12 @@
|
|||
# 2. tell python to use look for modules in the folder
|
||||
# 3. import sys; sys.path.insert(0,"~/my_folder")
|
||||
|
||||
# there are two files:
|
||||
# these are the important files:
|
||||
# 1. alignment.py (deals with images)
|
||||
# 2. xanes_analyzeRun.py (deals with run and images reading)
|
||||
# 3. xppl37_calibration.py (deals with spectrometer calibration)
|
||||
# 4. xppl37_theta_scan.py (deals with theta scans)
|
||||
# 5. xppl37_spectra.py (deals with calculation of absorption spectra for IN/OUT scans, etc.
|
||||
|
||||
%matplotlib nbagg
|
||||
import matplotlib
|
||||
|
@ -45,6 +48,8 @@ pars = dict( scalex = 0.6, intensity = 0.1, iblur1=2,fix_iblur1 = False )
|
|||
|
||||
# define the run object
|
||||
#### NOTE : for mec run swapx=True,swapy=False
|
||||
#### NOTE : for xpp un-focused run: swapx=False,swapy=False
|
||||
#### NOTE : for xpp focused run: swapx=False,swapy=True
|
||||
r = xanes_analyzeRun.AnalyzeRun(190,initAlign=pars,swapx=True,swapy=False)
|
||||
|
||||
# data are d.spec1 and d.spec2 (spec1 is the one **upbeam**)
|
||||
|
|
|
@ -84,6 +84,27 @@ def findRoi(img,height=100,axis=0):
|
|||
return roi
|
||||
|
||||
|
||||
def subtractBkg(imgs,nPix=100,bkg_type="line"):
|
||||
if imgs.ndim == 2: imgs = imgs[np.newaxis,:]
|
||||
imgs = imgs.astype(np.float)
|
||||
if bkg_type == "line":
|
||||
bkg = np.median(imgs[:,:nPix,:],axis=1)
|
||||
imgs = imgs-bkg[:,np.newaxis,:]
|
||||
elif bkg_type == "corner":
|
||||
q1 = imgs[:,:nPix,:nPix].mean(-1).mean(-1)
|
||||
imgs[:,:512,:512]-=q1[:,np.newaxis,np.newaxis]
|
||||
q2 = imgs[:,:nPix,-nPix:].mean(-1).mean(-1)
|
||||
imgs[:,:512,-512:]-=q2[:,np.newaxis,np.newaxis]
|
||||
q3 = imgs[:,-nPix:,-nPix:].mean(-1).mean(-1)
|
||||
imgs[:,-512:,-512:]-=q3[:,np.newaxis,np.newaxis]
|
||||
q4 = imgs[:,-nPix:,:nPix].mean(-1).mean(-1)
|
||||
imgs[:,-512:,:512]-=q4[:,np.newaxis,np.newaxis]
|
||||
elif bkg_type is None:
|
||||
if imgs.ndim == 2: imgs = imgs[np.newaxis,:].astype(np.float)
|
||||
else:
|
||||
print("Background subtraction '%s' Not impleted"%bkg_type)
|
||||
return imgs
|
||||
|
||||
# /--------------------\
|
||||
# | |
|
||||
# | PLOTS & CO. |
|
||||
|
@ -313,7 +334,7 @@ def findTransform(p1,p2,ttype="affine"):
|
|||
# \--------/
|
||||
|
||||
|
||||
def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroThreshold=0.05,doFit=True):
|
||||
def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroThreshold=0.0,doFit=True):
|
||||
import iminuit
|
||||
assert im1.dtype == im2.dtype
|
||||
t0 = time.time()
|
||||
|
@ -321,7 +342,7 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
|
|||
im1_toFit = im1.copy()
|
||||
im2_toFit = im2.copy()
|
||||
|
||||
# set anything below the 5% of the max to zero (one of the two opal is noisy)
|
||||
# set anything below the zeroThreshold of the max to zero (one of the two opal is noisy)
|
||||
im1_toFit[im1_toFit<im1.max()*zeroThreshold] = 0
|
||||
im2_toFit[im2_toFit<im2.max()*zeroThreshold] = 0
|
||||
p1 = im1.mean(0)
|
||||
|
@ -365,8 +386,8 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
|
|||
|
||||
# set default initial stepsize and limits
|
||||
r = im2.mean(0).sum()/im1.mean(0).sum()
|
||||
|
||||
default_kw = g_fit_default_kw.copy()
|
||||
# will be used only if not in initpars
|
||||
default_kw["intensity"] = r
|
||||
|
||||
init_kw = dict()
|
||||
|
@ -400,6 +421,7 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
|
|||
plotShot(i1,i2)
|
||||
fig = plt.gcf()
|
||||
fig.text(0.5,0.9,"Initial Pars")
|
||||
plt.draw()
|
||||
input("Enter to start fit")
|
||||
|
||||
if doFit: imin.migrad()
|
||||
|
|
|
@ -156,10 +156,8 @@ class AnalyzeRun(object):
|
|||
# rebin and swap im1 if necessary
|
||||
if im1.shape[-1] != 1024:
|
||||
im1 = mc.rebin(im1, (im1.shape[0],im1.shape[1],1024) )
|
||||
if self.swap[0]:
|
||||
im1 = im1[:,:,::-1]
|
||||
if self.swap[1]:
|
||||
im1 = im1[:,::-1,:]
|
||||
if self.swap[0]: im1 = im1[:,:,::-1]
|
||||
if self.swap[1]: im1 = im1[:,::-1,:]
|
||||
if roi is None:
|
||||
pass
|
||||
elif isinstance(roi,slice):
|
||||
|
@ -268,6 +266,7 @@ class AnalyzeRun(object):
|
|||
def save(self,fname="auto",overwrite=False):
|
||||
if len(self.results) == 0:
|
||||
print("self.results are empty, returning without saving")
|
||||
return
|
||||
if not os.path.isdir(g_folder_out): os.makedirs(g_folder_out)
|
||||
if fname == "auto":
|
||||
fname = g_folder_out+"/run%04d_analysis.npz" % self.run
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -22,8 +22,11 @@ gradual_colors = ['#014636', '#016c59', '#02818a', '#3690c0', '#67a9cf', '#a6bdd
|
|||
|
||||
|
||||
def calcSpectraForRun(run=82,calibs="all",realign=False,init="auto",alignCalib=0,force=False):
|
||||
""" init and alignCalib are used only if realignment is performed:
|
||||
init = run for initial alignment, use auto is you want to use same run
|
||||
""" Calculate spectra (spec1, spec2) for run based on alignment.
|
||||
|
||||
Alignment can be 'forced' with realign=True.
|
||||
In such a case
|
||||
init = run for initial alignment
|
||||
alignCalib = calibcycle for alignment
|
||||
"""
|
||||
if init=="auto": init=run
|
||||
|
@ -51,11 +54,17 @@ def calcSpectraForRun(run=82,calibs="all",realign=False,init="auto",alignCalib=0
|
|||
elif isinstance(calibs,slice):
|
||||
calibsForOut = list(range(r.nCalib))[calibs]
|
||||
elif calibs == "all":
|
||||
calibsForOut = list(range(r.nCalib))
|
||||
calibsForOut = r.results.keys()
|
||||
else:
|
||||
calibsForOut = calibs
|
||||
# focused data have one single calibcycle ...
|
||||
if len(r.results) > 1:
|
||||
p1 = [r.results[calib].p1 for calib in calibsForOut]
|
||||
p2 = [r.results[calib].p2 for calib in calibsForOut]
|
||||
else:
|
||||
idx = r.results[None].fom < 0.5
|
||||
p1 = [ r.results[None].p1[idx], r.results[None].p1[~idx] ]
|
||||
p2 = [ r.results[None].p2[idx], r.results[None].p2[~idx] ]
|
||||
return profile_ret( run =r, p1 =p1, p2=p2,calibs=calibsForOut)
|
||||
|
||||
|
||||
|
@ -80,9 +89,13 @@ def calcSpectraForRefAndSample(run=82,refCalibs=slice(None,None,2),forceSpectraC
|
|||
if isinstance(refCalibs,int): refCalibs = [refCalibs,]
|
||||
sampleCalibs = [c+1 for c in refCalibs]
|
||||
# need a single call (for sample and ref) to save all calibcycles
|
||||
calcSpectraForRun(run,calibs=refCalibs+sampleCalibs,\
|
||||
data= calcSpectraForRun(run,calibs=refCalibs+sampleCalibs,\
|
||||
force=forceSpectraCalculation);
|
||||
|
||||
# for focused runs
|
||||
if len(data.run.results) == 1:
|
||||
ref = profile_ret(run=data.run,p1=[data.p1[0],],p2=[data.p2[0],],calibs=[0,])
|
||||
sample = profile_ret(run=data.run,p1=[data.p1[1],],p2=[data.p2[1],],calibs=[0,])
|
||||
else:
|
||||
ref = calcSpectraForRun(run,calibs=refCalibs)
|
||||
sample = calcSpectraForRun(run,calibs=sampleCalibs)
|
||||
elif isinstance(run,(list,tuple)):
|
||||
|
|
Loading…
Reference in New Issue