main change: worked to avoid memory leaks trough view of original big array and iminuit object

This commit is contained in:
Marco Cammarata 2016-11-25 11:04:12 +01:00
parent a2c508fa5d
commit 6457520442
1 changed files with 11 additions and 13 deletions

View File

@ -77,7 +77,7 @@ kw_2dplot = dict(
cmap = cmap cmap = cmap
) )
fit_ret = collections.namedtuple("fit_ret",["fit_result","init_pars","final_pars",\ fit_ret = collections.namedtuple("fit_ret",["init_pars","final_pars",\
"final_transform1","final_transform2","im1","im2","E","p1","p1_sum","p2","p2_sum","fom","ratio","tneeded"] ) "final_transform1","final_transform2","im1","im2","E","p1","p1_sum","p2","p2_sum","fom","ratio","tneeded"] )
def calcFOM(p1,p2,ratio): def calcFOM(p1,p2,ratio):
@ -268,22 +268,21 @@ def loadAlignment(fname):
def unravel_results(res,nSaveImg='all'): def unravel_results(res,nSaveImg='all'):
final_pars = dict() final_pars = dict()
# res[0].fit_result is a list if we are trying to unravel retult from getShots # res[0].fit_result is a list if we are trying to unravel retult from getShots
if isinstance(res[0].fit_result,list): if isinstance(res[0].init_pars,list):
parnames = res[0].fit_result[0].parameters parnames = res[0].init_pars[0].get_keys()
else: else:
parnames = res[0].fit_result.parameters parnames = res[0].init_pars.keys()
final_pars = dict() final_pars = dict()
init_pars = dict() init_pars = dict()
for n in parnames: for n in parnames:
final_pars[n] = np.hstack( [r.final_pars[n] for r in res]) final_pars[n] = np.hstack( [r.final_pars[n] for r in res])
init_pars[n] = np.hstack( [r.init_pars[n] for r in res]) init_pars[n] = np.hstack( [r.init_pars[n] for r in res])
im1 = np.asarray( [r.im1 for r in res]) im1 = np.asarray( [r.im1 for r in res if r.im1.shape[0] != 0])
im2 = np.asarray( [r.im2 for r in res]) im2 = np.asarray( [r.im2 for r in res if r.im2.shape[0] != 0])
if nSaveImg != 'all': if nSaveImg != 'all':
im1 = im1[:nSaveImg] im1 = im1[:nSaveImg].copy(); # copy is necessary to free the original memory
im2 = im2[:nSaveImg] im2 = im2[:nSaveImg].copy(); # of original array
return fit_ret( return fit_ret(
fit_result = [r.fit_result for r in res],
init_pars = init_pars, init_pars = init_pars,
final_pars = final_pars, final_pars = final_pars,
final_transform1 = [r.final_transform1 for r in res], final_transform1 = [r.final_transform1 for r in res],
@ -439,7 +438,6 @@ def transformIminuit(im1,im2,init_transform=dict(),show=False,verbose=True,zeroT
idx = p1>np.nanmax(p1)/10. idx = p1>np.nanmax(p1)/10.
fom = calcFOM(p1,p2,r) fom = calcFOM(p1,p2,r)
return fit_ret( return fit_ret(
fit_result = imin,
init_pars = init_params, init_pars = init_params,
final_pars = final_params, final_pars = final_params,
final_transform1 = t1, final_transform1 = t1,
@ -581,9 +579,9 @@ def doShots(imgs1,imgs2,initpars,nJobs=16,doFit=False,returnBestTransform=False,
(joblib.delayed(doShot)(imgs1[i],imgs2[i],initpars,doFit=doFit) for i in range(N)) (joblib.delayed(doShot)(imgs1[i],imgs2[i],initpars,doFit=doFit) for i in range(N))
res = unravel_results(pool,nSaveImg=nSaveImg) res = unravel_results(pool,nSaveImg=nSaveImg)
if returnBestTransform: if returnBestTransform:
idx = np.argmin( res.fom ); idx = np.nanargmin( res.fom );
print("FOM for best alignment %.2f"%res.fom[fom]) print("FOM for best alignment %.2f"%res.fom[idx])
return res,res.final_transform1[idx] return res,pool[idx].final_pars
else: else:
return res return res
# out = collections.OrderedDict( enumerate(pool) ) # out = collections.OrderedDict( enumerate(pool) )