From 1b1f537f2c028bae194c52d3a9929a02374422be Mon Sep 17 00:00:00 2001 From: Eunseong Lee Date: Mon, 19 Dec 2022 21:03:44 -0500 Subject: [PATCH] update pytest for both cluster lkl first attempt of trying pytest for both binned and unbinned cluster likelihood --- .gitignore | 6 +- soliket/cash_data.py | 1 + .../input_files/test_binned_lkl_ccl.yaml | 14 +- .../test_binned_lkl_ccl_injection.yaml | 4 +- .../input_files/test_unbinned_lkl_ccl.yaml | 4 +- soliket/tests/test_clusters.py | 171 ++++++++++++------ 6 files changed, 132 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index 7e507fac..515c37cf 100644 --- a/.gitignore +++ b/.gitignore @@ -112,9 +112,9 @@ soliket/clusters/chains soliket/clusters/notebooks/figures soliket/clusters/data/act/* soliket/clusters/data/advact/* -soliket/binned_clusters .DS_Store -soliket/sz_binned_cluster_counts soliket/ymap soliket/cosmopower -soliket/clusters/temp/* +soliket/clusters/checks/* +soliket/clusters/notebooks/figs/* +soliket/clusters/notebooks/ongoing/* diff --git a/soliket/cash_data.py b/soliket/cash_data.py index 79e4dc30..982c2b95 100644 --- a/soliket/cash_data.py +++ b/soliket/cash_data.py @@ -25,6 +25,7 @@ def cash_c_logpdf(theory, data, usestirling=True, name = "binned"): ln_fac[obs == 0] = 0. loglike = obs * np.log(theory) - theory - ln_fac + print("\r ::: 2D ln likelihood = ", -np.nansum(loglike[np.isfinite(loglike)])) return np.nansum(loglike[np.isfinite(loglike)]) diff --git a/soliket/clusters/input_files/test_binned_lkl_ccl.yaml b/soliket/clusters/input_files/test_binned_lkl_ccl.yaml index 06c39504..210ef821 100644 --- a/soliket/clusters/input_files/test_binned_lkl_ccl.yaml +++ b/soliket/clusters/input_files/test_binned_lkl_ccl.yaml @@ -14,12 +14,12 @@ likelihood: # Data data: - data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/' # Path to data directory + #data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/' # Path to data directory #data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5-largeRef/NemoCCL_A10tSZ_DR5White_ACT-DR5-largeRef/' - #data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5/NemoCCL_A10tSZ_DR5White_ACT-DR5/' - cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned_mass.fits' # Path to cluster catalog file + data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5/NemoCCL_A10tSZ_DR5White_ACT-DR5/' + #cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned_mass.fits' # Path to cluster catalog file #cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5-largeRef_mass.fits' - #cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_mass.fits' + cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_mass.fits' Q_file: 'selFn/QFit.fits' # Path to Q function file tile_file: 'selFn/tileAreas.txt' # Path to tile file rms_file: 'selFn/RMSTab.fits' # Path to RMS file @@ -46,7 +46,7 @@ likelihood: # method: SNRbased / injection # SNRbased: completeness based in SNR bins # injection: estimate completeness using source injection method from nemo (i.e. no Q) - whichQ: 'injection' + whichQ: 'fit' # whichQ: fit / injection # fit : using Q fit data # injection : using Q source injection data @@ -55,7 +55,7 @@ likelihood: # downsample: average rms map, Q into n dwnsmpl_bins # full: consider full map, Q function, no downsampling dwnsmpl_bins : 50 # If resolution=downsample, number of bins to use - save_dwsmpld : False # Save downsampled Q and rms to npz file and once it exists read those + save_dwsmpld : True # Save downsampled Q and rms to npz file and once it exists read those binning: # redshift bins for number counts @@ -82,7 +82,7 @@ params: Omega_b : 0.049 Omega_c : 0.261 # sigma8 : 0.81 - tenToA0 : 1.9e-5 + tenToA0 : 4.0e-5 B0 : 0.08 scatter_sz : 0. bias_sz : 1 diff --git a/soliket/clusters/input_files/test_binned_lkl_ccl_injection.yaml b/soliket/clusters/input_files/test_binned_lkl_ccl_injection.yaml index 7fb894f0..6f51f34e 100644 --- a/soliket/clusters/input_files/test_binned_lkl_ccl_injection.yaml +++ b/soliket/clusters/input_files/test_binned_lkl_ccl_injection.yaml @@ -15,7 +15,9 @@ likelihood: # Data data: data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/' # Path to data directory + #data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5/NemoCCL_A10tSZ_DR5White_ACT-DR5/' # Path to data directory cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned_mass.fits' # Path to cluster catalog file + #cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_mass.fits' # Path to cluster catalog file Q_file: 'selFn/QFit.fits' # Path to Q function file tile_file: 'selFn/tileAreas.txt' # Path to tile file rms_file: 'selFn/RMSTab.fits' # Path to RMS file @@ -70,7 +72,7 @@ likelihood: dlogM: 0.01 #0.05 # when excluding low redshift bins for lkl, otherwise should be 0 exclude_zbin: 2 - + params: h: 0.68 n_s: 0.965 diff --git a/soliket/clusters/input_files/test_unbinned_lkl_ccl.yaml b/soliket/clusters/input_files/test_unbinned_lkl_ccl.yaml index d3dfe99a..be601dd4 100644 --- a/soliket/clusters/input_files/test_unbinned_lkl_ccl.yaml +++ b/soliket/clusters/input_files/test_unbinned_lkl_ccl.yaml @@ -17,7 +17,9 @@ likelihood: # Data data: data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/' # Path to data directory + #data_path: 'data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5/' # Path to data directory cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned_mass.fits' # Path to cluster catalog file + #cat_file: 'NemoCCL_A10tSZ_DR5White_ACT-DR5_mass.fits' # Path to cluster catalog file Q_file: 'selFn/QFit.fits' # Path to Q function file tile_file: 'selFn/tileAreas.txt' # Path to tile file rms_file: 'selFn/RMSTab.fits' # Path to RMS file @@ -35,7 +37,7 @@ likelihood: # method: SNRbased / injection # SNRbased: completeness based in SNR bins # injection: estimate completeness using source injection method from nemo (i.e. no Q) - whichQ: 'injection' #'injection' + whichQ: 'fit' #'injection' # whichQ: fit / injection # fit : using Q fit data # injection : using Q source injection data diff --git a/soliket/tests/test_clusters.py b/soliket/tests/test_clusters.py index cc94e2e4..6558dbcc 100644 --- a/soliket/tests/test_clusters.py +++ b/soliket/tests/test_clusters.py @@ -1,89 +1,148 @@ import numpy as np -import copy import pytest from cobaya.model import get_model -fiducial_params = { - "ombh2": 0.02225, - "omch2": 0.1198, - "H0": 67.3, - "tau": 0.06, - "As": 2.2e-9, - "ns": 0.96, - "mnu": 0.0, - "nnu": 3.046, - "omnuh2": 0., + +params = { + 'h': 0.68, + 'n_s': 0.965, + 'Omega_b': 0.049, + 'Omega_c': 0.261, + 'sigma8': 0.81, + 'm_nu': 0., + 'tenToA0': 1.9e-05, + 'B0': 0.08, + 'scatter_sz': 0., + 'bias_sz': 1., + 'C0': 2. } -info_unbinned = { - "params": fiducial_params, - "likelihood": {"soliket.UnbinnedClusterLikelihood": - {"stop_at_error": True, - "theorypred":{"choose_theory":'camb'}}}, - "theory": { - "camb": { - "extra_args": { - "accurate_massive_neutrino_transfers": True, - "num_massive_neutrinos": 1, - "redshifts": np.linspace(0, 2, 41), - "nonlinear": False, - "kmax": 10.0, - "dark_energy_model": "ppf" - }, - "ignore_obsolete": True - }, +path = './clusters/data/advact/DR5CosmoSims/sim-kit_NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned/' + +lkl_common = { + 'verbose': True, + 'stop_at_error': True, + 'data': { + 'data_path': path, + 'cat_file': 'NemoCCL_A10tSZ_DR5White_ACT-DR5_tenToA0Tuned_mass.fits', + 'Q_file': 'selFn/QFit.fits', + 'tile_file': 'selFn/tileAreas.txt', + 'rms_file': 'selFn/RMSTab.fits' + }, + 'theorypred': { + 'choose_theory': 'CCL', + 'massfunc_mode': 'ccl', + 'compl_mode': 'erf_diff', + 'md_hmf': '200c', + 'md_ym': '200c' }, + 'YM': { + 'Mpivot': 4.25e14 + }, + 'selfunc': { + 'SNRcut': 5., + 'method': 'SNRbased', + 'whichQ': 'injection', + 'resolution': 'downsample', + 'dwnsmpl_bins': 2, + 'save_dwsmpld': False, + }, + 'binning': { + 'z': { + 'zmin': 0., + 'zmax': 2., + 'dz': 0.1 + }, + 'q': { + 'log10qmin': 0.6, + 'log10qmax': 2.0, + 'dlog10q': 0.25 + }, + 'M': { + 'Mmin': 5e13, + 'Mmax': 1e16, + 'dlogM': 0.01 + }, + 'exclude_zbin': 2, + } } -info_binned = copy.copy(info_unbinned) -info_binned['likelihood'] = {"soliket.BinnedClusterLikelihood": - {"stop_at_error": True, - "datapath": './soliket/tests/data/toy_cashc.txt'}} +ccl_baseline = { + 'transfer_function': 'boltzmann_camb', + 'matter_pk': 'halofit', + 'baryons_pk': 'nobaryons', + 'md_hmf': '200c' +} -def test_clusters_unbinned_model(): - model_fiducial = get_model(info_unbinned) + +info_binned = { + 'params': params, + 'likelihood': {'soliket.BinnedClusterLikelihood': lkl_common}, + 'theory': {'soliket.clusters.CCL': ccl_baseline} +} + +info_unbinned = { + 'params': params, + 'likelihood': {'soliket.UnbinnedClusterLikelihood': lkl_common}, + 'theory': {'soliket.clusters.CCL': ccl_baseline} +} -def test_clusters_unbinned_model(): +def test_clusters_import(): - model_fiducial = get_model(info_unbinned) + from soliket.clusters import BinnedClusterLikelihood + from soliket.clusters import UnbinnedClusterLikelihood -def test_clusters_unbinned_loglike(): +def test_clusters_model(): - model_fiducial = get_model(info_unbinned) + binned_model = get_model(info_binned) + unbinned_model = get_model(info_unbinned) - lnl = model_fiducial.loglikes({})[0] - print('lnl: ',lnl) - # exit(0) +def test_clusters_loglike(): - # assert np.isclose(lnl, -885.678) + binned_model = get_model(info_binned) + unbinned_model = get_model(info_unbinned) + binned_lnl = binned_model.loglikes({})[0] + unbinned_lnl = unbinned_model.loglikes({})[0] -def test_clusters_unbinned_n_expected(): + assert np.isfinite(binned_lnl) + assert np.isfinite(unbinned_lnl) - model_fiducial = get_model(info_unbinned) - lnl = model_fiducial.loglikes({})[0] +def test_clusters_prediction(): - like = model_fiducial.likelihood["soliket.UnbinnedClusterLikelihood"] + binned_model = get_model(info_binned) + unbinned_model = get_model(info_unbinned) - print('like._get_n_expected():',like._get_n_expected()) - print('like._get_nz_expected():',like._get_nz_expected()) + binned_model.loglikes({})[0] + unbinned_model.loglikes({})[0] - assert np.isfinite(lnl) - assert like._get_n_expected() > 40 + binned_like = binned_model.likelihood['soliket.BinnedClusterLikelihood'] + unbinned_like = unbinned_model.likelihood['soliket.UnbinnedClusterLikelihood'] + binned_pk_intp = binned_like.theory.get_Pk_interpolator() + unbinned_pk_intp = unbinned_like.theory.get_Pk_interpolator() + SZparams = { + 'tenToA0': 1.9e-05, + 'B0': 0.08, + 'C0': 2., + 'scatter_sz': 0., + 'bias_sz': 1. + } -def test_clusters_binned_model(): + Nzq = binned_like._get_theory(binned_pk_intp, **SZparams) + Ntot = unbinned_like._get_n_expected(unbinned_pk_intp, **SZparams) + + assert np.isclose(Nzq.sum(), Ntot) - model_fiducial = get_model(info_binned) -# for debugging purposes: -# test_clusters_unbinned_loglike() -# test_clusters_unbinned_model() -test_clusters_unbinned_n_expected() +# test_clusters_import() +# test_clusters_model() +# test_clusters_loglike() +# test_clusters_prediction()