Skip to content

Commit

Permalink
update pytest for both cluster lkl
Browse files Browse the repository at this point in the history
first attempt of trying pytest for both binned and unbinned cluster likelihood
  • Loading branch information
eunseongleee committed Dec 20, 2022
1 parent 6e00212 commit 1b1f537
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
1 change: 1 addition & 0 deletions soliket/cash_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
14 changes: 7 additions & 7 deletions soliket/clusters/input_files/test_binned_lkl_ccl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion soliket/clusters/input_files/test_unbinned_lkl_ccl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
171 changes: 115 additions & 56 deletions soliket/tests/test_clusters.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1b1f537

Please sign in to comment.