def plotCombinedPDF(options,args): if options.sample.lower() == 'g': npops= 62 elif options.sample.lower() == 'k': npops= 54 if options.sample.lower() == 'g': savefile= open('binmapping_g.sav','rb') elif options.sample.lower() == 'k': savefile= open('binmapping_k.sav','rb') fehs= pickle.load(savefile) afes= pickle.load(savefile) savefile.close() #First calculate the derivative properties if not options.group is None: gafes, gfehs, legend= getMultiComparisonBins(options) else: legend= None if not options.multi is None: PDFs= multi.parallel_map((lambda x: calcAllPDFs(x,options,args)), range(npops), numcores=numpy.amin([options.multi, npops, multiprocessing.cpu_count()])) else: PDFs= [] for ii in range(npops): PDFs.append(calcAllPDFs(ii,options,args)) #Go through and combine combined_lnpdf= numpy.zeros((options.nrds,options.nfhs)) for ii in range(npops): if not options.group is None: if numpy.amin((gfehs-fehs[ii])**2./0.1+(gafes-afes[ii])**2./0.0025) > 0.001: continue combined_lnpdf+= PDFs[ii] alogl= combined_lnpdf-numpy.nanmax(combined_lnpdf) #Now plot bovy_plot.bovy_print() bovy_plot.bovy_dens2d(numpy.exp(alogl).T, origin='lower',cmap='gist_yarg', interpolation='nearest', xrange=[1.9,3.5],yrange=[-1./32.,1.+1./32.], xlabel=r'$R_d\ (\mathrm{kpc})$',ylabel=r'$f_h$') if not legend is None: bovy_plot.bovy_text(legend,top_left=True, size=14.) bovy_plot.bovy_end_print(options.outfilename) #Calculate and print derived properties derivProps= rawDerived(alogl,options, vo=options.fixvc/_REFV0,zh=options.fixzh, dlnvcdlnr=options.dlnvcdlnr) for key in derivProps.keys(): if not '_err' in key: print key, derivProps[key], derivProps[key+'_err'], \ derivProps[key]/derivProps[key+'_err'] return None
def plotVelComparisonDFMulti(options,args): #Read data etc. print "Reading the data ..." raw= read_rawdata(options) #Bin the data binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe) #Map the bins with ndata > minndata in 1D fehs, afes= [], [] for ii in range(len(binned.fehedges)-1): for jj in range(len(binned.afeedges)-1): data= binned(binned.feh(ii),binned.afe(jj)) if len(data) < options.minndata: continue #print binned.feh(ii), binned.afe(jj), len(data) fehs.append(binned.feh(ii)) afes.append(binned.afe(jj)) nabundancebins= len(fehs) fehs= numpy.array(fehs) afes= numpy.array(afes) gafes, gfehs, left_legend= getMultiComparisonBins(options) M= len(gfehs) if options.andistances: distancefacs= numpy.zeros_like(fehs) gdistancefacs= numpy.zeros_like(gfehs) for jj in range(M): #Find pop corresponding to this bin ii= numpy.argmin((gfehs[jj]-fehs)**2./0.1+(gafes[jj]-afes)**2./0.0025) print ii #Get the relevant data data= binned(fehs[ii],afes[ii]) distancefacs[ii]= AnDistance.AnDistance(data.dered_g-data.dered_r, data.feh) gdistancefacs[jj]= distancefacs[ii] options.fixdm= numpy.log10(distancefacs[ii])*5. #Apply distance factor to the data newraw= read_rawdata(options) newbinned= pixelAfeFeh(newraw,dfeh=options.dfeh,dafe=options.dafe) thisdataIndx= binned.callIndx(fehs[ii],afes[ii]) binned.data.xc[thisdataIndx]= newbinned.data.xc[thisdataIndx] binned.data.yc[thisdataIndx]= newbinned.data.yc[thisdataIndx] binned.data.zc[thisdataIndx]= newbinned.data.zc[thisdataIndx] binned.data.plate[thisdataIndx]= newbinned.data.plate[thisdataIndx] binned.data.dered_r[thisdataIndx]= newbinned.data.dered_r[thisdataIndx] else: distancefacs=numpy.ones_like(fehs) gdistancefacs=numpy.ones_like(gfehs) ##########POTENTIAL PARAMETERS#################### potparams1= numpy.array([numpy.log(2.5/8.),options.fixvc/220., numpy.log(400./8000.),0.2,0.]) if options.group.lower() == 'aenhanced': potparams2= numpy.array([numpy.log(2.8/8.),options.fixvc/220., numpy.log(400./8000.),0.266666666,0.]) potparams3= numpy.array([numpy.log(2.8/8.),options.fixvc/220., numpy.log(400./8000.),0.8,0.]) elif options.group.lower() == 'aintermediate': potparams2= numpy.array([numpy.log(3.0/8.),options.fixvc/220., numpy.log(400./8000.),0.3333333333333,0.]) potparams3= numpy.array([numpy.log(3.0/8.),options.fixvc/220., numpy.log(400./8000.),0.933333333333,0.]) elif options.group.lower() == 'apoor': potparams2= numpy.array([numpy.log(2.6/8.),options.fixvc/220., numpy.log(400./8000.),0.4,0.]) potparams3= numpy.array([numpy.log(2.6/8.),options.fixvc/220., numpy.log(400./8000.),1.0,0.]) options.potential= 'dpdiskplhalofixbulgeflatwgasalt' #Check whether fits exist, if not, pop removeBins= numpy.ones(M,dtype='bool') for jj in range(M): #Find pop corresponding to this bin pop= numpy.argmin((gfehs[jj]-fehs)**2./0.1+(gafes[jj]-afes)**2./0.0025) #Load savefile if not options.init is None: #Load initial parameters from file savename= options.init spl= savename.split('.') newname= '' for ll in range(len(spl)-1): newname+= spl[ll] if not ll == len(spl)-2: newname+= '.' newname+= '_%i.' % pop newname+= spl[-1] if not os.path.exists(newname): removeBins[jj]= False else: raise IOError("base filename not specified ...") if numpy.sum(removeBins) == 0: raise IOError("None of the group bins have been fit ...") elif numpy.sum(removeBins) < M: #Some bins have not been fit yet, and have to be remove gfehs= list((numpy.array(gfehs))[removeBins]) gafes= list((numpy.array(gafes))[removeBins]) print "Only using %i bins out of %i ..." % (numpy.sum(removeBins),M) M= len(gfehs) data= [] zs= [] velps= numpy.zeros((len(binned.data),options.nv)) velps[:,:]= numpy.nan velps2= numpy.zeros((len(binned.data),options.nv)) velps2[:,:]= numpy.nan velps3= numpy.zeros((len(binned.data),options.nv)) velps3[:,:]= numpy.nan cumulndata= 0 if options.type.lower() == 'vz': if options.group == 'aenhanced': vs= numpy.linspace(-180.,180.,options.nv) xrange=[-180.,180.] bins= 39 elif options.group == 'aintermediate': vs= numpy.linspace(-150.,150.,options.nv) xrange=[-150.,150.] bins= 33 else: # options.group == 'aenhanced': vs= numpy.linspace(-120.,120.,options.nv) xrange=[-140.,140.] bins= 26 xlabel=r'$V_Z\ (\mathrm{km\,s}^{-1})$' elif options.type.lower() == 'vr': if options.group == 'aenhanced': vs= numpy.linspace(-220.,220.,options.nv) xrange=[-220.,220.] bins= 39 else: # options.group == 'aenhanced': vs= numpy.linspace(-150.,150.,options.nv) xrange=[-150.,150.] bins= 26 xlabel=r'$V_R\ (\mathrm{km\,s}^{-1})$' elif options.type.lower() == 'vt': if options.group == 'aenhanced': vs= numpy.linspace(0.01,350.,options.nv) xrange=[0.,350.] bins= 39 else: # options.group == 'aenhanced': vs= numpy.linspace(0.01,350.,options.nv) xrange=[0.,350.] bins= 39 xlabel=r'$V_T\ (\mathrm{km\,s}^{-1})$' alts= True if not options.multi is None: #Generate list of temporary files tmpfiles= [] for jj in range(M): tfile, tmpfile= tempfile.mkstemp() os.close(tfile) #Close because it's open tmpfiles.append(tmpfile) try: dummy= multi.parallel_map((lambda x: run_calc_model_multi(x,M,gfehs,gafes,fehs,afes,options, vs, potparams1,potparams2,potparams3, distancefacs, binned,alts,True,tmpfiles)), range(M), numcores=numpy.amin([M, multiprocessing.cpu_count(), options.multi])) #Now read all of the temporary files for jj in range(M): tmpfile= open(tmpfiles[jj],'rb') tvelps= pickle.load(tmpfile) if tvelps is None: continue tvelps2= pickle.load(tmpfile) tvelps3= pickle.load(tmpfile) data.extend(pickle.load(tmpfile)) zs.extend(pickle.load(tmpfile)) tndata= pickle.load(tmpfile) velps[cumulndata:cumulndata+tndata,:]= tvelps velps2[cumulndata:cumulndata+tndata,:]= tvelps2 velps3[cumulndata:cumulndata+tndata,:]= tvelps3 cumulndata+= tndata tmpfile.close() finally: for jj in range(M): os.remove(tmpfiles[jj]) else: for jj in range(M): try: tvelps, tvelps2, tvelps3, tdata, tzs, tndata= run_calc_model_multi(jj,M,gfehs,gafes,fehs,afes,options, vs, potparams1,potparams2,potparams3, distancefacs, binned,alts, False,None) except TypeError: continue velps[cumulndata:cumulndata+tndata,:]= tvelps velps2[cumulndata:cumulndata+tndata,:]= tvelps2 velps3[cumulndata:cumulndata+tndata,:]= tvelps3 data.extend(tdata) zs.extend(tzs) cumulndata+= tndata bovy_plot.bovy_print() bovy_plot.bovy_hist(data,bins=26,normed=True,color='k', histtype='step', xrange=xrange,xlabel=xlabel) plotp= numpy.nansum(velps[:cumulndata,:],axis=0)/cumulndata print numpy.sum(plotp)*(vs[1]-vs[0]) bovy_plot.bovy_plot(vs,plotp,'k-',overplot=True) if alts: plotp= numpy.nansum(velps2,axis=0)/cumulndata bovy_plot.bovy_plot(vs,plotp,'k--',overplot=True) plotp= numpy.nansum(velps3,axis=0)/cumulndata bovy_plot.bovy_plot(vs,plotp,'k:',overplot=True) if not left_legend is None: bovy_plot.bovy_text(left_legend,top_left=True,size=_legendsize) bovy_plot.bovy_text(r'$\mathrm{full\ subsample}$' +'\n'+ '$%i \ \ \mathrm{stars}$' % len(data),top_right=True, size=_legendsize) bovy_plot.bovy_end_print(args[0]+'model_data_g_'+options.group+'_'+options.type+'dist_all.'+options.ext) if options.all: return None #Plot zranges #First determine the ranges that have nstars in them rranges_nstars= 1000 zs= numpy.array(zs) data= numpy.array(data) tdata_z= sorted(numpy.fabs(zs)) nbins= numpy.ceil(len(tdata_z)/float(rranges_nstars)) rranges_nstars= int(numpy.floor(float(len(tdata_z))/nbins)) accum= rranges_nstars zranges= [0.0] while accum < len(tdata_z): zranges.append(tdata_z[accum]) accum+= rranges_nstars zranges.append(5.0) print zranges #zranges= [0.5,1.,1.5,2.,3.,4.] nzranges= len(zranges)-1 sigzsd= numpy.empty(nzranges) esigzsd= numpy.empty(nzranges) sigzs1= numpy.empty(nzranges) sigzs2= numpy.empty(nzranges) sigzs3= numpy.empty(nzranges) for ii in range(nzranges): indx= (numpy.fabs(zs) >= zranges[ii])*(numpy.fabs(zs) < zranges[ii+1]) plotp= numpy.nansum(velps[indx,:],axis=0)/numpy.sum(indx) yrange= [0.,1.35*numpy.nanmax(plotp)] bovy_plot.bovy_print() bovy_plot.bovy_hist(data[indx],bins=26,normed=True,color='k', histtype='step', yrange=yrange, xrange=xrange,xlabel=xlabel) sigzsd[ii]= numpy.std(data[indx][(numpy.fabs(data[indx]) < 100.)]) esigzsd[ii]= sigzsd[ii]/numpy.sqrt(float(len(data[indx][(numpy.fabs(data[indx]) < 100.)]))) sigzs1[ii]= numpy.sqrt(numpy.sum(vs**2.*plotp)/numpy.sum(plotp)-(numpy.sum(vs*plotp)/numpy.sum(plotp))**2.) bovy_plot.bovy_plot(vs,plotp,'k-',overplot=True) if alts: plotp= numpy.nansum(velps2[indx,:],axis=0)/numpy.sum(indx) sigzs2[ii]= numpy.sqrt(numpy.sum(vs**2.*plotp)/numpy.sum(plotp)-(numpy.sum(vs*plotp)/numpy.sum(plotp))**2.) bovy_plot.bovy_plot(vs,plotp,'k--',overplot=True) plotp= numpy.nansum(velps3[indx,:],axis=0)/numpy.sum(indx) sigzs3[ii]= numpy.sqrt(numpy.sum(vs**2.*plotp)/numpy.sum(plotp)-(numpy.sum(vs*plotp)/numpy.sum(plotp))**2.) bovy_plot.bovy_plot(vs,plotp,'k:',overplot=True) bovy_plot.bovy_text(r'$ %i\ \mathrm{pc} \leq |Z| < %i\ \mathrm{pc}$' % (int(1000*zranges[ii]),int(1000*zranges[ii+1])), # +'\n'+ # '$%i \ \ \mathrm{stars}$' % (numpy.sum(indx)), top_right=True, size=_legendsize) bovy_plot.bovy_end_print(args[0]+'model_data_g_'+options.group+'_'+options.type+'dist_z%.1f_z%.1f.' % (zranges[ii],zranges[ii+1])+options.ext) #Plot velocity dispersion as a function of |Z| bovy_plot.bovy_print() bovy_plot.bovy_plot((((numpy.roll(zranges,-1)+zranges)/2.)[:-1]),sigzsd, 'ko', xlabel=r'$|Z|\ (\mathrm{kpc})$', ylabel=r'$\sigma_z\ (\mathrm{km\,s}^{-1})$', xrange=[0.,4.], yrange=[0.,60.]) pyplot.errorbar(((numpy.roll(zranges,-1)+zranges)/2.)[:-1],sigzsd, yerr=esigzsd, marker='o',color='k',linestyle='none') bovy_plot.bovy_plot((((numpy.roll(zranges,-1)+zranges)/2.)[:-1]),sigzs1, 'r+',overplot=True,ms=10.) bovy_plot.bovy_plot((((numpy.roll(zranges,-1)+zranges)/2.)[:-1]),sigzs2, 'cx',overplot=True,ms=10.) bovy_plot.bovy_plot((((numpy.roll(zranges,-1)+zranges)/2.)[:-1]),sigzs3, 'gd',overplot=True,ms=10.) bovy_plot.bovy_end_print(args[0]+'model_data_g_'+options.group+'_'+options.type+'dist_szvsz.' +options.ext) return None