Beispiel #1
0
def evolvePDF(bestfitloc='posteriorpdf.fits', stepsize=50000):
    """

    Plot the evolution of the PDF of each parameter of the model.

    """

    import setuputil

    # Get upper and lower limits on the parameters to set the plot limits
    paramData = setuputil.loadParams(config)
    p_u = paramData['p_u']
    p_l = paramData['p_l']
    limits = [p_l, p_u]

    # read posterior PDF
    fitresults = fits.getdata(bestfitloc)
    nresults = len(fitresults)
    print("Output from emcee has = " + str(nresults) + " iterations.")
    start = 0
    for iresult in range(0, nresults, stepsize):

        strstep = str(stepsize)
        nchar = len(str(nresults))
        striresult = str(iresult).zfill(nchar)
        tag = 'evolution' + strstep + '.' + striresult + '.'
        trimresults = fitresults[start:start + stepsize]
        start += stepsize
        visualutil.plotPDF(trimresults,
                           tag,
                           limits=limits,
                           Ngood=1000,
                           axes='initial')
Beispiel #2
0
def evolvePDF(bestfitloc='posteriorpdf.fits', stepsize=50000):

    """

    Plot the evolution of the PDF of each parameter of the model.

    """

    import setuputil


    # Get upper and lower limits on the parameters to set the plot limits
    paramData = setuputil.loadParams(config)
    p_u = paramData['p_u']
    p_l = paramData['p_l']
    limits = [p_l, p_u]

    # read posterior PDF
    fitresults = fits.getdata(bestfitloc)
    nresults = len(fitresults)
    print("Output from emcee has = " + str(nresults) + " iterations.")
    start = 0
    for iresult in range(0, nresults, stepsize):

        strstep = str(stepsize)
        nchar = len(str(nresults))
        striresult = str(iresult).zfill(nchar)
        tag = 'evolution' + strstep + '.' + striresult + '.'
        trimresults = fitresults[start:start + stepsize]
        start += stepsize
        visualutil.plotPDF(trimresults, tag, limits=limits, Ngood=1000, 
                axes='initial')
Beispiel #3
0
def plot(cleanup=True, configloc='sandbox.yaml', interactive=True, threshold=1.2, plot=True, tag='sandbox'):
    '''

    Parameters
    ----------
    threshold: float
        in mJy, cleaning threshold

    '''

    # read the input parameters
    configfile = open(configloc, 'r')
    config = yaml.load(configfile)

    paramSetup = setuputil.loadParams(config)
    fixindx = setuputil.fixParams(paramSetup)
    testfit = paramSetup['p_l']
    visfile = config['UVData']
    uuu, vvv, www = uvutil.uvload(visfile)
    pcd = uvutil.pcdload(visfile)
    vis_complex, wgt = uvutil.visload(visfile)

    # remove the data points with zero or negative weight
    positive_definite = wgt > 0
    vis_complex = vis_complex[positive_definite]
    wgt = wgt[positive_definite]
    uuu = uuu[positive_definite]
    vvv = vvv[positive_definite]

    testlnprob, testmu = lnprob(testfit, vis_complex, wgt, uuu, vvv, pcd,
           fixindx, paramSetup, computeamp=True)
    # prepend 1 dummy value to represent lnprob
    testfit = numpy.append(testlnprob, testfit)
    # append nmu dummy values that represent the magnification factors
    nlensedsource = paramSetup['nlensedsource']
    nlensedregions = paramSetup['nlensedregions']
    nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)

    for i in range(nmu):
        testfit = numpy.append(testfit, 0)

    print("lnprob: %f" %testlnprob)
    print("Using the following model parameters:")
    for k, v in zip(paramSetup['pname'], testfit[1:-4]):
        print("%s : %.4f" %(k,v))
    if plot:
        visualutil.plotFit(config, testfit, threshold,
                           tag=tag, cleanup=cleanup,
                           interactive=interactive)
    return testlnprob
Beispiel #4
0
def plot(cleanup=True, configloc='sandbox.yaml', interactive=True):

    # read the input parameters
    configfile = open(configloc, 'r')
    config = yaml.load(configfile)

    paramSetup = setuputil.loadParams(config)
    fixindx = setuputil.fixParams(paramSetup)
    testfit = paramSetup['p_l']
    visfile = config['UVData']
    uuu, vvv, www = uvutil.uvload(visfile)
    pcd = uvutil.pcdload(visfile)
    vis_complex, wgt = uvutil.visload(visfile)

    # remove the data points with zero or negative weight
    positive_definite = wgt > 0
    vis_complex = vis_complex[positive_definite]
    wgt = wgt[positive_definite]
    uuu = uuu[positive_definite]
    vvv = vvv[positive_definite]

    testlnprob, testmu = lnprob(testfit,
                                vis_complex,
                                wgt,
                                uuu,
                                vvv,
                                pcd,
                                fixindx,
                                paramSetup,
                                computeamp=True)
    # prepend 1 dummy value to represent lnprob
    testfit = numpy.append(testlnprob, testfit)
    # append nmu dummy values that represent the magnification factors
    nlensedsource = paramSetup['nlensedsource']
    nlensedregions = paramSetup['nlensedregions']
    nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)

    for i in range(nmu):
        testfit = numpy.append(testfit, 0)
    tag = 'sandbox'

    print("lnprob: %f" % testlnprob)
    print("Using the following model parameters:")
    for k, v in zip(paramSetup['pname'], testfit[1:-4]):
        print("%s : %.4f" % (k, v))
    visualutil.plotFit(config,
                       testfit,
                       tag=tag,
                       cleanup=cleanup,
                       interactive=interactive)
