various cahnges, worked on data saving
This commit is contained in:
parent
389d8b18a6
commit
367f48eed4
27
alignment.py
27
alignment.py
|
@ -6,6 +6,7 @@ import joblib
|
||||||
import collections
|
import collections
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import utils
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# /--------\
|
# /--------\
|
||||||
|
@ -157,14 +158,14 @@ def plotShot(im1,im2,transf1=None,transf2=None,fig=None,ax=None,res=None,E=defau
|
||||||
if (save is not None) and (save is not False): plt.savefig(save,transparent=True,dpi=500)
|
if (save is not None) and (save is not False): plt.savefig(save,transparent=True,dpi=500)
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
def plotRatios(r,fig=None,E=defaultE,save=None):
|
def plotRatios(r,shot='random',fig=None,E=defaultE,save=None):
|
||||||
if fig is None: fig = plt.subplots(2,1,sharex=True)[0]
|
if fig is None: fig = plt.subplots(2,1,sharex=True)[0]
|
||||||
ax = fig.axes
|
ax = fig.axes
|
||||||
n = r.shape[0]
|
n = r.shape[0]
|
||||||
i = ax[0].imshow(r,extent=(E[0],E[-1],0,n),**kw_2dplot)
|
i = ax[0].imshow(r,extent=(E[0],E[-1],0,n),**kw_2dplot)
|
||||||
i.set_clim(0,1.2)
|
i.set_clim(0,1.2)
|
||||||
idx = np.random.random_integers(0,n-1)
|
if shot == 'random' : shot = np.random.random_integers(0,n-1)
|
||||||
ax[1].plot(E,r[idx],label="Shot n %d"%idx)
|
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[:10],axis=0),label="median 10 shots")
|
||||||
ax[1].plot(E,np.nanmedian(r,axis=0),label="all shots")
|
ax[1].plot(E,np.nanmedian(r,axis=0),label="all shots")
|
||||||
ax[1].legend()
|
ax[1].legend()
|
||||||
|
@ -266,11 +267,13 @@ def loadAlignment(fname):
|
||||||
return np.load(fname).item()
|
return np.load(fname).item()
|
||||||
|
|
||||||
def unravel_results(res):
|
def unravel_results(res):
|
||||||
#out = dict()
|
final_pars = dict()
|
||||||
#parnames = res[0].fit_result.parameters
|
parnames = res[0].fit_result.parameters
|
||||||
#out["parameters"] = dict()
|
final_pars = dict()
|
||||||
#for n in parnames:
|
init_pars = dict()
|
||||||
# out["parameters"][n] = np.asarray( [r.final_pars[n] for r in res])
|
for n in parnames:
|
||||||
|
final_pars[n] = np.asarray( [r.final_pars[n] for r in res])
|
||||||
|
init_pars[n] = np.asarray( [r.init_pars[n] for r in res])
|
||||||
#out["ratio"] = np.asarray( [r.ratio for r in res])
|
#out["ratio"] = np.asarray( [r.ratio for r in res])
|
||||||
#out["p1"] = np.asarray( [r.p1 for r in res] )
|
#out["p1"] = np.asarray( [r.p1 for r in res] )
|
||||||
#out["p2"] = np.asarray( [r.p2 for r in res] )
|
#out["p2"] = np.asarray( [r.p2 for r in res] )
|
||||||
|
@ -278,14 +281,14 @@ def unravel_results(res):
|
||||||
#out["E"] = res[0].E
|
#out["E"] = res[0].E
|
||||||
return fit_ret(
|
return fit_ret(
|
||||||
fit_result = [r.fit_result for r in res],
|
fit_result = [r.fit_result for r in res],
|
||||||
init_pars = [r.init_pars for r in res],
|
init_pars = init_pars,
|
||||||
final_pars = [r.final_pars for r in res],
|
final_pars = final_pars,
|
||||||
final_transform1 = [r.final_transform1 for r in res],
|
final_transform1 = [r.final_transform1 for r in res],
|
||||||
final_transform2 = [r.final_transform2 for r in res],
|
final_transform2 = [r.final_transform2 for r in res],
|
||||||
im1 = np.asarray( [r.im1 for r in res]),
|
im1 = np.asarray( [r.im1 for r in res]),
|
||||||
im2 = np.asarray( [r.im2 for r in res]),
|
im2 = np.asarray( [r.im2 for r in res]),
|
||||||
E = defaultE,
|
E = defaultE,
|
||||||
p1 = np.asarray( [r.p1 for r in res]),
|
p1 = np.hstack( [r.p1 for r in res]),
|
||||||
p2 = np.asarray( [r.p2 for r in res]),
|
p2 = np.asarray( [r.p2 for r in res]),
|
||||||
ratio = np.asarray( [r.ratio for r in res]),
|
ratio = np.asarray( [r.ratio for r in res]),
|
||||||
fom = np.asarray( [r.fom for r in res] ),
|
fom = np.asarray( [r.fom for r in res] ),
|
||||||
|
@ -529,7 +532,7 @@ class GuiAlignment(object):
|
||||||
|
|
||||||
|
|
||||||
def getAverageTransformation(out):
|
def getAverageTransformation(out):
|
||||||
res = unravel_results(out)
|
res = append_results(out)
|
||||||
# get average parameters
|
# get average parameters
|
||||||
tx = np.median(res["transx"])
|
tx = np.median(res["transx"])
|
||||||
ty = np.median(res["transy"])
|
ty = np.median(res["transy"])
|
||||||
|
|
1293
mcutils.py
1293
mcutils.py
File diff suppressed because it is too large
Load Diff
|
@ -5,6 +5,7 @@ np.warnings.simplefilter('ignore')
|
||||||
import time
|
import time
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import h5py
|
import h5py
|
||||||
|
import collections
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from x3py import x3py
|
from x3py import x3py
|
||||||
|
@ -67,7 +68,7 @@ def readDataset(fnameOrRun=7,
|
||||||
else:
|
else:
|
||||||
if isinstance(fnameOrRun,int):
|
if isinstance(fnameOrRun,int):
|
||||||
fnameOrRun=g_folder_data+"/"+g_exp+"-r%04d.h5" % fnameOrRun
|
fnameOrRun=g_folder_data+"/"+g_exp+"-r%04d.h5" % fnameOrRun
|
||||||
d = x3py.Dataset(fnameOrRun,detectors=["opal0","opal1","fee_spec","opal2"])
|
d = x3py.Dataset(fnameOrRun,detectors=["opal0","opal1","fee_spec","opal2","ebeam"])
|
||||||
if g_bml == "xpp":
|
if g_bml == "xpp":
|
||||||
d.spec1 = d.opal0
|
d.spec1 = d.opal0
|
||||||
d.spec2 = d.opal1
|
d.spec2 = d.opal1
|
||||||
|
@ -111,15 +112,18 @@ class AnalyzeRun(object):
|
||||||
3. an integer (to look for xppl3716_init_pars/run????_transform.npy)
|
3. an integer (to look for xppl3716_init_pars/run????_transform.npy)
|
||||||
4. a file name (that has been previosly saved with r.saveTransform(fname)
|
4. a file name (that has been previosly saved with r.saveTransform(fname)
|
||||||
"""
|
"""
|
||||||
self.d = readDataset(run)
|
self.data = readDataset(run)
|
||||||
|
self.scanpos = self.data.scan.scanmotor0_values
|
||||||
|
self.nCalib = self.data.spec1.nCalib
|
||||||
|
self.nShotsPerCalib = self.data.spec1.lens
|
||||||
if isinstance(run,str):
|
if isinstance(run,str):
|
||||||
run = int( re.search("\d{3,4}",run).group() )
|
run = int( re.search("\d{3,4}",run).group() )
|
||||||
self.run = run
|
self.run = run
|
||||||
self.results = dict()
|
self.results = collections.OrderedDict()
|
||||||
self.swap = (swapx,swapy)
|
self.swap = (swapx,swapy)
|
||||||
#self.clearCache()
|
#self.clearCache()
|
||||||
|
|
||||||
d = self.d
|
d = self.data
|
||||||
self.spec1 = d.spec1 ; # spec1 is the one that is moved
|
self.spec1 = d.spec1 ; # spec1 is the one that is moved
|
||||||
self.spec2 = d.spec2 ;
|
self.spec2 = d.spec2 ;
|
||||||
|
|
||||||
|
@ -173,9 +177,10 @@ class AnalyzeRun(object):
|
||||||
def analyzeScan(self,initpars=None,nShotsPerCalib=20,nC=None,doFit=False,fitEveryCalib=False):
|
def analyzeScan(self,initpars=None,nShotsPerCalib=20,nC=None,doFit=False,fitEveryCalib=False):
|
||||||
""" this is a comment """
|
""" this is a comment """
|
||||||
if initpars is None: initpars= self.initAlign
|
if initpars is None: initpars= self.initAlign
|
||||||
d = self.d
|
d = self.data
|
||||||
if nC is None: nC = d.opal1.nCalib
|
if nC is None: nC = d.opal1.nCalib
|
||||||
shots = slice(nShotsPerCalib)
|
shots = slice(nShotsPerCalib)
|
||||||
|
out = []
|
||||||
for i in range(nC):
|
for i in range(nC):
|
||||||
s1,s2 = self.getShot(shots,calib=i)
|
s1,s2 = self.getShot(shots,calib=i)
|
||||||
if fitEveryCalib is not False:
|
if fitEveryCalib is not False:
|
||||||
|
@ -184,11 +189,12 @@ class AnalyzeRun(object):
|
||||||
idx = np.argmin( [p.fom for p in res] )
|
idx = np.argmin( [p.fom for p in res] )
|
||||||
res = res[idx]
|
res = res[idx]
|
||||||
initpars = res.final_pars; self.initAlign=res.final_pars
|
initpars = res.final_pars; self.initAlign=res.final_pars
|
||||||
print("Calib cycle %d -> %.3f aligned (best FOM: %.2f)" % (i,self.d.scan.scanmotor0_values[i],res.fom))
|
print("Calib cycle %d -> %.3f aligned (best FOM: %.2f)" % (i,self.data.scan.scanmotor0_values[i],res.fom))
|
||||||
ret = alignment.doShots(s1,s2,initpars=initpars,doFit=doFit)
|
ret = alignment.doShots(s1,s2,initpars=initpars,doFit=doFit)
|
||||||
res = alignment.unravel_results(ret)
|
res = alignment.unravel_results(ret)
|
||||||
self.results[i] = res
|
self.results[i] = res
|
||||||
return list(self.results.values())
|
out.append(res)
|
||||||
|
return out
|
||||||
|
|
||||||
def doShot(self,shot=0,calib=None,initpars=None,im1=None,im2=None,doFit=True,show=False,showInit=False,save=False,savePlot="auto"):
|
def doShot(self,shot=0,calib=None,initpars=None,im1=None,im2=None,doFit=True,show=False,showInit=False,save=False,savePlot="auto"):
|
||||||
if initpars is None: initpars= self.initAlign
|
if initpars is None: initpars= self.initAlign
|
||||||
|
@ -208,7 +214,7 @@ class AnalyzeRun(object):
|
||||||
|
|
||||||
def doShots(self,shots=slice(0,50),calib=None,initpars=None,doFit=False,returnBestTransform=False,unravel=True):
|
def doShots(self,shots=slice(0,50),calib=None,initpars=None,doFit=False,returnBestTransform=False,unravel=True):
|
||||||
if initpars is None: initpars= self.initAlign
|
if initpars is None: initpars= self.initAlign
|
||||||
d = self.d
|
d = self.data
|
||||||
s1,s2 = self.getShot(shots,calib=calib)
|
s1,s2 = self.getShot(shots,calib=calib)
|
||||||
ret,transformForBestFit = alignment.doShots(s1,s2,initpars=initpars,doFit=doFit,\
|
ret,transformForBestFit = alignment.doShots(s1,s2,initpars=initpars,doFit=doFit,\
|
||||||
returnBestTransform=True)
|
returnBestTransform=True)
|
||||||
|
@ -223,6 +229,8 @@ class AnalyzeRun(object):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def save(self,fname="auto",overwrite=False):
|
def save(self,fname="auto",overwrite=False):
|
||||||
|
if len(self.results) == 0: print("self.results are empty, returning without saving")
|
||||||
|
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.h5" % self.run
|
fname = g_folder_out+"/run%04d_analysis.h5" % self.run
|
||||||
if os.path.exists(fname) and not overwrite:
|
if os.path.exists(fname) and not overwrite:
|
||||||
|
@ -233,11 +241,19 @@ class AnalyzeRun(object):
|
||||||
h = h5py.File(fname)
|
h = h5py.File(fname)
|
||||||
h["roi1"] = (self.roi1.start,self.roi1.stop)
|
h["roi1"] = (self.roi1.start,self.roi1.stop)
|
||||||
h["roi2"] = (self.roi2.start,self.roi2.stop)
|
h["roi2"] = (self.roi2.start,self.roi2.stop)
|
||||||
|
h["scanmot0"] = self.data.scan.scanmotor0
|
||||||
|
h["scanpos0"] = self.data.scan.scanmotor0_values
|
||||||
|
if hasattr(self.data.scan,"scanmotor1"):
|
||||||
|
h["scanmot1"] = self.data.scan.scanmotor1
|
||||||
|
h["scanpos1"] = self.data.scan.scanmotor1_values
|
||||||
#h["transform"] = self.initAlign
|
#h["transform"] = self.initAlign
|
||||||
for (c,v) in self.results.items():
|
for (c,v) in self.results.items():
|
||||||
cname = "calib%04d/" % c if isinstance(c,int) else "calib%s/" % c
|
cname = "calib%04d/" % c if isinstance(c,int) else "calib%s/" % c
|
||||||
for p,vv in v.items():
|
for p,vv in mc.objToDict(v).items():
|
||||||
if p == "parameters":
|
# cannot save in hfd5 certain python objects
|
||||||
|
if p == "fit_result" or p.find("final_transform")==0:
|
||||||
|
continue
|
||||||
|
if isinstance(vv,dict):
|
||||||
for pname,parray in vv.items():
|
for pname,parray in vv.items():
|
||||||
name = cname + p + "/" + pname
|
name = cname + p + "/" + pname
|
||||||
h[name] = parray
|
h[name] = parray
|
||||||
|
|
Loading…
Reference in New Issue