Ejemplo n.º 1
0
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])
Ejemplo n.º 2
0
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])
Ejemplo n.º 3
0
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])
Ejemplo n.º 4
0
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])
Ejemplo n.º 5
0
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])
Ejemplo n.º 6
0
    try:
        beta0,phi0=initBetaPhi(subim,mode='basic')
        print beta0, phi0
        beta0,phi0=initBetaPhi(subim,mode='fit')
        print beta0, phi0
    except:
        print 'Test failed (%i):'%tc, sys.exc_info()[0]
        te+=1

    #genPolarBasisMatrix(beta,nmax,r,th):
    #solveCoeffs(m,im):
    tc+=1
    try:
        beta0,phi0=initBetaPhi(subim,mode='fit')
        xc=img.maxPos(subim)
        r0,th0=shapelet.polarArray(xc,subim.shape)
        mPolar=genPolarBasisMatrix(beta0,5,0.,r0,th0)
        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]
Ejemplo n.º 7
0
from time import time
import cshapelet as csh
import fshapelet as fsh
import shapelet as sh
import decomp
import cProfile, pstats

USE_PROFILER = True

n = 16
m = 2
PB_DIM = (1000, 500)
xc = (800, 1300)
beta0 = 100.
mask = None
r, th = sh.polarArray(xc, PB_DIM)
print 'Problem dim:', PB_DIM

backend_list = {'original': decomp,
                'opt + numexpr': fsh,
                'opt + cython': csh
                 }

pr = {}

for backend, mod in backend_list.iteritems():
    if USE_PROFILER:
        pr[backend] = cProfile.Profile()
        pr[backend].enable()
    t = time()
    res0 = mod.genPolarBasisMatrix(beta0, n, r, th),
Ejemplo n.º 8
0
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])
Ejemplo n.º 9
0
    try:
        beta0, phi0 = initBetaPhi(subim, mode='basic')
        print beta0, phi0
        beta0, phi0 = initBetaPhi(subim, mode='fit')
        print beta0, phi0
    except:
        print 'Test failed (%i):' % tc, sys.exc_info()[0]
        te += 1

    #genPolarBasisMatrix(beta,nmax,r,th):
    #solveCoeffs(m,im):
    tc += 1
    try:
        beta0, phi0 = initBetaPhi(subim, mode='fit')
        xc = img.maxPos(subim)
        r0, th0 = shapelet.polarArray(xc, subim.shape)
        mPolar = genPolarBasisMatrix(beta0, 5, 0., r0, th0)
        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]
Ejemplo n.º 10
0
from time import time
import cshapelet as csh
import fshapelet as fsh
import shapelet as sh
import decomp
import cProfile, pstats

USE_PROFILER = True

n = 16
m = 2
PB_DIM = (1000, 500)
xc = (800, 1300)
beta0 = 100.
mask = None
r, th = sh.polarArray(xc, PB_DIM)
print('Problem dim:', PB_DIM)

backend_list = {'original': decomp, 'opt + numexpr': fsh, 'opt + cython': csh}

pr = {}

for backend, mod in backend_list.items():
    if USE_PROFILER:
        pr[backend] = cProfile.Profile()
        pr[backend].enable()
    t = time()
    res0 = mod.genPolarBasisMatrix(beta0, n, r, th),
    t = time() - t
    if USE_PROFILER:
        pr[backend].disable()
Ejemplo n.º 11
0
def test_polarArray():
    xc = (100., 200.)
    res0 = fsh.polarArray(xc, PB_DIM)
    res1 = sh.polarArray(xc, PB_DIM)
    yield assert_allclose, res0, res1, 1e-7, 1e-9
Ejemplo n.º 12
0
def setup():
    global r, th, xc, beta, PB_DIM
    xc = (100, 130)
    beta = 100.
    PB_DIM = (300, 200)
    r, th = sh.polarArray(xc, PB_DIM)