Beispiel #5
0
def makeSBmap(config, fitresult):
    """

    Make a surface brightness map of the lensed image for a given set of model
    parameters.

    """

    import lensutil
    from astropy.io import fits
    import os
    import setuputil
    import re
    import numpy

    # Loop over each region

    # read the input parameters
    paramData = setuputil.loadParams(config)
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    SBmap_all = 0
    LensedSBmap_all = 0
    nregion = len(regionlist)
    for regioni in range(nregion):
        regstring = 'Region' + str(regioni)
        #indx = paramData['regionlist'].index(regstring)
        cr = config[regstring]

        nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
        if nmu > 0:
            allparameters0 = list(fitresult)[1:-nmu]
        else:
            allparameters0 = list(fitresult)[1:]

        # search poff_models for parameters fixed relative to other parameters
        fixindx = setuputil.fixParams(paramData)
        poff = paramData['poff']
        ndim_total = len(poff)
        fixed = (numpy.where(fixindx >= 0))[0]
        nfixed = fixindx[fixed].size
        parameters_offset = numpy.zeros(ndim_total)
        for ifix in range(nfixed):
            ifixed = fixed[ifix]
            subindx = fixindx[ifixed]
            par0 = 0
            if fixindx[subindx] > 0:
                par0 = fitresult[fixindx[subindx] + 1]
            parameters_offset[ifixed] = fitresult[subindx + 1] + par0

        allparameters = allparameters0 + parameters_offset

        # count the number of lenses
        configkeys = cr.keys()
        configkeystring = " ".join(configkeys)
        lenslist = re.findall('Lens.', configkeystring)
        nlens = len(lenslist)

        # count the number of sources
        sourcelist = re.findall('Source.', configkeystring)
        nsource = len(sourcelist)

        nparperlens = 5
        nparpersource = 6
        nparlens = nparperlens * nlens
        nparsource = nparpersource * nsource
        npar = nparlens + nparsource + npar_previous
        parameters = allparameters[npar_previous:npar]
        npar_previous = npar

        #nlens = paramData['nlens_regions'][indx]
        #nsource = paramData['nsource_regions'][indx]
        x = paramData['x'][regioni]
        y = paramData['y'][regioni]
        modelheader = paramData['modelheader'][regioni]
        model_types = paramData['model_types'][regioni]

        SBmap, LensedSBmap, Aperture, LensedAperture, mu_tot, mu_mask = \
                lensutil.sbmap(x, y, nlens, nsource, parameters, model_types, \
                computeamp=True)

        caustics = False
        if caustics:
            deltapar = parameters[0:nparlens + nparpersource]
            refine = 2
            nx = x[:, 0].size * refine
            ny = y[:, 0].size * refine
            x1 = x[0, :].min()
            x2 = x[0, :].max()
            linspacex = numpy.linspace(x1, x2, nx)
            y1 = y[:, 0].min()
            y2 = y[:, 0].max()
            linspacey = numpy.linspace(y1, y2, ny)
            onex = numpy.ones(nx)
            oney = numpy.ones(ny)
            finex = numpy.outer(oney, linspacex)
            finey = numpy.outer(linspacey, onex)

            mumap = numpy.zeros([ny, nx])
            for ix in range(nx):
                for iy in range(ny):
                    deltapar[-nparpersource + 0] = finex[ix, iy]
                    deltapar[-nparpersource + 1] = finey[ix, iy]
                    xcell = paramData['celldata']
                    deltapar[-nparpersource + 2] = xcell
                    deltaunlensed, deltalensed, A1, A2, mu_xy, mu_xymask = \
                            lensutil.sbmap(finex, finey, nlens, 1, deltapar, ['Delta'])
                    mumap[ix, iy] = mu_xy[0]

            import matplotlib.pyplot as plt
            plt.imshow(mumap, origin='lower')
            plt.contour(mumap, levels=[mumap.max() / 1.1])
            import pdb
            pdb.set_trace()
        SBmap_all += SBmap
        LensedSBmap_all += LensedSBmap

    LensedSBmapLoc = 'LensedSBmap.fits'
    SBmapLoc = 'SBmap_Region.fits'
    cmd = 'rm -rf ' + LensedSBmapLoc + ' ' + SBmapLoc
    os.system(cmd)

    fits.writeto(LensedSBmapLoc, LensedSBmap_all, modelheader)
    fits.writeto(SBmapLoc, SBmap_all, modelheader)

    return
