Exemplo n.º 1
0
def getFisher(N_fid, paramList, priorNameList, priorValueList, derivRoot,
              pzcutoff, z_edges, fsky):
    numParams = len(paramList)
    Fisher = np.zeros((numParams, numParams))
    paramCombs = itertools.combinations_with_replacement(paramList, 2)
    for param1, param2 in paramCombs:
        i = paramList.index(param1)
        j = paramList.index(param2)
        if not (param1 == 'tau' or param2 == 'tau'):
            ppfstr1 = ""
            ppfstr2 = ""

            new_z_edges, dN1 = rebinN(
                np.load(derivRoot + param1 + ppfstr1 + ".npy"), pzcutoff,
                z_edges)  #,mass_bin=None)
            new_z_edges, dN2 = rebinN(
                np.load(derivRoot + param2 + ppfstr2 + ".npy"), pzcutoff,
                z_edges)  #,mass_bin=None)
            dN1 = dN1 * fsky
            dN2 = dN2 * fsky

            assert not (np.any(np.isnan(dN1)))
            assert not (np.any(np.isnan(dN2)))
            assert not (np.any(np.isnan(N_fid)))

            with np.errstate(divide='ignore'):
                FellBlock = dN1 * dN2 * np.nan_to_num(old_div(
                    1., (N_fid)))  #+(N_fid*N_fid*sovernsquare)))
            #Ncollapsed = N_fid.sum(axis=0).sum(axis=-1)
            #print N_fid[np.where(Ncollapsed<1.)].sum() ," clusters fall in bins where N<1"
            #FellBlock[np.where(Ncollapsed<1.)] = 0.
            Fell = FellBlock.sum()
        else:
            Fell = 0.

        if i == j and (param1 in priorNameList):
            priorIndex = priorNameList.index(param1)
            priorVal = old_div(1., priorValueList[priorIndex]**2.)
        else:
            priorVal = 0.

        Fisher[i, j] = Fell + priorVal
        if j != i: Fisher[j, i] = Fell

    return Fisher
