Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download data function #12

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion act_dr6_lenslike/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Likelihood for the Atacama Cosmology Telescope DR6 CMB lensing data."""
__author__ = ["Mathew Madhavacheril", "Ian Harrison"]
__version__ = "1.0.1"
__version__ = "1.0.3"

from .act_dr6_lenslike import *
56 changes: 55 additions & 1 deletion act_dr6_lenslike/act_dr6_lenslike.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,54 @@
# HELPER FUNCTIONS
# ================

def download(url, filename):
# thanks to https://stackoverflow.com/a/63831344
# this function can be considered CC-BY-SA 4.0
import functools
import pathlib
import shutil
import requests
from tqdm.auto import tqdm

r = requests.get(url, stream=True, allow_redirects=True)
if r.status_code != 200:
r.raise_for_status() # Will only raise for 4xx codes, so...
raise RuntimeError(f"Request to {url} returned status code {r.status_code}")
file_size = int(r.headers.get('Content-Length', 0))

path = pathlib.Path(filename).expanduser().resolve()
path.parent.mkdir(parents=True, exist_ok=True)

desc = "(Unknown total file size)" if file_size == 0 else ""
r.raw.read = functools.partial(r.raw.read, decode_content=True) # Decompress if needed
with tqdm.wrapattr(r.raw, "read", total=file_size, desc=desc) as r_raw:
with path.open("wb") as f:
shutil.copyfileobj(r_raw, f)

return path

def get_data(data_url="https://lambda.gsfc.nasa.gov/data/suborbital/ACT/ACT_dr6/likelihood/data/",
data_filename="ACT_dr6_likelihood_v1.1.tgz"):

if os.path.exists(os.path.join(file_dir, data_dir)):
print('Data already exists at {}, not downloading again.'.format(os.path.join(file_dir, data_dir)))
else:
import tarfile

orig_cwd = os.getcwd()
os.mkdir(os.path.join(file_dir, data_dir))
os.chdir(os.path.join(file_dir, data_dir))

print('Downloading data {} and placing it in likelihood folder.'.format(data_filename))
download(data_url+data_filename, data_filename)

tar = tarfile.open(data_filename)
tar.extractall()
tar.close()

os.remove(data_filename)
os.chdir(orig_cwd)

def pp_to_kk(clpp,ell):
return clpp * (ell*(ell+1.))**2. / 4.

Expand Down Expand Up @@ -175,6 +223,12 @@ def load_data(variant, ddir=data_dir,
"""
# TODO: review defaults

if not os.path.exists(ddir):
raise FileNotFoundError("Requested data directory {} does not exist.\
Please place the data there. Default data can \
be downloaded to the default location \
with the act_dr6_lenslike.get_data() function.".format(ddir))


v,baseline,include_planck = parse_variant(variant)

Expand Down Expand Up @@ -345,7 +399,7 @@ def generic_lnlike(data_dict,ell_kk,cl_kk,ell_cmb,cl_tt,cl_ee,cl_te,cl_bb,trim_l
delta = d['data_binned_clkk'] - bclkk
lnlike = -0.5 * np.dot(delta,np.dot(cinv,delta))
if return_theory:
lnlike, bclkk
return lnlike, bclkk
else:
return lnlike

Expand Down
14 changes: 11 additions & 3 deletions act_dr6_lenslike/tests/test_act.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ACTLikeTest(unittest.TestCase):

def generic_call(self,variant,lens_only,exp_chisq=None):
def generic_call(self,variant,lens_only,exp_chisq=None,return_theory=False):
data_file = data_dir+'like_corrs/cosmo2017_10K_acc3_lenspotentialCls.dat'
try:
ell, cl_tt, cl_ee, cl_bb, cl_te, cl_pp, cl_tp, cl_ep= np.loadtxt(data_file, unpack=True)
Expand All @@ -26,13 +26,20 @@ def generic_call(self,variant,lens_only,exp_chisq=None):
data_dict = alike.load_data(variant,lens_only=lens_only,like_corrections=not(lens_only))
ell_kk = ell
ell_cmb=ell
chisq=-2*alike.generic_lnlike(data_dict,ell_kk,cl_kk,ell_cmb,cl_tt,cl_ee,cl_te,cl_bb,trim_lmax = 2998)
self.assertAlmostEqual(chisq, exp_chisq, 1)

if return_theory:
chisq,bclkk=alike.generic_lnlike(data_dict,ell_kk,cl_kk,ell_cmb,cl_tt,cl_ee,cl_te,cl_bb,trim_lmax = 2998,return_theory=True)
self.assertAlmostEqual(-2*chisq, exp_chisq, 1)
else:
chisq=-2*alike.generic_lnlike(data_dict,ell_kk,cl_kk,ell_cmb,cl_tt,cl_ee,cl_te,cl_bb,trim_lmax = 2998)
self.assertAlmostEqual(chisq, exp_chisq, 1)

def test_act_baseline_lensonly(self):
self.generic_call('act_baseline',True,14.06)
def test_act_baseline(self):
self.generic_call('act_baseline',False,14.71)
def test_act_baseline_return_theory(self):
self.generic_call('act_baseline',False,14.71,return_theory=True)
def test_actplanck_baseline_lensonly(self):
self.generic_call('actplanck_baseline',True,21.07)
def test_actplanck_baseline(self):
Expand All @@ -55,6 +62,7 @@ def test_act_cinpaint_lensonly(self):
if __name__ == '__main__':
ACTLikeTest().test_act_baseline_lensonly()
ACTLikeTest().test_act_baseline()
ACTLikeTest().test_act_baseline_return_theory()
ACTLikeTest().test_actplanck_baseline_lensonly()
ACTLikeTest().test_actplanck_baseline()
ACTLikeTest().test_act_extended_lensonly()
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pytest
numpy
scipy
scipy
requests
tqdm