Beispiel #6
0
def plotImage(model, data, config, modeltype, fitresult, tag=''):
    """

    Make a surface brightness map of a given model image.  Overlay with red
    contours a surface brightness map of the data image to which the model was
    fit. 

    """

    import numpy
    from astropy import wcs
    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse
    from pylab import savefig
    import setuputil
    import re

    # set font properties
    font = {'family': 'Arial Narrow', 'weight': 'bold', 'size': 10}
    matplotlib.rc('font', **font)
    matplotlib.rcParams['axes.linewidth'] = 1.5

    fig = plt.figure(figsize=(3.0, 3.0))
    ax = fig.add_subplot(1, 1, 1)
    plt.subplots_adjust(left=0.08,
                        right=0.97,
                        top=0.97,
                        bottom=0.08,
                        wspace=0.35)
    paramData = setuputil.loadParams(config)
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    nregion = len(regionlist)
    for iregion in range(nregion):
        region = 'Region' + str(iregion)
        cr = config[region]

        ra_centroid = cr['RACentroid']
        dec_centroid = cr['DecCentroid']
        radialextent = cr['RadialExtent']

        nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
        if nmu > 0:
            allparameters0 = list(fitresult)[1:-nmu]
        else:
            allparameters0 = list(fitresult)[1:]

        # search poff_models for parameters fixed relative to other parameters
        fixindx = setuputil.fixParams(paramData)
        poff = paramData['poff']
        ndim_total = len(poff)
        fixed = (numpy.where(fixindx >= 0))[0]
        nfixed = fixindx[fixed].size
        parameters_offset = numpy.zeros(ndim_total)
        for ifix in range(nfixed):
            ifixed = fixed[ifix]
            subindx = fixindx[ifixed]
            par0 = 0
            if fixindx[subindx] > 0:
                par0 = fitresult[fixindx[subindx] + 1]
            parameters_offset[ifixed] = fitresult[subindx + 1] + par0

        allparameters = allparameters0 + parameters_offset

        # count the number of lenses
        configkeys = cr.keys()
        configkeystring = " ".join(configkeys)
        lenslist = re.findall('Lens.', configkeystring)
        nlens = len(lenslist)

        # count the number of sources
        sourcelist = re.findall('Source.', configkeystring)
        nsource = len(sourcelist)

        nparlens = 5 * nlens
        nparsource = 6 * nsource
        npar = nparlens + nparsource + npar_previous
        parameters = allparameters[npar_previous:npar]
        npar_previous = npar

        for i in range(nsource):
            i6 = i * 6
            xxx = parameters[i6 + 2 + nparlens]
            yyy = parameters[i6 + 3 + nparlens]
            source_pa = 90 - parameters[i6 + 5 + nparlens]
            #model_type = model_types[i]
            #if model_type == 'gaussian':
            norm = 2.35
            #if model_type == 'cylinder':
            #    norm = numpy.sqrt(2)
            meansize = norm * parameters[i6 + 1 + nparlens]
            source_bmaj = meansize / numpy.sqrt(parameters[i6 + 4 + nparlens])
            source_bmin = meansize * numpy.sqrt(parameters[i6 + 4 + nparlens])
            e = Ellipse((xxx, yyy), source_bmaj, source_bmin, \
                    angle=source_pa, ec='white', lw=0.5, fc='magenta', \
                    zorder=2, fill=True, alpha=0.5)
            ax.add_artist(e)
            if config.keys().count('xtextoff') > 0:
                xytext = (xxx + config['xtextoff'][i],
                          yyy + config['ytextoff'][i])
                if iregion == 0:
                    if modeltype != 'optical':
                        plt.annotate(str(i),
                                     xy=(xxx, yyy),
                                     xytext=xytext,
                                     fontsize='xx-large')
                else:
                    xytext = (xxx + config['xtextoff'][i + 2],
                              yyy + config['ytextoff'][i + 2])
                    if modeltype != 'optical':
                        plt.annotate(str(i + 2),
                                     xy=(xxx, yyy),
                                     xytext=xytext,
                                     fontsize='xx-large')

        for i in range(nlens):
            i5 = i * 5
            xxx = numpy.array([parameters[i5 + 1]])
            yyy = numpy.array([parameters[i5 + 2]])
            plt.plot(xxx, yyy, 'o', ms=5., mfc='black', mec='white', mew=0.5, \
                label='Lens Position', zorder=20)
            lens_pa = 90 - parameters[i5 + 4]
            meansize = 2 * parameters[i5]
            lens_bmaj = meansize / numpy.sqrt(parameters[i5 + 3])
            lens_bmin = meansize * numpy.sqrt(parameters[i5 + 3])
            elens = Ellipse((xxx, yyy), lens_bmaj, lens_bmin, \
                    angle=lens_pa, ec='orange', lw=1.0, \
                    zorder=20, fill=False)
            ax.add_artist(elens)

    # get the image centroid in model pixel coordinates
    headim = data[0].header
    headmod = model[0].header
    im = data[0].data
    im = im[0, 0, :, :]

    # good region is where mask is zero
    mask = setuputil.makeMask(config)
    goodregion = mask == 0

    # compute sigma image from cutout of SMA flux image
    # dv = 1.
    # bunit = headim['BUNIT']
    # if bunit == 'JY/BEAM.KM/S':
    #     dv = 500.
    rms = im[goodregion].std()  # * dv

    # Obtain measurements of beamsize and image min/max
    bmaj = headim['BMAJ'] * 3600
    bmin = headim['BMIN'] * 3600
    bpa = headim['BPA']
    cdelt1 = headim['CDELT1'] * 3600
    cdelt2 = headim['CDELT2'] * 3600
    cell = numpy.sqrt(abs(cdelt1) * abs(cdelt2))

    im_model = model[0].data
    if im_model.ndim == 4:
        im_model = im_model[0, 0, :, :]
    #nx_model = im_model[0, :].size
    pixextent = radialextent / cell
    datawcs = wcs.WCS(headim, naxis=2)
    pix = datawcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
    x0 = numpy.round(pix[0])
    y0 = numpy.round(pix[1])
    imrady = numpy.round(radialextent / cell)  # nymod / 2.
    imradx = numpy.round(radialextent / cell)  # nxmod / 2.

    # make data cutout
    totdx1 = x0 - imradx
    totdx2 = x0 + imradx
    totdy1 = y0 - imrady
    totdy2 = y0 + imrady
    datacut = im[totdy1:totdy2, totdx1:totdx2]

    # make cleaned model cutout
    headerkeys = headmod.keys()
    cd1_1 = headerkeys.count('CD1_1')
    cd1_2 = headerkeys.count('CD1_2')
    if cd1_1 == 0:
        cdelt1_model = numpy.abs(headmod['CDELT1'] * 3600)
        cdelt2_model = numpy.abs(headmod['CDELT2'] * 3600)
    else:
        cdelt1_model = numpy.abs(headmod['CD1_1'] * 3600)
        cdelt2_model = numpy.abs(headmod['CD2_2'] * 3600)
        cd11 = headmod['CD1_1']
        if cd1_2 == 0:
            cd12 = 0
            cd21 = 0
        else:
            cd12 = headmod['CD1_2']
            cd21 = headmod['CD2_1']
        cd22 = headmod['CD2_2']
        cdelt1_model = numpy.sqrt(cd11**2 + cd12**2) * 3600
        cdelt2_model = numpy.sqrt(cd21**2 + cd22**2) * 3600
        if cd12 == 0:
            cd12 = cd11 / 1e8
        cdratio = numpy.abs(cd11 / cd12)
        if cdratio < 1:
            cdratio = 1 / cdratio
    cellmod = numpy.sqrt(abs(cdelt1_model) * abs(cdelt2_model))

    modelwcs = wcs.WCS(headmod, naxis=2)
    pix = modelwcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
    x0 = numpy.round(pix[0])
    y0 = numpy.round(pix[1])
    modrady = numpy.round(radialextent / cellmod)
    modradx = numpy.round(radialextent / cellmod)
    totdx1 = x0 - modradx
    totdx2 = x0 + modradx
    totdy1 = y0 - modrady
    totdy2 = y0 + modrady
    modelcut = im_model[totdy1:totdy2, totdx1:totdx2]

    #cellp = cell * (2 * pixextent + 1.1) / (2 * pixextent)
    xlo = -radialextent
    xhi = radialextent
    ylo = -radialextent
    yhi = radialextent
    ncell = modelcut[:, 0].size  #(xhi - xlo) / cell
    modx = -numpy.linspace(xlo, xhi, ncell)
    mody = numpy.linspace(ylo, yhi, ncell)
    #modx = -(numpy.arange(2 * pixextent) - pixextent) * cellp - cell/2.
    #mody = (numpy.arange(2 * pixextent) - pixextent) * cellp + cell/2.
    cornerextent = [modx[0], modx[-1], mody[0], mody[-1]]
    if modeltype == 'residual':
        grayscalename = 'Residual'
        pcolor = 'white'
        ncolor = 'black'
        vmax = 5 * rms
        vmin = -5 * rms
    elif modeltype == 'model':
        grayscalename = 'Model'
        pcolor = 'red'
        ncolor = 'red'
        vmax = modelcut.max()
        vmin = modelcut.min()
    else:
        grayscalename = config['OpticalTag']
        filtindx = grayscalename.find(' ')
        filtname = grayscalename[filtindx + 1:]
        if filtname == 'F110W':
            modelcut = numpy.log10(modelcut - modelcut.min() + 1)
        pcolor = 'red'
        ncolor = 'red'
        vmax = modelcut.max()
        vmin = modelcut.min()
    plt.imshow(modelcut, cmap='gray_r', interpolation='nearest', \
            extent=cornerextent, origin='lower', vmax=vmax, vmin=vmin)

    plevs = 3 * rms * 2**(numpy.arange(10))
    nlevs = sorted(-3 * rms * 2**(numpy.arange(4)))
    pcline = 'solid'
    ncline = 'dashed'
    #nx_contour = datacut[0, :].size
    #ny_contour = datacut[:, 0].size
    #cmodx = -(numpy.arange(nx_contour) - pixextent) * cellp - cell/2.
    #cmody = (numpy.arange(ny_contour) - pixextent) * cellp + cell/2.
    ncell = datacut[:, 0].size  #(xhi - xlo) / cell
    cmodx = -numpy.linspace(xlo, xhi, ncell)
    cmody = numpy.linspace(ylo, yhi, ncell)
    plt.contour(cmodx, cmody, datacut, colors=pcolor, levels=plevs, \
            linestyles=pcline, linewidths=1.5)
    plt.contour(cmodx, cmody, datacut, colors=ncolor, levels=nlevs, \
            linestyles=ncline, linewidths=1.5)

    # plot the critical curve
    #plt.contour(cmodx, cmody, dmu, colors='orange', levels=[100])
    #axisrange = plt.axis()
    axisrange = numpy.array([xhi, xlo, ylo, yhi]).astype(float)
    plt.axis(axisrange)

    plt.minorticks_on()
    plt.tick_params(width=1.5, which='both')
    plt.tick_params(length=2, which='minor')
    plt.tick_params(length=4, which='major')
    #plt.xlabel(r'$\Delta$RA (arcsec)', fontsize='x-large')
    #plt.ylabel(r'$\Delta$Dec (arcsec)', fontsize='x-large')

    bparad = bpa / 180 * numpy.pi
    beamx = numpy.abs(numpy.sin(bparad) * bmaj) + \
            numpy.abs(numpy.cos(bparad) * bmin)
    beamy = numpy.abs(numpy.cos(bparad) * bmaj) + \
            numpy.abs(numpy.sin(bparad) * bmin)
    beamxhi = 2 * pixextent / cell
    beamxlo = -2 * pixextent / cell
    beamyhi = 2 * pixextent / cell
    beamylo = -2 * pixextent / cell
    beamdx = numpy.float(beamxhi) - numpy.float(beamxlo)
    beamdy = numpy.float(beamyhi) - numpy.float(beamylo)
    bufferx = 0.03 * beamdx / 6.0
    buffery = 0.03 * beamdx / 6.0
    xpos = 1 - beamx / beamdx / 2 - bufferx
    ypos = beamy / beamdy / 2 + buffery
    #beamx = bmaj * numpy.abs(numpy.cos(bparad))
    #beamy = bmaj * numpy.abs(numpy.sin(bparad))

    xpos = 0.95 * axisrange[1] + 0.95 * beamx / 2.
    ypos = 0.95 * axisrange[2] + 0.95 * beamy / 2.

    e = Ellipse((xpos,ypos), bmaj, bmin, angle=90 - bpa, ec='black', \
        hatch='//////', lw=1.0, fc='None', zorder=10, fill=True)
    ax.add_artist(e)

    plt.text(0.92,
             0.88,
             grayscalename,
             transform=ax.transAxes,
             fontsize='xx-large',
             ha='right')
    try:
        from astropy.table import Table
        tloc = '../../../Papers/Bussmann_2015a/Bussmann2015/Data/targetlist.dat'
        hackstep = Table.read(tloc, format='ascii')
        objname = config['ObjectName']
        match = hackstep['dataname'] == objname
        shortname = hackstep['shortname'][match][0]
        plt.text(0.08,
                 0.88,
                 shortname,
                 transform=ax.transAxes,
                 fontsize='xx-large')
    except:
        pass
    bigtag = '.' + modeltype + '.' + tag

    savefig('LensedSBmap' + bigtag + '.pdf')
    fitdir = anloc + dataname[itarg] + '/' + goodfit + '/'
    os.chdir(fitdir)

    # ------------------------------------------------------------------------
    # Read posterior probability distributions
    fitloc = 'posteriorpdf.fits'
    fitresults = Table.read(fitloc)
    fitresults = fitresults[-5000:]
    fitresultsgood = modifypdf.cleanColumns(fitresults)
    fitresultsgood = modifypdf.bigScoop(fitresultsgood)
    fitresultsgood = modifypdf.prune(fitresultsgood, quiet=True)
    nfits = len(fitresultsgood)

    configfile = open('config.yaml')
    config = yaml.load(configfile)
    paramData = setuputil.loadParams(config)

    if objname != objname_previous:
        radeg_centroid = config['Region0']['RACentroid']
        decdeg_centroid = config['Region0']['DecCentroid']
        c = SkyCoord(ra=radeg_centroid*u.degree, 
                dec=decdeg_centroid*u.degree)
        fullRA = c.ra.to_string(unit='hour', pad=True, sep=':', precision=3)
        fullDec = c.dec.to_string('deg', pad=True, alwayssign=True, sep=':', precision=2)
        msg = '${0:9}$ ${1:12}$ ${2:12}$'
        msgfmt = msg.format(objname, fullRA, fullDec)
        #print(msgfmt)
    objname_previous = objname

    nregions = paramData['nregions']
def all(goodfit, update=False, mu_estimate=False):

    # Check to see whether we've already loaded the data
    if not update:
        ffile = 'flux_total_observed.dat'
        if os.path.exists(ffile):
            ffile = 'flux_total_observed.dat'
            flux_total_observed = Table.read(ffile, format='ascii')
            ffile = 'flux_indiv_observed.dat'
            flux_indiv_observed = Table.read(ffile, format='ascii')
            ffile = 'flux_indiv_intrinsic.dat'
            flux_indiv_intrinsic = Table.read(ffile, format='ascii')
        else:
            update = True
    if update:
        cwd = os.getcwd()

        # read in the sample targets
        anloc = '../../ModelFits/'
        dataphotloc = 'targetlist.dat'
        dataphot = Table.read(dataphotloc, format='ascii')
        dataname = dataphot['dataname']
        ntarg = dataname.size
        dataflag = numpy.zeros(ntarg)
        for i in range(ntarg):
            datanamei = dataname[i]
            dataloc = anloc + datanamei + '/' + goodfit
            if os.path.exists(dataloc):
                dataflag[i] = 1

        yesmodel = dataflag == 1
        dataphot = dataphot[yesmodel]
        dataname = dataphot['dataname']
        ntarg = len(dataphot)

        dataname_indiv = []
        flux_total = []
        e_flux_total = []
        flux_indiv = []
        e_flux_indiv = []
        flux_indiv_int = []
        e_flux_indiv_int = []
        for itarg in range(0, ntarg):

            # extract object name from directory
            objname = dataname[itarg]
            iauname = dataphot['iauname'][itarg]
            fitdir = anloc + dataname[itarg] + '/' + goodfit + '/'
            os.chdir(fitdir)

            # ------------------------------------------------------------------------
            # Read posterior probability distributions
            fitloc = 'posteriorpdf.fits'
            fitresults = Table.read(fitloc)
            fitresults = fitresults[-5000:]
            fitresultsgood = modifypdf.cleanColumns(fitresults)
            fitresultsgood = modifypdf.bigScoop(fitresultsgood)
            fitresultsgood = modifypdf.prune(fitresultsgood, quiet=True)

            configfile = open('config.yaml')
            config = yaml.load(configfile)
            paramData = setuputil.loadParams(config)

            nregions = paramData['nregions']

            flux_target = 0

            os.chdir(cwd)
            for iregion in range(nregions):
                sr = str(iregion)
                nsource = paramData['nsource_regions'][iregion]
                for isource in range(nsource):
                    ss = str(isource)
                    datanamesource = dataname[itarg] + '.Source' + ss
                    tag1 = 'IntrinsicFlux_Source' + ss + '_Region' + sr
                    if tag1 not in fitresults.keys():
                        tag1 = 'Region' + sr + ' Source' + ss  + ' IntrinsicFlux'
                    fluxdist = fitresultsgood[tag1]
                    tag2 = 'mu_tot.Source' + ss + '.Region' + sr

                    # get the by-eye magnification estimates
                    if mu_estimate:
                        datamu = Table.read('mu_estimate.dat', format='ascii')
                        muindx = datamu['target'] == datanamesource
                        muerr = datamu['muerr'][muindx][0]
                        if muerr == 0.25:
                            mu_by_eye = datamu['mu'][muindx][0]
                        else:
                            mu_by_eye = 1.
                    else:
                        mu_by_eye = 1.
                    flux_indiv_int.append(fluxdist.mean() / mu_by_eye)
                    e_flux_indiv_int.append(fluxdist.std() / mu_by_eye)
                    if tag2 in fitresults.keys():
                        if fitresultsgood[tag2].mean() == 100:
                            continue
                        observedflux = fluxdist * fitresultsgood[tag2]
                        print(datanamesource,
                                fitresultsgood[tag2].mean(),
                                fitresultsgood[tag2].std())
                    else:
                        observedflux = fluxdist
                        print(datanamesource, 1.0)
                    flux_target += observedflux
                    flux_indiv.append(observedflux.mean())
                    e_flux_indiv.append(observedflux.std())
                    dataname_indiv.append(datanamesource)
                
            flux_measure = flux_target.mean()
            flux_total.append(flux_measure)
            flux_error = flux_target.std()
            e_flux_total.append(flux_error)
            #import matplotlib.pyplot as plt
            #plt.hist(flux_target)
            #plt.show()
            #import pdb; pdb.set_trace()
            #print(objname, flux_measure, ' +/- ', flux_error)

        flux_total_observed = {'targets': dataname, 'fnu': flux_total, 
                'e_fnu': e_flux_total}
        t1 = Table(flux_total_observed)
        t1.write('flux_total_observed.dat', format='ascii')
        flux_indiv_observed = {'targets': dataname_indiv, 'fnu': flux_indiv, 
                'e_fnu': e_flux_indiv}
        t1 = Table(flux_indiv_observed)
        t1.write('flux_indiv_observed.dat', format='ascii')
        flux_indiv_intrinsic = {'targets': dataname_indiv, 'fnu': flux_indiv_int, 
                'e_fnu': e_flux_indiv_int}
        t1 = Table(flux_indiv_intrinsic)
        t1.write('flux_indiv_intrinsic.dat', format='ascii')

        #import matplotlib.pyplot as plt
        #m1 = 0
        #m2 = 1 + int(numpy.array(flux_total + flux_indiv + flux_indiv_int).max())
        #binwidth = 2
        #bins = numpy.arange(m1, m2 + binwidth, binwidth)
        #plt.hist(flux_total, bins = bins, histtype='stepfilled')
        #binwidth = 1.8
        #bins = numpy.arange(m1, m2 + binwidth, binwidth)
        #plt.hist(flux_indiv, bins = bins, alpha=0.5, histtype='stepfilled')
        #binwidth = 1.6
        #bins = numpy.arange(m1, m2 + binwidth, binwidth)
        #plt.hist(flux_indiv_int, bins = bins, alpha=0.2, histtype='stepfilled')
        #plt.show()
    return flux_total_observed, flux_indiv_observed, flux_indiv_intrinsic
