def run(self): #Get the columns we need, in reduced form #since this is not MCMC weight = self.weight_col() weight = weight / weight.max() #Sort the final 500 samples, since they #are otherwise un-ordered weight[-500:] = np.sort(weight[-500:]) #x-axis is just the iteration number x = np.arange(weight.size) #Choose a filename and make a figure for your plot. figure, filename = self.figure("nest_weight") #Do the plotting, and set the limits and labels pylab.plot(x, weight / weight.max()) #pylab.xlim(-353, -341.5) pylab.xlim(0, 8000) pylab.xlabel("Iteration number $i$") pylab.ylabel(r"$p_i$") #Add some helpful text, on the title and the plot itself pylab.title("Normalised posterior weight $p_i$", size='medium') pylab.text(500, 0.4, "Increasing likelihood,\n volume still large", size="small") pylab.text(5500, 0.65, "Volume\ndecreasing", size='small') pylab.text(6500, 0.12, "Final $n_\mathrm{live}$\npoints", size='small') #Return the list of filenames we made return [filename]
def plot(self): super(ShearCorrelationMinusPlot, self).plot() nbin = 0 for i in range(1, 100): filename = self.file_path("shear_xi_minus", "bin_{0}_{0}".format(i)) if os.path.exists(filename): nbin += 1 else: break if nbin == 0: IOError("No data for plot: %s" % self.__class__.__name__[:-4]) theta = self.load_file("shear_xi_minus", "theta") sz = 1.0 / (nbin + 2) for i in range(1, nbin + 1): for j in range(1, i + 1): rect = (i * sz, j * sz, sz, sz) self.figure.add_axes(rect) #pylab.ploy() #pylab.subplot(nbin, nbin, (nbin*nbin)-nbin*(j-1)+i) xi = self.load_file("shear_xi_minus", "bin_{0}_{1}".format(i, j)) pylab.loglog(theta, xi) pylab.xlim(1e-4, 1e-1) pylab.ylim(2e-7, 1e-3) if i == 1 and j == 1: pylab.xlabel("$\\theta$") pylab.ylabel("$\\xi_+(\\theta)$") else: pylab.gca().xaxis.set_ticklabels([]) pylab.gca().yaxis.set_ticklabels([]) pylab.gca().tick_params(length=0.0, which='minor') pylab.gca().tick_params(length=3.0, which='major') pylab.gca().tick_params(labelsize=10) pylab.text(1.5e-3, 1.8e-4, "(%d,%d)" % (i, j), fontsize=8, color='red') pylab.grid()
def make_2d_plot(self, name1, name2): #Get the data if 'planck' in self.source.name.lower(): def smooth_likelihood(obj, x, y): n = obj.options.get("n_kde", 100) factor = obj.options.get("factor_kde", 2.0) kde = KDE([x, y], factor=factor) x_range = (x.min(), x.max()) y_range = (y.min(), y.max()) (x_axis, y_axis), like = kde.grid_evaluate(n, [x_range, y_range]) return n, x_axis, y_axis, like x = self.reduced_col(name1) y = self.reduced_col(name2) else: if name1[-2:] == 's8': omm = self.reduced_col('cosmological_parameters--omega_m') sigma_8 = self.reduced_col('cosmological_parameters--sigma_8') x = sigma_8 * (omm / 0.3)**0.5 else: x = self.find_or_pad(name1) if name2[-2:] == 's8': omm = self.reduced_col('cosmological_parameters--omega_m') sigma_8 = self.reduced_col('cosmological_parameters--sigma_8') y = sigma_8 * (omm / 0.3)**0.5 else: y = self.find_or_pad(name2) if x.max() - x.min() == 0 or y.max() - y.min() == 0: return print " (making %s vs %s)" % (name1, name2) #import pdb ; pdb.set_trace() #Interpolate using KDE try: n, x_axis, y_axis, like = self.smooth_likelihood(x, y) except: try: n, x_axis, y_axis, like = self.smooth_likelihood_noweight(x, y) except: def smooth_likelihood(obj, x, y): n = 100 factor = 2.0 kde = KDE([x, y], factor=factor) x_range = (x.min(), x.max()) y_range = (y.min(), y.max()) (x_axis, y_axis), like = kde.grid_evaluate(n, [x_range, y_range]) return n, x_axis, y_axis, like n, x_axis, y_axis, like = smooth_likelihood(self, x, y) figure, filename = self.figure("2D", name1, name2) #Choose levels at which to plot contours contour1 = 1 - 0.68 contour2 = 1 - 0.95 try: level1, level2, total_mass = self._find_contours( like, x, y, n, x_axis[0], x_axis[-1], y_axis[0], y_axis[-1], contour1, contour2) except: def find_contours(like, x, y, n, xmin, xmax, ymin, ymax, contour1, contour2): N = len(x) x_axis = np.linspace(xmin, xmax, n + 1) y_axis = np.linspace(ymin, ymax, n + 1) histogram, _, _ = np.histogram2d(x, y, bins=[x_axis, y_axis]) def objective(limit, target): w = np.where(like >= limit) count = histogram[w] return count.sum() - target target1 = histogram.sum() * (1 - contour1) target2 = histogram.sum() * (1 - contour2) level1 = scipy.optimize.bisect(objective, like.min(), like.max(), args=(target1, )) level2 = scipy.optimize.bisect(objective, like.min(), like.max(), args=(target2, )) return level1, level2, like.sum() level1, level2, total_mass = find_contours(like, x, y, n, x_axis[0], x_axis[-1], y_axis[0], y_axis[-1], contour1, contour2) #import pdb ; pdb.set_trace() level0 = 1.1 levels = [level2, level1, level0] #Create the figure fig, filename = self.figure("2D", name1, name2) pylab.figure(fig.number) #Make the plot #pylab.figure(figure.number, figsize=(8,7)) keywords = self.keywords_2d() fill = self.options.get("fill", True) if self.fill_list != None: fill = self.fill_list[self.source.index] imshow = self.imshow[self.source.index] #plot_points = self.options.get("plot_points", False) color = self.colors[self.source.index] linestyle = self.linestyles[self.source.index] if self.linecolors[self.source.index] is not None: linecolor = self.linecolors[self.source.index] else: linecolor = color print self.source.index, linecolor if self.opaque_list[self.source.index]: pylab.contourf(x_axis, y_axis, like.T, [level2, level0], colors=['white']) print "fill", self.source.index, fill if imshow: pylab.imshow(like.T, extent=(x_axis[0], x_axis[-1], y_axis[0], y_axis[-1]), aspect='auto', origin='lower', cmap=cmap_white_to_color(color)) elif fill: if self.fill_colors[self.source.index] is not None: c = self.fill_colors[self.source.index] pylab.contourf(x_axis, y_axis, like.T, [level2, level0], colors=[c[0]]) pylab.contourf(x_axis, y_axis, like.T, [level1, level0], colors=[c[1]]) else: pylab.contourf(x_axis, y_axis, like.T, [level2, level0], colors=[color], alpha=self.alphas[self.source.index]) if self.opaque_centre[self.source.index]: pylab.contourf(x_axis, y_axis, like.T, [level1, level0], colors=[color]) else: pylab.contourf(x_axis, y_axis, like.T, [level1, level0], colors=[color], alpha=self.alphas[self.source.index]) if (not fill) or self.line_list[self.source.index]: print self.source.index, linecolor pylab.contour(x_axis, y_axis, like.T, [level2, level1], colors=[linecolor], linestyles=linestyle, linewidths=self.linewidths[self.source.index]) #if self.labels is not None: # self.proxies.append(pylab.plot([],[], color=color, linestyle=linestyle, linewidth=self.linewidths[self.source.index])[0]) if self.labels is not None: if self.fill_colors[self.source.index] is not None: print self.fill_colors[self.source.index] elif fill: print self.source.index, self.line_list[self.source.index] if self.line_list[self.source.index] is not None: conv = matplotlib.colors.ColorConverter() edgecolor = linecolor self.proxies.append( pylab.Line2D([0, 2.5], [0, 0], linestyle=linestyle, linewidth=3, color=linecolor)) #facecolor=conv.to_rgba(color,alpha=1-(1-self.alphas[self.source.index])**2) #self.proxies.append(pylab.Rectangle((0,0),1,1,facecolor=facecolor,edgecolor=edgecolor))#,alpha=(1-(1-self.alphas[self.source.index])**2))) else: self.proxies.append( pylab.Rectangle( (0, 0), 1, 1, fc=color, edgecolor=color, alpha=(1 - (1 - self.alphas[self.source.index])**2))) else: self.proxies.append( pylab.plot( [], [], color=color, linestyle=linestyle, linewidth=self.linewidths[self.source.index])[0]) if self.plot_points is not None: point_markers = ['x', '+', '^'] for p, m in zip(self.plot_points, point_markers): print p, m pylab.plot(p[0], p[1], m, color='k', markersize=10, markerfacecolor='none') #Do the labels print self.proxies, self.labels if self.labels is not None: leg = pylab.legend(self.proxies, self.labels, loc="upper left", fontsize=13) leg.get_frame().set_alpha( 0.75) # this will make the box totally transparent leg.get_frame().set_edgecolor( 'white') # this will make the edges of the pylab.ylabel(r"$A_\mathrm{IA}$", fontsize=30, fontname='serif') pylab.xlabel(r"$b_g$", fontsize=30, fontname='serif') pylab.tight_layout() for col, text in zip(self.colors, leg.get_texts()): text.set_color(col) if col == 'pink': text.set_color('hotpink') if blind: pylab.xticks(visible=False) pylab.xticks(visible=True, fontsize=25) pylab.yticks(visible=True, fontsize=25) pylab.xlim(self.axis[0], self.axis[1]) pylab.ylim(self.axis[2], self.axis[3]) pylab.title('$z=1.0$', fontsize=25) # pylab.subplots_adjust(top=0.95) x0 = [4.6] dx = [0.5] y0 = [1.77] pylab.errorbar(x0, y0, xerr=dx, yerr=[0.04], markersize=9.0, markerfacecolor='k', color='k', marker='*', markeredgecolor='k', linestyle='none') # pylab.errorbar([4.35341365],[1.80416667], xerr=7.080459e-01, yerr=1.706998e-02,color='pink',marker='o') #pylab.title('$\gamma \gamma + \delta_g \gamma + \delta_g\delta_g$') #pylab.axhline(0,color="k",alpha=0.5,ls=':') #pylab.axhline(5.0,color="darkviolet",lw=1, ls="--") #print "Plotting bounds line..." return filename
def plot(self): super(BBPlot, self).plot() pylab.xlim(0, 400)
def make_1d_plot(self, name1): #Get the data #import pdb ; pdb.set_trace() filename = self.filename(name1) col1 = self.source.get_col(name1) like = self.source.get_col("post") vals1 = np.unique(col1) n1 = len(vals1) like_sum = np.zeros(n1) #normalizing like this is a big help #numerically like = like-like.max() #marginalize for k,v1 in enumerate(vals1): w = np.where(col1==v1) like_sum[k] = np.log(np.exp(like[w]).sum()) like = like_sum.flatten() like -= like.max() #linearly interpolate n1_interp = n1*10 vals1_interp = np.linspace(vals1[0], vals1[-1], n1_interp) like_interp = np.interp(vals1_interp, vals1, like) if np.isfinite(like_interp).any(): vals1 = vals1_interp like = like_interp n1 = n1_interp else: print() print("Parameter %s has a very wide range in likelihoods " % name1) print("So I couldn't do a smooth likelihood interpolation for plotting") print() #normalize like[np.isnan(like)] = -np.inf like -= like.max() #Determine the spacing in the different parameters dx = vals1[1]-vals1[0] #Set up the figure fig,filename = self.figure(name1) pylab.figure(fig.number) #Plot the likelihood print('----------------------- HITTA',self.source.index, self.colors[self.source.index]) pylab.plot(vals1, np.exp(like), linewidth=self.linewidths[self.source.index], label=self.labels[self.source.index], color=self.colors[self.source.index], linestyle='-') self.proxies.append(pylab.Line2D([0,2.5],[0,0], color=self.colors[self.source.index], linewidth=self.linewidths[self.source.index])) #Find the levels of the 68% and 95% contours X, L = self.find_edges(np.exp(like), 0.68, 0.95, vals1) #Plot black dotted lines from the y-axis at these contour levels for (x, l) in zip(X,L): if np.isnan(x[0]): continue pylab.plot([x[0],x[0]], [0, l[0]], ':', color='black') pylab.plot([x[1],x[1]], [0, l[1]], ':', color='black') #import pdb ; pdb.set_trace() if self.labels is not None: leg = pylab.legend(self.proxies,self.labels,loc="upper left", fontsize=16) leg.get_frame().set_alpha(0.75) # this will make the box totally transparent leg.get_frame().set_edgecolor('white') # this will make the edges of the #Set the x and y limits pylab.ylim(0,1.05) pylab.yticks(visible=False) pylab.xticks(visible=True, fontsize=16) pylab.xlim(self.axis[0],self.axis[1]) #Add label pylab.xlabel('$A_1$', fontsize=18) return filename
def make_2d_plot(self, name1, name2): #Get the data x = self.reduced_col(name1) y = self.reduced_col(name2) if x.max()-x.min()==0 or y.max()-y.min()==0: return print " (making %s vs %s)" % (name1, name2) #Interpolate using KDE try: n, x_axis, y_axis, like = self.smooth_likelihood(x, y) except np.linalg.LinAlgError: print " -- these two parameters have singular covariance - probably a linear relation" print "Not making a 2D plot of them" return [] figure,filename = self.figure("2D", name1, name2) #Choose levels at which to plot contours contour1=1-0.68 contour2=1-0.95 try: level1, level2, total_mass = self._find_contours(like, x, y, n, x_axis[0], x_axis[-1], y_axis[0], y_axis[-1], contour1, contour2) except: import pdb ; pdb.set_trace() level0 = 1.1 levels = [level2, level1, level0] #Create the figure fig,filename = self.figure("2D", name1, name2) pylab.figure(fig.number) #Make the plot #pylab.figure(figure.number, figsize=(8,7)) keywords = self.keywords_2d() fill = self.options.get("fill", True) if self.fill_list!=None: fill = self.fill_list[self.source.index] imshow = self.imshow[self.source.index] #plot_points = self.options.get("plot_points", False) color = self.colors[self.source.index] linestyle = self.linestyles[self.source.index] if self.linecolors[self.source.index] is not None: linecolor=self.linecolors[self.source.index] else: linecolor=color print self.source.index,linecolor if self.opaque_list[self.source.index]: pylab.contourf(x_axis, y_axis, like.T, [level2,level0], colors=['white']) print "fill",self.source.index, fill if imshow: pylab.imshow(like.T, extent=(x_axis[0], x_axis[-1], y_axis[0], y_axis[-1]), aspect='auto', origin='lower', cmap=cmap_white_to_color(color)) elif fill: if self.fill_colors[self.source.index] is not None: c=self.fill_colors[self.source.index] pylab.contourf(x_axis, y_axis, like.T, [level2,level0], colors=[c[0]]) pylab.contourf(x_axis, y_axis, like.T, [level1,level0], colors=[c[1]]) else: pylab.contourf(x_axis, y_axis, like.T, [level2,level0], colors=[color], alpha=self.alphas[self.source.index]) if self.opaque_centre[self.source.index]: pylab.contourf(x_axis, y_axis, like.T, [level1,level0], colors=[color]) else: pylab.contourf(x_axis, y_axis, like.T, [level1,level0], colors=[color], alpha=self.alphas[self.source.index]) if (not fill) or self.line_list[self.source.index]: print self.source.index,linecolor pylab.contour(x_axis, y_axis, like.T, [level2,level1], colors=[linecolor], linestyles=linestyle, linewidths=self.linewidths[self.source.index]) #if self.labels is not None: # self.proxies.append(pylab.plot([],[], color=color, linestyle=linestyle, linewidth=self.linewidths[self.source.index])[0]) if self.labels is not None: if self.fill_colors[self.source.index] is not None: print self.fill_colors[self.source.index] elif fill: print self.source.index,self.line_list[self.source.index] if self.line_list[self.source.index] is not None: conv=matplotlib.colors.ColorConverter() edgecolor=linecolor self.proxies.append(pylab.Line2D([0,2.5], [0,0], linestyle=linestyle, linewidth=3, color=linecolor)) #facecolor=conv.to_rgba(color,alpha=1-(1-self.alphas[self.source.index])**2) #self.proxies.append(pylab.Rectangle((0,0),1,1,facecolor=facecolor,edgecolor=edgecolor))#,alpha=(1-(1-self.alphas[self.source.index])**2))) else: self.proxies.append(pylab.Rectangle((0,0),1,1,fc=color,edgecolor=color,alpha=(1-(1-self.alphas[self.source.index])**2))) else: self.proxies.append(pylab.plot([],[], color=color, linestyle=linestyle, linewidth=self.linewidths[self.source.index])[0]) if self.plot_points is not None: point_markers=['x','+','^'] for p,m in zip(self.plot_points,point_markers): print p,m pylab.plot(p[0], p[1], m, color='k', markersize=10, markerfacecolor='none') #Do the labels print self.proxies,self.labels if self.labels is not None: leg=pylab.legend(self.proxies,self.labels,loc="upper right") leg.get_frame().set_alpha(0) # this will make the box totally transparent leg.get_frame().set_edgecolor('white') # this will make the edges of the pylab.xlabel(r"$S_8 \equiv \sigma_8 \left( \Omega_\mathrm{m} / 0.3 \right )^{\frac{1}{2}}$",fontsize=22) pylab.ylabel("$A_\mathrm{IA}$", fontsize=22) pylab.tight_layout() if blind: pylab.xticks(visible=False) pylab.yticks(visible=False) np.random.seed(2007) dx = (np.random.rand()*2 - 1.0) * (0.4-1.2)*0.1 #pylab.xticks(np.array([0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2])+dx,["0.4","0.5","0.6","0.7",'0.8','0.9','1.0','1.1','1.2']) #pylab.yticks(np.array([0.6,0.7,0.8,0.9,1.0])+d pylab.xlim(self.axis[0], self.axis[1]) pylab.ylim(self.axis[2], self.axis[3]) pylab.xlim(self.axis[0], self.axis[1]) pylab.ylim(self.axis[2], self.axis[3]) #pylab.axhline(5.0,color="darkviolet",lw=1, ls="--") #print "Plotting bounds line..." return filename