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])
xc=img.maxPos(subim) rx=np.array(range(0,subim.shape[0]),dtype=float)-xc[0] ry=np.array(range(0,subim.shape[1]),dtype=float)-xc[1] xx,yy=shapelet.xy2Grid(rx,ry) mCart=genBasisMatrix(beta0,[5,5],phi0,xx,yy) coeffs=solveCoeffs(mCart,subim) print coeffs if write_files: fileio.writeHermiteCoeffs('testHermite.pkl',coeffs,xc,subim.shape,beta0,0.,5,pos=[hdr['ra'],hdr['dec'],hdr['dra'],hdr['ddec']],info='Test Hermite coeff file') except: print 'Test failed (%i):'%tc, sys.exc_info()[0] te+=1 beta0,phi0=initBetaPhi(subim,mode='fit') xc=img.maxPos(subim) mean,std=img.estimateNoise(subim,mode='basic') nm=img.makeNoiseMap(subim.shape,mean,std) nmax=[10,10] #chi2PolarFunc(params,nmax,im,nm): tc+=1 try: print chi2PolarFunc([beta0[0],beta0[1],phi0,xc[0],xc[1]],nmax,subim,nm,order=['beta0','beta1','phi','xc','yc']) #fit: all print chi2PolarFunc([beta0[0],beta0[1]],nmax,subim,nm,order=['beta0','beta1'],set_phi=phi0,set_xc=xc) #fit: beta print chi2PolarFunc([phi0],nmax,subim,nm,order=['phi'],set_beta=beta0,set_xc=xc) #fit: phi print chi2PolarFunc([xc[0],xc[1]],nmax,subim,nm,order=['xc','yc'],set_beta=beta0,set_phi=phi0) #fit: xc print chi2PolarFunc([beta0[0],beta0[1],phi0],nmax,subim,nm,order=['beta0','beta1','phi'],set_xc=xc) #fit: beta, phi print chi2PolarFunc([beta0[0],beta0[1],xc[0],xc[1]],nmax,subim,nm,order=['beta0','beta1','xc','yc'],set_phi=phi0) #fit: beta, xc print chi2PolarFunc([phi0,xc[0],xc[1]],nmax,subim,nm,order=['phi','xc','yc'],set_beta=beta0) #fit: phi, xc except: print 'Test failed (%i):'%tc, sys.exc_info()[0] te+=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])
coeffs, xc, subim.shape, beta0, 0., 5, pos=[hdr['ra'], hdr['dec'], hdr['dra'], hdr['ddec']], info='Test Hermite coeff file') except: print 'Test failed (%i):' % tc, sys.exc_info()[0] te += 1 beta0, phi0 = initBetaPhi(subim, mode='fit') xc = img.maxPos(subim) mean, std = img.estimateNoise(subim, mode='basic') nm = img.makeNoiseMap(subim.shape, mean, std) nmax = [10, 10] #chi2PolarFunc(params,nmax,im,nm): tc += 1 try: print chi2PolarFunc([beta0[0], beta0[1], phi0, xc[0], xc[1]], nmax, subim, nm, order=['beta0', 'beta1', 'phi', 'xc', 'yc']) #fit: all print chi2PolarFunc([beta0[0], beta0[1]], nmax, subim, nm,