def calc_effsel(args, options, sample=None): # Work-horse function to compute the effective selection function, # sample is a data sample of stars to consider for the (JK,Z) sampling # Setup selection function selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) else: # Setup selection function apo = apogee.select.apogeeSelect() # Delete these because they're big and we don't need them del apo._specdata del apo._photdata save_pickles(selectFile, apo) # Get the full data sample for the locations (need all locations where # stars could be observed, so the whole sample, not just the subsample # being analyzed) data = get_rcsample() locations = list(set(list(data['LOCATION_ID']))) # Load the dust map and setup the effective selection function if options.dmap.lower() == 'green15': dmap3d = mwdust.Green15(filter='2MASS H') elif options.dmap.lower() == 'marshall06': dmap3d = mwdust.Marshall06(filter='2MASS H') elif options.dmap.lower() == 'drimmel03': dmap3d = mwdust.Drimmel03(filter='2MASS H') elif options.dmap.lower() == 'sale14': dmap3d = mwdust.Sale14(filter='2MASS H') elif options.dmap.lower() == 'zero': dmap3d = mwdust.Zero(filter='2MASS H') # Sample the M_H distribution if options.samplemh: if sample is None: sample = data MH = sample['H0'] - sample['RC_DM'] MH = numpy.random.permutation(MH)[:1000] # do 1,000 max else: MH = -1.49 apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=dmap3d, MH=MH) # Distances at which to calculate the effective selection function distmods = numpy.linspace(options.dm_min, options.dm_max, options.ndm) ds = 10.**(distmods / 5 - 2.) # Now compute all selection functions out= multi.parallel_map((lambda x: _calc_effsel_onelocation(\ locations[x],apof,apo,ds)), range(len(locations)), numcores=numpy.amin([len(locations), multiprocessing.cpu_count(),options.multi])) # Save out out = numpy.array(out) save_pickles(args[0], locations, out, distmods, ds) return None
def plot_effsel_location(location, plotname): # Setup selection function selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) else: # Setup selection function apo = apogee.select.apogeeSelect() # Delete these because they're big and we don't need them del apo._specdata del apo._photdata save_pickles(selectFile, apo) effselFile = '../savs/effselfunc-%i.sav' % location if not os.path.exists(effselFile): # Distances at which to calculate the effective selection function distmods = numpy.linspace(7., 15.5, 301) ds = 10.**(distmods / 5 - 2.) # Setup default effective selection function do_samples = True gd = mwdust.Green15(filter='2MASS H', load_samples=do_samples) apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=gd) sf_default = apof(location, ds) # Also calculate for a sample of MH data = get_rcsample() MH = data['H0'] - data['RC_DM'] MH = numpy.random.permutation(MH)[:1000] sf_jkz = apof(location, ds, MH=MH) # Go through the samples sf_samples = numpy.zeros((20, len(ds))) if do_samples: for ii in range(20): # Swap in a sample for bestfit in the Green et al. (2015) dmap gd.substitute_sample(ii) apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=gd) sf_samples[ii] = apof(location, ds) zerodust = mwdust.Zero(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=zerodust) sf_zero = apof(location, ds) drimmel = mwdust.Drimmel03(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=drimmel) sf_drimmel = apof(location, ds) marshall = mwdust.Marshall06(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=marshall) try: sf_marshall = apof(location, ds) except IndexError: sf_marshall = -numpy.ones_like(ds) sale = mwdust.Sale14(filter='2MASS H') apof = apogee.select.apogeeEffectiveSelect(apo, dmap3d=sale) try: sf_sale = apof(location, ds) except (TypeError, ValueError): sf_sale = -numpy.ones_like(ds) save_pickles(effselFile, distmods, sf_default, sf_jkz, sf_samples, sf_zero, sf_drimmel, sf_marshall, sf_sale) else: with open(effselFile, 'rb') as savefile: distmods = pickle.load(savefile) sf_default = pickle.load(savefile) sf_jkz = pickle.load(savefile) sf_samples = pickle.load(savefile) sf_zero = pickle.load(savefile) sf_drimmel = pickle.load(savefile) sf_marshall = pickle.load(savefile) sf_sale = pickle.load(savefile) # Now plot bovy_plot.bovy_print(fig_height=3.) rc('text.latex', preamble=r'\usepackage{amsmath}' + '\n' + r'\usepackage{amssymb}' + '\n' + r'\usepackage{yfonts}') if _PLOTDIST: distmods = 10.**(distmods / 5 - 2.) xrange = [0., 12.] xlabel = r'$D\,(\mathrm{kpc})$' ylabel = r'$\textswab{S}(\mathrm{location},D)$' else: xrange = [7., 15.8], xlabel = r'$\mathrm{distance\ modulus}\ \mu$' ylabel = r'$\textswab{S}(\mathrm{location},\mu)$' line_default = bovy_plot.bovy_plot(distmods, sf_default, 'b-', lw=_LW, zorder=12, xrange=xrange, xlabel=xlabel, yrange=[0., 1.2 * numpy.amax(sf_zero)], ylabel=ylabel) pyplot.fill_between(distmods, sf_default-_EXAGGERATE_ERRORS\ *(sf_default-numpy.amin(sf_samples,axis=0)), sf_default+_EXAGGERATE_ERRORS\ *(numpy.amax(sf_samples,axis=0)-sf_default), color='0.65',zorder=0) line_jkz = bovy_plot.bovy_plot(distmods, sf_jkz, 'g-.', lw=2. * _LW, overplot=True, zorder=13) line_zero = bovy_plot.bovy_plot(distmods, sf_zero, 'k--', lw=_LW, overplot=True, zorder=7) line_drimmel = bovy_plot.bovy_plot(distmods, sf_drimmel, '-', color='gold', lw=_LW, overplot=True, zorder=8) line_marshall = bovy_plot.bovy_plot(distmods, sf_marshall, 'r-', lw=_LW, overplot=True, zorder=9) line_sale = bovy_plot.bovy_plot(distmods, sf_sale, 'c-', lw=_LW, overplot=True, zorder=10) if location == 4378: pyplot.legend( (line_default[0], line_jkz[0], line_zero[0]), (r'$\mathrm{Green\ et\ al.\ (2015)}$', r'$\mathrm{Green\ et\ al.} + p(M_H)$', r'$\mathrm{zero\ extinction}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) elif location == 4312: pyplot.legend( (line_sale[0], line_marshall[0], line_drimmel[0]), (r'$\mathrm{Sale\ et\ al.\ (2014)}$', r'$\mathrm{Marshall\ et\ al.\ (2006)}$', r'$\mathrm{Drimmel\ et\ al.\ (2003)}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) # Label lcen, bcen = apo.glonGlat(location) if numpy.fabs(bcen) < 0.1: bcen = 0. bovy_plot.bovy_text(r'$(l,b) = (%.1f,%.1f)$' % (lcen, bcen), top_right=True, size=16.) bovy_plot.bovy_end_print(plotname) return None
def plot_ah_location(location, plotname): # Setup selection function selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) else: # Setup selection function apo = apogee.select.apogeeSelect() # Delete these because they're big and we don't need them del apo._specdata del apo._photdata save_pickles(selectFile, apo) glon, glat = apo.glonGlat(location) glon = glon[0] glat = glat[0] ahFile = '../savs/ah-%i.sav' % location if not os.path.exists(ahFile): # Distances at which to calculate the extinction distmods = numpy.linspace(7., 15.5, 301) ds = 10.**(distmods / 5 - 2.) # Setup Green et al. (2015) dust map gd = mwdust.Green15(filter='2MASS H') pa, ah = gd.dust_vals_disk(glon, glat, ds, apo.radius(location)) meanah_default = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah, axis=0) / numpy.sum(pa) stdah_default= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\ *ah**2.,axis=0)\ /numpy.sum(pa)-meanah_default**2.) # Marshall et al. (2006) marshall = mwdust.Marshall06(filter='2MASS H') try: pa, ah = marshall.dust_vals_disk(glon, glat, ds, apo.radius(location)) except IndexError: meanah_marshall = -numpy.ones_like(ds) stdah_marshall = -numpy.ones_like(ds) else: meanah_marshall = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah, axis=0) / numpy.sum(pa) stdah_marshall= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\ *ah**2.,axis=0)\ /numpy.sum(pa)-meanah_marshall**2.) if True: # Drimmel et al. (2003) drimmel = mwdust.Drimmel03(filter='2MASS H') pa, ah = drimmel.dust_vals_disk(glon, glat, ds, apo.radius(location)) meanah_drimmel = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah, axis=0) / numpy.sum(pa) stdah_drimmel= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\ *ah**2.,axis=0)\ /numpy.sum(pa)-meanah_drimmel**2.) else: meanah_drimmel = -numpy.ones_like(ds) stdah_drimmel = -numpy.ones_like(ds) if True: # Sale et al. (2014) sale = mwdust.Sale14(filter='2MASS H') try: pa, ah = sale.dust_vals_disk(glon, glat, ds, apo.radius(location)) meanah_sale = numpy.sum(numpy.tile(pa, (len(ds), 1)).T * ah, axis=0) / numpy.sum(pa) except (TypeError, ValueError): meanah_sale = -numpy.ones_like(ds) stdah_sale = -numpy.ones_like(ds) else: stdah_sale= numpy.sqrt(numpy.sum(numpy.tile(pa,(len(ds),1)).T\ *ah**2.,axis=0)\ /numpy.sum(pa)-meanah_sale**2.) else: meanah_sale = -numpy.ones_like(ds) stdah_sale = -numpy.ones_like(ds) save_pickles(ahFile, distmods, meanah_default, stdah_default, meanah_marshall, stdah_marshall, meanah_drimmel, stdah_drimmel, meanah_sale, stdah_sale) else: with open(ahFile, 'rb') as savefile: distmods = pickle.load(savefile) meanah_default = pickle.load(savefile) stdah_default = pickle.load(savefile) meanah_marshall = pickle.load(savefile) stdah_marshall = pickle.load(savefile) meanah_drimmel = pickle.load(savefile) stdah_drimmel = pickle.load(savefile) meanah_sale = pickle.load(savefile) stdah_sale = pickle.load(savefile) # Now plot bovy_plot.bovy_print(fig_height=3.) if _PLOTDIST: distmods = 10.**(distmods / 5 - 2.) xrange = [0., 12.] xlabel = r'$D\,(\mathrm{kpc})$' else: xrange = [7., 15.8], xlabel = r'$\mathrm{distance\ modulus}\ \mu$' ylabel = r'$A_H$' yrange = [ 0., 1.2 * numpy.amax( numpy.vstack( (meanah_default + stdah_default, meanah_marshall + stdah_marshall, meanah_drimmel + stdah_drimmel, meanah_sale + stdah_sale))) ] line_default = bovy_plot.bovy_plot(distmods, meanah_default, 'b-', lw=_LW, zorder=12, xrange=xrange, xlabel=xlabel, yrange=yrange, ylabel=ylabel) pyplot.fill_between(distmods, meanah_default - stdah_default, meanah_default + stdah_default, hatch='/', facecolor=(0, 0, 0, 0), color='b', lw=0.25, zorder=4) line_marshall = bovy_plot.bovy_plot(distmods, meanah_marshall, 'r-', lw=_LW, overplot=True, zorder=8) pyplot.fill_between(distmods, meanah_marshall - stdah_marshall, meanah_marshall + stdah_marshall, hatch='\\', facecolor=(0, 0, 0, 0), color='r', lw=0.25, zorder=2) line_drimmel = bovy_plot.bovy_plot(distmods, meanah_drimmel, '-', lw=_LW, color='gold', overplot=True, zorder=7) pyplot.fill_between(distmods, meanah_drimmel - stdah_drimmel, meanah_drimmel + stdah_drimmel, hatch='///', facecolor=(0, 0, 0, 0), color='gold', lw=0.25, zorder=1) line_sale = bovy_plot.bovy_plot(distmods, meanah_sale, '-', lw=_LW, color='c', overplot=True, zorder=9) pyplot.fill_between(distmods, meanah_sale - stdah_sale, meanah_sale + stdah_sale, hatch='//', facecolor=(0, 0, 0, 0), color='c', lw=0.25, zorder=3) if True: data = get_rcsample() data = data[data['LOCATION_ID'] == location] bovy_plot.bovy_plot(data['RC_DIST'], data['AK_TARG'] * 1.55, 'ko', zorder=20, overplot=True, ms=2.) if location == 4318: pyplot.legend( (line_default[0], line_sale[0]), (r'$\mathrm{Green\ et\ al.\ (2015)}$', r'$\mathrm{Sale\ et\ al.\ (2014)}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) elif location == 4242: pyplot.legend( (line_marshall[0], line_drimmel[0]), (r'$\mathrm{Marshall\ et\ al.\ (2006)}$', r'$\mathrm{Drimmel\ et\ al.\ (2003)}$'), loc='lower right', #bbox_to_anchor=(.91,.375), numpoints=8, prop={'size': 14}, frameon=False) # Label lcen, bcen = apo.glonGlat(location) if numpy.fabs(bcen) < 0.1: bcen = 0. bovy_plot.bovy_text(r'$(l,b) = (%.1f,%.1f)$' % (lcen, bcen), top_right=True, size=16.) bovy_plot.bovy_end_print(plotname, dpi=300, bbox_extra_artists=pyplot.gca().get_children(), bbox_inches='tight') return None
def generate(locations, type='exp', sample='lowlow', extmap='green15', nls=101, nmock=1000, H0=-1.49, _dmapg15=None, ncpu=1): """ NAME: generate PURPOSE: generate mock data following a given density INPUT: locations - locations to be included in the sample type= ('exp') type of density profile to sample from sample= ('lowlow') for selecting mock parameters extmap= ('green15') extinction map to use ('marshall06' and others use Green15 to fill in unobserved regions) nls= (101) number of longitude bins to use for each field nmock= (1000) number of mock data points to generate H0= (-1.49) absolute magnitude (can be array w/ sampling spread) ncpu= (1) number of cpus to use to compute the probability OUTPUT: mockdata recarray with tags 'RC_GALR_H', 'RC_GALPHI_H', 'RC_GALZ_H' HISTORY: 2015-04-03 - Written - Bovy (IAS) """ if isinstance(H0, float): H0 = [H0] # Setup the density function and its initial parameters rdensfunc = fitDens._setup_densfunc(type) mockparams = _setup_mockparams_densfunc(type, sample) densfunc = lambda x, y, z: rdensfunc(x, y, z, params=mockparams) # Setup the extinction map global dmap global dmapg15 if _dmapg15 is None: dmapg15 = mwdust.Green15(filter='2MASS H') else: dmapg15 = _dmapg15 if isinstance(extmap, mwdust.DustMap3D.DustMap3D): dmap = extmap elif extmap.lower() == 'green15': dmap = dmapg15 elif extmap.lower() == 'marshall06': dmap = mwdust.Marshall06(filter='2MASS H') elif extmap.lower() == 'sale14': dmap = mwdust.Sale14(filter='2MASS H') elif extmap.lower() == 'drimmel03': dmap = mwdust.Drimmel03(filter='2MASS H') # Use brute-force rejection sampling to make no approximations # First need to estimate the max probability to use in rejection; # Loop through all locations and compute sampling probability on grid in # (l,b,D) # First restore the APOGEE selection function (assumed pre-computed) global apo selectFile = '../savs/selfunc-nospdata.sav' if os.path.exists(selectFile): with open(selectFile, 'rb') as savefile: apo = pickle.load(savefile) # Now compute the necessary coordinate transformations and evaluate the # maximum probability distmods = numpy.linspace(7., 15.5, 301) ds = 10.**(distmods / 5 - 2.) nbs = nls lnprobs = numpy.empty((len(locations), len(distmods), nbs, nls)) radii = [] lcens, bcens = [], [] lnprobs = multi.parallel_map(lambda x: _calc_lnprob( locations[x], nls, nbs, ds, distmods, H0, densfunc), range(len(locations)), numcores=numpy.amin([ len(locations), multiprocessing.cpu_count(), ncpu ])) lnprobs = numpy.array(lnprobs) for ll, loc in enumerate(locations): lcen, bcen = apo.glonGlat(loc) rad = apo.radius(loc) radii.append(rad) # save for later lcens.append(lcen[0]) bcens.append(bcen[0]) maxp = (numpy.exp(numpy.nanmax(lnprobs)) - 10.**-8.) * 1.1 # Just to be sure # Now generate mock data using rejection sampling nout = 0 arlocations = numpy.array(locations) arradii = numpy.array(radii) arlcens = numpy.array(lcens) arbcens = numpy.array(bcens) out = numpy.recarray((nmock, ), dtype=[('RC_DIST_H', 'f8'), ('RC_DM_H', 'f8'), ('RC_GALR_H', 'f8'), ('RC_GALPHI_H', 'f8'), ('RC_GALZ_H', 'f8')]) while nout < nmock: nnew = 2 * (nmock - nout) # nnew new locations locIndx = numpy.floor( numpy.random.uniform(size=nnew) * len(locations)).astype('int') newlocations = arlocations[locIndx] # Point within these locations newds_coord = numpy.random.uniform(size=nnew) newds= 10.**((newds_coord*(numpy.amax(distmods)-numpy.amin(distmods))\ +numpy.amin(distmods))/5.-2.) newdls_coord = numpy.random.uniform(size=nnew) newdls= newdls_coord*2.*arradii[locIndx]\ -arradii[locIndx] newdbs_coord = numpy.random.uniform(size=nnew) newdbs= newdbs_coord*2.*arradii[locIndx]\ -arradii[locIndx] newr2s = newdls**2. + newdbs**2. keepIndx = newr2s < arradii[locIndx]**2. newlocations = newlocations[keepIndx] newds_coord = newds_coord[keepIndx] newdls_coord = newdls_coord[keepIndx] newdbs_coord = newdbs_coord[keepIndx] newds = newds[keepIndx] newdls = newdls[keepIndx] newdbs = newdbs[keepIndx] newls = newdls + arlcens[locIndx][keepIndx] newbs = newdbs + arbcens[locIndx][keepIndx] # Reject? tps = numpy.zeros_like(newds) for nloc in list(set(newlocations)): lindx = newlocations == nloc pindx = arlocations == nloc coord = numpy.array([ newds_coord[lindx] * (len(distmods) - 1.), newdbs_coord[lindx] * (nbs - 1.), newdls_coord[lindx] * (nls - 1.) ]) tps[lindx]= \ numpy.exp(ndimage.interpolation.map_coordinates(\ lnprobs[pindx][0], coord,cval=-10., order=1))-10.**-8. XYZ = bovy_coords.lbd_to_XYZ(newls, newbs, newds, degree=True) Rphiz = bovy_coords.XYZ_to_galcencyl(XYZ[:, 0], XYZ[:, 1], XYZ[:, 2], Xsun=define_rcsample._R0, Ysun=0., Zsun=define_rcsample._Z0) testp = numpy.random.uniform(size=len(newds)) * maxp keepIndx = tps > testp if numpy.sum(keepIndx) > nmock - nout: rangeIndx = numpy.zeros(len(keepIndx), dtype='int') rangeIndx[keepIndx] = numpy.arange(numpy.sum(keepIndx)) keepIndx *= (rangeIndx < nmock - nout) out['RC_DIST_H'][nout:nout + numpy.sum(keepIndx)] = newds[keepIndx] out['RC_DM_H'][nout:nout+numpy.sum(keepIndx)]= newds_coord[keepIndx]*(numpy.amax(distmods)-numpy.amin(distmods))\ +numpy.amin(distmods) out['RC_GALR_H'][nout:nout + numpy.sum(keepIndx)] = Rphiz[0][keepIndx] out['RC_GALPHI_H'][nout:nout + numpy.sum(keepIndx)] = Rphiz[1][keepIndx] out['RC_GALZ_H'][nout:nout + numpy.sum(keepIndx)] = Rphiz[2][keepIndx] nout = nout + numpy.sum(keepIndx) return (out, lnprobs)
import mwdust #import healpy as hp #import time from pylab import * from matplotlib.colors import LogNorm from mpl_toolkits.axes_grid1.inset_locator import inset_axes #mpl.rcParams['pdf.fonttype'] = 42 #mpl.rcParams['ps.fonttype'] = 42 #mpl.rcParams['text.usetex'] = True #marshall = mwdust.Marshall06(sf10=True) drimmel = mwdust.Drimmel03(sf10=True) green = mwdust.Green15(sf10=True) sale = mwdust.Sale14(sf10=True) zero = mwdust.Zero(sf10=True) #sfd = mwdust.SFD(sf10=True) #combined = mwdust.Combined15(sf10=True) #D = np.array([0.25,0.5,1.,2.,3.,4.,5.,6.]) Ndist = 1000 D = np.linspace(0.05,10.,Ndist) L = 205.09 # 54.7 B = -0.93 #0.08 f = open("Green15.dat", "w") for i in range(Ndist): f.write("%.15E %.15E\n"%(D[i],green(L,B,D)[i])) f.close() f = open("Drimmel03.dat", "w")