-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
68 lines (57 loc) · 1.98 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import numpy as np
from functools import wraps
import time
import hashlib
def timing(f):
@wraps(f)
def wrap(*args, **kw):
ts = time.time()
result = f(*args, **kw)
te = time.time()
t = time.strftime("%H:%M:%S", time.gmtime(te-ts))
print(f"func:{f.__name__} took: {t}")
return result
return wrap
def hash_array(arr):
return hashlib.sha224(arr).hexdigest()
def camb_clfile(fname, lmax=None):
"""CAMB spectra (lenspotentialCls, lensedCls, tensCls or ScalCls types) returned as a dict of numpy arrays.
Args:
fname (str): path to CAMB output file
lmax (int, optional): outputs cls truncated at this multipole.
"""
with open(fname) as f:
firstline = next(f)
keys = [i.lower() for i in firstline.split(' ') if i.isalpha()][1:]
cols = np.loadtxt(fname).transpose()
ell = np.int_(cols[0])
if lmax is None: lmax = ell[-1]
assert ell[-1] >= lmax, (ell[-1], lmax)
cls = {k : np.zeros(lmax + 1, dtype=float) for k in keys}
idc = np.where(ell <= lmax) if lmax is not None else np.arange(len(ell), dtype=int) # type: ignore
w = lambda ell :ell * (ell + 1) / (2. * np.pi)
wpp = lambda ell : ell ** 2 * (ell + 1) ** 2 / (2. * np.pi)
wptpe = lambda ell :np.sqrt(ell.astype(float) ** 3 * (ell + 1.) ** 3) / (2. * np.pi)
for i, k in enumerate(keys):
if k == 'pp':
we = wpp(ell)
elif 'p' in k and ('e' in k or 't' in k):
we = wptpe(ell)
else:
we = w(ell)
cls[k][ell[idc]] = cols[i + 1][idc] / we[idc]
return cls
def cli(cl):
ret = np.zeros_like(cl)
ret[np.where(cl > 0)] = 1. / cl[np.where(cl > 0)]
return ret
def arc2cl(arc):
return np.radians(arc/60)**2
def cl2arc(cl):
return np.rad2deg(np.sqrt(cl))*60
def noise(arr):
return cl2arc(1/sum(1/arc2cl(arr)))
def ini_full(ini_file):
dirpath = os.path.dirname(os.path.realpath(__file__))
return os.path.join(dirpath,'ini',ini_file)