Exemplo n.º 2
0
def cluster_fisher_from_config(Config,expName,gridName,calName,fishName,
                               overridePlanck=None,overrideBAO=None,overrideOther=None,pickling=True,s8=False,
                               tauOverride=None,do_clkk_override=None):


    """
    Returns
    1. Fisher - the Fisher matrix
    2. paramList - final parameter list defining the Fisher contents. Might add extra params (e.g. sigR, bwl)

    Accepts
    1. Config - a ConfigParser object containing the ini file contents
    2. expName - name of experiment section in ini file
    3. gridName - name of M,q,z grid definition in ini file
    4. calName - name of weak lensing calibration section
    5. fishName - looks for a section in ini file named "fisher-"+fishName for Fisher options
    6. overridePlanck - Fisher matrix to add to upper left corner of original in place of Planck fisher 
                        matrix specified in ini. Can be zero.
    7. overrideBAO - Fisher matrix to add to upper left corner of original in place of BAO fisher 
                     matrix specified in ini. Can be zero.
    8. overrideOther - Fisher matrix to add to upper left corner of original in place of "other" fisher 
                        matrix specified in ini. Can be zero.
    
    """

    pickling = False #!!!!!
    
    bigDataDir = Config.get('general','bigDataDirectory')
    version = Config.get('general','version') 
    pzcutoff = Config.getfloat('general','photoZCutOff')
    fsky = Config.getfloat(expName,'fsky')
    # Fisher params
    fishSection = 'fisher-'+fishName
    paramList = Config.get(fishSection,'paramList').split(',')

    
    
    zs = list_from_config(Config,gridName,'zrange')
    z_edges = np.arange(zs[0],zs[1]+zs[2],zs[2])


    
    

    saveId = save_id(expName,gridName,calName,version)
    derivRoot = deriv_root(bigDataDir,saveId)
    # Fiducial number counts
    new_z_edges, N_fid = rebinN(np.load(fid_file(bigDataDir,saveId)),pzcutoff,z_edges)#,mass_bin=None)
    N_fid = N_fid*fsky


    # get mass and z grids
    # ms = list_from_config(Config,gridName,'mexprange')
    # mexp_edges = np.arange(ms[0],ms[1]+ms[2],ms[2])
    # M_edges = 10**mexp_edges
    # Masses = (M_edges[1:]+M_edges[:-1])/2.
    # print Masses.shape
    # print N_fid.shape
    # print N_fid.sum()
    # print N_fid[Masses>2.e14,:,:].sum()
    # sys.exit()


    
    print("Effective number of clusters: ", N_fid.sum())

    paramList, priorNameList, priorValueList = priors_from_config(Config,expName,calName,fishName,paramList,tauOverride)

    if s8:
        zrange = old_div((z_edges[1:]+z_edges[:-1]),2.)
        zlist = ["S8Z"+str(i) for i in range(len(zrange))]
        paramList = paramList+zlist

    
    Fisher = getFisher(N_fid,paramList,priorNameList,priorValueList,derivRoot,pzcutoff,z_edges,fsky)

    # Number of non-SZ params (params that will be in Planck/BAO)
    numCosmo = Config.getint(fishSection,'numCosmo')
    #numLeft = len(paramList) - numCosmo


    try:
        do_cmb_fisher = Config.getboolean(fishSection,"do_cmb_fisher")
    except:
        do_cmb_fisher = False

    try:
        do_clkk_fisher = Config.getboolean(fishSection,"do_clkk_fisher")
    except:
        do_clkk_fisher = False

    if do_clkk_override is not None:
        do_clkk_fisher = do_clkk_override
        
    if do_clkk_fisher:
        assert do_cmb_fisher, "Sorry, currently Clkk fisher requires CMB fisher to be True as well."
        lensName = Config.get(fishSection,"clkk_section")
    else:
        lensName = None
        
    if do_cmb_fisher:

        
        import pyfisher.clFisher as pyfish
        # Load fiducials and derivatives
        cmbDerivRoot = Config.get("general","cmbDerivRoot")
        cmbParamList = paramList[:numCosmo]


        cmb_fisher_loaded = False
        if pickling:
            import time
            hashval = hash_func(cmbParamList,expName,lensName,do_clkk_fisher,time.strftime('%Y%m%d'))
            pkl_file = "output/pickledFisher_"+hashval+".pkl"

            try:
                cmb_fisher = pickle.load(open(pkl_file,'rb'))
                cmb_fisher_loaded = True
                print("Loaded pickled CMB fisher.")
            except:
                pass
            
        if not(cmb_fisher_loaded):
            fidCls = np.loadtxt(cmbDerivRoot+'_fCls.csv',delimiter=',')
            dCls = {}
            for paramName in cmbParamList:
                dCls[paramName] = np.loadtxt(cmbDerivRoot+'_dCls_'+paramName+'.csv',delimiter=',')

            print("Calculating CMB fisher matrix...")
            print(cmbParamList,expName,lensName)
            cmb_fisher = pyfish.fisher_from_config(fidCls,dCls,cmbParamList,Config,expName,lensName)
            if pickling:
                print("Pickling CMB fisher...")
                pickle.dump(cmb_fisher,open(pkl_file,'wb'))

        numLeft = len(paramList) - cmb_fisher.shape[0]
        print("numLeft , " ,numLeft)
        cmb_fisher = pad_fisher(cmb_fisher,numLeft)
    else:
        cmb_fisher = 0.    

    print(len(paramList))
    print(Fisher.shape)

    from orphics import stats
    tszFish = stats.FisherMatrix(Fisher+cmb_fisher,param_list=paramList)
    stats.write_fisher("tsz_fish.txt",tszFish)
    #np.savetxt("tsz.txt",Fisher)

    try:
        otherFishers = Config.get(fishSection,'otherFishers').split(',')
    except:
        traceback.print_exc()
        print("No other fishers found.")
        otherFishers = []

    try:
        external_param_list = Config.get(fishSection,'external_param_list').split(',')
    except:
        traceback.print_exc()
        external_param_list = "H0,ombh2,omch2,tau,As,ns,mnu,w0,wa".split(',')
        print("No external param list found in fisher section. Assuming ", external_param_list)

    nex = len(external_param_list)
    all_others = np.zeros((nex,nex))
    for otherFisherFile in otherFishers:
        do_other = True
        try:
            other_fisher = np.loadtxt(otherFisherFile)
        except:
            try:
                other_fisher = np.loadtxt(otherFisherFile,delimiter=',') #[:numCosmo,:numCosmo]
            except:
                print("WARNING: Skipped ",otherFisherFile, " either because it was not found or doesn't have enough elements. This means no external fishers have been added!!!")
                do_other = False
                pass
        if do_other:
            #numLeft = len(paramList) - other_fisher.shape[0]
            #other_fisher = pad_fisher(other_fisher,numLeft)
            print("shapes",other_fisher.shape)
            all_others += other_fisher

    otherFish = stats.FisherMatrix(all_others,param_list=external_param_list)
    for p in otherFish.params:
        if p not in paramList:
            otherFish.delete(p)
            print("Deleted ", p , " from external fishers.")
    print("External params : ",otherFish.params)

    #print(otherFish.sigmas()['mnu']*1000.)
    
    retfish = (tszFish + otherFish).ix[paramList,paramList]
    #print(stats.FisherMatrix(retfish.as_matrix(),paramList).marge_var_2param('mnu','w0'))
    return retfish.as_matrix(), paramList
Exemplo n.º 3
0
ms = list_from_config(Config, gridName, 'mexprange')
mexp_edges = np.arange(ms[0], ms[1] + ms[2], ms[2])
zs = list_from_config(Config, gridName, 'zrange')
z_edges = np.arange(zs[0], zs[1] + zs[2], zs[2])
dm = np.diff(10**mexp_edges)
dz = np.diff(z_edges)

# Fisher params
fishSection = 'fisher-' + fishName
paramList = Config.get(fishSection, 'paramList').split(',')
paramLatexList = Config.get(fishSection, 'paramLatexList').split(',')
saveName = Config.get(fishSection, 'saveSuffix')

# Fiducial number counts
new_z_edges, N_fid = rebinN(
    np.load(bigDataDir + "N_mzq_" + saveId + "_fid" + ".npy"), pzcutoff,
    z_edges)

N_fid = N_fid[:, :, :] * fsky
print("Total number of clusters: ",
      N_fid.sum())  #getTotN(N_fid,mgrid,zgrid,qbins)

sId = expName + "_" + gridName + "_v" + version
#sovernsquareEach = np.loadtxt(bigDataDir+"sampleVarGrid_"+sId+".txt")
#sovernsquare =  np.dstack([sovernsquareEach]*len(qbins))

# Planck and BAO Fishers
planckFile = Config.get(fishSection, 'planckFile')
try:
    baoFile = Config.get(fishSection, 'baoFile')
except: