def chi2PolarFunc(params,nmax,im,nm): """Function which is to be minimized in the chi^2 analysis for Polar shapelets params = [beta, xc, yc] beta: characteristic size of shapelets, fit parameter xc: x centroid of shapelets, fit parameter yc: y centroid of shapelets, fit parameter nmax: number of coefficents to use in the Laguerre polynomials im: observed image nm: noise map """ import sys beta=params[0] xc=params[1] yc=params[2] if beta<0.: print 'warning: beta going negative, setting to 0.0' beta=0. print 'beta: %f\txc: (%f,%f)'%(beta,xc,yc) size=im.shape r,th=shapelet.polarArray([xc,yc],size) bvals=fgenPolarBasisMatrix(beta,nmax,r,th) coeffs=solveCoeffs(bvals,im) mdl=np.abs(img.constructModel(bvals,coeffs,[xc,yc],size)) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2betaFunc(params,xc,yc,nmax,im,nm): """Function which is to be minimized in the chi^2 analysis params = [beta] beta: characteristic size of shapelets, fit parameter xc: x centroid of shapelets yc: y centroid of shapelets nmax: number of coefficents to use in x,y im: observed image nm: noise map """ betaX=params[0] betaY=params[1] if betaX<0.: print 'warning: betaX going negative, setting to 0.0' betaX=0. if betaY<0.: print 'warning: betaY going negative, setting to 0.0' betaY=0. print 'beta: (%f,%f)'%(betaX,betaY) size=im.shape #shift the (0,0) point to the centroid rx=np.array(range(0,size[0]),dtype=float)-xc ry=np.array(range(0,size[1]),dtype=float)-yc bvals=genBasisMatrix([betaX,betaY],nmax,rx,ry) coeffs=solveCoeffs(bvals,im) mdl=img.constructModel(bvals,coeffs,[xc,yc],size) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2nmaxPolarFunc(params,im,nm,beta,xc): """ params = [nmax] nmax: number of coefficents im: observed image nm: noise map beta: fit beta value xc: fit centroid position """ print params nmax=params size=im.shape r,th=shapelet.polarArray(xc,size) bvals=genPolarBasisMatrix(beta,nmax,r,th) coeffs=solveCoeffs(bvals,im) mdl=np.abs(img.constructModel(bvals,coeffs,xc,size)) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2nmaxPolarFunc(params,im,nm,beta0,beta1,phi,xc): """ params = [nmax] nmax: number of coefficents im: observed image nm: noise map beta0: fit beta value beta1: fit beta value phi: rotation angle xc: fit centroid position """ #nmax=params[0] nmax=[params, params] size=im.shape r,th=shapelet.polarArray(xc,size) bvals=genPolarBasisMatrix([beta0,beta1],nmax,phi,r,th) coeffs=solveCoeffs(bvals,im) mdl=np.abs(img.constructModel(bvals,coeffs,size)) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2nmaxPolarFunc(params, im, nm, beta0, beta1, phi, xc): """ params = [nmax] nmax: number of coefficents im: observed image nm: noise map beta0: fit beta value beta1: fit beta value phi: rotation angle xc: fit centroid position """ #nmax=params[0] nmax = [params, params] size = im.shape r, th = shapelet.polarArray(xc, size) bvals = genPolarBasisMatrix([beta0, beta1], nmax, phi, r, th) coeffs = solveCoeffs(bvals, im) mdl = np.abs(img.constructModel(bvals, coeffs, size)) return np.sum((im - mdl)**2 / nm**2) / (size[0] * size[1])
def chi2nmaxFunc(params,im,nm,beta,xc): """ params = [nmax] nmax: number of coefficents to use in x,y im: observed image nm: noise map beta: fit beta values xc: fit centroid position """ print params nmax=[params,params] size=im.shape #shift the (0,0) point to the centroid rx=np.array(range(0,size[0]),dtype=float)-xc[0] ry=np.array(range(0,size[1]),dtype=float)-xc[1] bvals=genBasisMatrix(beta,nmax,rx,ry) coeffs=solveCoeffs(bvals,im) mdl=img.constructModel(bvals,coeffs,xc,size) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2nmaxFunc(params,im,nm,betaY,betaX,phi,xc): """ params = [nmaxX,nmaxY] nmax: number of coefficents to use in x,y im: observed image nm: noise map betaY: characteristic size of shapelet betaX: characteristic size of shapelet phi: rotation angle of shapelets xc: fit centroid position """ nmax=params size=im.shape #shift the (0,0) point to the centroid ry=np.array(range(0,size[0]),dtype=float)-xc[0] rx=np.array(range(0,size[1]),dtype=float)-xc[1] yy,xx=shapelet.xy2Grid(ry,rx) bvals=genBasisMatrix([betaY,betaX],[nmax,nmax],phi,yy,xx) coeffs=solveCoeffs(bvals,im) mdl=img.constructModel(bvals,coeffs,size) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2nmaxFunc(params, im, nm, betaY, betaX, phi, xc): """ params = [nmaxX,nmaxY] nmax: number of coefficents to use in x,y im: observed image nm: noise map betaY: characteristic size of shapelet betaX: characteristic size of shapelet phi: rotation angle of shapelets xc: fit centroid position """ nmax = params size = im.shape #shift the (0,0) point to the centroid ry = np.array(range(0, size[0]), dtype=float) - xc[0] rx = np.array(range(0, size[1]), dtype=float) - xc[1] yy, xx = shapelet.xy2Grid(ry, rx) bvals = genBasisMatrix([betaY, betaX], [nmax, nmax], phi, yy, xx) coeffs = solveCoeffs(bvals, im) mdl = img.constructModel(bvals, coeffs, size) return np.sum((im - mdl)**2 / nm**2) / (size[0] * size[1])
def chi2xcFunc(params,beta,nmax,im,nm): """Function which is to be minimized in the chi^2 analysis params = [xc, yc] xc: x centroid of shapelets, fit parameter yc: y centroid of shapelets, fit parameter beta: characteristic size of shapelet nmax: number of coefficents to use in x,y im: observed image nm: noise map """ xc=params[2] yc=params[3] print xc,yc size=im.shape #shift the (0,0) point to the centroid rx=np.array(range(0,size[0]),dtype=float)-xc ry=np.array(range(0,size[1]),dtype=float)-yc bvals=genBasisMatrix(beta,nmax,rx,ry) coeffs=solveCoeffs(bvals,im) mdl=img.constructModel(bvals,coeffs,[xc,yc],size) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2betaPolarFunc(params,xc,yc,r,th,nmax,im,nm): """Function which is to be minimized in the chi^2 analysis for Polar shapelets params = [beta] beta: characteristic size of shapelets, fit parameter xc: x centroid of shapelets yc: y centroid of shapelets r: radius from centroid, array of im.shape th: angle from centroid, array of im.shape nmax: number of coefficents to use in the Laguerre polynomials im: observed image nm: noise map """ beta=params[0] if beta<0.: print 'warning: beta going negative, setting to 0.0' beta=0. print 'beta: %f'%(beta) size=im.shape bvals=genPolarBasisMatrix(beta,nmax,r,th) coeffs=solveCoeffs(bvals,im) mdl=np.abs(img.constructModel(bvals,coeffs,[xc,yc],size)) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2Func(params,nmax,im,nm,order=['beta0','beta1','phi','yc','xc'],set_beta=[None,None],set_phi=None,set_xc=[None,None],xx=None,yy=None): """Function which is to be minimized in the chi^2 analysis for Cartesian shapelets params = [beta0, beta1, phi, xc, yc] or some subset beta0: characteristic size of shapelets, fit parameter beta1: characteristic size of shapelets, fit parameter phi: rotation angle of shapelets, fit parameter yc: y centroid of shapelets, fit parameter xc: x centroid of shapelets, fit parameter nmax: number of coefficents to use in the Hermite polynomials im: observed image nm: noise map order: order of parameters fixed parameters: set_beta, set_phi, set_xc xx: X position grid, array of im.shape, not required if xc and yc being fit yy: Y position grid, array of im.shape, not required if xc and yc being fit """ #determine which parameters are being fit for, and which are not betaY=set_beta[0] betaX=set_beta[1] phi=set_phi yc=set_xc[0] xc=set_xc[1] fitParams={'beta':False,'phi':False,'xc':False} for pid,paramName in enumerate(order): if paramName=='beta0': betaY=params[pid] fitParams['beta']=True elif paramName=='beta1': betaX=params[pid] fitParams['beta']=True elif paramName=='phi': phi=params[pid] fitParams['phi']=True elif paramName=='xc': xc=params[pid] fitParams['xc']=True elif paramName=='yc': yc=params[pid] fitParams['xc']=True #if betaX<0.: # print 'warning: beta going negative, setting to 0.0' # betaX=0. #if betaY<0.: # print 'warning: beta going negative, setting to 0.0' # betaY=0. if betaX<0.: print 'warning: beta going negative, taking absolute value' betaX = np.abs(betaY) if betaY<0.: print 'warning: beta going negative, taking absolute value' betaY = np.abs(betaX) print 'beta: (%f,%f)\t phi: %f\txc: (%f,%f)'%(betaX,betaY,phi,xc,yc) #update noise map nm=img.makeNoiseMap(nm.shape,np.mean(nm),np.std(nm)) size=im.shape if fitParams['xc'] or xx is None: #shift the (0,0) point to the centroid ry=np.array(range(0,size[0]),dtype=float)-yc rx=np.array(range(0,size[1]),dtype=float)-xc yy,xx=shapelet.xy2Grid(ry,rx) bvals=genBasisMatrix([betaY,betaX],nmax,phi,yy,xx) coeffs=solveCoeffs(bvals,im) mdl=img.constructModel(bvals,coeffs,size) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2PolarFunc(params,nmax,im,nm,order=['beta0','beta1','phi','yc','xc'],set_beta=[None,None],set_phi=None,set_xc=[None,None],r=None,th=None): """Function which is to be minimized in the chi^2 analysis for Polar shapelets params = [beta0, beta1, phi, xc, yc] or some subset beta0: characteristic size of shapelets, fit parameter beta1: characteristic size of shapelets, fit parameter phi: rotation angle of shapelets, fit parameter yc: y centroid of shapelets, fit parameter xc: x centroid of shapelets, fit parameter nmax: number of coefficents to use in the Laguerre polynomials im: observed image nm: noise map order: order of parameters fixed parameters: set_beta, set_phi, set_xc r: radius from centroid, array of im.shape, not required if xc and yc being fit th: angle from centroid, array of im.shape, not required if xc and yc being fit """ #determine which parameters are being fit for, and which are not beta0=set_beta[0] beta1=set_beta[1] phi=set_phi yc=set_xc[0] xc=set_xc[1] fitParams={'beta':False,'phi':False,'xc':False} for pid,paramName in enumerate(order): if paramName=='beta0': beta0=params[pid] fitParams['beta']=True elif paramName=='beta1': beta1=params[pid] fitParams['beta']=True elif paramName=='phi': phi=params[pid] fitParams['phi']=True elif paramName=='xc': xc=params[pid] fitParams['xc']=True elif paramName=='yc': yc=params[pid] fitParams['xc']=True #if beta0<0.: # print 'warning: beta going negative, setting to 0.0' # beta0=0. #if beta1<0.: # print 'warning: beta going negative, setting to 0.0' # beta1=0. if beta0<0.: print 'warning: beta going negative, taking absolute value' beta0 = np.abs(beta0) if beta1<0.: print 'warning: beta going negative, taking absolute value' beta1 = np.abs(beta1) print 'beta: (%f,%f)\t phi: %f\txc: (%f,%f)'%(beta0,beta1,phi,xc,yc) #update noise map nm=img.makeNoiseMap(nm.shape,np.mean(nm),np.std(nm)) size=im.shape if fitParams['xc'] or r is None: r,th=shapelet.polarArray([yc,xc],size) #the radius,theta pairs need to updated if fitting for the xc centre or if not using the r,th inputs bvals=genPolarBasisMatrix([beta0,beta1],nmax,phi,r,th) coeffs=solveCoeffs(bvals,im) mdl=np.abs(img.constructModel(bvals,coeffs,size)) return np.sum((im-mdl)**2 / nm**2)/(size[0]*size[1])
def chi2Func(params, nmax, im, nm, order=['beta0', 'beta1', 'phi', 'yc', 'xc'], set_beta=[None, None], set_phi=None, set_xc=[None, None], xx=None, yy=None): """Function which is to be minimized in the chi^2 analysis for Cartesian shapelets params = [beta0, beta1, phi, xc, yc] or some subset beta0: characteristic size of shapelets, fit parameter beta1: characteristic size of shapelets, fit parameter phi: rotation angle of shapelets, fit parameter yc: y centroid of shapelets, fit parameter xc: x centroid of shapelets, fit parameter nmax: number of coefficents to use in the Hermite polynomials im: observed image nm: noise map order: order of parameters fixed parameters: set_beta, set_phi, set_xc xx: X position grid, array of im.shape, not required if xc and yc being fit yy: Y position grid, array of im.shape, not required if xc and yc being fit """ #determine which parameters are being fit for, and which are not betaY = set_beta[0] betaX = set_beta[1] phi = set_phi yc = set_xc[0] xc = set_xc[1] fitParams = {'beta': False, 'phi': False, 'xc': False} for pid, paramName in enumerate(order): if paramName == 'beta0': betaY = params[pid] fitParams['beta'] = True elif paramName == 'beta1': betaX = params[pid] fitParams['beta'] = True elif paramName == 'phi': phi = params[pid] fitParams['phi'] = True elif paramName == 'xc': xc = params[pid] fitParams['xc'] = True elif paramName == 'yc': yc = params[pid] fitParams['xc'] = True #if betaX<0.: # print 'warning: beta going negative, setting to 0.0' # betaX=0. #if betaY<0.: # print 'warning: beta going negative, setting to 0.0' # betaY=0. if betaX < 0.: print 'warning: beta going negative, taking absolute value' betaX = np.abs(betaY) if betaY < 0.: print 'warning: beta going negative, taking absolute value' betaY = np.abs(betaX) print 'beta: (%f,%f)\t phi: %f\txc: (%f,%f)' % (betaX, betaY, phi, xc, yc) #update noise map nm = img.makeNoiseMap(nm.shape, np.mean(nm), np.std(nm)) size = im.shape if fitParams['xc'] or xx is None: #shift the (0,0) point to the centroid ry = np.array(range(0, size[0]), dtype=float) - yc rx = np.array(range(0, size[1]), dtype=float) - xc yy, xx = shapelet.xy2Grid(ry, rx) bvals = genBasisMatrix([betaY, betaX], nmax, phi, yy, xx) coeffs = solveCoeffs(bvals, im) mdl = img.constructModel(bvals, coeffs, size) return np.sum((im - mdl)**2 / nm**2) / (size[0] * size[1])
def chi2PolarFunc(params, nmax, im, nm, order=['beta0', 'beta1', 'phi', 'yc', 'xc'], set_beta=[None, None], set_phi=None, set_xc=[None, None], r=None, th=None): """Function which is to be minimized in the chi^2 analysis for Polar shapelets params = [beta0, beta1, phi, xc, yc] or some subset beta0: characteristic size of shapelets, fit parameter beta1: characteristic size of shapelets, fit parameter phi: rotation angle of shapelets, fit parameter yc: y centroid of shapelets, fit parameter xc: x centroid of shapelets, fit parameter nmax: number of coefficents to use in the Laguerre polynomials im: observed image nm: noise map order: order of parameters fixed parameters: set_beta, set_phi, set_xc r: radius from centroid, array of im.shape, not required if xc and yc being fit th: angle from centroid, array of im.shape, not required if xc and yc being fit """ #determine which parameters are being fit for, and which are not beta0 = set_beta[0] beta1 = set_beta[1] phi = set_phi yc = set_xc[0] xc = set_xc[1] fitParams = {'beta': False, 'phi': False, 'xc': False} for pid, paramName in enumerate(order): if paramName == 'beta0': beta0 = params[pid] fitParams['beta'] = True elif paramName == 'beta1': beta1 = params[pid] fitParams['beta'] = True elif paramName == 'phi': phi = params[pid] fitParams['phi'] = True elif paramName == 'xc': xc = params[pid] fitParams['xc'] = True elif paramName == 'yc': yc = params[pid] fitParams['xc'] = True #if beta0<0.: # print 'warning: beta going negative, setting to 0.0' # beta0=0. #if beta1<0.: # print 'warning: beta going negative, setting to 0.0' # beta1=0. if beta0 < 0.: print 'warning: beta going negative, taking absolute value' beta0 = np.abs(beta0) if beta1 < 0.: print 'warning: beta going negative, taking absolute value' beta1 = np.abs(beta1) print 'beta: (%f,%f)\t phi: %f\txc: (%f,%f)' % (beta0, beta1, phi, xc, yc) #update noise map nm = img.makeNoiseMap(nm.shape, np.mean(nm), np.std(nm)) size = im.shape if fitParams['xc'] or r is None: r, th = shapelet.polarArray( [yc, xc], size ) #the radius,theta pairs need to updated if fitting for the xc centre or if not using the r,th inputs bvals = genPolarBasisMatrix([beta0, beta1], nmax, phi, r, th) coeffs = solveCoeffs(bvals, im) mdl = np.abs(img.constructModel(bvals, coeffs, size)) return np.sum((im - mdl)**2 / nm**2) / (size[0] * size[1])