Beispiel #9
0
def makeSBmap(config, fitresult):

    """

    Make a surface brightness map of the lensed image for a given set of model
    parameters.

    """

    import lensutil
    from astropy.io import fits
    import os
    import setuputil
    import re
    import numpy


    # Loop over each region

    # read the input parameters
    paramData = setuputil.loadParams(config)
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    SBmap_all = 0
    LensedSBmap_all = 0
    nregion = len(regionlist)
    for regioni in range(nregion):
        regstring = 'Region' + str(regioni)
        #indx = paramData['regionlist'].index(regstring)
        cr = config[regstring]

        nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
        if nmu > 0:
            allparameters0 = list(fitresult)[1:-nmu]
        else:
            allparameters0 = list(fitresult)[1:]

        # search poff_models for parameters fixed relative to other parameters
        fixindx = setuputil.fixParams(paramData)
        poff = paramData['poff']
        ndim_total = len(poff)
        fixed = (numpy.where(fixindx >= 0))[0]
        nfixed = fixindx[fixed].size
        parameters_offset = numpy.zeros(ndim_total)
        for ifix in range(nfixed):
            ifixed = fixed[ifix]
            subindx = fixindx[ifixed]
            par0 = 0
            if fixindx[subindx] > 0:
                par0 = fitresult[fixindx[subindx] + 1]
            parameters_offset[ifixed] = fitresult[subindx + 1] + par0

        allparameters = allparameters0 + parameters_offset

        # count the number of lenses
        configkeys = cr.keys()
        configkeystring = " ".join(configkeys)
        lenslist = re.findall('Lens.', configkeystring)
        nlens = len(lenslist)

        # count the number of sources
        sourcelist = re.findall('Source.', configkeystring)
        nsource = len(sourcelist)

        nparperlens = 5
        nparpersource = 6
        nparlens = nparperlens * nlens
        nparsource = nparpersource * nsource
        npar = nparlens + nparsource + npar_previous
        parameters = allparameters[npar_previous:npar]
        npar_previous = npar

        #nlens = paramData['nlens_regions'][indx]
        #nsource = paramData['nsource_regions'][indx]
        x = paramData['x'][regioni]
        y = paramData['y'][regioni]
        modelheader = paramData['modelheader'][regioni]
        model_types = paramData['model_types'][regioni]

        SBmap, LensedSBmap, Aperture, LensedAperture, mu_tot, mu_mask = \
                lensutil.sbmap(x, y, nlens, nsource, parameters, model_types, \
                computeamp=True)

        caustics = False
        if caustics:
            deltapar = parameters[0:nparlens + nparpersource]
            refine = 2
            nx = x[:, 0].size * refine
            ny = y[:, 0].size * refine
            x1 = x[0, :].min()
            x2 = x[0, :].max()
            linspacex = numpy.linspace(x1, x2, nx)
            y1 = y[:, 0].min()
            y2 = y[:, 0].max()
            linspacey = numpy.linspace(y1, y2, ny)
            onex = numpy.ones(nx)
            oney = numpy.ones(ny)
            finex = numpy.outer(oney, linspacex)
            finey = numpy.outer(linspacey, onex)
            
            mumap = numpy.zeros([ny, nx])
            for ix in range(nx):
                for iy in range(ny):
                    deltapar[-nparpersource + 0] = finex[ix, iy]
                    deltapar[-nparpersource + 1] = finey[ix, iy]
                    xcell = paramData['celldata']
                    deltapar[-nparpersource + 2] = xcell
                    deltaunlensed, deltalensed, A1, A2, mu_xy, mu_xymask = \
                            lensutil.sbmap(finex, finey, nlens, 1, deltapar, ['Delta'])
                    mumap[ix, iy] = mu_xy[0]

            import matplotlib.pyplot as plt
            plt.imshow(mumap, origin='lower')
            plt.contour(mumap, levels=[mumap.max()/1.1])
            import pdb; pdb.set_trace()
        SBmap_all += SBmap
        LensedSBmap_all += LensedSBmap

    LensedSBmapLoc = 'LensedSBmap.fits'
    SBmapLoc = 'SBmap_Region.fits'
    cmd = 'rm -rf ' + LensedSBmapLoc + ' ' + SBmapLoc
    os.system(cmd)

    fits.writeto(LensedSBmapLoc, LensedSBmap_all, modelheader)
    fits.writeto(SBmapLoc, SBmap_all, modelheader)

    return
Beispiel #10
0
def plotImage(model, data, config, modeltype, fitresult, tag=''):

    """

    Make a surface brightness map of a given model image.  Overlay with red
    contours a surface brightness map of the data image to which the model was
    fit. 

    """

    import numpy
    from astropy import wcs
    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse
    from pylab import savefig
    import setuputil
    import re

    # set font properties
    font = {'family' : 'Arial Narrow',
            'weight' : 'bold',
            'size'   : 10}
    matplotlib.rc('font', **font)
    matplotlib.rcParams['axes.linewidth'] = 1.5

    fig = plt.figure(figsize=(3.0, 3.0))
    ax = fig.add_subplot(1, 1, 1)
    plt.subplots_adjust(left=0.08, right=0.97, top=0.97, 
            bottom=0.08, wspace=0.35)
    paramData = setuputil.loadParams(config)
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    nregion = len(regionlist)
    for iregion in range(nregion):
        region = 'Region' + str(iregion)
        cr = config[region]

        ra_centroid = cr['RACentroid']
        dec_centroid = cr['DecCentroid']
        radialextent = cr['RadialExtent']

        nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
        if nmu > 0:
            allparameters0 = list(fitresult)[1:-nmu]
        else:
            allparameters0 = list(fitresult)[1:]

        # search poff_models for parameters fixed relative to other parameters
        fixindx = setuputil.fixParams(paramData)
        poff = paramData['poff']
        ndim_total = len(poff)
        fixed = (numpy.where(fixindx >= 0))[0]
        nfixed = fixindx[fixed].size
        parameters_offset = numpy.zeros(ndim_total)
        for ifix in range(nfixed):
            ifixed = fixed[ifix]
            subindx = fixindx[ifixed]
            par0 = 0
            if fixindx[subindx] > 0:
                par0 = fitresult[fixindx[subindx] + 1]
            parameters_offset[ifixed] = fitresult[subindx + 1] + par0

        allparameters = allparameters0 + parameters_offset

        # count the number of lenses
        configkeys = cr.keys()
        configkeystring = " ".join(configkeys)
        lenslist = re.findall('Lens.', configkeystring)
        nlens = len(lenslist)

        # count the number of sources
        sourcelist = re.findall('Source.', configkeystring)
        nsource = len(sourcelist)

        nparlens = 5 * nlens
        nparsource = 6 * nsource
        npar = nparlens + nparsource + npar_previous
        parameters = allparameters[npar_previous:npar]
        npar_previous = npar

        for i in range(nsource):
            i6 = i * 6
            xxx = parameters[i6 + 2 + nparlens]
            yyy = parameters[i6 + 3 + nparlens]
            source_pa = 90 - parameters[i6 + 5 + nparlens]
            #model_type = model_types[i]
            #if model_type == 'gaussian':
            norm = 2.35
            #if model_type == 'cylinder':
            #    norm = numpy.sqrt(2)
            meansize = norm * parameters[i6 + 1 + nparlens]
            source_bmaj = meansize / numpy.sqrt(parameters[i6 + 4 + nparlens])
            source_bmin = meansize * numpy.sqrt(parameters[i6 + 4 + nparlens])
            e = Ellipse((xxx, yyy), source_bmaj, source_bmin, \
                    angle=source_pa, ec='white', lw=0.5, fc='magenta', \
                    zorder=2, fill=True, alpha=0.5)
            ax.add_artist(e)
            if config.keys().count('xtextoff') > 0:
                xytext = (xxx + config['xtextoff'][i], 
                        yyy + config['ytextoff'][i])
                if iregion == 0:
                    if modeltype != 'optical':
                        plt.annotate(str(i), xy=(xxx, yyy), xytext=xytext, 
                                fontsize='xx-large')
                else:
                    xytext = (xxx + config['xtextoff'][i+2], 
                            yyy + config['ytextoff'][i+2])
                    if modeltype != 'optical':
                        plt.annotate(str(i + 2), xy=(xxx, yyy), xytext=xytext, 
                                fontsize='xx-large')

        for i in range(nlens):
            i5 = i * 5
            xxx = numpy.array([parameters[i5 + 1]])
            yyy = numpy.array([parameters[i5 + 2]])
            plt.plot(xxx, yyy, 'o', ms=5., mfc='black', mec='white', mew=0.5, \
                label='Lens Position', zorder=20)
            lens_pa = 90 - parameters[i5 + 4]
            meansize = 2 * parameters[i5]
            lens_bmaj = meansize / numpy.sqrt(parameters[i5 + 3])
            lens_bmin = meansize * numpy.sqrt(parameters[i5 + 3])
            elens = Ellipse((xxx, yyy), lens_bmaj, lens_bmin, \
                    angle=lens_pa, ec='orange', lw=1.0, \
                    zorder=20, fill=False)
            ax.add_artist(elens)

    # get the image centroid in model pixel coordinates
    headim = data[0].header
    headmod = model[0].header
    im = data[0].data
    im = im[0, 0, :, :]

    # good region is where mask is zero
    mask = setuputil.makeMask(config)
    goodregion = mask == 0

    # compute sigma image from cutout of SMA flux image
    # dv = 1.
    # bunit = headim['BUNIT']
    # if bunit == 'JY/BEAM.KM/S':
    #     dv = 500.
    rms = im[goodregion].std()# * dv

    # Obtain measurements of beamsize and image min/max
    bmaj = headim['BMAJ'] * 3600
    bmin = headim['BMIN'] * 3600
    bpa = headim['BPA']
    cdelt1 = headim['CDELT1'] * 3600
    cdelt2 = headim['CDELT2'] * 3600
    cell = numpy.sqrt( abs(cdelt1) * abs(cdelt2) )

    im_model = model[0].data
    if im_model.ndim == 4:
        im_model = im_model[0, 0, :, :]
    #nx_model = im_model[0, :].size
    pixextent = radialextent / cell
    datawcs = wcs.WCS(headim, naxis=2)
    pix = datawcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
    x0 = numpy.round(pix[0])
    y0 = numpy.round(pix[1])
    imrady = numpy.round(radialextent / cell)# nymod / 2.
    imradx = numpy.round(radialextent / cell)# nxmod / 2.

    # make data cutout
    totdx1 = x0 - imradx
    totdx2 = x0 + imradx
    totdy1 = y0 - imrady
    totdy2 = y0 + imrady
    datacut = im[totdy1:totdy2,totdx1:totdx2]

    # make cleaned model cutout
    headerkeys = headmod.keys()
    cd1_1 = headerkeys.count('CD1_1')
    cd1_2 = headerkeys.count('CD1_2')
    if cd1_1 == 0:
        cdelt1_model = numpy.abs(headmod['CDELT1'] * 3600)
        cdelt2_model = numpy.abs(headmod['CDELT2'] * 3600)
    else:
        cdelt1_model = numpy.abs(headmod['CD1_1'] * 3600)
        cdelt2_model = numpy.abs(headmod['CD2_2'] * 3600)
        cd11 = headmod['CD1_1']
        if cd1_2 == 0:
            cd12 = 0
            cd21 = 0
        else:
            cd12 = headmod['CD1_2']
            cd21 = headmod['CD2_1']
        cd22 = headmod['CD2_2']
        cdelt1_model = numpy.sqrt(cd11 ** 2 + cd12 ** 2) * 3600
        cdelt2_model = numpy.sqrt(cd21 ** 2 + cd22 ** 2) * 3600
        if cd12 == 0:
            cd12 = cd11 / 1e8
        cdratio = numpy.abs(cd11 / cd12)
        if cdratio < 1:
            cdratio = 1 / cdratio
    cellmod = numpy.sqrt( abs(cdelt1_model) * abs(cdelt2_model) )

    modelwcs = wcs.WCS(headmod, naxis=2)
    pix = modelwcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
    x0 = numpy.round(pix[0])
    y0 = numpy.round(pix[1])
    modrady = numpy.round(radialextent / cellmod)
    modradx = numpy.round(radialextent / cellmod)
    totdx1 = x0 - modradx
    totdx2 = x0 + modradx
    totdy1 = y0 - modrady
    totdy2 = y0 + modrady
    modelcut = im_model[totdy1:totdy2,totdx1:totdx2]

    #cellp = cell * (2 * pixextent + 1.1) / (2 * pixextent)
    xlo = -radialextent
    xhi = radialextent
    ylo = -radialextent
    yhi = radialextent
    ncell = modelcut[:, 0].size#(xhi - xlo) / cell
    modx = -numpy.linspace(xlo, xhi, ncell)
    mody = numpy.linspace(ylo, yhi, ncell)
    #modx = -(numpy.arange(2 * pixextent) - pixextent) * cellp - cell/2.
    #mody = (numpy.arange(2 * pixextent) - pixextent) * cellp + cell/2.
    cornerextent = [modx[0], modx[-1], mody[0], mody[-1] ]
    if modeltype == 'residual':
        grayscalename = 'Residual'
        pcolor = 'white'
        ncolor = 'black'
        vmax = 5 * rms
        vmin = -5 * rms
    elif modeltype == 'model':
        grayscalename = 'Model'
        pcolor = 'red'
        ncolor = 'red'
        vmax = modelcut.max()
        vmin = modelcut.min()
    else:
        grayscalename = config['OpticalTag']
        filtindx = grayscalename.find(' ')
        filtname = grayscalename[filtindx + 1:]
        if filtname == 'F110W':
            modelcut = numpy.log10(modelcut - modelcut.min() + 1)
        pcolor = 'red'
        ncolor = 'red'
        vmax = modelcut.max()
        vmin = modelcut.min()
    plt.imshow(modelcut, cmap='gray_r', interpolation='nearest', \
            extent=cornerextent, origin='lower', vmax=vmax, vmin=vmin)

    plevs = 3*rms * 2**(numpy.arange(10))
    nlevs = sorted(-3 * rms * 2**(numpy.arange(4)))
    pcline = 'solid'
    ncline = 'dashed'
    #nx_contour = datacut[0, :].size
    #ny_contour = datacut[:, 0].size
    #cmodx = -(numpy.arange(nx_contour) - pixextent) * cellp - cell/2.
    #cmody = (numpy.arange(ny_contour) - pixextent) * cellp + cell/2.
    ncell = datacut[:, 0].size#(xhi - xlo) / cell
    cmodx = -numpy.linspace(xlo, xhi, ncell)
    cmody = numpy.linspace(ylo, yhi, ncell)
    plt.contour(cmodx, cmody, datacut, colors=pcolor, levels=plevs, \
            linestyles=pcline, linewidths=1.5)
    plt.contour(cmodx, cmody, datacut, colors=ncolor, levels=nlevs, \
            linestyles=ncline, linewidths=1.5)

    # plot the critical curve
    #plt.contour(cmodx, cmody, dmu, colors='orange', levels=[100])
    #axisrange = plt.axis()
    axisrange = numpy.array([xhi,xlo,ylo,yhi]).astype(float)
    plt.axis(axisrange)

    plt.minorticks_on()
    plt.tick_params(width=1.5, which='both')
    plt.tick_params(length=2, which='minor')
    plt.tick_params(length=4, which='major')
    #plt.xlabel(r'$\Delta$RA (arcsec)', fontsize='x-large')
    #plt.ylabel(r'$\Delta$Dec (arcsec)', fontsize='x-large')

    bparad = bpa / 180 * numpy.pi
    beamx = numpy.abs(numpy.sin(bparad) * bmaj) + \
            numpy.abs(numpy.cos(bparad) * bmin)
    beamy = numpy.abs(numpy.cos(bparad) * bmaj) + \
            numpy.abs(numpy.sin(bparad) * bmin)
    beamxhi = 2 * pixextent / cell
    beamxlo = -2 * pixextent / cell
    beamyhi = 2 * pixextent / cell
    beamylo = -2 * pixextent / cell
    beamdx = numpy.float(beamxhi) - numpy.float(beamxlo)
    beamdy = numpy.float(beamyhi) - numpy.float(beamylo)
    bufferx = 0.03 * beamdx / 6.0
    buffery = 0.03 * beamdx / 6.0
    xpos = 1 - beamx/beamdx/2 - bufferx
    ypos = beamy/beamdy/2 + buffery
    #beamx = bmaj * numpy.abs(numpy.cos(bparad))
    #beamy = bmaj * numpy.abs(numpy.sin(bparad))

    xpos = 0.95 * axisrange[1] + 0.95 * beamx / 2.
    ypos = 0.95 * axisrange[2] + 0.95 * beamy / 2.

    e = Ellipse((xpos,ypos), bmaj, bmin, angle=90 - bpa, ec='black', \
        hatch='//////', lw=1.0, fc='None', zorder=10, fill=True)
    ax.add_artist(e)

    plt.text(0.92, 0.88, grayscalename, transform=ax.transAxes,
            fontsize='xx-large', ha='right')
    try:
        from astropy.table import Table
        tloc = '../../../Papers/Bussmann_2015a/Bussmann2015/Data/targetlist.dat'
        hackstep = Table.read(tloc, format='ascii')
        objname = config['ObjectName']
        match = hackstep['dataname'] == objname
        shortname = hackstep['shortname'][match][0]
        plt.text(0.08, 0.88, shortname, transform=ax.transAxes,
                fontsize='xx-large')
    except:
        pass
    bigtag = '.' + modeltype + '.' + tag

    savefig('LensedSBmap' + bigtag + '.pdf')
Beispiel #11
0
        raise TypeError


# remove the data points with zero or negative weight
positive_definite = wgt > 0
vis_complex = vis_complex[positive_definite]
wgt = wgt[positive_definite]
uuu = uuu[positive_definite]
vvv = vvv[positive_definite]
#www = www[positive_definite]

npos = wgt.size

#----------------------------------------------------------------------------
# Load input parameters
paramSetup = setuputil.loadParams(config)
nwalkers = paramSetup['nwalkers']
nregions = paramSetup['nregions']
nparams = paramSetup['nparams']
pname = paramSetup['pname']
nsource_regions = paramSetup['nsource_regions']

# Use an intermediate posterior PDF to initialize the walkers if it exists
posteriorloc = 'posteriorpdf.fits'
if os.path.exists(posteriorloc):

    # read the latest posterior PDFs
    print("Found existing posterior PDF file: {:s}".format(posteriorloc))
    posteriordat = Table.read(posteriorloc)
    if len(posteriordat) > 1: