Beispiel #1
0
def bovy_dens2d(x,y, *args,**kwargs):
  '''
  wrapper around bovy_dens2d
  hist_2d = bovy_dens2d(x,y, *args,**kwargs)
  '''

  try:
    import bovy_plot
  except ImportError:
    print 'Import bovyplot failed'

  if kwargs.has_key('xrange'):
      xrange=kwargs['xrange']
      kwargs.pop('xrange')
  else:
      xrange=[x.min(),x.max()]
  if kwargs.has_key('yrange'):
      yrange=kwargs['yrange']
      kwargs.pop('yrange')
  else:
      yrange=[y.min(),y.max()]
  ndata= len(x)
  if kwargs.has_key('bins'):
      bins= kwargs['bins']
      kwargs.pop('bins')
  else:
      bins= round(0.3*sc.sqrt(ndata))
  if kwargs.has_key('aspect'):
      aspect= kwargs['aspect']
      kwargs.pop('aspect')
  else:
      aspect= (xrange[1]-xrange[0])/(yrange[1]-yrange[0])
  if kwargs.has_key('weights'):
      weights= kwargs['weights']
      kwargs.pop('weights')
  else:
      weights= None
  if kwargs.has_key('levels'):
      levels= kwargs['levels']
      kwargs.pop('levels')
  else:
      levels= special.erf(0.5*sc.arange(1,4))

  hh_2d, edges= sc.histogramdd(sc.array([x, y]).T,
                                          bins=bins, range=[xrange ,yrange])

  bovy_plot.bovy_dens2d(hh_2d.T,
          contours=True,levels=levels,cntrmass=True,
          cmap='gist_yarg',origin='lower',
          xrange=xrange, yrange=yrange, aspect=aspect,
          interpolation='nearest',  retCumImage=True, **kwargs)

  return hh_2d
