Add VELMASS analysis scripts
This commit is contained in:
parent
6cde261fcf
commit
7ce772730a
8 changed files with 695 additions and 293 deletions
|
@ -20,16 +20,16 @@ bias_sampler_blocked= true
|
||||||
nmean_sampler_blocked= true
|
nmean_sampler_blocked= true
|
||||||
sigma8_sampler_blocked = true
|
sigma8_sampler_blocked = true
|
||||||
omega_m_sampler_blocked = true
|
omega_m_sampler_blocked = true
|
||||||
muA_sampler_blocked = true
|
muA_sampler_blocked = false
|
||||||
alpha_sampler_blocked = true
|
alpha_sampler_blocked = false
|
||||||
lam_sampler_blocked = true
|
lam_sampler_blocked = false
|
||||||
sig_v_sampler_blocked = true
|
sig_v_sampler_blocked = false
|
||||||
bulk_flow_sampler_blocked = true
|
bulk_flow_sampler_blocked = false
|
||||||
ares_heat = 1.0
|
ares_heat = 1.0
|
||||||
|
|
||||||
[mcmc]
|
[mcmc]
|
||||||
number_to_generate = 15000
|
number_to_generate = 15000
|
||||||
warmup_model = 0
|
warmup_model = 500
|
||||||
warmup_cosmo = 0
|
warmup_cosmo = 0
|
||||||
random_ic = false
|
random_ic = false
|
||||||
init_random_scaling = 0.1
|
init_random_scaling = 0.1
|
||||||
|
|
BIN
figs/field_slice_velmass_delta.png
Normal file
BIN
figs/field_slice_velmass_delta.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 171 KiB |
BIN
figs/field_slice_velmass_delta_cropped.png
Normal file
BIN
figs/field_slice_velmass_delta_cropped.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 100 KiB |
BIN
figs/pk_velmass_delta_cropped.png
Normal file
BIN
figs/pk_velmass_delta_cropped.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 94 KiB |
|
@ -30,7 +30,13 @@
|
||||||
" try:\n",
|
" try:\n",
|
||||||
" yield\n",
|
" yield\n",
|
||||||
" finally:\n",
|
" finally:\n",
|
||||||
" sys.stdout = old_stdout"
|
" sys.stdout = old_stdout\n",
|
||||||
|
" \n",
|
||||||
|
"from analysis import (\n",
|
||||||
|
" get_mcmc_steps, load_param_samples, get_truths,\n",
|
||||||
|
" crop_field, compute_ensemble_mean_field, \n",
|
||||||
|
" get_mock_field, get_spectra, get_both_fields,\n",
|
||||||
|
" get_likelihood_values)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -41,290 +47,7 @@
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": []
|
||||||
"def get_mcmc_steps(dirname, nframe, iter_max, iter_min=0):\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" Obtain evenly-spaced sample of MCMC steps to make movie from\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" all_mcmc = glob.glob(dirname + '/mcmc_*.h5')\n",
|
|
||||||
" x = [m[len(dirname + '/mcmc_'):-3] for m in all_mcmc]\n",
|
|
||||||
" all_mcmc = np.sort([int(m[len(dirname + '/mcmc_'):-3]) for m in all_mcmc])\n",
|
|
||||||
" if iter_max >= 0:\n",
|
|
||||||
" all_mcmc = all_mcmc[all_mcmc <= iter_max]\n",
|
|
||||||
" all_mcmc = all_mcmc[all_mcmc >= iter_min]\n",
|
|
||||||
" if nframe > 0:\n",
|
|
||||||
" max_out = max(all_mcmc)\n",
|
|
||||||
" min_out = min(all_mcmc)\n",
|
|
||||||
" step = max(int((max_out - min_out+1) / nframe), 1)\n",
|
|
||||||
" all_mcmc = all_mcmc[::step]\n",
|
|
||||||
" if max_out not in all_mcmc:\n",
|
|
||||||
" all_mcmc = np.concatenate([all_mcmc, [max_out]])\n",
|
|
||||||
" return all_mcmc\n",
|
|
||||||
"\n",
|
|
||||||
"def load_param_samples(ini_name, dirname, nframe, iter_max, iter_min):\n",
|
|
||||||
" \n",
|
|
||||||
" config = configparser.ConfigParser()\n",
|
|
||||||
" config.read(ini_name)\n",
|
|
||||||
" to_sample = []\n",
|
|
||||||
" for k,v in config['block_loop'].items():\n",
|
|
||||||
" if v.strip() == 'false':\n",
|
|
||||||
" i = k.index('_sampler')\n",
|
|
||||||
" if k[:i] not in ['hades', 'bias', 'nmean']:\n",
|
|
||||||
" to_sample.append(k[:i])\n",
|
|
||||||
" \n",
|
|
||||||
" print(\"TO SAMPLE\", to_sample)\n",
|
|
||||||
" nsamp = int(config['run']['nsamp'])\n",
|
|
||||||
" new_to_sample = []\n",
|
|
||||||
" for s in to_sample:\n",
|
|
||||||
" if s in ['omega_m', 'sigma8', 'sig_v']:\n",
|
|
||||||
" new_to_sample.append(s)\n",
|
|
||||||
" elif s == 'bulk_flow':\n",
|
|
||||||
" for d in ['_x', '_y', '_z']:\n",
|
|
||||||
" new_to_sample.append(f'{s}{d}')\n",
|
|
||||||
" else:\n",
|
|
||||||
" for i in range(nsamp):\n",
|
|
||||||
" new_to_sample.append(f'{s}{i}')\n",
|
|
||||||
" \n",
|
|
||||||
" # This is desired list to sample\n",
|
|
||||||
" to_sample = new_to_sample\n",
|
|
||||||
" \n",
|
|
||||||
" # Which steps to use\n",
|
|
||||||
" all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)\n",
|
|
||||||
" \n",
|
|
||||||
" sampler = config['sampling']['algorithm'].lower()\n",
|
|
||||||
" samples = np.empty((len(to_sample),len(all_mcmc)))\n",
|
|
||||||
" \n",
|
|
||||||
" print('MY SAMPLER IS', sampler)\n",
|
|
||||||
" \n",
|
|
||||||
" if sampler == 'slice': \n",
|
|
||||||
"\n",
|
|
||||||
" for i in tqdm(range(len(all_mcmc))):\n",
|
|
||||||
" with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:\n",
|
|
||||||
" for j, s in enumerate(to_sample):\n",
|
|
||||||
" if 'model_params_' + s in f['scalars'].keys():\n",
|
|
||||||
" samples[j,i] = f['scalars/model_params_' + s][:][0]\n",
|
|
||||||
" elif 'model_params_cosmology.' + s in f['scalars'].keys():\n",
|
|
||||||
" samples[j,i] = f['scalars/model_params_cosmology.' + s][:][0]\n",
|
|
||||||
" elif s == 'sig_v':\n",
|
|
||||||
" samples[j,i] = float(config['model'][s])\n",
|
|
||||||
" elif s.startswith('bulk_flow'):\n",
|
|
||||||
" if s[-1] == 'x':\n",
|
|
||||||
" samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[0]\n",
|
|
||||||
" elif s[-1] == 'y':\n",
|
|
||||||
" samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[1]\n",
|
|
||||||
" elif s[-1] == 'z':\n",
|
|
||||||
" samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[2]\n",
|
|
||||||
" else:\n",
|
|
||||||
" raise NotImplementedError\n",
|
|
||||||
" else:\n",
|
|
||||||
" if s in config[f'cosmology'].keys():\n",
|
|
||||||
" samples[j,i] = float(config['cosmology'][s])\n",
|
|
||||||
" else:\n",
|
|
||||||
" print(\"NOT THERE\")\n",
|
|
||||||
" samples[j,i] = float(config[f'sample_{s[-1]}'][s[:-1]]) \n",
|
|
||||||
" \n",
|
|
||||||
" elif sampler in ['hmc', 'mvslice', 'transformedblackjax', 'blackjax']:\n",
|
|
||||||
" \n",
|
|
||||||
" if sampler in ['hmc', 'transformedblackjax']:\n",
|
|
||||||
" key_name = 'attributes'\n",
|
|
||||||
" key_name = 'model_params'\n",
|
|
||||||
" elif sampler in ['mvslice', 'blackjax']:\n",
|
|
||||||
" key_name = 'model_paramsattributes'\n",
|
|
||||||
" \n",
|
|
||||||
" # Get order in which model parameters are stored\n",
|
|
||||||
" if os.path.isfile(f'{dirname}/model_params.txt'):\n",
|
|
||||||
" with open(f'{dirname}/model_params.txt', 'r') as file:\n",
|
|
||||||
" model_params = [line.strip() for line in file]\n",
|
|
||||||
" else:\n",
|
|
||||||
" model_params = []\n",
|
|
||||||
" \n",
|
|
||||||
" print(model_params)\n",
|
|
||||||
" \n",
|
|
||||||
" for i in tqdm(range(len(all_mcmc))):\n",
|
|
||||||
" with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:\n",
|
|
||||||
" if key_name in f['scalars'].keys():\n",
|
|
||||||
" data = f[f'scalars/{key_name}'][:]\n",
|
|
||||||
" else:\n",
|
|
||||||
" data = None\n",
|
|
||||||
" for j, s in enumerate(to_sample):\n",
|
|
||||||
" if s in model_params:\n",
|
|
||||||
" samples[j,i] = data[model_params.index(s)]\n",
|
|
||||||
" elif 'model_params_cosmology.' + s in f['scalars'].keys():\n",
|
|
||||||
" samples[j,i] = f['scalars/model_params_cosmology.' + s][:][0]\n",
|
|
||||||
" elif s == 'sig_v':\n",
|
|
||||||
" samples[j,i] = float(config['model'][s])\n",
|
|
||||||
" elif s in config[f'cosmology'].keys():\n",
|
|
||||||
" samples[j,i] = float(config['cosmology'][s])\n",
|
|
||||||
" elif s.startswith('bulk_flow'):\n",
|
|
||||||
" idx = {'x':0, 'y':1, 'z':2}\n",
|
|
||||||
" idx = idx[s[-1]]\n",
|
|
||||||
" samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[idx]\n",
|
|
||||||
" else:\n",
|
|
||||||
" samples[j,i] = float(config[f'sample_{s[-1]}'][s[:-1]]) \n",
|
|
||||||
" else:\n",
|
|
||||||
" raise NotImplementedError\n",
|
|
||||||
"\n",
|
|
||||||
" return to_sample, all_mcmc, samples\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_truths(ini_name, to_sample):\n",
|
|
||||||
" \n",
|
|
||||||
" config = configparser.ConfigParser()\n",
|
|
||||||
" config.read(ini_name)\n",
|
|
||||||
" \n",
|
|
||||||
" truths = [None] * len(to_sample)\n",
|
|
||||||
" \n",
|
|
||||||
" for i, s in enumerate(to_sample):\n",
|
|
||||||
" if s in config[f'cosmology'].keys():\n",
|
|
||||||
" truths[i] = float(config['cosmology'][s])\n",
|
|
||||||
" elif s == 'sig_v':\n",
|
|
||||||
" truths[i] = float(config['model'][s])\n",
|
|
||||||
" elif s.startswith('bulk_flow'):\n",
|
|
||||||
" idx = {'x':0, 'y':1, 'z':2}\n",
|
|
||||||
" idx = idx[s[-1]]\n",
|
|
||||||
" truths[i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[idx]\n",
|
|
||||||
" else:\n",
|
|
||||||
" truths[i] = float(config[f'sample_{s[-1]}'][s[:-1]]) \n",
|
|
||||||
" \n",
|
|
||||||
" return truths\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def crop_field(ini_name, field):\n",
|
|
||||||
" \n",
|
|
||||||
" config = configparser.ConfigParser()\n",
|
|
||||||
" config.read(ini_name)\n",
|
|
||||||
" Rmax = float(config['mock']['R_max'])\n",
|
|
||||||
" xmin = float(config['system']['corner0'])\n",
|
|
||||||
" L = float(config['system']['L0'])\n",
|
|
||||||
" N = int(config['system']['N0'])\n",
|
|
||||||
" x = np.linspace(xmin, xmin+L, N)\n",
|
|
||||||
" m = np.abs(x) < Rmax\n",
|
|
||||||
" L = x[m].max() - x[m].min()\n",
|
|
||||||
" \n",
|
|
||||||
" return field[m][:, m][:, :, m], L\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def compute_ensemble_mean_field(ini_name, dirname, nframe, iter_max, iter_min, cut_field=True):\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" Compute the mean and std deviation of the inferred density field\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
"\n",
|
|
||||||
" print('Computing ensemble mean field')\n",
|
|
||||||
" \n",
|
|
||||||
" # Which steps to use\n",
|
|
||||||
" all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)\n",
|
|
||||||
"\n",
|
|
||||||
" #COMPUTE THE MEAN-DENSITY FIELD\n",
|
|
||||||
" for i in tqdm(range(len(all_mcmc))):\n",
|
|
||||||
" idx = all_mcmc[i]\n",
|
|
||||||
" with h5.File(dirname + \"/mcmc_%d.h5\" % idx,'r') as mcmc_file:\n",
|
|
||||||
" temp_field = np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)\n",
|
|
||||||
" if i == 0:\n",
|
|
||||||
" mean_field = np.array(np.full(temp_field.shape,0),dtype=np.float64)\n",
|
|
||||||
" std_field = np.array(np.full(temp_field.shape,0),dtype=np.float64)\n",
|
|
||||||
" mean_field += temp_field\n",
|
|
||||||
" std_field += temp_field*temp_field\n",
|
|
||||||
" mean_field = mean_field/np.float64(len(all_mcmc))\n",
|
|
||||||
" std_field = std_field/np.float64(len(all_mcmc)) # < delta^2 >\n",
|
|
||||||
" std_field = np.sqrt(std_field - mean_field **2) # < delta^2 > - < delta >^2\n",
|
|
||||||
" \n",
|
|
||||||
" # Cut the density field if needed\n",
|
|
||||||
" if cut_field:\n",
|
|
||||||
" mean_field, _ = crop_field(ini_name, mean_field)\n",
|
|
||||||
" std_field, _ = crop_field(ini_name, std_field)\n",
|
|
||||||
" \n",
|
|
||||||
" return mean_field, std_field\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_mock_field(ini_name, dirname, which_field='delta', cut_field=True):\n",
|
|
||||||
" with h5.File(f'{dirname}/mock_data.h5', 'r') as f:\n",
|
|
||||||
" if which_field == 'delta':\n",
|
|
||||||
" dens = f['scalars/BORG_final_density'][:]\n",
|
|
||||||
" elif which_field == 'ics':\n",
|
|
||||||
" dens = f['scalars/s_field'][:]\n",
|
|
||||||
" if cut_field:\n",
|
|
||||||
" dens, L = crop_field(ini_name, dens)\n",
|
|
||||||
" else:\n",
|
|
||||||
" config = configparser.ConfigParser()\n",
|
|
||||||
" config.read(ini_name)\n",
|
|
||||||
" L = float(config['system']['L0'])\n",
|
|
||||||
" return dens, L\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_spectra(ini_file, dirname, nframe, iter_max, iter_min, which_field='delta', cut_field=True):\n",
|
|
||||||
" \n",
|
|
||||||
" # Which steps to use\n",
|
|
||||||
" all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)\n",
|
|
||||||
" \n",
|
|
||||||
" if which_field == 'delta':\n",
|
|
||||||
" MAS = \"CIC\"\n",
|
|
||||||
" elif which_field == 'ics':\n",
|
|
||||||
" MAS = None\n",
|
|
||||||
" else:\n",
|
|
||||||
" raise NotImplementedError\n",
|
|
||||||
" \n",
|
|
||||||
" # Compute original power spectrum\n",
|
|
||||||
" delta1, boxsize = get_mock_field(ini_name, dirname, which_field=which_field, cut_field=cut_field)\n",
|
|
||||||
" print(\"BOXSIZE\", boxsize)\n",
|
|
||||||
" Pk = PKL.Pk(delta1.astype(np.float32), boxsize, axis=0, MAS=MAS, threads=1, verbose=True)\n",
|
|
||||||
" k = Pk.k3D\n",
|
|
||||||
" Pk_true = Pk.Pk[:,0]\n",
|
|
||||||
" \n",
|
|
||||||
" # Get other spectra\n",
|
|
||||||
" all_pk = np.zeros((len(all_mcmc), len(k)))\n",
|
|
||||||
" all_r = np.zeros((len(all_mcmc), len(k)))\n",
|
|
||||||
" for i in tqdm(range(len(all_mcmc))):\n",
|
|
||||||
" idx = all_mcmc[i]\n",
|
|
||||||
" with h5.File(dirname + \"/mcmc_%d.h5\" % idx,'r') as mcmc_file:\n",
|
|
||||||
" if which_field == 'delta':\n",
|
|
||||||
" delta2= np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)\n",
|
|
||||||
" elif which_field == 'ics':\n",
|
|
||||||
" delta2 = np.array(mcmc_file['scalars/s_field'][...],dtype=np.float64)\n",
|
|
||||||
" else:\n",
|
|
||||||
" raise NotImplementedError\n",
|
|
||||||
" if cut_field:\n",
|
|
||||||
" delta2, _ = crop_field(ini_name, delta2)\n",
|
|
||||||
" with suppress_stdout():\n",
|
|
||||||
" Pk = PKL.XPk([delta1.astype(np.float32),delta2.astype(np.float32)], boxsize, axis=0, MAS=[MAS, MAS], threads=1)\n",
|
|
||||||
" all_pk[i,:] = Pk.Pk[:,0,1] #monopole of field 2\n",
|
|
||||||
" all_r[i,:] = Pk.XPk[:,0,0] / np.sqrt(Pk.Pk[:,0,1] * Pk.Pk[:,0,0])\n",
|
|
||||||
" \n",
|
|
||||||
" return k, Pk_true, all_pk, all_r\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_both_fields(ini_file, dirname, step, which_field='delta', cut_field=True):\n",
|
|
||||||
" \n",
|
|
||||||
" # Mock\n",
|
|
||||||
" delta1, boxsize = get_mock_field(ini_name, dirname, which_field=which_field, cut_field=cut_field)\n",
|
|
||||||
" \n",
|
|
||||||
" # Step\n",
|
|
||||||
" with h5.File(dirname + \"/mcmc_%d.h5\" % step,'r') as mcmc_file:\n",
|
|
||||||
" if which_field == 'delta':\n",
|
|
||||||
" delta2= np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)\n",
|
|
||||||
" elif which_field == 'ics':\n",
|
|
||||||
" delta2 = np.array(mcmc_file['scalars/s_field'][...],dtype=np.float64)\n",
|
|
||||||
" else:\n",
|
|
||||||
" raise NotImplementedError\n",
|
|
||||||
" if cut_field:\n",
|
|
||||||
" delta2, _ = crop_field(ini_name, delta2)\n",
|
|
||||||
" \n",
|
|
||||||
" return delta1, delta2\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_likelihood_values(dirname, nframe, iter_max, iter_min):\n",
|
|
||||||
" \n",
|
|
||||||
" all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)\n",
|
|
||||||
" \n",
|
|
||||||
" all_logL = np.zeros(len(all_mcmc))\n",
|
|
||||||
" all_logprior = np.zeros(len(all_mcmc))\n",
|
|
||||||
" for i in tqdm(range(len(all_mcmc))):\n",
|
|
||||||
" with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:\n",
|
|
||||||
" s_hat = f['scalars/s_hat_field'][:]\n",
|
|
||||||
" all_logL[i] = f['scalars/hmc_Elh'][:]\n",
|
|
||||||
" all_logprior[i] = f['scalars/hmc_Eprior'][:]\n",
|
|
||||||
" \n",
|
|
||||||
" return all_logL, all_logprior"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
|
259
notebooks/Velmass_analysis.ipynb
Normal file
259
notebooks/Velmass_analysis.ipynb
Normal file
File diff suppressed because one or more lines are too long
420
notebooks/analysis.py
Normal file
420
notebooks/analysis.py
Normal file
|
@ -0,0 +1,420 @@
|
||||||
|
import numpy as np
|
||||||
|
import glob
|
||||||
|
import configparser
|
||||||
|
import h5py as h5
|
||||||
|
import ast
|
||||||
|
import Pk_library as PKL
|
||||||
|
import os
|
||||||
|
from tqdm import tqdm
|
||||||
|
import sys
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def suppress_stdout():
|
||||||
|
with open(os.devnull, 'w') as devnull:
|
||||||
|
old_stdout = sys.stdout
|
||||||
|
sys.stdout = devnull
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
|
||||||
|
def get_mcmc_steps(dirname, nframe, iter_max, iter_min=0):
|
||||||
|
"""
|
||||||
|
Obtain evenly-spaced sample of MCMC steps to make movie from
|
||||||
|
"""
|
||||||
|
all_mcmc = glob.glob(dirname + '/mcmc_*.h5')
|
||||||
|
x = [m[len(dirname + '/mcmc_'):-3] for m in all_mcmc]
|
||||||
|
all_mcmc = np.sort([int(m[len(dirname + '/mcmc_'):-3]) for m in all_mcmc])
|
||||||
|
if iter_max >= 0:
|
||||||
|
all_mcmc = all_mcmc[all_mcmc <= iter_max]
|
||||||
|
all_mcmc = all_mcmc[all_mcmc >= iter_min]
|
||||||
|
if nframe > 0:
|
||||||
|
max_out = max(all_mcmc)
|
||||||
|
min_out = min(all_mcmc)
|
||||||
|
step = max(int((max_out - min_out+1) / nframe), 1)
|
||||||
|
all_mcmc = all_mcmc[::step]
|
||||||
|
if max_out not in all_mcmc:
|
||||||
|
all_mcmc = np.concatenate([all_mcmc, [max_out]])
|
||||||
|
return all_mcmc
|
||||||
|
|
||||||
|
def load_param_samples(ini_name, dirname, nframe, iter_max, iter_min):
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_name)
|
||||||
|
to_sample = []
|
||||||
|
for k,v in config['block_loop'].items():
|
||||||
|
if v.strip() == 'false':
|
||||||
|
i = k.index('_sampler')
|
||||||
|
if k[:i] not in ['hades', 'bias', 'nmean']:
|
||||||
|
to_sample.append(k[:i])
|
||||||
|
|
||||||
|
print("TO SAMPLE", to_sample)
|
||||||
|
nsamp = int(config['run']['nsamp'])
|
||||||
|
new_to_sample = []
|
||||||
|
for s in to_sample:
|
||||||
|
if s in ['omega_m', 'sigma8', 'sig_v']:
|
||||||
|
new_to_sample.append(s)
|
||||||
|
elif s == 'bulk_flow':
|
||||||
|
for d in ['_x', '_y', '_z']:
|
||||||
|
new_to_sample.append(f'{s}{d}')
|
||||||
|
else:
|
||||||
|
for i in range(nsamp):
|
||||||
|
new_to_sample.append(f'{s}{i}')
|
||||||
|
|
||||||
|
# This is desired list to sample
|
||||||
|
to_sample = new_to_sample
|
||||||
|
|
||||||
|
# Which steps to use
|
||||||
|
all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)
|
||||||
|
|
||||||
|
sampler = config['sampling']['algorithm'].lower()
|
||||||
|
samples = np.empty((len(to_sample),len(all_mcmc)))
|
||||||
|
|
||||||
|
print('MY SAMPLER IS', sampler)
|
||||||
|
|
||||||
|
if sampler == 'slice':
|
||||||
|
|
||||||
|
for i in tqdm(range(len(all_mcmc))):
|
||||||
|
with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:
|
||||||
|
for j, s in enumerate(to_sample):
|
||||||
|
if 'model_params_' + s in f['scalars'].keys():
|
||||||
|
samples[j,i] = f['scalars/model_params_' + s][:][0]
|
||||||
|
elif 'model_params_cosmology.' + s in f['scalars'].keys():
|
||||||
|
samples[j,i] = f['scalars/model_params_cosmology.' + s][:][0]
|
||||||
|
elif s == 'sig_v':
|
||||||
|
samples[j,i] = float(config['model'][s])
|
||||||
|
elif s.startswith('bulk_flow'):
|
||||||
|
if s[-1] == 'x':
|
||||||
|
samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[0]
|
||||||
|
elif s[-1] == 'y':
|
||||||
|
samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[1]
|
||||||
|
elif s[-1] == 'z':
|
||||||
|
samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[2]
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
else:
|
||||||
|
if s in config[f'cosmology'].keys():
|
||||||
|
samples[j,i] = float(config['cosmology'][s])
|
||||||
|
else:
|
||||||
|
print("NOT THERE")
|
||||||
|
samples[j,i] = float(config[f'sample_{s[-1]}'][s[:-1]])
|
||||||
|
|
||||||
|
elif sampler in ['hmc', 'mvslice', 'transformedblackjax', 'blackjax']:
|
||||||
|
|
||||||
|
if sampler in ['hmc', 'transformedblackjax']:
|
||||||
|
key_name = 'attributes'
|
||||||
|
key_name = 'model_params'
|
||||||
|
elif sampler in ['mvslice', 'blackjax']:
|
||||||
|
key_name = 'model_paramsattributes'
|
||||||
|
|
||||||
|
# Get order in which model parameters are stored
|
||||||
|
if os.path.isfile(f'{dirname}/model_params.txt'):
|
||||||
|
with open(f'{dirname}/model_params.txt', 'r') as file:
|
||||||
|
model_params = [line.strip() for line in file]
|
||||||
|
else:
|
||||||
|
model_params = []
|
||||||
|
|
||||||
|
print(model_params)
|
||||||
|
|
||||||
|
for i in tqdm(range(len(all_mcmc))):
|
||||||
|
with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:
|
||||||
|
if key_name in f['scalars'].keys():
|
||||||
|
data = f[f'scalars/{key_name}'][:]
|
||||||
|
else:
|
||||||
|
data = None
|
||||||
|
for j, s in enumerate(to_sample):
|
||||||
|
if s in model_params:
|
||||||
|
samples[j,i] = data[model_params.index(s)]
|
||||||
|
elif 'model_params_cosmology.' + s in f['scalars'].keys():
|
||||||
|
samples[j,i] = f['scalars/model_params_cosmology.' + s][:][0]
|
||||||
|
elif s == 'sig_v':
|
||||||
|
samples[j,i] = float(config['model'][s])
|
||||||
|
elif s in config[f'cosmology'].keys():
|
||||||
|
samples[j,i] = float(config['cosmology'][s])
|
||||||
|
elif s.startswith('bulk_flow'):
|
||||||
|
idx = {'x':0, 'y':1, 'z':2}
|
||||||
|
idx = idx[s[-1]]
|
||||||
|
samples[j,i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[idx]
|
||||||
|
else:
|
||||||
|
samples[j,i] = float(config[f'sample_{s[-1]}'][s[:-1]])
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
return to_sample, all_mcmc, samples
|
||||||
|
|
||||||
|
|
||||||
|
def get_truths(ini_name, to_sample):
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_name)
|
||||||
|
|
||||||
|
truths = [None] * len(to_sample)
|
||||||
|
|
||||||
|
for i, s in enumerate(to_sample):
|
||||||
|
if s in config[f'cosmology'].keys():
|
||||||
|
truths[i] = float(config['cosmology'][s])
|
||||||
|
elif s == 'sig_v':
|
||||||
|
truths[i] = float(config['model'][s])
|
||||||
|
elif s.startswith('bulk_flow'):
|
||||||
|
idx = {'x':0, 'y':1, 'z':2}
|
||||||
|
idx = idx[s[-1]]
|
||||||
|
truths[i] = np.array(ast.literal_eval(config['model']['bulk_flow']))[idx]
|
||||||
|
else:
|
||||||
|
truths[i] = float(config[f'sample_{s[-1]}'][s[:-1]])
|
||||||
|
|
||||||
|
return truths
|
||||||
|
|
||||||
|
|
||||||
|
def crop_field(ini_name, field):
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_name)
|
||||||
|
Rmax = float(config['mock']['R_max'])
|
||||||
|
xmin = float(config['system']['corner0'])
|
||||||
|
L = float(config['system']['L0'])
|
||||||
|
N = int(config['system']['N0'])
|
||||||
|
x = np.linspace(xmin, xmin+L, N)
|
||||||
|
m = np.abs(x) < Rmax
|
||||||
|
L = x[m].max() - x[m].min()
|
||||||
|
|
||||||
|
return field[m][:, m][:, :, m], L
|
||||||
|
|
||||||
|
|
||||||
|
def compute_ensemble_mean_field(ini_name, dirname, nframe, iter_max, iter_min, cut_field=True):
|
||||||
|
"""
|
||||||
|
Compute the mean and std deviation of the inferred density field
|
||||||
|
"""
|
||||||
|
|
||||||
|
print('Computing ensemble mean field')
|
||||||
|
|
||||||
|
# Which steps to use
|
||||||
|
all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)
|
||||||
|
|
||||||
|
#COMPUTE THE MEAN-DENSITY FIELD
|
||||||
|
for i in tqdm(range(len(all_mcmc))):
|
||||||
|
idx = all_mcmc[i]
|
||||||
|
with h5.File(dirname + "/mcmc_%d.h5" % idx,'r') as mcmc_file:
|
||||||
|
temp_field = np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)
|
||||||
|
if i == 0:
|
||||||
|
mean_field = np.array(np.full(temp_field.shape,0),dtype=np.float64)
|
||||||
|
std_field = np.array(np.full(temp_field.shape,0),dtype=np.float64)
|
||||||
|
mean_field += temp_field
|
||||||
|
std_field += temp_field*temp_field
|
||||||
|
mean_field = mean_field/np.float64(len(all_mcmc))
|
||||||
|
std_field = std_field/np.float64(len(all_mcmc)) # < delta^2 >
|
||||||
|
std_field = np.sqrt(std_field - mean_field **2) # < delta^2 > - < delta >^2
|
||||||
|
|
||||||
|
# Cut the density field if needed
|
||||||
|
if cut_field:
|
||||||
|
mean_field, _ = crop_field(ini_name, mean_field)
|
||||||
|
std_field, _ = crop_field(ini_name, std_field)
|
||||||
|
|
||||||
|
return mean_field, std_field
|
||||||
|
|
||||||
|
|
||||||
|
def get_mock_field(ini_name, dirname, which_field='delta', cut_field=True):
|
||||||
|
with h5.File(f'{dirname}/mock_data.h5', 'r') as f:
|
||||||
|
if which_field == 'delta':
|
||||||
|
dens = f['scalars/BORG_final_density'][:]
|
||||||
|
elif which_field == 'ics':
|
||||||
|
dens = f['scalars/s_field'][:]
|
||||||
|
if cut_field:
|
||||||
|
dens, L = crop_field(ini_name, dens)
|
||||||
|
else:
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_name)
|
||||||
|
L = float(config['system']['L0'])
|
||||||
|
return dens, L
|
||||||
|
|
||||||
|
|
||||||
|
def get_velmass_field(ini_file, field_type):
|
||||||
|
"""
|
||||||
|
Load the true field from the Velmass directory.
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
field_type can be one of 'delta', 'vx', 'vy', 'vz', 'ics'
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
dirname = config['mock']['velmass_dirname']
|
||||||
|
if field_type == 'ics':
|
||||||
|
f = np.load('/data101/bartlett/fsigma8/VELMASS/velmass_density.npy')
|
||||||
|
else:
|
||||||
|
if field_type == 'delta':
|
||||||
|
key = 'd'
|
||||||
|
else:
|
||||||
|
key = field_type
|
||||||
|
f = np.load(dirname + '/../density.npz')[key]
|
||||||
|
if field_type == 'delta':
|
||||||
|
f = f / np.mean(f) - 1
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
def get_velmass_Lbox(ini_file):
|
||||||
|
"""
|
||||||
|
Load the true box length from the Velmass directory.
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
velmass_dirname = config['mock']['velmass_dirname']
|
||||||
|
# Get box size
|
||||||
|
with open(velmass_dirname + '/auto-rockstar.cfg') as f:
|
||||||
|
data = [r for r in f]
|
||||||
|
Lbox = [r for r in data if r.startswith('BOX_SIZE')][0].strip()
|
||||||
|
Lbox = float(Lbox.split('=')[1])
|
||||||
|
return Lbox
|
||||||
|
|
||||||
|
|
||||||
|
def get_borg_Lbox(ini_file):
|
||||||
|
"""
|
||||||
|
Load the box length used by BORG
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
Lbox = float(config['system']['L0'])
|
||||||
|
return Lbox
|
||||||
|
|
||||||
|
|
||||||
|
def get_borg_N(ini_file):
|
||||||
|
"""
|
||||||
|
Load the box length used by BORG
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
N = int(config['system']['N0'])
|
||||||
|
return N
|
||||||
|
|
||||||
|
|
||||||
|
def get_borg_Rmax(ini_file):
|
||||||
|
"""
|
||||||
|
Load the Rmax used by BORG
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
R_max = float(config['mock']['R_max'])
|
||||||
|
return R_max
|
||||||
|
|
||||||
|
|
||||||
|
def get_borg_corner(ini_file):
|
||||||
|
"""
|
||||||
|
Load the corner used by BORG
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
"""
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(ini_file)
|
||||||
|
corn = float(config['system']['corner0'])
|
||||||
|
return corn
|
||||||
|
|
||||||
|
|
||||||
|
def crop_velmass_to_borg(ini_file, field_type):
|
||||||
|
"""
|
||||||
|
Load the true field from the Velmass directory.
|
||||||
|
Then crop the field to only contain the region used by BORG
|
||||||
|
Uses the ini file given in dirname
|
||||||
|
field_type can be one of 'delta', 'vx', 'vy', 'vz', or 'ics'
|
||||||
|
"""
|
||||||
|
true_field = get_velmass_field(ini_file, field_type)
|
||||||
|
Lbox_true = get_velmass_Lbox(ini_file)
|
||||||
|
Lbox_borg = get_borg_Lbox(ini_file)
|
||||||
|
N = true_field.shape[0]
|
||||||
|
imin = int((1 - Lbox_borg / Lbox_true) * N / 2)
|
||||||
|
imax = int((1 + Lbox_borg / Lbox_true) * N / 2)
|
||||||
|
true_field = true_field[imin:imax,imin:imax,imin:imax]
|
||||||
|
return true_field
|
||||||
|
|
||||||
|
|
||||||
|
def get_spectra(ini_name, dirname, nframe, iter_max, iter_min, which_field='delta', cut_field=True, mock_type='borg'):
|
||||||
|
|
||||||
|
# Which steps to use
|
||||||
|
all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)
|
||||||
|
|
||||||
|
if which_field == 'delta':
|
||||||
|
MAS = "CIC"
|
||||||
|
elif which_field == 'ics':
|
||||||
|
MAS = None
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
# Compute original power spectrum
|
||||||
|
if mock_type == 'borg':
|
||||||
|
delta1, boxsize = get_mock_field(ini_name, dirname, which_field=which_field, cut_field=cut_field)
|
||||||
|
elif mock_type == 'velmass':
|
||||||
|
delta1 = crop_velmass_to_borg(ini_name, which_field)
|
||||||
|
if cut_field:
|
||||||
|
delta1, boxsize = crop_field(ini_name, delta1)
|
||||||
|
else:
|
||||||
|
boxsize = get_borg_Lbox(ini_name)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
print("BOXSIZE", boxsize)
|
||||||
|
Pk = PKL.Pk(delta1.astype(np.float32), boxsize, axis=0, MAS=MAS, threads=1, verbose=True)
|
||||||
|
k = Pk.k3D
|
||||||
|
Pk_true = Pk.Pk[:,0]
|
||||||
|
|
||||||
|
# Get other spectra
|
||||||
|
all_pk = np.zeros((len(all_mcmc), len(k)))
|
||||||
|
all_r = np.zeros((len(all_mcmc), len(k)))
|
||||||
|
for i in tqdm(range(len(all_mcmc))):
|
||||||
|
idx = all_mcmc[i]
|
||||||
|
with h5.File(dirname + "/mcmc_%d.h5" % idx,'r') as mcmc_file:
|
||||||
|
if which_field == 'delta':
|
||||||
|
delta2= np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)
|
||||||
|
elif which_field == 'ics':
|
||||||
|
delta2 = np.array(mcmc_file['scalars/s_field'][...],dtype=np.float64)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
if cut_field:
|
||||||
|
delta2, _ = crop_field(ini_name, delta2)
|
||||||
|
with suppress_stdout():
|
||||||
|
Pk = PKL.XPk([delta1.astype(np.float32),delta2.astype(np.float32)], boxsize, axis=0, MAS=[MAS, MAS], threads=1)
|
||||||
|
all_pk[i,:] = Pk.Pk[:,0,1] #monopole of field 2
|
||||||
|
all_r[i,:] = Pk.XPk[:,0,0] / np.sqrt(Pk.Pk[:,0,1] * Pk.Pk[:,0,0])
|
||||||
|
|
||||||
|
return k, Pk_true, all_pk, all_r
|
||||||
|
|
||||||
|
|
||||||
|
def get_both_fields(ini_name, dirname, step, which_field='delta', cut_field=True, mock_type='borg'):
|
||||||
|
|
||||||
|
# Mock
|
||||||
|
if mock_type == 'borg':
|
||||||
|
delta1, boxsize = get_mock_field(ini_name, dirname, which_field=which_field, cut_field=cut_field)
|
||||||
|
elif mock_type == 'velmass':
|
||||||
|
delta1 = crop_velmass_to_borg(ini_name, which_field)
|
||||||
|
if cut_field:
|
||||||
|
delta1, boxsize = crop_field(ini_name, delta1)
|
||||||
|
else:
|
||||||
|
boxsize = get_borg_Lbox(ini_name)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
# Step
|
||||||
|
with h5.File(dirname + "/mcmc_%d.h5" % step,'r') as mcmc_file:
|
||||||
|
if which_field == 'delta':
|
||||||
|
delta2= np.array(mcmc_file['scalars/BORG_final_density'][...],dtype=np.float64)
|
||||||
|
elif which_field == 'ics':
|
||||||
|
delta2 = np.array(mcmc_file['scalars/s_field'][...],dtype=np.float64)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
if cut_field:
|
||||||
|
delta2, _ = crop_field(ini_name, delta2)
|
||||||
|
|
||||||
|
return delta1, delta2
|
||||||
|
|
||||||
|
|
||||||
|
def get_likelihood_values(dirname, nframe, iter_max, iter_min):
|
||||||
|
|
||||||
|
all_mcmc = get_mcmc_steps(dirname, nframe, iter_max, iter_min=iter_min)
|
||||||
|
|
||||||
|
all_logL = np.zeros(len(all_mcmc))
|
||||||
|
all_logprior = np.zeros(len(all_mcmc))
|
||||||
|
for i in tqdm(range(len(all_mcmc))):
|
||||||
|
with h5.File(f'{dirname}/mcmc_{all_mcmc[i]}.h5', 'r') as f:
|
||||||
|
s_hat = f['scalars/s_hat_field'][:]
|
||||||
|
all_logL[i] = f['scalars/hmc_Elh'][:]
|
||||||
|
all_logprior[i] = f['scalars/hmc_Eprior'][:]
|
||||||
|
|
||||||
|
return all_logL, all_logprior
|
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#SBATCH --job-name=velmass_ics
|
#SBATCH --job-name=velmass_ics_model
|
||||||
#SBATCH --nodes=1
|
#SBATCH --nodes=1
|
||||||
#SBATCH --exclusive
|
#SBATCH --exclusive
|
||||||
#SBATCH --ntasks=40
|
#SBATCH --ntasks=40
|
||||||
|
@ -28,7 +28,7 @@ set -e
|
||||||
|
|
||||||
# Path variables
|
# Path variables
|
||||||
BORG=/data101/bartlett/build_borg/tools/hades_python/hades_python
|
BORG=/data101/bartlett/build_borg/tools/hades_python/hades_python
|
||||||
RUN_DIR=/data101/bartlett/fsigma8/borg_velocity/velmass_ics
|
RUN_DIR=/data101/bartlett/fsigma8/borg_velocity/velmass_ics_model
|
||||||
|
|
||||||
mkdir -p $RUN_DIR
|
mkdir -p $RUN_DIR
|
||||||
cd $RUN_DIR
|
cd $RUN_DIR
|
||||||
|
|
Loading…
Add table
Reference in a new issue