Example #1
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
Example #2
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)
Example #3
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
Example #4
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')
Example #5
0
def preProcess(config,
               paramData,
               fitresult,
               tag='',
               cleanup=True,
               showOptical=False,
               interactive=True):
    """

    Cycle through each region and run plotFit, selecting parameters
    appropriately.

    """

    import setuputil
    import numpy
    import re

    # Loop over each region
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    for regioni, region in enumerate(regionlist):
        cr = config[region]

        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
        plotFit(config,
                paramData,
                parameters,
                regioni,
                tag=tag,
                cleanup=cleanup,
                showOptical=showOptical,
                interactive=interactive)
Example #6
0
def preProcess(config, paramData, fitresult, tag='', cleanup=True,
        showOptical=False, interactive=True):

    """

    Cycle through each region and run plotFit, selecting parameters
    appropriately.

    """

    import setuputil
    import numpy
    import re


    # Loop over each region
    nlensedsource = paramData['nlensedsource']
    nlensedregions = paramData['nlensedregions']
    npar_previous = 0

    configkeys = config.keys()
    configkeystring = " ".join(configkeys)
    regionlist = re.findall('Region.', configkeystring)
    for regioni, region in enumerate(regionlist):
        cr = config[region]

        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
        plotFit(config, paramData, parameters, regioni, tag=tag,
                cleanup=cleanup, showOptical=showOptical,
                interactive=interactive)
Example #7
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
Example #8
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')
Example #9
0
    pzero = numpy.array(paramSetup['pzero'])

# make sure no parts of pzero exceed p_u or p_l
#arrayp_u = numpy.array(p_u)
#arrayp_l = numpy.array(p_l)
#for j in range(nwalkers):
#    exceed = arraypzero[j] >= arrayp_u
#    arraypzero[j, exceed] = 2 * arrayp_u[exceed] - arraypzero[j, exceed]
#    exceed = arraypzero[j] <= arrayp_l
#    arraypzero[j, exceed] = 2 * arrayp_l[exceed] - arraypzero[j, exceed]
#pzero = arraypzero
#p_u = arrayp_u
#p_l = arrayp_l

# determine the indices for fixed parameters
fixindx = setuputil.fixParams(paramSetup)

# Initialize the sampler with the chosen specs.
if mpi:
    sampler = emcee.EnsembleSampler(nwalkers, nparams, lnprob, pool=pool, \
        args=[vis_complex, wgt, uuu, vvv, pcd, \
        fixindx, paramSetup, computeamp])
else:
    sampler = emcee.EnsembleSampler(nwalkers, nparams, lnprob, \
        args=[vis_complex, wgt, uuu, vvv, pcd, \
        fixindx, paramSetup, computeamp], threads=Nthreads)

# Sample, outputting to a file
#os.system('date')
currenttime = time.time()