Beispiel #2
0
def exNew(exclude=sc.array([1,2,3,4]),
          plotfilename='exNew.png',nburn=20000,nsamples=200000,
          parsigma=[5,.075,.01,1,.1],dsigma=1.):
    """exMix1: solve the new exercise using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename   - filename for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(1) #In the interest of reproducibility (if that's a word)
    #Read the data
    data= read_data('data_yerr.dat')
    ndata= len(data)
    if not exclude == None:
        nsample= ndata- len(exclude)
    else:
        nsample= ndata
    #First find the chi-squared solution, which we will use as an
    #initial guess
    #Put the data in the appropriate arrays and matrices
    Y= sc.zeros(nsample)
    X= sc.zeros(nsample)
    A= sc.ones((nsample,2))
    C= sc.zeros((nsample,nsample))
    yerr= sc.zeros(nsample)
    jj= 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj]= data[ii][1][1]
            X[jj]= data[ii][1][0]
            A[jj,1]= data[ii][1][0]
            C[jj,jj]= data[ii][2]**2./dsigma**2.
            yerr[jj]= data[ii][2]/dsigma
            jj= jj+1
    #Now compute the best fit and the uncertainties
    bestfit= sc.dot(linalg.inv(C),Y.T)
    bestfit= sc.dot(A.T,bestfit)
    bestfitvar= sc.dot(linalg.inv(C),A)
    bestfitvar= sc.dot(A.T,bestfitvar)
    bestfitvar= linalg.inv(bestfitvar)
    bestfit= sc.dot(bestfitvar,bestfit)
    initialguess= sc.array([bestfit[0],bestfit[1],0.,sc.mean(Y),m.log(sc.var(Y))])#(m,b,Pb,Yb,Vb)
    #With this initial guess start off the sampling procedure
    initialX= objective(initialguess,X,Y,yerr)
    currentX= initialX
    bestX= initialX
    bestfit= initialguess
    currentguess= initialguess
    naccept= 0
    samples= []
    samples.append(currentguess)
    for jj in range(nburn+nsamples):
        #Draw a sample from the proposal distribution
        newsample= sc.zeros(5)
        newsample[0]= currentguess[0]+stats.norm.rvs()*parsigma[0]
        newsample[1]= currentguess[1]+stats.norm.rvs()*parsigma[1]
        #newsample[2]= stats.uniform.rvs()
        newsample[2]= currentguess[2]+stats.norm.rvs()*parsigma[2]
        newsample[3]= currentguess[3]+stats.norm.rvs()*parsigma[3]
        newsample[4]= currentguess[4]+stats.norm.rvs()*parsigma[4]
        #Calculate the objective function for the newsample
        newX= objective(newsample,X,Y,yerr)
        #Accept or reject
        #Reject with the appropriate probability
        u= stats.uniform.rvs()
        if u < m.exp(newX-currentX):
            #Accept
            currentX= newX
            currentguess= newsample
            naccept= naccept+1
        if currentX > bestX:
            bestfit= currentguess
            bestX= currentX
        samples.append(currentguess)
    if double(naccept)/(nburn+nsamples) < .2 or double(naccept)/(nburn+nsamples) > .6:
        print "Acceptance ratio was "+str(double(naccept)/(nburn+nsamples))

    samples= sc.array(samples).T[:,nburn:-1]
    print "Best-fit, overall"
    print bestfit, sc.mean(samples[2,:]), sc.median(samples[2,:])

    histmb,edges= sc.histogramdd(samples.T[:,0:2],bins=round(sc.sqrt(nsamples)/5.))
    indxi= sc.argmax(sc.amax(histmb,axis=1))
    indxj= sc.argmax(sc.amax(histmb,axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi-1], edges[1][indxj-1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi+1], edges[1][indxj+1]
        
    #2D histogram
    plot.bovy_print()
    levels= special.erf(0.5*sc.arange(1,4))
    #xrange=[edges[0][0],edges[0][-1]]
    #yrange=[edges[1][0],edges[1][-1]]
    xrange=[-120,120]
    yrange=[1.5,3.2]
    histmb,edges= sc.histogramdd(samples.T[:,0:2],
                                 range=[[-120,120],[1.5,3.2]],
                                 bins=(round(sc.sqrt(nsamples)/5.)/(edges[0][-1]-edges[0][0])*(xrange[1]-xrange[0]),
                                       round(sc.sqrt(nsamples)/5.)/(edges[1][-1]-edges[1][0])*(yrange[1]-yrange[0])))
    aspect=(xrange[1]-xrange[0])/(yrange[1]-yrange[0])
    plot.bovy_dens2d(histmb.T,origin='lower',cmap='gist_yarg',
                     contours=True,cntrmass=True,
                     xrange=xrange,yrange=yrange,
                     levels=levels,
                     aspect=aspect,
                     xlabel=r'$b$',ylabel=r'$m$')
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1a.png')
    else:
        plot.bovy_end_print('exNew2a.png')

    #Data with MAP line and sampling
    plot.bovy_print()
    bestb= edges[0][indxi]
    bestm= edges[1][indxj]
    xrange=[0,300]
    yrange=[0,700]
    plot.bovy_plot(xrange,bestm*sc.array(xrange)+bestb,'k-',
                   xrange=xrange,yrange=yrange,
                   xlabel=r'$x$',ylabel=r'$y$',zorder=2)
    errorbar(X,Y,yerr,color='k',marker='o',color='k',linestyle='None',zorder=1)
    for ii in range(10):
        #Random sample
        ransample= sc.floor((stats.uniform.rvs()*nsamples))
        ransample= samples.T[ransample,0:2]
        bestb= ransample[0]
        bestm= ransample[1]
        plot.bovy_plot(xrange,bestm*sc.array(xrange)+bestb,
                       overplot=True,xrange=xrange,yrange=yrange,
                       xlabel=r'$x$',ylabel=r'$y$',color='0.75',zorder=1)
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1b.png')
    else:
        plot.bovy_end_print('exNew2b.png')
    
    #Pb plot
    plot.bovy_print()
    plot.bovy_hist(samples.T[:,2],color='k',bins=round(sc.sqrt(nsamples)/5.),
                   xlabel=r'$P_\mathrm{b}$',normed=True,histtype='step',
                   range=[0,1])
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1c.png')
    else:
        plot.bovy_end_print('exNew2c.png')
    
    return
Beispiel #3
0
def exMix1(
    exclude=None,
    plotfilenameA="exMix1a.png",
    plotfilenameB="exMix1b.png",
    plotfilenameC="exMix1c.png",
    nburn=20000,
    nsamples=1000000,
    parsigma=[5, 0.075, 0.2, 1, 0.1],
    dsigma=1.0,
    bovyprintargs={},
    sampledata=None,
):
    """exMix1: solve exercise 5 (mixture model) using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename*  - filenames for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(-1)  # In the interest of reproducibility (if that's a word)
    # Read the data
    data = read_data("data_yerr.dat")
    ndata = len(data)
    if not exclude == None:
        nsample = ndata - len(exclude)
    else:
        nsample = ndata
    # First find the chi-squared solution, which we will use as an
    # initial guess
    # Put the data in the appropriate arrays and matrices
    Y = sc.zeros(nsample)
    X = sc.zeros(nsample)
    A = sc.ones((nsample, 2))
    C = sc.zeros((nsample, nsample))
    yerr = sc.zeros(nsample)
    jj = 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj] = data[ii][1][1]
            X[jj] = data[ii][1][0]
            A[jj, 1] = data[ii][1][0]
            C[jj, jj] = data[ii][2] ** 2.0 / dsigma ** 2.0
            yerr[jj] = data[ii][2] / dsigma
            jj = jj + 1

    brange = [-120, 120]
    mrange = [1.5, 3.2]

    # This matches the order of the parameters in the "samples" vector
    mbrange = [brange, mrange]

    if sampledata is None:
        sampledata = runSampler(X, Y, A, C, yerr, nburn, nsamples, parsigma, mbrange)

    (histmb, edges, mbsamples, pbhist, pbedges) = sampledata

    # Hack -- produce fake Pbad samples from Pbad histogram.
    pbsamples = hstack([array([x] * N) for x, N in zip((pbedges[:-1] + pbedges[1:]) / 2, pbhist)])

    indxi = sc.argmax(sc.amax(histmb, axis=1))
    indxj = sc.argmax(sc.amax(histmb, axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi - 1], edges[1][indxj - 1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi + 1], edges[1][indxj + 1]

    # 2D histogram
    plot.bovy_print(**bovyprintargs)
    levels = special.erf(0.5 * sc.arange(1, 4))
    xe = [edges[0][0], edges[0][-1]]
    ye = [edges[1][0], edges[1][-1]]
    aspect = (xe[1] - xe[0]) / (ye[1] - ye[0])
    plot.bovy_dens2d(
        histmb.T,
        origin="lower",
        cmap=cm.gist_yarg,
        interpolation="nearest",
        contours=True,
        cntrmass=True,
        extent=xe + ye,
        levels=levels,
        aspect=aspect,
        xlabel=r"$b$",
        ylabel=r"$m$",
    )
    xlim(brange)
    ylim(mrange)

    plot.bovy_end_print(plotfilenameA)

    # Data with MAP line and sampling
    plot.bovy_print(**bovyprintargs)
    bestb = edges[0][indxi]
    bestm = edges[1][indxj]
    xrange = [0, 300]
    yrange = [0, 700]
    plot.bovy_plot(
        xrange,
        bestm * sc.array(xrange) + bestb,
        "k-",
        xrange=xrange,
        yrange=yrange,
        xlabel=r"$x$",
        ylabel=r"$y$",
        zorder=2,
    )
    errorbar(X, Y, yerr, marker="o", color="k", linestyle="None", zorder=1)

    for m, b in mbsamples:
        plot.bovy_plot(
            xrange,
            m * sc.array(xrange) + b,
            overplot=True,
            xrange=xrange,
            yrange=yrange,
            xlabel=r"$x$",
            ylabel=r"$y$",
            color="0.75",
            zorder=1,
        )

    plot.bovy_end_print(plotfilenameB)

    # Pb plot
    if not "text_fontsize" in bovyprintargs:
        bovyprintargs["text_fontsize"] = 11
    plot.bovy_print(**bovyprintargs)
    plot.bovy_hist(
        pbsamples,
        bins=round(sc.sqrt(nsamples) / 5.0),
        xlabel=r"$P_\mathrm{b}$",
        normed=True,
        histtype="step",
        range=[0, 1],
        edgecolor="k",
    )
    ylim(0, 4.0)
    if dsigma == 1.0:
        plot.bovy_text(r"$\mathrm{using\ correct\ data\ uncertainties}$", top_right=True)
    else:
        plot.bovy_text(r"$\mathrm{using\ data\ uncertainties\ /\ 2}$", top_left=True)

    plot.bovy_end_print(plotfilenameC)

    return sampledata
def bar_detectability_convolve(parser,nconvsamples=1000,
                               dx=_XWIDTH/20.,dy=_YWIDTH/20.,
                               nx=100,ny=20,
                               ngrid=201,rrange=[0.7,1.3],
                               phirange=[-m.pi/2.,m.pi/2.],
                               saveDir='../bar/1dLarge/',
                               saveDirConv='../bar/1dLargeConv/'):
    """
    NAME:
       bar_detectability_convolve
    PURPOSE:
       analyze the detectability of the Hercules moving group in the 
       los-distribution around the Galaxy, convolving with distance 
       uncertainties
    INPUT:
       parser - from optparse
       nconvsamples - number of samples to take to perform the convolution
       nx - number of plots in the x-direction
       ny - number of plots in the y direction
       dx - x-spacing
       dy - y-spacing
       ngrid - number of gridpoints to evaluate the density on
       rrange - range of Galactocentric radii to consider
       phirange - range of Galactic azimuths to consider
       saveDir - directory to save the pickles in
    OUTPUT:
       plot in plotfilename
    HISTORY:
       2010-05-09 - Written - Bovy (NYU)
    """
    (options,args)= parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        return 

    vloslinspace= (-.9,.9,ngrid)
    vloss= sc.linspace(*vloslinspace)

    picklebasename= '1d_%i_%i_%i_%.1f_%.1f_%.1f_%.1f' % (nx,ny,ngrid,rrange[0],rrange[1],phirange[0],phirange[1])

    #First load all of the precalculated velocity distributions
    
    phis= sc.zeros(nx)
    rs= sc.zeros(ny)
    bisplphis= sc.zeros(nx*ny)
    bisplrs= sc.zeros(nx*ny)
    vlosds= sc.zeros((nx*ny,ngrid))
    axivlosds= sc.zeros((nx*ny,ngrid))
    for ii in range(nx):
        for jj in range(ny):
            thisR= (rrange[0]+(rrange[1]-rrange[0])/
                    (ny*_YWIDTH+(ny-1)*dy)*(jj*(_YWIDTH+dy)+_YWIDTH/2.))
            thisphi= (phirange[0]+(phirange[1]-phirange[0])/
                      (nx*_XWIDTH+(nx-1)*dx)*(ii*(_XWIDTH+dx)+_XWIDTH/2.))
            phis[ii]= thisphi
            rs[jj]= thisR
            bisplphis[ii*ny+jj]= thisphi
            bisplrs[ii*ny+jj]= thisR
            thissavefilename= os.path.join(saveDir,picklebasename+'_%i_%i.sav' %(ii,jj))
            if os.path.exists(thissavefilename):
                print "Restoring los-velocity distribution at %.2f, %.2f ..." %(thisR,thisphi)
                savefile= open(thissavefilename,'r')
                vlosd= pickle.load(savefile)
                axivlosd= pickle.load(savefile)
                savefile.close()
                vlosds[ii*ny+jj,:]= vlosd
                axivlosds[ii*ny+jj,:]= axivlosd
            else:
                print "Did not find the los-velocity distribution at at %.2f, %.2f ..." %(thisR,thisphi)
                print "returning ..."
                return
    
    #Now convolve and calculate Kullback-Leibler divergence
    picklebasename= '1d_%i_%i_%i_%.1f_%.1f_%.1f_%.1f_%.1f' % (nx,ny,ngrid,rrange[0],rrange[1],phirange[0],phirange[1],options.convolve)
    detect= sc.zeros((nx,ny))
    losd= sc.zeros((nx,ny))
    for ii in range(nx):
        for jj in range(ny):
            if ii == 45 and jj == 13:
                continue#BOVY: FIX FOR NOW
            thisR= rs[jj]
            thisphi= phis[ii]
            thissavefilename= os.path.join(saveDirConv,picklebasename+'_%i_%i.sav' %(ii,jj))
            if os.path.exists(thissavefilename):
                print "Restoring convolved los-velocity distribution at %.2f, %.2f ..." %(thisR,thisphi)
                savefile= open(thissavefilename,'r')
                convvlosd= pickle.load(savefile)
                convaxivlosd= pickle.load(savefile)
                savefile.close()
            else:
                print "Calculating convolved los-velocity distribution at %.2f, %.2f ..." %(thisR,thisphi)
                #los distance
                losd[ii,jj]= m.sqrt(thisR**2.+1.-2.*thisR*m.cos(thisphi))
                thislosd= losd[ii,jj]
                #Galactic longitude
                if 1./m.cos(thisphi) < thisR and m.cos(thisphi) > 0.:
                    thisl= m.pi-m.asin(thisR/thislosd*m.sin(thisphi))
                else:
                    thisl= m.asin(thisR/thislosd*m.sin(thisphi))
                convvlosd= sc.zeros(ngrid)
                convaxivlosd= sc.zeros(ngrid)
                broke= False
                for kk in range(ngrid):
                    weights= invdist2(bisplrs[ii*ny+jj],bisplphis[ii*ny+jj],
                                      bisplrs,bisplphis)
                    weights[sc.isinf(weights)]= sc.amax(weights[sc.isfinite(weights)])
                    splindx= (weights > 1./(thislosd*options.convolve*5.)**2.)
                    try:
                        tck= interpolate.bisplrep(bisplphis[splindx],
                                                  bisplrs[splindx],
                                                  vlosds[splindx,kk],
                                                  w=weights[splindx])
                    except TypeError:
                        #Trye interp2d?
                        broke= True
                        break
                    except ValueError:
                        splindx= (weights > 1./(2.*thislosd*options.convolve*5.)**2.)
                        try:
                            tck= interpolate.bisplrep(bisplphis[splindx],
                                                      bisplrs[splindx],
                                                      vlosds[splindx,kk],
                                                      w=weights[splindx])
                        except TypeError:
                            broke= True
                            break                      
                        except ValueError:
                            broke= True
                            break
                    #Now convolve
                    thisnsamples= 0
                    for ll in range(nconvsamples):
                        samplelosd= thislosd+stats.norm.rvs()*thislosd*options.convolve
                        sampleR, samplephi= dlToRphi(samplelosd,thisl)
                        addvlos= interpolate.bisplev(samplephi,sampleR,tck)
                        convvlosd[kk]+= addvlos
                        if not addvlos == 0.:
                            thisnsamples+= 1
                    convvlosd[kk]/= thisnsamples
                    try:
                        tck= interpolate.bisplrep(bisplphis[splindx],
                                                  bisplrs[splindx],
                                                  axivlosds[splindx,kk],
                                                  w=weights[splindx])
                    except TypeError:
                        broke= True
                        break
                    except ValueError:
                        splindx= (weights > 1./(2.*thislosd*options.convolve*5.)**2.)
                        try:
                            tck= interpolate.bisplrep(bisplphis[splindx],
                                                      bisplrs[splindx],
                                                      axivlosds[splindx,kk],
                                                      w=weights[splindx])
                        except TypeError:
                            broke= True
                            break                      
                        except ValueError:
                            broke= True
                            break
                    #Now convolve
                    thisnsamples= 0
                    for ll in range(nconvsamples):
                        samplelosd= thislosd+stats.norm.rvs()*thislosd*options.convolve
                        sampleR, samplephi= dlToRphi(samplelosd,thisl)
                        addvlos= interpolate.bisplev(samplephi,sampleR,tck)
                        convaxivlosd[kk]+= addvlos
                        if not addvlos == 0.:
                            thisnsamples+= 1
                    convaxivlosd[kk]/= thisnsamples
                savefile= open(thissavefilename,'w')
                pickle.dump(convvlosd,savefile)
                pickle.dump(convaxivlosd,savefile)
                savefile.close()
                if broke:
                    continue#BOVY: FIX FOR NOW
            ddx= 1./sc.sum(axivlosd)
            #skipCenter
            if not options.skipCenter == 0.:
                skipIndx= (sc.fabs(vloss) < options.skipCenter)
                indx= (sc.fabs(vloss) >= options.skipCenter)
                convvlosd= convvlosd/sc.sum(convvlosd[indx])/ddx
                convaxivlosd= convaxivlosd/sc.sum(convaxivlosd[indx])/ddx
                convvlosd[skipIndx]= 1.
                convaxivlosd[skipIndx]= 1.
            convvlosd_zeroindx= (convvlosd == 0.)
            convaxivlosd_zeroindx= (convaxivlosd == 0.)
            convvlosd[convvlosd_zeroindx]= 1.
            convaxivlosd[convvlosd_zeroindx]= 1.
            convvlosd[convaxivlosd_zeroindx]= 1.
            convaxivlosd[convaxivlosd_zeroindx]= 1.
            detect[ii,jj]= probDistance.kullbackLeibler(convvlosd,convaxivlosd,
                                                        ddx,nan=True)
    detect[(detect < 0)]= 0.
    detect[(detect > 0.07)]= 0.
    #Now plot
    plot.bovy_print()
    plot.bovy_dens2d(detect.T,origin='lower',#interpolation='nearest',
                     xlabel=r'$\mathrm{Galactocentric\ azimuth}\ [\mathrm{deg}]$',
                     ylabel=r'$\mathrm{Galactocentric\ radius}\ /R_0$',
                     cmap='gist_yarg',xrange=sc.array(phirange)*_RADTODEG,
                     yrange=rrange,
                     aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]))
    #contour the los distance
    levels= [2/8.2*(ii+1/2.) for ii in range(10)]
    contour(losd.T,levels,colors='0.25',origin='lower',linestyles='--',
            aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]),
            extent=(phirange[0]*_RADTODEG,phirange[1]*_RADTODEG,
                    rrange[0],rrange[1]))
    plot.bovy_end_print(args[0])
