def test_get_cosmo(self): lmax = 10 acc_opts = dict(AccuracyBoost=1, lSampleBoost=1, lAccuracyBoost=1) F = Fisher(self.test_dir) F.get_cosmo(lmax=lmax, **acc_opts)
def test_first_num_pass(self): F = Fisher(self.test_dir) F.init_bins(lmin=10, lmax=30, parity='odd') # Only root has access to full-sized arrays. if F.mpi_rank == 0: n_gd_bins = np.sum(F.bins['num_pass_full'].astype(bool)) n_gd_tripl = np.sum(F.bins['first_pass_full'].astype(bool)) / 3. self.assertEqual(n_gd_bins, n_gd_tripl) passed_trpl = F.bins['first_pass_full'][ F.bins['num_pass_full'].astype(bool)] self.assertEqual(n_gd_bins, np.sum(passed_trpl.astype(bool)) / 3.)
def test_init_cosmo(self): F = Fisher(self.test_dir) self.assertEqual(F.cosmo['init_cosmo'], False) lmax = 10 acc_opts = dict(AccuracyBoost=1, lSampleBoost=1, lAccuracyBoost=1) F.init_cosmo(lmax=lmax, verbose=True, **acc_opts) self.assertEqual(F.cosmo['init_cosmo'], True) self.assertTrue('transfer' in F.cosmo) self.assertTrue('cls' in F.cosmo) self.assertTrue('opts' in F.cosmo) self.assertEqual(F.cosmo['opts']['acc']['AccuracyBoost'], 1) self.assertEqual(F.cosmo['opts']['acc']['lSampleBoost'], 1) self.assertEqual(F.cosmo['opts']['acc']['lAccuracyBoost'], 1)
def test_first_pass(self): # first_pass with unbinned mulitpoles should agree # with get_good_triplets from tools. lmin = 4 lmax = 10 bins = np.arange(lmin, lmax + 1) parity = 'odd' pmod = 1 F = Fisher(self.test_dir) F.init_bins(lmin=lmin, lmax=lmax, parity=parity, bins=bins) if F.mpi_rank == 0: num_pass = F.bins['num_pass_full'] first_pass = F.bins['first_pass_full'] good_triplets_exp = first_pass[num_pass.astype(bool)] good_triplets = np.zeros_like(good_triplets_exp) tools.get_good_triplets(lmin, lmax, lmax, good_triplets, pmod) np.testing.assert_array_equal(good_triplets_exp, good_triplets)
def run(ell, prim_template='local', out_dir=None, lmin=2, lmax=120): ''' Calculate and save bins, beta and bispec. Then return bispec slice. Keyword Arguments --------- ell : int Multipole for bispec slice. prim_template : str out_dir : str lmin : int lmax : int ''' interp_factor = 10 ac = 16 ke = 10 # tag = 'l{}'.format(lmax) # beta_tag = 'l{}_i{}'.format(lmax, interp_factor) # cosmo_tag = 'l{}_ac{}_ke{}'.format(lmax, ac, ke) bins_tag = '5200' beta_tag = 'r1_i1_l{}_16_7'.format(lmax, interp_factor) cosmo_tag = '5200_16_7'.format(lmax, ac, ke) bispec_tag = '5200_16_7' F = Fisher(out_dir) F.get_cosmo(load=True, tag=cosmo_tag, verbose=True, lmax=lmax * 2, AccuracyBoost=ac, k_eta_fac=ke) F.get_bins(load=True, parity='odd', verbose=True, tag=bins_tag, lmin=lmin, lmax=lmax) F.get_beta(load=True, tag=beta_tag, verbose=True, interp_factor=interp_factor) F.get_binned_bispec(prim_template, load=True, tag=bispec_tag) b_slice, doublets = F.get_bispec_slice(ell, verbose=2) return F, b_slice, doublets
def bin_test(parity, bins=None, lmin=2, lmax=23): ''' Init bins, run over them and test them. ''' base_dir = '/mn/stornext/d8/ITA/spider/adri/analysis/20181025_sst/test/precomputed' F = Fisher(base_dir) F.init_bins(lmin=lmin, lmax=lmax, parity=parity, bins=bins) bins = F.bins['bins'] pint = 1 if parity == 'odd' else 0 if F.mpi_rank == 0: for i1, b1 in enumerate(bins): for i2, b2 in enumerate(bins[i1:]): i2 += i1 for i3, b3 in enumerate(bins[i2:]): i3 += i2 ell1, ell2, ell3 = F.bins['first_pass'][i1,i2,i3] num = F.bins['num_pass'][i1,i2,i3] try: if num == 0: assert (ell1, ell2, ell3) == (0,0,0) else: assert (ell1, ell2, ell3) != (0,0,0) if parity is not None: assert (ell1 + ell2 + ell3) % 2 == pint # check if first pass ells fit in bins assert ell1 >= b1 assert ell2 >= b2 assert ell3 >= b3 try: assert ell1 < bins[i1+1] except IndexError: if (i1 + 1) >= bins.size: pass else: raise try: assert ell2 < bins[i2+1] except IndexError: if (i2 + 1) >= bins.size: pass else: raise try: assert ell3 < bins[i3+1] except IndexError: if (i3 + 1) >= bins.size: pass else: raise # Check if first pass matches triangle cond. assert abs(ell1 - ell2) <= ell3 assert ell3 <= (ell1 + ell2) except: print 'error in bin:' print 'bin_idx: ({},{},{}), bin: ({},{},{}), no. gd_tuples: {}, '\ 'u_ell: ({},{},{})'.format(i1, i2, i3, b1, b2, b3, F.bins['num_pass'][i1,i2,i3], ell1, ell2, ell3) raise print 'bins: ', F.bins['bins'] print 'lmin: ', F.bins['lmin'] print 'lmax: ', F.bins['lmax'] print 'sum num_pass: '******'num_pass']) print 'unique_ells: ', F.bins['unique_ells'] print 'num bins: ', F.bins['bins'].size print 'shape num_pass: '******'num_pass'].shape print 'shape first_pass: '******'first_pass'].shape, '\n'
def run(out_dir, num_chunks, camb_opts=None, bin_opts=None, beta_opts=None): F = Fisher(out_dir) F.get_camb_output(**camb_opts) cls = get_cls(camb_opts['camb_out_dir'], bin_opts['lmax'], A_lens=1, tag=camb_opts['tag']) lmax = cls.shape[1] + 2 # hacky stuff # totcov = np.zeros((6, cls.shape[1])) totcov = np.zeros((6, 3999)) totcov[:3] += 1e12 # fill diagonal totcov[:4, :cls.shape[1]] = cls F.get_bins(**bin_opts) radii = F.get_updated_radii() F.get_beta(radii=radii, **beta_opts) chunks = np.array_split(radii, num_chunks) fisher_r = np.zeros(len(chunks)) for cidx, chunk in enumerate(chunks): F.get_binned_bispec('equilateral', radii_sub=chunk, load=True, tag=str(cidx)) print(F.bispec['bispec'].size) print(F.bispec['bispec'][F.bins['num_pass'].astype(bool)].size) exit() # amp = get_prim_amp('equilateral') # not needed anymore # F.bispec['bispec'] *= amp fisher, _ = F.naive_fisher(2, lmax, totcov, fsky=1) fisher_r[cidx] = fisher print(fisher) # save in fisher np.save(opj(F.subdirs['fisher'], 'fisher_r.npy'), fisher_r) np.save(opj(F.subdirs['fisher'], 'chunks.npy'), chunks) return fisher_r, chunks
import numpy as np from sst import Fisher test_dir = '/mn/stornext/d8/ITA/spider/adri/analysis/20181025_sst/test' camb_dir = '/mn/stornext/d8/ITA/spider/adri/analysis/20180911_sst/camb_output/lensed_r0_4000' F = Fisher(test_dir) F.get_camb_output(camb_out_dir=camb_dir) radii = F.get_updated_radii() radii = radii[::10] F.get_bins(lmin=2, lmax=10, load=True, verbose=True) F.get_beta(func='equilateral', load=True, verbose=True, radii=radii) F.barrier() b = F.bins['num_pass'].astype(bool) F.get_binned_bispec('equilateral', load=True) #if F.mpi_rank == 0: # print F.bispec['bispec'][4,:,:,0] #for rank in xrange(F.mpi_size): # if F.mpi_rank == rank: # print 'beta' # print F.beta['beta_s'].shape # print F.beta['beta_s'][20,0,0,0,0,:] # bins needs to be recomputed when kwargs dont match # beta needs to be recomputed when kwargs, radii, ks, transfers do not match # bispec only needs to be recomputed when template doesnt match. #beta = F.beta #print beta #beta_s = F.beta['beta_s']
def run(prim_template='equilateral', out_dir=None, lmin=2, lmax=5000): ''' Calculate and save beta. Keyword Arguments --------- prim_template : str out_dir : str camb_dir : str lmin : int lmax : int ''' # camb_opts = dict(camb_out_dir = camb_dir, # tag='', # lensed=False, # high_ell=False, # interp_factor=None) F = Fisher(out_dir) ac = 16 ke = 10 interp_factor = 1 radii_factor = 1 # F.get_camb_output(**camb_opts) F.get_cosmo(lmax=lmax, load=True, tag='{}_{}_{}'.format(lmax, ac, ke), AccuracyBoost=ac, k_eta_fac=ke) F.get_bins(lmin=lmin, lmax=lmax, load=True, parity='odd', verbose=True, tag=str(lmax)) beta_tag = 'r{}_i{}_l{}_{}_{}'.format(radii_factor, interp_factor, lmax, ac, ke) radii = F.get_updated_radii() radii = radii[::radii_factor] F.get_beta(func=prim_template, radii=radii, verbose=True, optimize=False, interp_factor=interp_factor, load=False, sparse=False, tag=beta_tag) exit() F.get_binned_bispec('equilateral', load=False) if F.mpi_rank == 0: print(F.bispec['bispec'].shape) print(F.bispec['bispec'][F.bispec['bispec'] != 0]) print(F.bispec['bispec'][F.bispec['bispec'] != 0].size) print(np.sum(F.bins['num_pass_full'].astype(bool))) print(F.bispec['bispec'][F.bins['num_pass_full'].astype(bool)]) print(np.sum(F.bispec['bispec'][~F.bins['num_pass_full'].astype(bool)])) print(F.bispec['bispec'][np.isnan(F.bispec['bispec'])]) print(F.bispec['bispec'][np.logical_and(F.bispec['bispec'] == 0,F.bins['num_pass_full'][:,:,:,np.newaxis].astype(bool))]) print(F.bispec['bispec'][0,:4,:4,:]) print(F.bins['num_pass_full'][0,:4,:4])
def run_fisher(template, ana_dir, camb_dir, totcov, ells, lmin=2, lmax=4999, fsky=0.03, plot_tag='', tag=None): F = Fisher(ana_dir) camb_opts = dict(camb_out_dir=camb_dir, tag='r0', lensed=False, high_ell=True) F.get_camb_output(**camb_opts) radii = F.get_updated_radii() radii = radii[::2] F.get_bins(lmin=lmin, lmax=lmax, load=True, verbose=False, parity='odd', tag=tag) # F.get_beta(func='equilateral', load=True, verbose=False, radii=radii, tag=tag) F.get_beta(func='equilateral', load=True, verbose=True, radii=radii, tag=tag, interp_factor=10) # F.get_binned_bispec(template, load=True, tag=tag) F.get_binned_bispec(template, load=True, tag=tag) bin_invcov, bin_cov = F.get_binned_invcov(ells, totcov, return_bin_cov=True) # Plot invcov, cov plot_opts = dict(lmin=2) bins = F.bins['bins'] plot_tools.cls_matrix(plot_tag, bins, bin_invcov, log=False, plot_dell=False, inv=True, **plot_opts) plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov_dell'), bins, bin_cov, log=False, plot_dell=True, **plot_opts) print(lmin, lmax) fisher = F.naive_fisher(bin_invcov, lmin=lmin, lmax=lmax, fsky=fsky) sigma = 1 / np.sqrt(fisher) return fisher, sigma ###### OLD amp = get_prim_amp(template) F.bispec['bispec'] *= amp F.get_binned_invcov(nls=totcov) bin_invcov = F.bin_invcov bin_cov = F.bin_cov bin_size = F.bins['bins'].size bins = F.bins['bins'] num_pass = F.bins['num_pass_full'] bispec = F.bispec['bispec'] # Plot invcov, cov plot_opts = dict(lmin=2) plot_tools.cls_matrix(plot_tag, bins, bin_invcov, log=False, plot_dell=False, inv=True, **plot_opts) plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov_dell'), bins, bin_cov, log=False, plot_dell=True, **plot_opts) plot_tools.cls_matrix(plot_tag.replace('invcov', 'cov'), bins, bin_cov, log=False, plot_dell=False, **plot_opts) # allocate bin-sized fisher matrix (same size as outer loop) fisher_per_bin = np.ones(bin_size) * np.nan # allocate 12 x 12 cov for use in inner loop invcov = np.zeros((F.bispec['pol_trpl'].size, F.bispec['pol_trpl'].size)) # create (binned) inverse cov matrix for each ell # i.e. use the fact that 12x12 pol invcov can be factored # as (Cl-1)_l1^ip (Cl-1)_l2^jq (Cl-1)_l3^kr invcov1 = np.ones((bin_size, 12, 12)) invcov2 = np.ones((bin_size, 12, 12)) invcov3 = np.ones((bin_size, 12, 12)) f_check = 0 for tidx_a, ptrp_a in enumerate(F.bispec['pol_trpl']): # ptrp_a = ijk for tidx_b, ptrp_b in enumerate(F.bispec['pol_trpl']): # ptrp_a = pqr # a is first bispectrum, b second one # ptrp = pol triplet ptrp_a1 = ptrp_a[0] ptrp_a2 = ptrp_a[1] ptrp_a3 = ptrp_a[2] ptrp_b1 = ptrp_b[0] ptrp_b2 = ptrp_b[1] ptrp_b3 = ptrp_b[2] invcov1[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a1, ptrp_b1] invcov2[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a2, ptrp_b2] invcov3[:, tidx_a, tidx_b] = bin_invcov[:, ptrp_a3, ptrp_b3] # Depending on lmin, start outer loop not at first bin. start_bidx = np.where(bins >= lmin)[0][0] end_bidx = np.where(bins >= min(lmax, bins[-1]))[0][0] + 1 # loop same loop as in binned_bispectrum for idx1, i1 in enumerate(bins[start_bidx:end_bidx]): idx1 += start_bidx cl1 = invcov1[idx1, :, :] # 12x12 # init fisher_per_bin[idx1] = 0. for idx2, i2 in enumerate(bins[idx1:end_bidx]): idx2 += idx1 cl2 = invcov2[idx1, :, :] # 12x12 cl12 = cl1 * cl2 for idx3, i3 in enumerate(bins[idx2:end_bidx]): idx3 += idx2 num = num_pass[idx1, idx2, idx3] if num == 0: continue cl123 = cl12 * invcov3[idx3, :, :] #12x12 B = bispec[idx1, idx2, idx3, :] f = np.einsum("i,ij,j", B, cl123, B) # f0 = np.einsum("i,i", B, B) # b0 = np.einsum("ij,ij", cl123, cl123) # both B's have num f /= float(num) if i1 == i2 == i3: f /= 6. elif i1 != i2 != i3: pass else: f /= 2. fisher_per_bin[idx1] += f f_check += f fisher_per_bin *= fsky f_check *= fsky min_f = [] # print 'fisher_check:', f_check * (4*np.pi / np.sqrt(8))**2 # print 'sigma:', 1/np.sqrt(f_check) * (np.sqrt(8)/4./np.pi) fisher_check = f_check * (4 * np.pi / np.sqrt(8))**2 sigma = 1 / np.sqrt(f_check) * (np.sqrt(8) / 4. / np.pi) return fisher_check, sigma
def loop_over_bins(self, lmin, lmax, parity, bins): # make aux directory where this works F = Fisher(self.test_dir) F.init_bins(lmin=lmin, lmax=lmax, parity=parity, bins=bins) bins = F.bins['bins'] pint = 1 if parity == 'odd' else 0 for i1, b1 in enumerate(bins): for i2, b2 in enumerate(bins): for i3, b3 in enumerate(bins): ell1, ell2, ell3 = F.bins['first_pass'][i1, i2, i3] num = F.bins['num_pass'][i1, i2, i3] try: if b1 > b2: assert num == 0 if b2 > b3: assert num == 0 if num == 0: assert (ell1, ell2, ell3) == (0, 0, 0) else: assert (ell1, ell2, ell3) != (0, 0, 0) if parity is not None: assert (ell1 + ell2 + ell3) % 2 == pint # check if first pass ells fit in bins assert ell1 >= b1 assert ell2 >= b2 assert ell3 >= b3 try: assert ell1 < bins[i1 + 1] except IndexError: if (i1 + 1) >= bins.size: pass else: raise try: assert ell2 < bins[i2 + 1] except IndexError: if (i2 + 1) >= bins.size: pass else: raise try: assert ell3 < bins[i3 + 1] except IndexError: if (i3 + 1) >= bins.size: pass else: raise # Check if first pass matches triangle cond. assert abs(ell1 - ell2) <= ell3 assert ell3 <= (ell1 + ell2) except: print('error in bin:') print('bin_idx: ({},{},{}), ' 'bin: ({},{},{}), no. gd_tuples: {}, '\ 'u_ell: ({},{},{})'.format( i1, i2, i3, b1, b2, b3, F.bins['num_pass'][i1,i2,i3], ell1, ell2, ell3)) raise
def test_save_load_bispectrum(self): lmin = 4 lmax = 10 bins = [4, 5, 6, 8, 10] parity = 'odd' F = Fisher(self.test_dir) F.init_bins(lmin=lmin, lmax=lmax, parity=parity, bins=bins) F.init_pol_triplets() b_shape = (len(bins), len(bins), len(bins), 12) test_bispec = np.arange(len(bins)**3 * 12, dtype=float).reshape(b_shape) # set forbidden ell triplets to zero in the most stupid way. num_pass_bool = F.bins['num_pass_full'].astype(bool) for i1 in xrange(len(bins)): for i2 in xrange(len(bins)): for i3 in xrange(len(bins)): if num_pass_bool[i1, i2, i3] == False: test_bispec[i1, i2, i3, :] *= 0 F.bispec['bispec'] = test_bispec.copy() F._save_binned_bispec('test_bispec.pkl') F._load_binned_bispec('test_bispec.pkl') np.testing.assert_almost_equal(test_bispec, F.bispec['bispec'])
def run(out_dir, tag, prim_template='local', add_noise_opts={}, get_cls_opts={}, interp_fisher_opts={}, dry=False): ''' Calculate and save bins, beta and bispec. Then calculate fisher. Arguments --------- out_dir : str Output directory for Fisher. tag : str Fisher tag for output file. Keyword Arguments --------- prim_template : str dry : bool Do not run fisher estimate (but do everything else). save_cov : bool Save covariance and inverse convariance. kwargs : {add_noise_opts}, {get_cls_opts}, {interp_fisher_opts} ''' F = Fisher(out_dir) if F.mpi_rank == 0: print('working on {}'.format(tag)) if F.mpi_rank == 0: print(tag) beta_tag = 'r1_i1_l5200_16_7' bins_tag = '5200' cosmo_tag = '5200_16_7' bispec_tag = '5200_16_7' F.get_cosmo(load=True, tag=cosmo_tag, verbose=True) F.get_bins(load=True, parity='odd', verbose=True, tag=bins_tag, lmin=2, lmax=5000) F.get_beta(load=True, tag=beta_tag, verbose=True) F.get_binned_bispec(prim_template, load=True, tag=bispec_tag) cls, ells = get_cls(F.cosmo, **get_cls_opts) add_noise(cls, ells, **add_noise_opts) invcov, cov = F.get_invcov(ells, cls, return_cov=True, write=True, write_tag=tag) lmax_outer = 200 if not dry: f_i = F.interp_fisher(invcov, ells, lmin=2, lmax_outer=lmax_outer, verbose=False, **interp_fisher_opts) if F.mpi_rank == 0: print(f_i) r = get_cls_opts['r'] F.save_fisher(f_i, r=r, tag=tag) return
def run(prim_template='equilateral', out_dir=None, camb_dir=None, lmin=2, lmax=5000): ''' Calculate and save bins, beta and bispec. Then calculate fisher. Keyword Arguments --------- prim_template : str out_dir : str camb_dir : str lmin : int lmax : int ''' tag = 'l{}'.format(lmax) camb_opts = dict(camb_out_dir=camb_dir, tag='', lensed=False, high_ell=False, interp_factor=None) F = Fisher(out_dir) F.get_camb_output(**camb_opts) F.get_bins(lmin=lmin, lmax=lmax, load=True, parity='odd', verbose=True, tag=tag) interp_factor = 1 radii_factor = 10 beta_tag = 'r{}_i{}_l{}'.format(radii_factor, interp_factor, lmax) radii = F.get_updated_radii() radii = radii[::radii_factor] F.get_beta(func=prim_template, radii=radii, verbose=True, optimize=True, interp_factor=interp_factor, load=True, sparse=True, tag=beta_tag) F.get_binned_bispec('local', load=True, tag=tag) cls, ells = get_cls(camb_dir, lmax, A_lens=1, no_ee=False, no_tt=False) # NOTE NOTE # cls[:3,:18] += 1e20 # cls[3,:] *= 3 invcov, cov = F.get_invcov(ells, cls, return_cov=True) f_i = F.interp_fisher(invcov, ells, lmin=2, lmax=lmax, verbose=2) if F.mpi_rank == 0: print(f_i) F.barrier() b_invcov, b_cov = F.get_binned_invcov(ells, cls, return_bin_cov=True) if F.mpi_rank == 0: f_n = F.naive_fisher(b_invcov, lmin=2, lmax=lmax) print('naive') print(f_n)