def main_fgfile(argv): plikfile = argv[1] parfile = argv[2] minfile = argv[3] lmax = 2999 where = argv[4] assert lmax < 3000, "lmax too high (2999 max)" # lkl = clik.clik(plikfile) lkl = getLk(plikfile) keys = lkl.get_extra_parameter_names() lmaxs = lkl.lmax # del(lkl) prms = smh.parametric_from_smica(plikfile, lmin=0, lmax=lmax) dc = parse_minimumfile(minfile) ppf = parse_paraname(parfile) rdc = dict([(keys[i], dc[ppf[i]]) for i in range(len(ppf))]) res = nm.zeros((lmax + 1, 3, 3)) for p in prms: ppars = [rdc[k] for k in p.varpar] pes = p(ppars) if pes.shape[1] > res.shape[1]: pes[:, :res.shape[1], :res.shape[1]] += res res = pes else: res = res + pes if res.shape[1] != 6: ges = nm.zeros((lmax + 1, 6, 6)) if lmaxs[0] == -1: ges[:, 3:, 3:] = res else: ges[:, :3, :3] = res res = ges llp1 = (nm.arange(lmax + 1) * (nm.arange(lmax + 1) + 1.) / 2. / nm.pi) Dl = res * (llp1[:, nm.newaxis, nm.newaxis]) print("save fg file to :", where) f = open(where, "w") print( "# l TT100x100 TT143X143 TT143X217 TT217X217 EE100X100 EE100X143 EE100X217 EE143X143 EE143X217 EE217X217 TE100X100 TE100X143 TE100X217 TE143X143 TE143X217 TE217X217", file=f) print("#", file=f) for i in range(2, lmax + 1): print(("%6d" + " %14E" * 16) % (i, Dl[i, 0, 0], Dl[i, 1, 1], Dl[i, 1, 2], Dl[i, 2, 2], Dl[i, 3, 3], Dl[i, 3, 4], Dl[i, 3, 5], Dl[i, 4, 4], Dl[i, 4, 5], Dl[i, 5, 5], Dl[i, 0, 3], Dl[i, 0, 4], Dl[i, 0, 5], Dl[i, 1, 4], Dl[i, 1, 5], Dl[i, 2, 5]), file=f) f.close()
def main_fgfile(argv): plikfile = argv[1] parfile = argv[2] minfile = argv[3] lmax = 2999 where = argv[4] assert lmax < 3000, "lmax too high (2999 max)" # lkl = clik.clik(plikfile) lkl = getLk(plikfile) keys = lkl.get_extra_parameter_names() lmaxs = lkl.lmax # del(lkl) prms = smh.parametric_from_smica(plikfile, lmin=0, lmax=lmax) dc = parse_minimumfile(minfile) ppf = parse_paraname(parfile) rdc = dict([(keys[i], dc[ppf[i]]) for i in range(len(ppf))]) res = nm.zeros((lmax + 1, 3, 3)) for p in prms: ppars = [rdc[k] for k in p.varpar] pes = p(ppars) if pes.shape[1] > res.shape[1]: pes[:, :res.shape[1], :res.shape[1]] += res res = pes else: res = res + pes if res.shape[1] != 6: ges = nm.zeros((lmax + 1, 6, 6)) if lmaxs[0] == -1: ges[:, 3:, 3:] = res else: ges[:, :3, :3] = res res = ges llp1 = (nm.arange(lmax + 1) * (nm.arange(lmax + 1) + 1.) / 2. / nm.pi) Dl = res * (llp1[:, nm.newaxis, nm.newaxis]) print("save fg file to :", where) f = open(where, "w") print("# l TT100x100 TT143X143 TT143X217 TT217X217 EE100X100 EE100X143 EE100X217 EE143X143 EE143X217 EE217X217 TE100X100 TE100X143 TE100X217 TE143X143 TE143X217 TE217X217", file=f) print("#", file=f) for i in range(2, lmax + 1): print(("%6d" + " %14E" * 16) % ( i, Dl[i, 0, 0], Dl[i, 1, 1], Dl[i, 1, 2], Dl[i, 2, 2], Dl[i, 3, 3], Dl[i, 3, 4], Dl[i, 3, 5], Dl[i, 4, 4], Dl[i, 4, 5], Dl[i, 5, 5], Dl[i, 0, 3], Dl[i, 0, 4], Dl[i, 0, 5], Dl[i, 1, 4], Dl[i, 1, 5], Dl[i, 2, 5]), file=f) f.close()
def __init__(self, clikfile, paramnames=None): # paramnames will be used to do the mapping to the extra parameters self.dffile = clikfile self.name = os.path.splitext(os.path.basename(clikfile))[0] if isinstance(paramnames, (list, tuple)): self.parnames = paramnames else: if paramnames is None: # match versions to the baseline .paramnames file for rem in ['_', 'a_', 'b_', 'c_', 'd_']: name = self.name.replace(rem, '_').replace('_bin1', '') paramnames = os.path.join( os.path.dirname(__file__), '../../data/' + name + '.paramnames') if os.path.exists(paramnames): break self.paramnamefile = paramnames self.paramnames = ParamNames(paramnames) self.parnames = self.paramnames.list() self.clik = clik.clik(clikfile) self._translate_parname(self.parnames) self.fi = hpy.File(self.dffile) # some metadata self.hascl = self.fi["clik/lkl_0/has_cl"] self.lmin = self.fi["clik/lkl_0/lmin"] self.lmax = self.fi["clik/lkl_0/lmax"] self.mt = self.fi["clik/lkl_0/m_channel_T"] * self.hascl[0] self.me = self.fi["clik/lkl_0/m_channel_P"] * self.hascl[1] self.mb = self.fi["clik/lkl_0/m_channel_P"] * self.hascl[2] self.m = self.mt + self.me + self.mb self.nb = self.fi["clik/lkl_0/nbins"] / self.hascl.sum() self.rq_shape = (self.nb, self.m, self.m) # binning details self.blmin = self.fi["clik/lkl_0/bin_lmin"] self.blmax = self.fi["clik/lkl_0/bin_lmax"] self.b_ws = self.fi["clik/lkl_0/bin_ws"] # the binning matrix is also simply obtained this way (but using it is slower than using the binning details, 'cause it's full of zeros) self.bns = php.read_bins(self.fi["clik/lkl_0"]) # compute the binned ells self.lm = np.dot(self.bns[:self.nb, :self.lmax - self.lmin + 1], np.arange(self.lmin, self.lmax + 1)) # get the calibration part (and beam for plik 2015) # cal and bal are functions that expect a vector of parameters whose name and ordering are given by cal.varpar and bal.varpar # overal calibration is given by cal(pars)*vec(pars)*outer(acmb,acmb)[nm.newaxis,:,:] self.cal = smh.calTP_from_smica(self.dffile) self.bal = smh.beamTP_from_smica(self.dffile) self.acmb = self.fi["clik/lkl_0/A_cmb"] # get the binned Cl data array self.rqh = self.fi["clik/lkl_0/Rq_hat"] self.rqh.shape = self.rq_shape # get the additive nuisance components self.prms = smh.parametric_from_smica(self.dffile) self.prms_name = [p.get_name() for p in self.prms] # get the selection vector self.oo, self.Jt = smh.ordering_from_smica(self.dffile) # get the inverse covariance self.siginv = self.fi["clik/lkl_0/criterion_gauss_mat"] self.siginv.shape = (len(self.oo), len(self.oo)) ls = np.arange(self.lmax + 1) self.llp1 = ls * (ls + 1) / (2 * np.pi) self.llp1[0] = 1 self.indices = [(0, 0), (1, 1), (2, 2), (0, 1), (0, 2), (1, 2)] self.spectra = ["tt", "ee", "bb", "te", "tb", "eb"]
def main_fg2000(argv, tag=''): plikfile = argv[1] parfile = argv[2] minfile = argv[3] chainparfile = argv[4] chains = argv[5:] # lkl = clik.clik(plikfile) lkl = getLk(plikfile) keys = lkl.get_extra_parameter_names() lmaxs = lkl.lmax # del(lkl) assert lmaxs[0] != -1, "can only add T fg in T cases !" llp1 = 2000 * 2001 / 2. / nm.pi prms = smh.parametric_from_smica(plikfile, lmin=2000, lmax=2000) ppf = parse_paraname(parfile) cpf = parse_paraname(chainparfile) ldc = [] for i in range(len(ppf)): if ppf[i] in cpf: ldc += [(keys[i], cpf.index(ppf[i]))] ldc = dict(ldc) nchainparfile = chainparfile[:-len(".paramnames")] + tag + ".paramnames" ls = open(chainparfile, "r").readlines() print("save paranames file to :", nchainparfile) f = open(nchainparfile, "w") for i, l in enumerate(ls): if re.search("^chi2_", l): break print(l.rstrip(), file=f) ib = i print("f2000_143*\tf_{2000}^{143}", file=f) print("f2000_x*\tf_{2000}^{143\\times217}", file=f) print("f2000_217*\tf_{2000}^{217}", file=f) for i, l in enumerate(ls[ib:]): print(l.rstrip(), file=f) f.close() if minfile.endswith("ranges"): dc = {} for l in open(minfile): a, b, c = l.split() if b == c: dc[a] = float(b) rdc = dict([(keys[i], dc.get(ppf[i], None)) for i in range(len(ppf))]) print(rdc) else: dc = parse_minimumfile(minfile) rdc = dict([(keys[i], dc[ppf[i]]) for i in range(len(ppf))]) nminimumfile = minfile[:-len(".minimum")] + tag + ".minimum" print("save minimum file to :", nminimumfile) fo = open(minfile, "r") ls = fo.readlines() fo.close() f = open(nminimumfile, "w") fnd = False for i, l in enumerate(ls): if re.search(r"\s+\d+\s+.+?\s+(chi2_.+?)\s+", l): fnd = True break assert fnd, "bad format for %s" % minfile ibreak = i for l in ls[:ibreak]: print(l.rstrip(), file=f) vl = int(ls[ibreak].strip().split()[0]) res = 0 for p in prms: ppars = [rdc[k] for k in p.varpar] res = res + p(ppars)[:, :3, :3] print("%5d %#14E %-21s %s" % (vl, res[0, 1, 1] * llp1, "f2000_143", "f_{2000}^{143}"), file=f) print("%5d %#14E %-21s %s" % (vl + 1, res[0, 1, 2] * llp1, "f2000_x", "f_{2000}^{143\\times217}"), file=f) print("%5d %#14E %-21s %s" % (vl + 2, res[0, 2, 2] * llp1, "f2000_217", "f_{2000}^{217}"), file=f) cnt = vl + 3 keep = True for l in ls[ibreak:]: rr = re.search("\s+(\d+)\s+(.+?)\s+(.+?)\s+", l) if rr and keep: print("%5d%s" % (cnt, l[5:].rstrip()), file=f) cnt += 1 else: keep = False print(l.rstrip(), file=f) f.close() for chain in chains: ch = nm.loadtxt(chain) nmm = chain.split("_") if tag: nchain = "_".join(nmm[:-1] + [tag.replace('_', ''), nmm[-1]]) else: nchain = chain f = open(nchain, "w") print(nchain, ":", end=' ') for i in range((ch.shape[0])): if i % 1000 == 0: print("%d/%d" % (i, ch.shape[0]), end=' ') sys.stdout.flush() res = 0 for p in prms: ppars = [] for k in p.varpar: if k in ldc: ppars += [ch[i, ldc[k] + 2]] else: ppars += [rdc[k]] res = res + p(ppars)[:, :3, :3] print("", end=' ', file=f) for v in ch[i][:ib + 2]: print(" %14E" % v, end=' ', file=f) print(" %14E %14E %14E" % (res[0, 1, 1] * llp1, res[0, 1, 2] * llp1, res[0, 2, 2] * llp1), end=' ', file=f) for v in ch[i][ib + 2:]: print(" %14E" % v, end=' ', file=f) print("", file=f) f.close() print("")