def contours(M, contourKDEthin = 1): ''' Plot likelihood contours for each pair of paramters Parameters ---------- M : pymc.MCMC or pymc.database The Markov chain to be plotted contourKDEthin : int, optional Thins the samples used for estimating the gaussian kernel density. Defaults to 1 (no thinning). See Also -------- plot2Ddist (https://gist.github.com/665605) ''' keys = ['R0', 'G0', 'Q', 'Beta', 'Delta', 'Tbase'] N = len(keys) - 1 pl.figure(figsize = (4 * N, 4 * N)) for i in range(N): j = 0 while(j <= i): ax = pl.subplot(N, N, i * N + j + 1) p2d.plot2Ddist([M.trace(keys[i + 1])[:], M.trace(keys[j])[:]], labels = [keys[i + 1], keys[j]], contourKDEthin = contourKDEthin, axeslist = [ax], plotscatter = False) j += 1 pl.savefig('contours.png')
def compareto_normapprox(mcmodel, nmodel, varnames, truevalues=None, markvalues=None, axeslist=None, traceArgs = {}, **ellipseArgs): """Plot of confidence ellipses and samples from a NormApprox model over points from another model trace. logLStar versus alpha. """ nvars = names_to_obj(varnames, nmodel) mu = nmodel.mu[nvars] C = nmodel.C[nvars] mcvars = names_to_obj(varnames, mcmodel) mctraces = names_to_trace(varnames, mcmodel) results = plot2Ddist.plot2Ddist(mcvars, axeslist=axeslist, truevalues=truevalues, markvalues=markvalues, **traceArgs) ells = plot_confidence_ellipse(mu, C, results['axeslist'][0], **ellipseArgs) results['ells'] = ells plot_normal(mu[0], C[0,0], ax=results['axeslist'][1]), #normalization=mctraces[0].length()) plot_normal(mu[1], C[1,1], ax=results['axeslist'][2], rotate=True) #test_confidence_ellipse(mcmodel, mu, C, varnames=varnames) return results
for i in chain: st = statistics_hist(i[int(burn_in*len(i)):],0.68,"stats") print "Maximum Likelihood Value and 68% Confidence Interval: ", st[0],st[1] #************************************************************************ """ Parameter density estimation and pair correlations. """ from plot2Ddist import plot2Ddist, plot2DdistsPairs plot_conts = False if plot_conts: for i in [12,13,14,15]: for j in [12,13,14,15]: if i !=j: plot2Ddist([chain[i][int(burn_in*len(chain[i])):],chain[j][int(burn_in*len(chain[j])):]],plotcontours=True,plothists=True,contourFractions = [0.6827,0.9545,0.9973],labelcontours=False) plt.show() #************************************************************************ """ Pair correlation between specific pair """ one_pair = False param_1 = 0 param_2 = 12 if one_pair: plot2Ddist([chain[param_1][int(burn_in*len(chain[param_1])):],chain[param_2][int(burn_in*len(chain[param_2])):]],plotcontours=True,plothists=True,contourFractions=[0.6827,0.9545,0.9973],labelcontours=False) plt.show()
del fitsfile del padding_cols del prefix del ext for file in files: scatterstyle={'color':'r', 'alpha':0.5} styleargs = {'color':'k', 'scatterstyle':scatterstyle} padding_cols = file['pad'] ncols = file['ncols'] for i in range (padding_cols, ncols): for j in range (i + 1, ncols): x = file['cols'][i] y = file['cols'][j] xlim = [col_name_att[file['col_names'][i]]['min'], col_name_att[file['col_names'][i]]['max']] ylim = [col_name_att[file['col_names'][j]]['min'], col_name_att[file['col_names'][j]]['max']] labelx = file['col_symbols'][i] labely = file['col_symbols'][j] plot2Ddist ([x, y], plothists = True, labels = [labelx, labely], xlim = xlim, ylim = ylim, **styleargs) figfile = "%s_%s_%s.pdf" % (file['prefix'], file['col_names'][i], file['col_names'][j]) print ("# Saving figure: %s." % figfile) pl.savefig (figfile) pl.close ()
def test(nSims=100, lmax=100, lmin=2, partialMax=4, useCLASS=1, useLensing=1, cutSky=True, myNSIDE=128, newSC2=True, saveFile='simpleSonehalfC2.npy', nGrid=100): """ Purpose: function for testing S_{1/2} calculations Inputs: nSims: the number of simulations to do Overriden if newSC2 = False Default: 100 lmax: the highest l to use in the calculation Default: 100 lmin: the lowest l to use in the calculation Default: 2 partialMax: the maximum l to use for partial Sonehalf plots must be more than lmin Overriden if newSC2 = False Default: 4 useCLASS: set to 1 to use CLASS Cl, 0 for CAMB Default: 1 useLensing: set to 1 to use lensed Cls Default: 1 cutSky: set to True to do cut-sky sims Default: True myNSIDE: HEALPix parameter for simulated maps if cutSky=True Default: 128 newSC2: set to True to simulate new ensemble and save S,C2 results in file, False to skip simulation and load previous results If false, values of nSims and partialMax will come from file Default: True saveFile: filename to save S,C2 result if newSC2 is true, to load if false Default: 'simpleSonehalfC2.npy' nGrid: to pass to plot2Ddist; controls grid for binning for contours Default: 100 """ # get power spectrum # starts with ell[0]=2 ell, fullCl, primCl, lateCl, crossCl = gcp.loadCls(useCLASS=useCLASS, useLensing=useLensing) # fill beginning with zeros startEll = int(ell[0]) ell = np.append(np.arange(startEll), ell) Cl = np.append(np.zeros(startEll), fullCl) #conv = ell*(ell+1)/(2*np.pi) # Note: optimizeSx2 includes a multiplication of Cl by (beam*window)**2 at this point, # but in this program I'm omitting it. Why? Effects are small, esp. at low ell # get Jmn matrix for harmonic space S_{1/2} calc. myJmn = getJmn(lmax=lmax) # do not include monopole, dipole if cutSky: # yeah.. disk access is annoying so... RAMdisk = '/Volumes/ramdisk/' ClTempFile = RAMdisk + 'tempCl.fits' mapTempFile = RAMdisk + 'tempMap.fits' mapDegFile = RAMdisk + 'smicaMapDeg.fits' #created by sim_stats.getSMICA maskDegFile = RAMdisk + 'maskMapDeg.fits' #created by sim_stats.getSMICA # create RAM Disk for SpICE and copy these files there using bash RAMsize = 4 #Mb ramDiskOutput = subprocess.check_output('./ramdisk.sh create ' + str(RAMsize), shell=True) print ramDiskOutput diskID = ramDiskOutput[ 31: 41] # this might not grab the right part; works for '/dev/disk1' subprocess.call('cp smicaMapDeg.fits ' + RAMdisk, shell=True) subprocess.call('cp maskMapDeg.fits ' + RAMdisk, shell=True) ispice(mapDegFile, ClTempFile, maskfile1=maskDegFile, subav="YES", subdipole="YES") Clsmica = hp.read_cl(ClTempFile) else: ClTempFile = 'tempCl.fits' mapTempFile = 'tempMap.fits' mapDegFile = 'smicaMapDeg.fits' #created by sim_stats.getSMICA maskDegFile = 'maskMapDeg.fits' #created by sim_stats.getSMICA ispice(mapDegFile, ClTempFile, subav="YES", subdipole="YES") Clsmica = hp.read_cl(ClTempFile) # collect results if newSC2: sEnsemblePartial = np.zeros([nSims, partialMax + 1]) C2Ensemble = np.zeros(nSims) for i in range(nSims): print "starting sim ", i + 1, " of ", nSims, "... " almSim = hp.synalm(Cl, lmax=lmax) # should start with ell[0] = 0 if cutSky: mapSim = hp.alm2map(almSim, myNSIDE, lmax=lmax) hp.write_map(mapTempFile, mapSim) ispice(mapTempFile, ClTempFile, maskfile1=maskDegFile, subav="YES", subdipole="YES") ClSim = hp.read_cl(ClTempFile) else: ClSim = hp.alm2cl(almSim) for myLmin in range(lmin, partialMax + 1): sEnsemblePartial[i, myLmin] = np.dot( ClSim[myLmin:lmax + 1], np.dot(myJmn[myLmin:, myLmin:], ClSim[myLmin:lmax + 1])) C2Ensemble[i] = ClSim[2] # save results np.save(saveFile, np.hstack((np.array([C2Ensemble]).T, sEnsemblePartial))) else: # load from file sEnsemblePartial = np.load(saveFile) C2Ensemble = sEnsemblePartial[:, 0] sEnsemblePartial = sEnsemblePartial[:, 1:] nSims = sEnsemblePartial.shape[0] partialMax = sEnsemblePartial.shape[1] - 1 if cutSky: # free the RAM used by SpICE's RAM disk ramDiskOutput = subprocess.check_output('./ramdisk.sh delete ' + diskID, shell=True) print ramDiskOutput # plot results print 'plotting S_{1/2} distributions... ' #myBins = np.logspace(2,7,100) myBins = np.logspace(2, 6, 100) #plt.axvline(x=6763,color='b',linewidth=3,label='SMICA inpainted') #plt.axvline(x=2145,color='g',linewidth=3,label='SMICA masked') #plt.hist(sEnsembleFull,bins=myBins,color='b',histtype='step',label='full sky') #plt.hist(sEnsembleCut, bins=myBins,color='g',histtype='step',label='cut sky') myColors = ('g', 'b', 'r', 'c', 'm', 'k') #need more? prob. not. myLines = ('-', '--', '-.') #need more? for myEll in range(lmin, partialMax + 1): plt.hist(sEnsemblePartial[:, myEll], bins=myBins, histtype='step', label=r'sims: $\ell_{\rm min}$ = ' + str(myEll), color=myColors[myEll - lmin], linestyle=myLines[myEll - lmin], linewidth=2) Sonehalf = np.dot( Clsmica[myEll:lmax + 1], np.dot(myJmn[myEll:, myEll:], Clsmica[myEll:lmax + 1])) * 1e24 plt.axvline(x=Sonehalf, linewidth=3, label=r'SMICA: $\ell_{\rm min}$=' + str(myEll), color=myColors[myEll - lmin], linestyle=myLines[myEll - lmin]) # calculate and print p-value pval = pValue(sEnsemblePartial[:, myEll], Sonehalf) print 'l_min: ', myEll, ', Sonehalf: ', Sonehalf, ', p-value: ', pval plt.gca().set_xscale("log") plt.legend() myfs = 16 # font size for labels plt.xlabel(r'$S_{1/2} (\mu K^4)$', fontsize=myfs) plt.ylabel('Counts', fontsize=myfs) plt.xlim((500, 10**6)) if cutSky: sName = ' cut-sky' else: sName = ' full-sky' #plt.title(r'$S_{1/2}$ of '+str(nSims)+sName+' simulated CMBs') plt.show() print 'plotting C_2 vs. S_{1/2} histogram... ' SMICAvals = (np.log10(2145), 171.8 ) # KLUDGE!!! #moved to earlier in program SonehalfLabel = "$log_{10}(\ S_{1/2}\ /\ (\mu K)^4\ )$" C2Label = "$C_2\ /\ (\mu K)^2$" C2Label3 = "$C_2\ /\ (10^3 (\mu K)^2)$" log10SonehalfEnsemble = np.log10(sEnsemblePartial[:, lmin]) myBinsLog10S = np.linspace(2, 6, 100) myBinsC2 = np.linspace(0, 3000, 100) cmap = cm.magma #Greens#Blues plt.hist2d(log10SonehalfEnsemble, C2Ensemble, bins=[myBinsLog10S, myBinsC2], cmap=cmap) plt.plot(SMICAvals[0], SMICAvals[1], 'cD') plt.colorbar() #myfs = 16 # font size for labels plt.xlabel(SonehalfLabel, fontsize=myfs) plt.ylabel(C2Label, fontsize=myfs) plt.show() print 'plotting C_2 vs. S_{1/2} contours... ' H, xedges, yedges = np.histogram2d(log10SonehalfEnsemble, C2Ensemble, bins=(myBinsLog10S, myBinsC2)) H = H.T # Let each row list bins with common y range myXedges = (xedges[1:] + xedges[:-1]) / 2 #find midpoint of linspace for plotting myYedges = (yedges[1:] + yedges[:-1]) / 2 hMax = np.max(H) #levels = [hMax*0.0009,hMax*0.009,hMax*0.09,hMax*0.9,hMax] #levels = [hMax*0.01,hMax*0.05,hMax*0.1,hMax*0.5,hMax*0.9,hMax] levels = np.logspace(np.log10(0.01 * hMax), np.log10(0.9 * hMax), 5) norm = cm.colors.Normalize(vmax=abs(H).max(), vmin=0) #cmap = cm.PRGn #plt.figure() #plt.imshow(H,origin='lower',norm=norm,cmap=cmap)#,extent=extent) #extent is a coordinate zoom #plt.imshow(H,norm=norm,cmap=cmap,extent=(2,6,0,3000)) #should match linspace above #v = plt.axis() CS = plt.contour(myXedges, myYedges, H, levels, colors='k', thickness=2) plt.clabel(CS, inline=1, fontsize=10) #plt.axis(v) plt.colorbar() #plt.title('do i want a title here?') plt.xlim(2.8, 5.8) #myfs = 16 # font size for labels plt.xlabel(SonehalfLabel, fontsize=myfs) plt.ylabel(C2Label, fontsize=myfs) plt.plot(SMICAvals[0], SMICAvals[1], 'cD') plt.show() print 'plotting corner plot... ' toPlot = np.vstack((log10SonehalfEnsemble, C2Ensemble)) toPlot = toPlot.T figure = corner.corner(toPlot, labels=[SonehalfLabel, C2Label], show_titles=False, truths=SMICAvals, range=((2.5, 6), (0, 3000)), label_kwargs={'fontsize': myfs}) plt.show() print 'plotting contours again but now using plot2Ddist (please wait)... ' doTime = True startTime = time.time() scatterstyle = {'color': 'r', 'alpha': 0.5} styleargs = {'color': 'k', 'scatterstyle': scatterstyle} bw_method = 0.05 #'scott' axSize = "20%" #1.5 nstart = 600 # create separate figures to contain separate plots """plt.figure(1) ax1=plt.gca() plt.figure(2) ax2=plt.gca() plt.figure(3) ax3=plt.gca()""" #fig = plt.figure() #should be the same one used by plot2Ddist # divide C2Ensemble by 1000 since that is approximate factor between ranges of C2,Sonehalf # presumably useful for accuracy in contour plotting via kernel density estimation fig1, axeslist = plot2Ddist.plot2Ddist( [log10SonehalfEnsemble, C2Ensemble / 1000], truevalues=[SMICAvals[0], SMICAvals[1] / 1000], labels=[SonehalfLabel, C2Label3], contourNGrid=nGrid, bw_method=bw_method, axSize=axSize, nstart=nstart, returnfigure=True, **styleargs) #bw_method=bw_method,axSize=axSize,axeslist=[ax1,ax2,ax3],**styleargs) ax1, ax2, ax3 = axeslist timeInterval1 = time.time() - startTime if doTime: print 'time elapsed: ', int(timeInterval1), ' seconds' print 'starting second plot2Ddist call... ' ax1.set_xlim(left=2.9, right=6.1) ax1.set_ylim(top=5.5) ax1.plot(SMICAvals[0], SMICAvals[1] / 1000, 'cD') #inset plot left, bottom, width, height = [0.2, 0.4, 0.3, 0.3] ax4 = fig1.add_axes([left, bottom, width, height]) #ax4.plot(range(10)) plt.figure(5) ax5 = plt.gca() plt.figure(6) ax6 = plt.gca() plot2Ddist.plot2Ddist([log10SonehalfEnsemble, C2Ensemble / 1000], truevalues=[SMICAvals[0], SMICAvals[1] / 1000], contourNGrid=nGrid, bw_method=bw_method, axSize=axSize, nstart=nstart, axeslist=[ax4, ax5, ax6], contourFractions=[0.91, 0.93, 0.95, 0.97, 0.99], labelcontours=False, **styleargs) timeInterval2 = time.time() - startTime if doTime: print 'time elapsed for both: ', int(timeInterval2), ' seconds' ax4.set_xlim(left=3.15, right=3.45) ax4.set_ylim(top=0.5) ax4.plot(SMICAvals[0], SMICAvals[1] / 1000, 'cD') ax4.xaxis.set_ticks((3.2, 3.3, 3.4)) #plt.figure(1) #plt.xlim(2.9,6.1) #plt.ylim(-0.03,5.5) plt.show() # calculate and print 1D p-values pValueS12 = pValue(log10SonehalfEnsemble, SMICAvals[0]) pValueC2 = pValue(C2Ensemble, SMICAvals[1]) print 'S_{1/2} p-value = ', pValueS12 print 'C_2 p-value = ', pValueC2 print ''
def plot_fits( filelist, varname_pairs=None, thin=1, trim=0, plotthin=None, plot_spectra=True, output_callback=None, savename=None, minlen=None, ): """Plot pairs of parameters from a set of fits. returns value_pairs, which has shape: (len(filelist), len(vpairs), 2, len(traces)) """ if minlen is None: minlen = trim if varname_pairs is None: m = PowerGaussMCMC.load(filelist[0]) if m.mcmodel.n_lines == 0: varname_pairs = [("dw_alpha", "dw_lambda_e"), ("dw_alpha", "dw_tau0"), ("dw_lambda_e", "dw_tau0")] if m.mcmodel.n_components == 2: if m.mcmodel.vshifts[2] is m.mcmodel.vshifts[0]: varname_pairs = [("vshift_1", "vshift_0")] else: varname_pairs = [("vshift_2", "vshift_0"), ("vshift_3", "vshift_1")] elif m.mcmodel.n_components == 1: if m.mcmodel.vshifts[1] is m.mcmodel.vshifts[0]: varname_pairs = [ ("peak_1", "peak_0"), ((numpy.divide, "peak_1", "contflux"), (numpy.divide, "peak_0", "contflux")), ("peak_1", "epeak_4"), ("peak_0", "epeak_4"), ("width_1", "width_0"), ("vshift_0", "evshift_4"), ("vshift_0", "peak_0"), ("vshift_0", "peak_1"), ("vshift_0", (numpy.divide, "peak_0", "peak_1")), ("vshift_0", (numpy.divide, "width_0", "width_1")), ("vshift_0", (numpy.divide, "ewidth_4", "width_1")), ("width_1", "ewidth_4"), ("width_0", "ewidth_4"), ("contflux", "vshift_1"), ("contindex", "vshift_1"), ] else: varname_pairs = [ ("vshift_1", "vshift_0"), ("vshift_1", (numpy.subtract, "vshift_0", "vshift_1")), ((numpy.divide, "peak_1", "contflux"), (numpy.divide, "peak_0", "contflux")), ("vshift_1", (numpy.divide, "peak_0", "peak_1")), ("width_1", "width_0"), ("vshift_1", (numpy.divide, "width_0", "width_1")), ("evshift_4", (numpy.subtract, "vshift_0", "vshift_1")), ((numpy.subtract, "evshift_4", "vshift_1"), (numpy.subtract, "vshift_0", "vshift_1")), ("ewidth_4", (numpy.subtract, "vshift_0", "vshift_1")), ((numpy.divide, "ewidth_4", "width_1"), (numpy.subtract, "vshift_0", "vshift_1")), ((numpy.divide, "epeak_4", "peak_1"), (numpy.subtract, "vshift_0", "vshift_1")), ("evshift_3", "vshift_0"), ("ewidth_3", "width_0"), ("epeak_3", "peak_0"), ("contflux", "vshift_1"), ("contindex", "vshift_1"), ] m.mcmodel.db.close() del m value_dict = defaultdict(list) unique_varnames = set() for vnames in varname_pairs: unique_varnames.update(vnames) final_filelist = [] for filename in filelist: label = os.path.split(filename)[-1] print filename try: m = PowerGaussMCMC.load(filename) except Exception as e: print "Couldn't load file. Got error:", e continue tracelen = len(m.mcmodel.db.deviance[:]) print "Found trace of len %i" % tracelen if tracelen < minlen: print "Not enough iterations in fit! Skipping", filename continue if plot_spectra: pthin = plotthin if pthin is None: pthin = tracelen / 15 axeslist, total = m.plot_spectra(trim=trim, thin=pthin) axes, resaxes, vaxes, vresaxes = axeslist axes.set_title(label) vaxes.set_title(label) axes.figure.set_label(label + "_spectrum") vaxes.figure.set_label(label + "_vspectrum") xmin = 1150 xmax = 1275 axes.set_xlim(xmin, xmax) vmin = max((xmin / 1215.67 - 1) * cc.c_light_cm_s / 1e5, -1.2e4) vmax = min((xmax / 1215.67 - 1) * cc.c_light_cm_s / 1e5, 1.2e4) vaxes.set_xlim(vmin, vmax) scale = numpy.max(total) axes.set_ylim(-0.1 * scale, 1.1 * scale) vaxes.set_ylim(-0.1 * scale, 1.1 * scale) resaxes.set_ylim(-0.1 * scale, 0.1 * scale) vresaxes.set_ylim(-0.1 * scale, 0.1 * scale) if output_callback is not None: output_callback() for vname in list(unique_varnames): if type(vname) is tuple: v1 = m.mcmodel.db.trace(vname[1])[trim::thin] v2 = m.mcmodel.db.trace(vname[2])[trim::thin] if not vname[1] in unique_varnames: value_dict[vname[1]].append(v1) unique_varnames.add(vname[1]) if not vname[2] in unique_varnames: value_dict[vname[2]].append(v2) unique_varnames.add(vname[2]) value_dict[vname].append(vname[0](v1, v2)) else: value_dict[vname].append(m.mcmodel.db.trace(vname)[trim::thin]) print "mean", vname, numpy.mean(value_dict[vname][-1]) final_filelist.append(filename) m.mcmodel.db.close() del m if savename is not None: import hdf5_utils hdf5_utils.save_hdf5(savename, dict((str(k), v) for k, v in value_dict.iteritems())) colors = itertools.cycle(["r", "g", "b", "c", "m", "y", "k"]) alpha = 0.05 bins = 10 histbinslist = [bins] * 2 import plot2Ddist for (ipair, vnames) in enumerate(varname_pairs): results = dict(axeslist=None) for ifile in xrange(len(final_filelist)): results = plot2Ddist.plot2Ddist( [value_dict[vnames[0]][ifile], value_dict[vnames[1]][ifile]], labels=vnames, axeslist=results["axeslist"], color=colors.next(), scatterstyle=dict(alpha=alpha), histbinslist=histbinslist, ) if output_callback is not None: output_callback() return value_dict
for i in range(padding_cols, ncols): for j in range(i + 1, ncols): x = file['cols'][i] y = file['cols'][j] xlim = [ col_name_att[file['col_names'][i]]['min'], col_name_att[file['col_names'][i]]['max'] ] ylim = [ col_name_att[file['col_names'][j]]['min'], col_name_att[file['col_names'][j]]['max'] ] labelx = file['col_symbols'][i] labely = file['col_symbols'][j] plot2Ddist([x, y], plothists=True, labels=[labelx, labely], xlim=xlim, ylim=ylim, **styleargs) figfile = "%s_%s_%s.pdf" % (file['prefix'], file['col_names'][i], file['col_names'][j]) print("# Saving figure: %s." % figfile) pl.savefig(figfile) pl.close()