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 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])
coeffs=solveCoeffs(mPolar,subim) print coeffs if write_files: fileio.writeLageurreCoeffs('testLageurre.pkl',coeffs,xc,subim.shape,beta0,0.,[5,5],pos=[hdr['ra'],hdr['dec'],hdr['dra'],hdr['ddec']],info='Test Lageurre coeff file') except: print 'Test failed (%i):'%tc, sys.exc_info()[0] te+=1 #genBasisMatrix(beta,nmax,rx,ry): #solveCoeffs(m,im): tc+=1 try: beta0,phi0=initBetaPhi(subim,mode='fit') 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):
mean, std = estimateNoise(im, mode='basic') nm = makeNoiseMap(im.shape, mean, std) print nm.shape except: print 'Test failed (%i):' % tc, sys.exc_info()[0] te += 1 #constructModel(bvals,coeffs,size): tc += 1 try: shapeDict = fileio.readLageurreCoeffs('../data/testHermite.pkl') rx = np.array(range(0, shapeDict['size'][0]), dtype=float) - shapeDict['xc'][0] ry = np.array(range(0, shapeDict['size'][1]), dtype=float) - shapeDict['xc'][1] xx, yy = shapelet.xy2Grid(rx, ry) bvals = decomp.genBasisMatrix(shapeDict['beta'], shapeDict['norder'], shapeDict['phi'], xx, yy) mdl = constructModel(bvals, shapeDict['coeffs'], shapeDict['size']) print mdl.shape except: print 'Test failed (%i):' % tc, sys.exc_info()[0] te += 1 #polarCoeffImg(coeffs,nmax): tc += 1 try: shapeDict = fileio.readLageurreCoeffs('../data/testLageurre.pkl') cim = polarCoeffImg(shapeDict['coeffs'].real, shapeDict['norder']) print cim except:
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])