def periodradius(epos, Nth=False, MC=True): f, (ax, axR, axP) = helpers.make_panels(plt) if MC: sim = epos.synthetic_survey ID = sim['ID'] P = sim['P'] Y = sim['Y'] outdir = 'output' title = 'Simulated Detections' else: ID = epos.obs_starID P = epos.obs_xvar Y = epos.obs_yvar outdir = 'survey' title = r'Planet Candidates (score$\geq$0.9)' ''' plot R(P), main panel''' ax.set_title(title) helpers.set_axes(ax, epos, Trim=True) ''' Period side panel ''' helpers.set_axis_distance(axP, epos, Trim=True) #axP.set_yscale('log') #axP.set_ylim([2e-3,5]) #axP.set_yticks([0.01,0.1,1]) #axP.set_yticklabels(['1%','10%','100%']) axP.yaxis.tick_right() axP.yaxis.set_ticks_position('both') #axP.tick_params(axis='y', which='minor',left='off',right='off') #axP.hist(sim['P'], bins=epos.MC_xvar, color='0.7') ''' Radius side panel''' helpers.set_axis_size(axR, epos, Trim=True, In=epos.MassRadius) #axR.set_xscale('log') #axR.set_xlim([2e-3,5]) #axR.set_xticks([1,10,100,1000]) #axR.set_xticklabels(['1','10','100','1000'], rotation=70) for tick in axR.get_xticklabels(): tick.set_rotation(70) #axR.tick_params(axis='x', which='minor',top='off',bottom='off') #axP.tick_params(axis='y', which='minor',left='off',right='off') ''' which multiplanets to color ''' # single, multi= EPOS.multi.indices(sim['ID']) # for k, (label, subset) in enumerate(zip(['single','multi'],[single, multi])): # ax.plot(sim['P'][subset], sim['Y'][subset], ls='', marker='.', mew=0, ms=5.0, \ # label=label) if Nth: single, multi, ksys, multis = EPOS.multi.nth_planet(ID, P) suffix = '.nth' label_single = 'single' else: single, multi, ksys, multis = EPOS.multi.indices(ID) suffix = '' label_single = '1' ax.plot(P[single], Y[single], ls='', marker='.', \ color='0.7', label=label_single) ''' Multiplanets with colors''' Stacked = True plist = [] ylist = [] colors = [] CDF = False #stacked histogram for k, subset in zip(ksys, multis): ht, = ax.plot(P[subset], Y[subset], ls='', marker='.', \ label=k) if not CDF: if Stacked: plist.insert(0, P[subset]) ylist.insert(0, Y[subset]) colors.insert(0, ht.get_color()) # pdf else: axP.hist(P[subset], bins=epos.MC_xvar, color=ht.get_color(), histtype='step') if k == ksys[-1]: axP.hist(P[single], bins=epos.MC_xvar, color='0.7', histtype='step') else: # cumulative Plist = np.sort(P[subset]) axP.step(Plist, np.arange(Plist.size, dtype=float) / Plist.size) if Stacked: plist.append(P[single]) ylist.append(Y[single]) colors.append('0.7') axP.hist(plist, bins=epos.MC_xvar, color=colors, histtype='barstacked') axR.hist(ylist, bins=epos.MC_yvar, orientation='horizontal', color=colors, histtype='barstacked') else: #axR.hist(Y,orientation='horizontal', bins=epos.MC_yvar, color='0.7') axR.hist(Y, orientation='horizontal', bins=epos.MC_yvar, color='k', histtype='step') axR.hist(Y[single], orientation='horizontal', bins=epos.MC_yvar, color='0.7') #ax.legend(loc='lower left', shadow=False, prop={'size':14}, numpoints=1) ax.legend(bbox_to_anchor=(1.0, 1.0), markerscale=2, frameon=True, borderpad=0.2, handlelength=1, handletextpad=0.2) helpers.save(plt, '{}{}/PR.multi{}'.format(epos.plotdir, outdir, suffix))
def panels_radius(epos, Population=False, Occurrence=False, Observation=False, Tag=False, color='C0', clr_obs='C3', Shade=True, Fancy=True, Zoom=False): f, (ax, axR, axP) = helpers.make_panels(plt, Fancy=Fancy) pfm = epos.pfm eta = epos.modelpars.get('eta', Init=True) title = '' if not 'R' in pfm: pfm['R'], _ = epos.MR(pfm['M']) if Tag: # function that return a simulation subset based on the tag subset = { 'Fe/H<=0': lambda tag: tag <= 0, 'Fe/H>0': lambda tag: tag > 0 } ''' Bins ''' dwR = 0.2 # bin width in ln space dwP = 0.3 if Zoom: xbins = np.exp( np.arange(np.log(epos.xzoom[0]), np.log(epos.xzoom[-1]) + dwP, dwP)) ybins = np.exp( np.arange(np.log(epos.yzoom[0]), np.log(epos.yzoom[-1]) + dwR, dwR)) else: xbins = np.exp( np.arange(np.log(epos.xtrim[0]), np.log(epos.xtrim[-1]) + dwP, dwP)) ybins = np.exp( np.arange(np.log(epos.ytrim[0]), np.log(epos.ytrim[-1]) + dwR, dwR)) ''' Plot model occurrence or observed counts''' if Observation: # plot model planets * completeness weights = eta * epos.occurrence['model']['completeness'] / pfm['ns'] else: weights = np.full(pfm['np'], eta / pfm['ns']) if 'draw prob' in pfm and not Tag: prob = pfm['draw prob'][pfm['ID']] weights *= prob * pfm['ns'] # system weights sum up to 1 #nonzero= np.where(prob>0, 1., 0.) #weights*= nonzero*(pfm['np']/nonzero.sum()) # histograms if Tag: for key, f in subset.iteritems(): toplot = f(pfm['tag']) #weights= eta*epos.occurrence['model']['completeness'] \ # *np.where(toplot,1.,0.)/f(pfm['system tag']).sum() weights = np.where(toplot, eta, 0.) / f(pfm['system tag']).sum() axP.hist(pfm['P'], bins=xbins, weights=weights, histtype='step', label=key) axR.hist(pfm['R'], bins=ybins, orientation='horizontal', weights=weights, histtype='step') else: # color have to be 1-element lists ?? axP.hist(pfm['P'], bins=xbins, weights=weights, color=[color]) axR.hist(pfm['R'], bins=ybins, orientation='horizontal', weights=weights, color=[color]) ''' Overplot observations? ''' if Population: assert hasattr(epos, 'func') fname = '.pop' + ('.zoom' if Zoom else '') title = epos.title pps, pdf, pdf_X, pdf_Y = periodradius(epos, Init=True) _, _, pdf_X, _ = periodradius(epos, Init=True, ybin=ybins) _, _, _, pdf_Y = periodradius(epos, Init=True, xbin=xbins) pps, _, _, _ = periodradius(epos, Init=True, xbin=xbins, ybin=ybins) #pdf/= np.max(pdf) #pdflog= np.log10(pdf) # in % levels = np.linspace(0, np.max(pdf)) lines = np.array([0.1, 0.5]) * np.max(pdf) if Shade: ax.contourf(epos.X_in, epos.Y_in, pdf, cmap='Purples', levels=levels) #ax.contour(epos.X_in, epos.Y_in, pdf, levels=lines) # Side panels #print 'pps model= {}'.format(eta) axP.plot(epos.MC_xvar, pdf_X * dwP, marker='', ls='-', color='purple') axR.plot(pdf_Y * dwR, epos.in_yvar, marker='', ls='-', color='purple') else: # renormalize xnorm = axP.get_ylim()[1] / max(pdf_X) ynorm = axR.get_xlim()[1] / max(pdf_Y) axP.plot(epos.MC_xvar, pdf_X * xnorm, marker='', ls='-', color=clr_obs) axR.plot(pdf_Y * ynorm, epos.in_yvar, marker='', ls='-', color=clr_obs) elif Observation: fname = '.obs' + ('.zoom' if Zoom else '') title = epos.title + ': Counts' ax.plot(epos.obs_xvar, epos.obs_yvar, ls='', marker='.', ms=5.0, color='0.5') weights = np.full(epos.obs_xvar.size, 1. / epos.nstars) axP.hist(epos.obs_xvar, bins=xbins, weights=weights, histtype='step', color='0.5') axR.hist(epos.obs_yvar, bins=ybins, weights=weights, orientation='horizontal', histtype='step', color='0.5') elif Occurrence: fname = '.occ' + ('.zoom' if Zoom else '') title = epos.title + r': Occurrence, $\eta={:.2g}$'.format(eta) ax.plot(epos.obs_xvar, epos.obs_yvar, ls='', marker='.', ms=5.0, color='0.5') cut = epos.obs_yvar > 0.45 weights = 1. / (epos.occurrence['planet']['completeness'][cut] * epos.nstars) axP.hist(epos.obs_xvar[cut], bins=xbins, weights=weights, histtype='step', color='k') axR.hist(epos.obs_yvar[cut], bins=ybins, weights=weights, orientation='horizontal', histtype='step', color='k') elif Tag: fname = '.tag' ax.set_title(epos.title + ': Tag') axP.legend(frameon=False, fontsize='small') # for k, tag in enumerate(subset): # axP.text(0.98,0.95-0.05*k,tag,ha='right',va='top',color='C1', # transform=axP.transAxes) else: fname = '' if Fancy: plt.suptitle(title, ha='center') #, x=0.05) else: ax.set_title(title) ''' plot main panel''' #helpers.set_axes(ax, epos, Trim=True) helpers.set_axes(ax, epos, Trim=True) if Tag: for key, f in subset.iteritems(): todraw = f(pfm['tag']) ax.plot(pfm['P'][todraw], pfm['R'][todraw], **fmt_symbol) elif 'draw prob' in pfm: #fmt_symbol['alpha']= 0.6*pfm['draw prob'][pfm['ID']] # alpha can't be array todraw = pfm['draw prob'][pfm['ID']] > 0 ax.plot(pfm['P'][todraw], pfm['R'][todraw], color=color, **fmt_symbol) else: ax.plot(pfm['P'], pfm['R'], color=color, **fmt_symbol) ''' Period side panel ''' #axP.yaxis.tick_right() #axP.yaxis.set_ticks_position('both') #axP.tick_params(axis='y', which='minor',left='off',right='off') helpers.set_axis_distance(axP, epos, Trim=True) ''' Mass side panel''' helpers.set_axis_size(axR, epos, Trim=True) #, In= epos.MassRadius) helpers.save(plt, '{}model/input.radius{}'.format(epos.plotdir, fname))
def periodratio_size(epos, color='C1'): pfm = epos.pfm f, (ax, axR, axP) = helpers.make_panels_right(plt) ''' Inc-sma''' ax.set_title('Input Multi-planets {}'.format(epos.name)) axP.set_xlabel('Period ratio') #ax.set_ylabel(r'Size [$R_\bigoplus$]') ax.set_xlim(0.9, 10) #ax.set_ylim(0.3,20) ax.set_xscale('log') #ax.set_yscale('log') helpers.set_axis_size(ax, epos, Trim=True) ''' grids ''' dP = np.logspace(0, 1) dP_bins = np.logspace(0, 1, 15) # exoplanet data + hist ax.plot(epos.multi['Pratio'], epos.multi['Rpair'], ls='', marker='.', ms=5.0, color='0.5') #ax.axhline(np.median(pfm['inc']), ls='--') single = pfm['dP'] == np.nan inner = pfm['dP'] == 1 nth = pfm['dP'] > 1 # print pfm['dP'][single] # print pfm['dP'][nth] # print pfm['dP'][inner] # print pfm['dP'][nth].size, pfm['np'] # ok ax.plot(pfm['dP'][single], pfm['R'][single], color='0.7', **fmt_symbol) ax.plot(pfm['dP'][nth], pfm['R'][nth], color=color, **fmt_symbol) ax.plot(pfm['dP'][inner], pfm['R'][inner], color='C1', **fmt_symbol) ''' Histogram Period Ratio''' axP.set_xscale('log') axP.set_xlim(0.9, 10) axP.hist(pfm['dP'][nth], bins=dP_bins, color=color) ax.axvline(np.median(pfm['dP'][nth]), ls='--', color=color) ''' Model best-fit ''' xmax = axP.get_ylim()[-1] with np.errstate(divide='ignore'): Dgrid = np.log10(2. * (dP**(2. / 3.) - 1.) / (dP**(2. / 3.) + 1.)) Dgrid[0] = -2 for scale, ls in zip([-0.30, -0.37, -0.41], [':', '--', ':']): pdf = scipy.stats.norm(scale, 0.19).pdf(Dgrid) pdf *= xmax / max(pdf) axP.plot(dP, pdf, ls=ls, color='purple') pscale = np.interp(scale, Dgrid, dP) ax.axvline(pscale, color='purple', ls=ls) ''' Raw data''' scale = 1. * pfm['dP'][nth].size / epos.multi['Pratio'].size weights = np.full(epos.multi['Pratio'].size, scale) axP.hist(epos.multi['Pratio'], bins=dP_bins, weights=weights, histtype='step', color='0.5', zorder=1) ''' Histogram Planet Size''' axR.set_yscale('log') axR.set_ylim(ax.get_ylim()) radius = np.geomspace(*ax.get_ylim(), num=15) axR.hist(pfm['R'][inner], bins=radius, orientation='horizontal', color='C1', label='Inner Planet') #helpers.set_axes(ax, epos, Trim=True) #helpers.set_axis_distance(axP, epos, Trim=True) ''' Linear regression ''' try: # data slope, intercept, r_value, p_value, std_err= \ scipy.stats.linregress(np.log(epos.multi['Pratio']), np.log(epos.multi['Rpair'])) ax.plot(dP, np.exp(intercept + slope * np.log(dP)), label='r={:.2f}'.format(r_value), marker='', ls='-', color='0.5') #print slope, intercept, r_value, p_value slope, intercept, r_value, p_value, std_err= \ scipy.stats.linregress(np.log(pfm['dP'][nth]), np.log(pfm['R'][nth])) ax.plot(dP, np.exp(intercept + slope * np.log(dP)), label='r={:.2f}'.format(r_value), marker='', ls='-', color=color) ax.legend(loc='lower right') except Exception as e: print(e) helpers.save(plt, epos.plotdir + 'model/Pratio-size')
def panels(epos, MCMC=False): f, (ax, axR, axP)= helpers.make_panels(plt) sim=epos.synthetic_survey clr_bf= 'g' ''' plot R(P), main panel''' ax.set_title('Simulated Detections') helpers.set_axes(ax, epos, Trim=True) if epos.MonteCarlo: ax.plot(sim['P'], sim['Y'], ls='', marker='.', color=clr_bf if MCMC else 'C0') else: levels= np.linspace(0,np.max(sim['pdf'])) ax.contourf(epos.MC_xvar, epos.MC_yvar, sim['pdf'].T, cmap='Blues', levels=levels) ''' Period side panel ''' helpers.set_axis_distance(axP, epos, Trim=True) #axP.set_yscale('log') #axP.set_ylim([2e-3,5]) #axP.set_yticks([0.01,0.1,1]) #axP.set_yticklabels(['1%','10%','100%']) axP.yaxis.tick_right() axP.yaxis.set_ticks_position('both') #axP.tick_params(axis='y', which='minor',left='off',right='off') ''' Radius side panel''' helpers.set_axis_size(axR, epos, Trim=True) #, In= epos.MassRadius) #axR.set_xscale('log') #axR.set_xlim([2e-3,5]) #axR.set_xticks([1,10,100,1000]) #axR.set_xticklabels(['1','10','100','1000'], rotation=70) for tick in axR.get_xticklabels(): tick.set_rotation(70) #axR.tick_params(axis='x', which='minor',top='off',bottom='off') #axP.tick_params(axis='y', which='minor',left='off',right='off') ''' Histograms / posterior samples ''' try: xbins= np.geomspace(*epos.xzoom, num=20) ybins= np.geomspace(*epos.yzoom, num=10) except: xbins= np.logspace(*np.log10(epos.xzoom), num=20) ybins= np.logspace(*np.log10(epos.yzoom), num=10) xscale= np.log(xbins[1]/xbins[0]) yscale= np.log(ybins[1]/ybins[0]) if MCMC: histkeys= {'color':'b', 'alpha':0.1} for ss in epos.ss_sample: if epos.MonteCarlo: axP.hist(ss['P zoom'], bins=xbins, histtype='step', **histkeys) axR.hist(ss['Y zoom'], bins=ybins, orientation='horizontal', \ histtype='step', **histkeys) else: axP.plot(ss['P zoom'], ss['P zoom pdf']*xscale, marker='', ls='-', **histkeys) axR.plot(ss['Y zoom pdf']*yscale, ss['Y zoom'], marker='', ls='-', **histkeys) histdict= {'histtype':'step', 'color':clr_bf} else: histdict={} if epos.MonteCarlo: axP.hist(sim['P zoom'], bins=xbins, **histdict) axR.hist(sim['Y zoom'], bins=ybins, orientation='horizontal', **histdict) else: axP.plot(sim['P zoom'], sim['P zoom pdf']*xscale, marker='', ls='-') axR.plot(sim['Y zoom pdf']*yscale, sim['Y zoom'], marker='', ls='-') ''' Observations''' axP.hist(epos.obs_zoom['x'], bins=xbins,histtype='step', color='C1') axR.hist(epos.obs_zoom['y'], bins=ybins, orientation='horizontal',histtype='step', color='C1') ''' Box/ lines''' if epos.Zoom: for zoom in epos.xzoom: axP.axvline(zoom, ls='--', color='k') for zoom in epos.yzoom: axR.axhline(zoom, ls='--', color='k') ax.add_patch(patches.Rectangle( (epos.xzoom[0],epos.yzoom[0]), epos.xzoom[1]-epos.xzoom[0], epos.yzoom[1]-epos.yzoom[0],fill=False, zorder=1) ) #ax.legend(loc='lower left', shadow=False, prop={'size':14}, numpoints=1) fdir= 'mcmc' if MCMC else 'output' helpers.save(plt, '{}{}/pdf.zoom'.format(epos.plotdir, fdir))