def optimize_img_scale(img_data,img_std,img_model,optimizer='curve_fit',show_residualimg=False,verbose=True):
"""
optimize the (flux) scaling of an image with respect to a (noisy) data image
--- INPUT ---
img_data The (noisy) data image to scale model image provide in img_model to
img_std Standard deviation image for data to use in optimization
img_model Model image to (flux) scale to match img_data
optimizer The optimizer to use when scaling the layers
show_residualimg To show the residual image (data - model) for the optimize layers, set this to true
verbose Toggle verbosity
--- EXAMPLE OF USE ---
import tdose_model_cube as tmc
scale, cov = tmc.optimize_img_scale()
"""
if verbose: print ' - Optimize residual between model (multiple Gaussians) and data with least squares in 2D'
if verbose: print ' ----------- Started on '+tu.get_now_string()+' ----------- '
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if optimizer == 'leastsq':
sys.exit('optimizer = "leastsq" no enabled')
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
elif optimizer == 'curve_fit':
imgsize = img_data.shape
xgrid, ygrid = tu.gen_gridcomponents(imgsize)
scale_best, scale_cov = opt.curve_fit(lambda (xgrid, ygrid), scale:
tmc.curve_fit_fct_wrapper_imgscale((xgrid, ygrid), scale, img_model),
(xgrid, ygrid),
img_data.ravel(), p0 = [1.0], sigma=img_std.ravel() )
output = scale_best, scale_cov
else:
sys.exit(' ---> Invalid optimizer ('+optimizer+') chosen in optimize_img_scale()')
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if verbose: print ' ----------- Finished on '+tu.get_now_string()+' ----------- '
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if show_residualimg:
if verbose: print ' - Displaying the residual image between data and scaled model image '
res_img = img_model-img_data
plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm())
plt.title('Initial Residual = Initial Model Image - Data Image')
plt.show()
res_img = img_model*scale_best-img_data
plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm())
plt.title('Best Residual = Scaled (by '+str(scale_best)+') Model Image - Data Image')
plt.show()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return output
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
评论列表
文章目录