def bestFit(bestfitloc='posteriorpdf.fits', showOptical=False, cleanup=True, interactive=True, plotonly=False): """ Read posterior PDF and identify best-fit parameters. Plot the best-fit model and compare to the data. Also plot the residuals obtained after subtracting the best-fit model from the data and compare to the data. Optionally plot the best available optical image and compare to the data. """ # read the posterior PDFs print("Found posterior PDF file: {:s}".format(bestfitloc)) fitresults = fits.getdata(bestfitloc) from astropy.table import Table fitKeys = Table.read(bestfitloc).keys() # identify best-fit model minchi2 = fitresults['lnprob'].max() index = fitresults['lnprob'] == minchi2 bestfit = fitresults[index][0] tag = 'bestfit' printFitParam(bestfit, fitKeys) visualutil.plotFit(config, bestfit, tag=tag, cleanup=cleanup, showOptical=showOptical, interactive=interactive, plotonly=plotonly)
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
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)
def goodFits(bestfitloc='posteriorpdf.fits', Nfits=12, Ngood=5000, cleanup=True, interactive=True, showOptical=False): """ Read posterior PDF and draw Nfits realizations from the final Ngood models at random. Plot the model from each realization and compare to the data. Also plot the residuals obtained after subtracting the model from the data and compare to the data. By default: Nfits = 12, Ngood=5000. """ import modifypdf import numpy # read the posterior PDFs print("Found posterior PDF file: {:s}".format(bestfitloc)) fitresults = fits.getdata(bestfitloc) fitresults = fitresults[-Ngood:] fitresults = modifypdf.prune(fitresults) # get keys from astropy.table import Table fitKeys = Table.read(bestfitloc).keys() # select the random realizations model Nunprune = len(fitresults) realids = numpy.floor(numpy.random.uniform(0, Nunprune, Nfits)) for ifit in range(Nfits): realid = numpy.int(realids[ifit]) fitresult = fitresults[realid] tag = 'goodfit' + str(realid).zfill(4) printFitParam(fitresult, fitKeys) visualutil.plotFit(config, fitresult, tag=tag, showOptical=showOptical, cleanup=cleanup, interactive=interactive)