def bar_detectability(parser,
                      dx=_XWIDTH/20.,dy=_YWIDTH/20.,
                      nx=100,ny=20,
                      ngrid=201,rrange=[0.7,1.3],
                      phirange=[-m.pi/2.,m.pi/2.],
                      saveDir='../bar/1dLarge/'):
    """
    NAME:
       bar_detectability
    PURPOSE:
       analyze the detectability of the Hercules moving group in the 
       los-distribution around the Galaxy
    INPUT:
       nx - number of plots in the x-direction
       ny - number of plots in the y direction
       dx - x-spacing
       dy - y-spacing
       ngrid - number of gridpoints to evaluate the density on
       rrange - range of Galactocentric radii to consider
       phirange - range of Galactic azimuths to consider
       saveDir - directory to save the pickles in
    OUTPUT:
       plot in plotfilename
    HISTORY:
       2010-05-09 - Written - Bovy (NYU)
    """
    (options,args)= parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        return 
    
    if not options.convolve == None:
        bar_detectability_convolve(parser,dx=dx,dy=dy,nx=nx,ny=ny,ngrid=ngrid,
                                   rrange=rrange,phirange=phirange,
                                   saveDir=saveDir)
        return

    vloslinspace= (-.9,.9,ngrid)
    vloss= sc.linspace(*vloslinspace)

    picklebasename= '1d_%i_%i_%i_%.1f_%.1f_%.1f_%.1f' % (nx,ny,ngrid,rrange[0],rrange[1],phirange[0],phirange[1])

    detect= sc.zeros((nx,ny))
    losd= sc.zeros((nx,ny))
    gall= sc.zeros((nx,ny))
    for ii in range(nx):
        for jj in range(ny):
            thisR= (rrange[0]+(rrange[1]-rrange[0])/
                    (ny*_YWIDTH+(ny-1)*dy)*(jj*(_YWIDTH+dy)+_YWIDTH/2.))
            thisphi= (phirange[0]+(phirange[1]-phirange[0])/
                      (nx*_XWIDTH+(nx-1)*dx)*(ii*(_XWIDTH+dx)+_XWIDTH/2.))
            thissavefilename= os.path.join(saveDir,picklebasename+'_%i_%i.sav' %(ii,jj))
            if os.path.exists(thissavefilename):
                print "Restoring los-velocity distribution at %.2f, %.2f ..." %(thisR,thisphi)
                savefile= open(thissavefilename,'r')
                vlosd= pickle.load(savefile)
                axivlosd= pickle.load(savefile)
                savefile.close()
            else:
                print "Did not find the los-velocity distribution at at %.2f, %.2f ..." %(thisR,thisphi)
                print "returning ..."
                return
            ddx= 1./sc.sum(axivlosd)
            #skipCenter
            if not options.skipCenter == 0.:
                skipIndx= (sc.fabs(vloss) < options.skipCenter)
                indx= (sc.fabs(vloss) >= options.skipCenter)
                vlosd= vlosd/sc.sum(vlosd[indx])/ddx
                axivlosd= axivlosd/sc.sum(axivlosd[indx])/ddx
                vlosd[skipIndx]= 1.
                axivlosd[skipIndx]= 1.
            vlosd_zeroindx= (vlosd == 0.)
            axivlosd_zeroindx= (axivlosd == 0.)
            vlosd[vlosd_zeroindx]= 1.
            axivlosd[vlosd_zeroindx]= 1.
            vlosd[axivlosd_zeroindx]= 1.
            axivlosd[axivlosd_zeroindx]= 1.
            detect[ii,jj]= probDistance.kullbackLeibler(vlosd,axivlosd,ddx,nan=True)
            #los distance and Galactic longitude
            d= m.sqrt(thisR**2.+1.-2.*thisR*m.cos(thisphi))
            losd[ii,jj]= d
            if 1./m.cos(thisphi) < thisR and m.cos(thisphi) > 0.:
                l= m.pi-m.asin(thisR/d*m.sin(thisphi))
            else:
                l= m.asin(thisR/d*m.sin(thisphi))
            gall[ii,jj]= l

    #Find maximum, further than 3 kpc away
    detectformax= detect.flatten()
    detectformax[losd.flatten() < 3./8.2]= 0.
    x= sc.argmax(detectformax)
    indx = sc.unravel_index(x,detect.shape)
    maxR= (rrange[0]+(rrange[1]-rrange[0])/
           (ny*_YWIDTH+(ny-1)*dy)*(indx[1]*(_YWIDTH+dy)+_YWIDTH/2.))
    maxphi= (phirange[0]+(phirange[1]-phirange[0])/
                      (nx*_XWIDTH+(nx-1)*dx)*(indx[0]*(_XWIDTH+dx)+_XWIDTH/2.))
    print maxR, maxphi, losd[indx[0],indx[1]], detect[indx[0],indx[1]], gall[indx[0],indx[1]]*180./sc.pi

    #Now plot
    plot.bovy_print()
    plot.bovy_dens2d(detect.T,origin='lower',#interpolation='nearest',
                     xlabel=r'$\mathrm{Galactocentric\ azimuth}\ [\mathrm{deg}]$',
                     ylabel=r'$\mathrm{Galactocentric\ radius}\ /R_0$',
                     cmap='gist_yarg',xrange=sc.array(phirange)*_RADTODEG,
                     yrange=rrange,
                     aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]))
    #contour the los distance and gall
    #plot.bovy_text(-22.,1.1,r'$\mathrm{apogee}$',color='w',
    #                rotation=105.)
    plot.bovy_text(-18.,1.1,r'$\mathrm{APOGEE}$',color='w',
                    rotation=285.)
    levels= [2/8.2*(ii+1/2.) for ii in range(10)]
    contour(losd.T,levels,colors='0.25',origin='lower',linestyles='--',
            aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]),
            extent=(phirange[0]*_RADTODEG,phirange[1]*_RADTODEG,
                    rrange[0],rrange[1]))
    gall[gall < 0.]+= sc.pi*2.
    levels= [0.,sc.pi/2.,sc.pi,3.*sc.pi/2.]
    contour(gall.T,levels,colors='w',origin='lower',linestyles='--',
            aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]),
            extent=(phirange[0]*_RADTODEG,phirange[1]*_RADTODEG,
                    rrange[0],rrange[1]))
    levels= [-5/180.*sc.pi,250/180.*sc.pi]
    contour(gall.T,levels,colors='w',origin='lower',linestyles='-.',
            aspect=(phirange[1]-phirange[0])*_RADTODEG/(rrange[1]-rrange[0]),
            extent=(phirange[0]*_RADTODEG,phirange[1]*_RADTODEG,
                    rrange[0],rrange[1]))
    if options.skipCenter == 0.:
        plot.bovy_text(r'$\mathrm{KL\ divergence\ / \ all}\ v_{\mathrm{los}}$',
                       title=True)
    else:
        plot.bovy_text(r'$\mathrm{KL\ divergence\ / }\ |v_{\mathrm{los}}| \geq %.2f \ v_0$' % options.skipCenter,
                       title=True)
    plot.bovy_end_print(args[0])
def exMix1(exclude=None,
           plotfilenameA='exMix1a.png',
           plotfilenameB='exMix1b.png',
           plotfilenameC='exMix1c.png',
           nburn=20000,
           nsamples=1000000,
           parsigma=[5, .075, .2, 1, .1],
           dsigma=1.,
           bovyprintargs={},
           sampledata=None):
    """exMix1: solve exercise 5 (mixture model) using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename*  - filenames for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(-1)  #In the interest of reproducibility (if that's a word)
    #Read the data
    data = read_data('data_yerr.dat')
    ndata = len(data)
    if not exclude == None:
        nsample = ndata - len(exclude)
    else:
        nsample = ndata
    #First find the chi-squared solution, which we will use as an
    #initial guess
    #Put the data in the appropriate arrays and matrices
    Y = sc.zeros(nsample)
    X = sc.zeros(nsample)
    A = sc.ones((nsample, 2))
    C = sc.zeros((nsample, nsample))
    yerr = sc.zeros(nsample)
    jj = 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj] = data[ii][1][1]
            X[jj] = data[ii][1][0]
            A[jj, 1] = data[ii][1][0]
            C[jj, jj] = data[ii][2]**2. / dsigma**2.
            yerr[jj] = data[ii][2] / dsigma
            jj = jj + 1

    brange = [-120, 120]
    mrange = [1.5, 3.2]

    # This matches the order of the parameters in the "samples" vector
    mbrange = [brange, mrange]

    if sampledata is None:
        sampledata = runSampler(X, Y, A, C, yerr, nburn, nsamples, parsigma,
                                mbrange)

    (histmb, edges, mbsamples, pbhist, pbedges) = sampledata

    # Hack -- produce fake Pbad samples from Pbad histogram.
    pbsamples = hstack([
        array([x] * N)
        for x, N in zip((pbedges[:-1] + pbedges[1:]) / 2, pbhist)
    ])

    indxi = sc.argmax(sc.amax(histmb, axis=1))
    indxj = sc.argmax(sc.amax(histmb, axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi - 1], edges[1][indxj - 1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi + 1], edges[1][indxj + 1]

    #2D histogram
    plot.bovy_print(**bovyprintargs)
    levels = special.erf(0.5 * sc.arange(1, 4))
    xe = [edges[0][0], edges[0][-1]]
    ye = [edges[1][0], edges[1][-1]]
    aspect = (xe[1] - xe[0]) / (ye[1] - ye[0])
    plot.bovy_dens2d(histmb.T,
                     origin='lower',
                     cmap=cm.gist_yarg,
                     interpolation='nearest',
                     contours=True,
                     cntrmass=True,
                     extent=xe + ye,
                     levels=levels,
                     aspect=aspect,
                     xlabel=r'$b$',
                     ylabel=r'$m$')
    xlim(brange)
    ylim(mrange)

    plot.bovy_end_print(plotfilenameA)

    #Data with MAP line and sampling
    plot.bovy_print(**bovyprintargs)
    bestb = edges[0][indxi]
    bestm = edges[1][indxj]
    xrange = [0, 300]
    yrange = [0, 700]
    plot.bovy_plot(xrange,
                   bestm * sc.array(xrange) + bestb,
                   'k-',
                   xrange=xrange,
                   yrange=yrange,
                   xlabel=r'$x$',
                   ylabel=r'$y$',
                   zorder=2)
    errorbar(X, Y, yerr, marker='o', color='k', linestyle='None', zorder=1)

    for m, b in mbsamples:
        plot.bovy_plot(xrange,
                       m * sc.array(xrange) + b,
                       overplot=True,
                       xrange=xrange,
                       yrange=yrange,
                       xlabel=r'$x$',
                       ylabel=r'$y$',
                       color='0.75',
                       zorder=1)

    plot.bovy_end_print(plotfilenameB)

    #Pb plot
    if not 'text_fontsize' in bovyprintargs:
        bovyprintargs['text_fontsize'] = 11
    plot.bovy_print(**bovyprintargs)
    plot.bovy_hist(pbsamples,
                   bins=round(sc.sqrt(nsamples) / 5.),
                   xlabel=r'$P_\mathrm{b}$',
                   normed=True,
                   histtype='step',
                   range=[0, 1],
                   edgecolor='k')
    ylim(0, 4.)
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_left=True)

    plot.bovy_end_print(plotfilenameC)

    return sampledata