during marion visit
This commit is contained in:
parent
0df7c7b2f1
commit
487d8dae3d
7955
HOW-TO.ipynb
7955
HOW-TO.ipynb
File diff suppressed because one or more lines are too long
11
README.md
11
README.md
|
@ -19,9 +19,12 @@
|
||||||
# 2. tell python to use look for modules in the folder
|
# 2. tell python to use look for modules in the folder
|
||||||
# 3. import sys; sys.path.insert(0,"~/my_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)
|
# 1. alignment.py (deals with images)
|
||||||
# 2. xanes_analyzeRun.py (deals with run and images reading)
|
# 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
|
%matplotlib nbagg
|
||||||
import matplotlib
|
import matplotlib
|
||||||
|
@ -45,6 +48,8 @@ pars = dict( scalex = 0.6, intensity = 0.1, iblur1=2,fix_iblur1 = False )
|
||||||
|
|
||||||
# define the run object
|
# define the run object
|
||||||
#### NOTE : for mec run swapx=True,swapy=False
|
#### 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)
|
r = xanes_analyzeRun.AnalyzeRun(190,initAlign=pars,swapx=True,swapy=False)
|
||||||
|
|
||||||
# data are d.spec1 and d.spec2 (spec1 is the one **upbeam**)
|
# 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
|
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. |
|
# | 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
|
import iminuit
|
||||||
assert im1.dtype == im2.dtype
|
assert im1.dtype == im2.dtype
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
|
@ -321,7 +342,7 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
|
||||||
im1_toFit = im1.copy()
|
im1_toFit = im1.copy()
|
||||||
im2_toFit = im2.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
|
im1_toFit[im1_toFit<im1.max()*zeroThreshold] = 0
|
||||||
im2_toFit[im2_toFit<im2.max()*zeroThreshold] = 0
|
im2_toFit[im2_toFit<im2.max()*zeroThreshold] = 0
|
||||||
p1 = im1.mean(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
|
# set default initial stepsize and limits
|
||||||
r = im2.mean(0).sum()/im1.mean(0).sum()
|
r = im2.mean(0).sum()/im1.mean(0).sum()
|
||||||
|
|
||||||
default_kw = g_fit_default_kw.copy()
|
default_kw = g_fit_default_kw.copy()
|
||||||
|
# will be used only if not in initpars
|
||||||
default_kw["intensity"] = r
|
default_kw["intensity"] = r
|
||||||
|
|
||||||
init_kw = dict()
|
init_kw = dict()
|
||||||
|
@ -400,6 +421,7 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
|
||||||
plotShot(i1,i2)
|
plotShot(i1,i2)
|
||||||
fig = plt.gcf()
|
fig = plt.gcf()
|
||||||
fig.text(0.5,0.9,"Initial Pars")
|
fig.text(0.5,0.9,"Initial Pars")
|
||||||
|
plt.draw()
|
||||||
input("Enter to start fit")
|
input("Enter to start fit")
|
||||||
|
|
||||||
if doFit: imin.migrad()
|
if doFit: imin.migrad()
|
||||||
|
|
|
@ -156,10 +156,8 @@ class AnalyzeRun(object):
|
||||||
# rebin and swap im1 if necessary
|
# rebin and swap im1 if necessary
|
||||||
if im1.shape[-1] != 1024:
|
if im1.shape[-1] != 1024:
|
||||||
im1 = mc.rebin(im1, (im1.shape[0],im1.shape[1],1024) )
|
im1 = mc.rebin(im1, (im1.shape[0],im1.shape[1],1024) )
|
||||||
if self.swap[0]:
|
if self.swap[0]: im1 = im1[:,:,::-1]
|
||||||
im1 = im1[:,:,::-1]
|
if self.swap[1]: im1 = im1[:,::-1,:]
|
||||||
if self.swap[1]:
|
|
||||||
im1 = im1[:,::-1,:]
|
|
||||||
if roi is None:
|
if roi is None:
|
||||||
pass
|
pass
|
||||||
elif isinstance(roi,slice):
|
elif isinstance(roi,slice):
|
||||||
|
@ -268,6 +266,7 @@ class AnalyzeRun(object):
|
||||||
def save(self,fname="auto",overwrite=False):
|
def save(self,fname="auto",overwrite=False):
|
||||||
if len(self.results) == 0:
|
if len(self.results) == 0:
|
||||||
print("self.results are empty, returning without saving")
|
print("self.results are empty, returning without saving")
|
||||||
|
return
|
||||||
if not os.path.isdir(g_folder_out): os.makedirs(g_folder_out)
|
if not os.path.isdir(g_folder_out): os.makedirs(g_folder_out)
|
||||||
if fname == "auto":
|
if fname == "auto":
|
||||||
fname = g_folder_out+"/run%04d_analysis.npz" % self.run
|
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,9 +22,12 @@ gradual_colors = ['#014636', '#016c59', '#02818a', '#3690c0', '#67a9cf', '#a6bdd
|
||||||
|
|
||||||
|
|
||||||
def calcSpectraForRun(run=82,calibs="all",realign=False,init="auto",alignCalib=0,force=False):
|
def calcSpectraForRun(run=82,calibs="all",realign=False,init="auto",alignCalib=0,force=False):
|
||||||
""" init and alignCalib are used only if realignment is performed:
|
""" Calculate spectra (spec1, spec2) for run based on alignment.
|
||||||
init = run for initial alignment, use auto is you want to use same run
|
|
||||||
alignCalib = calibcycle for 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
|
if init=="auto": init=run
|
||||||
if isinstance(run,int):
|
if isinstance(run,int):
|
||||||
|
@ -51,11 +54,17 @@ def calcSpectraForRun(run=82,calibs="all",realign=False,init="auto",alignCalib=0
|
||||||
elif isinstance(calibs,slice):
|
elif isinstance(calibs,slice):
|
||||||
calibsForOut = list(range(r.nCalib))[calibs]
|
calibsForOut = list(range(r.nCalib))[calibs]
|
||||||
elif calibs == "all":
|
elif calibs == "all":
|
||||||
calibsForOut = list(range(r.nCalib))
|
calibsForOut = r.results.keys()
|
||||||
else:
|
else:
|
||||||
calibsForOut = calibs
|
calibsForOut = calibs
|
||||||
p1 = [r.results[calib].p1 for calib in calibsForOut]
|
# focused data have one single calibcycle ...
|
||||||
p2 = [r.results[calib].p2 for calib in calibsForOut]
|
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)
|
return profile_ret( run =r, p1 =p1, p2=p2,calibs=calibsForOut)
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,11 +89,15 @@ def calcSpectraForRefAndSample(run=82,refCalibs=slice(None,None,2),forceSpectraC
|
||||||
if isinstance(refCalibs,int): refCalibs = [refCalibs,]
|
if isinstance(refCalibs,int): refCalibs = [refCalibs,]
|
||||||
sampleCalibs = [c+1 for c in refCalibs]
|
sampleCalibs = [c+1 for c in refCalibs]
|
||||||
# need a single call (for sample and ref) to save all calibcycles
|
# 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);
|
force=forceSpectraCalculation);
|
||||||
|
# for focused runs
|
||||||
ref = calcSpectraForRun(run,calibs=refCalibs)
|
if len(data.run.results) == 1:
|
||||||
sample = calcSpectraForRun(run,calibs=sampleCalibs)
|
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)):
|
elif isinstance(run,(list,tuple)):
|
||||||
refRun = xanes_analyzeRun.AnalyzeRun(run[0])
|
refRun = xanes_analyzeRun.AnalyzeRun(run[0])
|
||||||
sampleRun = xanes_analyzeRun.AnalyzeRun(run[1],initAlign=run[0])
|
sampleRun = xanes_analyzeRun.AnalyzeRun(run[1],initAlign=run[0])
|
||||||
|
|
Loading…
Reference in New Issue