Exemple #1
0
def main(mask_path: Path, cosmo_path: Path, log_level: int):
    logging.basicConfig(
        stream=sys.stdout,
        level=log_level,
        datefmt='%Y-%m-%d %H:%M',
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    nside = 128
    nmc = 30
    mask = make_binary_mask(nside)
    masking = lynx.Masking(mask_path)
    masking.save_fitting_mask('one', mask)
    masking.calculate_apodizations(mask)
    masking.calculate_mode_coupling()
    cl_theory = read_cl(cosmo_path)
    ells = np.arange(0, len(cl_theory[0]))
    for mask_name, wsp, mask, binning, beam in masking.get_powerspectrum_tools(
    ):
        print(r"""
        Mask name: {:s}
        Fsky: {:.03f} 
        """.format(mask_name, np.mean(mask)))

        ee_bpw_window = wsp.get_bandpower_windows()[0, :, 0, :]
        bb_bpw_window = wsp.get_bandpower_windows()[3, :, 3, :]

        cl_mc = np.zeros((nmc, 4, binning.get_n_bands()))

        for i in range(nmc):
            cmb = get_cmb_realization(nside, cosmo_path, np.pi / 180.)
            cl_mc[i] = compute_nmt_spectra(cmb[1:], cmb[1:], mask, wsp)

        cl_mean = np.mean(cl_mc, axis=0)
        cl_covar_ee = (cl_mc[:, 0] - cl_mean[0])[..., None, :] * (
            cl_mc[:, 0] - cl_mean[0])[..., None]
        cl_covar_ee = np.sum(cl_covar_ee, axis=0) / float(nmc - 1)

        cl_covar_bb = (cl_mc[:, 3] - cl_mean[3])[..., None, :] * (
            cl_mc[:, 3] - cl_mean[3])[..., None]
        cl_covar_bb = np.sum(cl_covar_bb, axis=0) / float(nmc - 1)

        fig, ax = plt.subplots(1, 1)
        ax.errorbar(binning.get_effective_ells(),
                    cl_mean[0],
                    yerr=np.sqrt(np.diag(cl_covar_ee)))
        #ax.plot(binning.get_effective_ells(), cl_mean[0], 'C0-')
        ax.plot(binning.get_effective_ells(),
                np.dot(ee_bpw_window, cl_theory[1][:ee_bpw_window.shape[-1]]),
                'C0--')

        #ax.plot(binning.get_effective_ells(), cl_mean[3], 'C1-')
        ax.plot(binning.get_effective_ells(),
                np.dot(bb_bpw_window, cl_theory[2][:bb_bpw_window.shape[-1]]),
                'C1--')
        ax.errorbar(binning.get_effective_ells(),
                    cl_mean[3],
                    yerr=np.sqrt(np.diag(cl_covar_bb)))

        ax.set_xlim(0, 3 * nside)
        ax.set_yscale('log')
        plt.show()
Exemple #2
0
def main(data_path: Path, model_path: Path, mask_path: Path, log_level: int):
    logging.basicConfig(
        stream=sys.stdout,
        level=log_level,
        datefmt='%Y-%m-%d %H:%M',
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    with open(data_path) as f:
        data_cfg = yaml.load(f, Loader=yaml.FullLoader)
    nside = data_cfg['nside']
    npix = hp.nside2npix(nside)
    nmc = data_cfg['monte_carlo']
    amplitude_output_shape = (nmc, 2, npix)
    parameter_output_shape = (nmc, npix)
    frequencies = np.array(data_cfg['frequencies'])

    with h5py.File(data_cfg['hdf5_path'], 'r') as f:
        data = f['maps/monte_carlo/data'][...]
        cov = f['maps/monte_carlo/cov'][...]

    masking = lynx.Masking(mask_path)
    fitting_masks = list(masking.get_fitting_indices())

    tasks = get_tasks(data, cov, nmc, frequencies, fitting_masks, model_path)

    with MultiPool() as pool:
        results = pool.map(do_fitting, tasks)

    with h5py.File(data_cfg['hdf5_path'], 'a') as f:
        for result in results:
            save_data(f, amplitude_output_shape, parameter_output_shape,
                      result)
Exemple #3
0
def main(data_path: Path, mask_path: Path, model_path: Path, log_level: int):
    logging.basicConfig(
        stream=sys.stdout,
        level=log_level,
        datefmt='%Y-%m-%d %H:%M',
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    masking = lynx.Masking(mask_path)
    fitting_masks = list(masking.get_fitting_indices())

    model_identifier, lnP = hoover.LogProb.load_model_from_yaml(model_path)

    with h5py.File(data_path, 'r') as f:
        sky_config = yaml.load(f.attrs['config'], Loader=yaml.FullLoader)
        nmc = sky_config['monte_carlo']
        frequencies = np.array(sky_config['frequencies'])
        nside = sky_config['nside']

    amplitude_output_shape = (2, hp.nside2npix(nside))
    parameter_output_shape = (hp.nside2npix(nside), )

    for fitting_name, fitting_parameters in fitting_masks:
        T_bar = {
            comp: np.zeros(amplitude_output_shape)
            for comp in lnP._components
        }
        par = {
            par: np.zeros(parameter_output_shape)
            for par in lnP.free_parameters
        }
        for imc in range(nmc):
            logging.info(r"""
            Working on Monte Carlo realization: {:d}
            """.format(imc))
            hdf5_record = str(
                Path(model_identifier) / fitting_name / 'mc{:04d}'.format(imc))

            logging.info(r"""
            Working on fitting scheme: {:s}
            """.format(fitting_name))

            with h5py.File(data_path, 'a') as f:
                # Create a group which contains results for this sky
                # patch, model, and MC realization.
                opt = f[hdf5_record]
                # Create a dataset for the whole sky, and log the
                # results for this patch in the corresponding indices.
                # Do the same for the spectral parameters.
                for component in lnP._components:
                    T_bar[component] += opt[component][...]

                for parameter in lnP.free_parameters:
                    par[parameter] += opt[parameter][...]

        T_bar = {key: value / float(nmc) for key, value in T_bar.items()}
        par = {key: value / float(nmc) for key, value in par.items()}

        hp.mollview(T_bar['cmb'][0])
        hp.mollview(par['beta_d'])
        plt.show()
Exemple #4
0
def main(data_path: Path, model_path: Path, mask_path: Path,
         estimate_noise: bool, log_level: int):
    logging.basicConfig(
        stream=sys.stdout,
        level=log_level,
        datefmt='%Y-%m-%d %H:%M',
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    with open(data_path) as f:
        data_cfg = yaml.load(f, Loader=yaml.FullLoader)

    data_id = data_cfg['identifier']
    nmc = data_cfg['monte_carlo']
    hdf5_path = data_cfg['hdf5_path']

    logging.info(f"Working on data: {data_id}")

    masking = lynx.Masking(mask_path)
    fitting_masks = list(masking.get_fitting_indices())

    model_identifier, lnP = hoover.LogProb.load_model_from_yaml(model_path)

    for mask_name, wsp, mask, binning, beam in masking.get_powerspectrum_tools(
    ):
        # get the bandpower window function, which will be saved with the
        # computed spectra for comparison with theory later.
        bpw_window_function = wsp.get_bandpower_windows()

        logging.info(f"Working on power spectra for mask: {mask_name}")

        for fitting_name, _ in fitting_masks:

            logging.info(f"Working on fitting scheme: {fitting_name}")

            hdf5_record_amp = Path(model_identifier) / fitting_name
            hdf5_record_spec = hdf5_record_amp / 'spectra' / mask_name

            with h5py.File(hdf5_path, 'a') as f:
                # Create a group which contains results for this sky
                # patch, model, and MC realization.
                spec = f.require_group(f"{hdf5_record_spec}")
                spec.attrs.update({'config': yaml.dump(masking.cfg)})
                # save bandpower window function
                dset = spec.require_dataset('bpw_window_function',
                                            shape=bpw_window_function.shape,
                                            dtype=bpw_window_function.dtype)
                dset[...] = bpw_window_function
                dset = spec.require_dataset('beam',
                                            shape=beam.shape,
                                            dtype=beam.dtype)
                dset[...] = beam

                # Create a dataset for the whole sky, and log the
                # results for this patch in the corresponding indices.
                # Do the same for the spectral parameters.
                for component in lnP._components:
                    logging.info(f"Working on component: {component}")
                    cl_mc = np.zeros((int(nmc / 2), 4, binning.get_n_bands()))
                    for ispec, imc in enumerate(np.arange(nmc)[::2]):
                        jmc = imc + 1
                        logging.info(
                            f"Working on Monte Carlo realizations: {imc:d}, {jmc:d}"
                        )
                        T_bar_1 = f[f"{hdf5_record_amp / component}_T_bar"][
                            imc, ...]
                        T_bar_2 = f[f"{hdf5_record_amp / component}_T_bar"][
                            jmc, ...]
                        cl_mc[ispec] = compute_nmt_spectra(
                            T_bar_1, T_bar_2, mask, wsp)

                        if component == 'cmb':
                            N_T_1 = f[f"{hdf5_record_amp}"]['cmb_N_T'][...]
                            N_T_2 = f[f"{hdf5_record_amp}"]['cmb_N_T'][...]

                            noise_mc = 30
                            cl_n = np.zeros((noise_mc, binning.get_n_bands()))

                    logging.info(f"Saving in {spec.name}/{component}")
                    cl_dset = spec.require_dataset(component,
                                                   dtype=cl_mc.dtype,
                                                   shape=cl_mc.shape)
                    cl_dset[...] = cl_mc

                if estimate_noise:

                    for k in range(noise_mc):
                        n1 = get_realization(N_T_1)
                        n2 = get_realization(N_T_2)
                        cl_n[k] = compute_nmt_spectra(n1, n2, mask, wsp)[3]

                    cl_n_mean, cl_n_cov = compute_mean_cov(cl_n)
                    cl_n_dset = spec.require_dataset(f"{component}_cln_mean",
                                                     dtype=cl_n_mean.dtype,
                                                     shape=cl_n_mean.shape)
                    cl_n_dset[...] = cl_n_mean
                    cl_n_dset = spec.require_dataset(f"{component}_cln_cov",
                                                     dtype=cl_n_cov.dtype,
                                                     shape=cl_n_cov.shape)
                    cl_n_dset[...] = cl_n_cov
Exemple #5
0
def main(data_path: Path, mask_path: Path, model_path: Path, lkl_path: Path,
         log_level: int):
    logging.basicConfig(
        stream=sys.stdout,
        level=log_level,
        datefmt='%Y-%m-%d %H:%M',
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    with open(data_path) as f:
        data_cfg = yaml.load(f, Loader=yaml.FullLoader)

    hdf5_path = data_cfg['hdf5_path']

    with open(lkl_path) as f:
        lkl_cfg = yaml.load(f, Loader=yaml.FullLoader)

    masking = lynx.Masking(mask_path)

    fitting_masks = list(masking.get_fitting_indices())

    model_identifier, lnP = hoover.LogProb.load_model_from_yaml(model_path)

    for mask_name, wsp, _, binning, _ in masking.get_powerspectrum_tools():

        for fitting_identifier, _ in fitting_masks:

            results_dir = Path("data/results") / data_cfg[
                'identifier'] / mask_name / fitting_identifier / model_identifier / lkl_cfg[
                    'identifier']
            results_dir.mkdir(exist_ok=True, parents=True)

            hdf5_record = Path(
                model_identifier) / fitting_identifier / 'spectra' / mask_name

            with h5py.File(hdf5_path, 'r') as f:
                ee_mean, ee_cov = compute_mean_cov(f[f"{hdf5_record}/cmb"][:,
                                                                           0])
                bb_mean, bb_cov = compute_mean_cov(f[f"{hdf5_record}/cmb"][:,
                                                                           3])
                bb_mean_dust, bb_cov_dust = compute_mean_cov(
                    f[f"{hdf5_record}/dustmbb"][:, 3])
                ee_mean_dust, ee_cov_dust = compute_mean_cov(
                    f[f"{hdf5_record}/dustmbb"][:, 0])

            bpw_windows = wsp.get_bandpower_windows()
            ee_bpw_windows = bpw_windows[0, :, 0, :]
            bb_bpw_windows = bpw_windows[3, :, 3, :]
            bpws = binning.get_effective_ells()

            with h5py.File("data/planck_2018_acc.h5", 'r') as f:
                ee = f['lensed_scalar'][:ee_bpw_windows.shape[-1], 1]
                bb = f['lensed_scalar'][:bb_bpw_windows.shape[-1], 2]

            fig, ax = plt.subplots(1, 1)
            ax.set_title("BB bandpower covariance matrix")
            ax.imshow(bb_cov,
                      origin='lower',
                      extent=[bpws[0], bpws[-1], bpws[0], bpws[-1]])
            ax.set_xlabel(r"$\ell_b$")
            ax.set_ylabel(r"$\ell_b$")
            fig.savefig(results_dir / "bb_cov.pdf", bbox_inches='tight')

            fig, ax = plt.subplots(1, 1)
            ax.plot(bpws,
                    np.dot(ee_bpw_windows, ee),
                    'k-',
                    label='Binned theory input')
            ax.errorbar(bpws,
                        ee_mean,
                        np.sqrt(np.diag(ee_cov)),
                        color='k',
                        fmt='o',
                        label='Cleaned estimate')
            ax.set_yscale('log')
            ax.set_xlabel(r"$\ell_b$")
            ax.set_ylabel(r"$C_{\ell_b}^{\rm EE}~[{\rm \mu K}^2]$")
            ax.legend()
            fig.savefig(results_dir / "ee_recovered.pdf", bbox_inches='tight')

            lnP = lynx.BBLogLike(data=(bb_mean, bb_cov),
                                 bpw_window_function=bb_bpw_windows,
                                 model_config_path=lkl_path)
            res = minimize(lnP,
                           lnP.theta_0(),
                           args=(True),
                           method='Nelder-Mead')

            fig, ax = plt.subplots(1, 1)
            samples = np.random.multivariate_normal(res.x,
                                                    lnP.covariance(res.x), 100)
            samples = np.array([lnP.model(sample) for sample in samples])
            for s in samples:
                ax.plot(bpws, s, 'C1', alpha=0.1)

            ax.errorbar(bpws,
                        bb_mean_dust,
                        np.sqrt(np.diag(bb_cov_dust)),
                        color='C1',
                        fmt='o',
                        label="Dust at 353 GHz")

            ax.plot(bpws,
                    np.dot(bb_bpw_windows, bb),
                    'k-',
                    label='Binned theory input')
            #ax.plot(bpws, np.sqrt(np.diag(cln_bb_cov)), 'g-', label=r'$\sigma(C_\ell^{\rm BB})$')
            ax.errorbar(bpws,
                        bb_mean,
                        np.sqrt(np.diag(bb_cov)),
                        color='k',
                        fmt='o',
                        label='Cleaned estimate')
            ax.set_yscale('log')
            ax.set_ylim(1e-7, 4e-3)
            ax.set_xlabel(r"$\ell_b$")
            ax.set_ylabel(r"$C_{\ell_b}^{\rm BB}~[{\rm \mu K}^2]$")
            ax.legend()
            fig.savefig(results_dir / "bb_recovered.pdf", bbox_inches='tight')

            plot_fisher(res.x,
                        lnP.covariance(res.x),
                        truth=[1., 0.],
                        fpath=results_dir / "ellipses.pdf",
                        xlabel=r"$A_L$",
                        ylabel=r"$r$")

            print(res.x)
            print(np.sqrt(np.diag(lnP.covariance(res.x))))
            print('Reduced chi2: ', lnP.chi2(res.x))

            np.save(results_dir / "parameter_ML.npy", res.x)
            np.save(results_dir / "parameter_covariance.npy",
                    lnP.covariance(res.x))

            results = pd.DataFrame({
                'mean':
                pd.Series(res.x, index=lnP.free_parameters),
                '1-sigma':
                pd.Series(np.sqrt(np.diag(lnP.covariance(res.x))),
                          index=lnP.free_parameters)
            })
            print(results)
            results.to_csv(results_dir / "results.csv")