def set_base_model(disc):
    """Set the base cosmological model used for likelihood evaluation.

    Parameters
    ----------
    disc : :class:`harmonia.algorithms.discretisation.DiscreteSpectrum`
        Discrete spectrum object.

    Returns
    -------
    :class:`harmonia.reader.models.SphericalCorrelator`
        Spherical correlator model.

    """
    cosmo_dir = data_dir / "external" / "cosmology"
    cosmo = BaseModel(cosmo_dir / "simulation-GadgetAHF.txt")

    product_dir = data_dir / "processed" / "survey_products"
    couplings = Couplings.load(
        product_dir /
        ("couplings-(rmax=500.0,kmax={},mask={},selection={}).npz".format(
            KCUT, MASK_TAG, SELECTION_TAG)))

    return SphericalCorrelator(disc,
                               redshift=1.,
                               growth_rate=0.,
                               couplings=couplings,
                               cosmo=cosmo)
Beispiel #2
0
def define_survey_selection(random_catalogue_file):
    """Define survey selection functionfrom a random catalogue.

    Parameters
    ----------
    random_catalogue_file : *str or* :class:`pathlib.Path`
        Random catalogue source file.

    Returns
    -------
    selection_function : float :class:`numpy.ndarray`
        Selection function samples at discrete radii.
    sky_fraction : float
        Sky fractional coverage.

    """
    # pylint: disable=no-member
    # Read catalogue Z, RA and DEC columns.
    with fits.open(random_catalogue_file) as random_catalogue_source:
        random_catalogue = random_catalogue_source[1].data
        z_samples = random_catalogue['Z']
        dec, ra = random_catalogue['DEC'], random_catalogue['RA']
    logger.info("Loaded random catalogue file %s.", random_catalogue_file)

    # Determine sky fraction.
    sky_mask = np.zeros(hp.nside2npix(params.nside))
    hit_coords = sky_to_spherical(np.column_stack((dec, ra)))
    hit_pixels = hp.ang2pix(params.nside, hit_coords[:, 0], hit_coords[:, 1])
    sky_mask[hit_pixels] = 1
    sky_fraction = np.sum(sky_mask) / np.size(sky_mask)

    # Load cosmology for z-to-r conversion.
    cosmo = BaseModel(cosmo_dir / params.cosmology_file)

    try:
        bins = int(params.bins)
    except (TypeError, ValueError):
        bins = params.bins

    sampled_selection = tuple(
        reversed(
            generate_selection_samples(params.coord,
                                       params.max_coord,
                                       z_samples,
                                       cosmo=cosmo,
                                       sky_fraction=sky_fraction,
                                       bins=bins)))

    logger.info("Selection function sampled.")

    return sampled_selection, sky_fraction
        "mask=None", "selection=None"
    ]))

    confirm_directory(output_dir)

    # Make catalogue measurements.
    measurements = \
        make_catalogue_measurements(kmin=progrc.kmin, kmax=progrc.kmax)

    np.savez(
        output_dir/output_filename.replace("likelihood", "pk"),
        **measurements
    )

    # Make model predictions.
    cosmo = BaseModel(cosmo_dir/progrc.cosmology_file)

    mode_modifications = float(progrc.NG) \
        * scale_dependence_modification(cosmo, Z)(measurements['k'])
    mode_powers = LinearPower(cosmo, Z)(measurements['k'])

    gaussianised_data_vector = measurements['pk'] ** (1./3.)
    gaussianised_expectation_factor = np.array([
        np.exp(loggamma(n + 1./3.) - loggamma(n)) / n ** (1./3.)
        for n in measurements['nk']
    ])
    gaussianised_variance_factor = np.array([
        np.exp(loggamma(n + 2./3.) - loggamma(n)) / n ** (2./3.)
        - np.exp(loggamma(n + 1./3.) - loggamma(n)) ** 2 / n ** (2./3.)
        for n in measurements['nk']
    ])
def setup_likelihood():
    """Set up log-likelihood function.

    Returns
    -------
    :class:`~harmonia.reader.likelihoods.LogLikelihood`
        Log-likelihood function (object).

    """
    simulation_cosmo = BaseModel(cosmo_dir / params.cosmology_file, comm=comm)

    growth_rate = None if params.rsd else 0.

    if params.likelihood in ['spherical', 'hybrid']:
        spherical_data_file = data_dir / params.map_dir / input_filename.format(
            "spherical", params.kmin, params.khyb, None)
        spherical_data = SphericalArray.load(
            spherical_data_file.with_suffix(spherical_data_file.suffix +
                                            params.map_file_extension[0]))

        if params.couplings_file is not None:
            couplings = Couplings.load(
                survey_product_dir / params.couplings_file.format(
                    params.khyb, params.mask_tag, params.selection_tag))
        else:
            couplings = None

        spherical_model = SphericalCorrelator(spherical_data.disc,
                                              params.redshift,
                                              cosmo=simulation_cosmo,
                                              growth_rate=growth_rate,
                                              couplings=couplings,
                                              comm=comm)
    else:
        spherical_data = None
        spherical_model = None

    if params.likelihood in ['cartesian', 'hybrid']:
        cartesian_data_file = data_dir / params.map_dir / input_filename.format(
            "cartesian", params.khyb, params.kmax, order_tag)
        cartesian_data = CartesianArray.load(
            cartesian_data_file.with_suffix(cartesian_data_file.suffix +
                                            params.map_file_extension[1]))

        if params.mask_multipole_file is not None:
            mask_multipoles = np.load(survey_product_dir /
                                      params.mask_multipole_file,
                                      allow_pickle=True)
        else:
            mask_multipoles = None

        if params.window_multipole_file is not None:
            window_multipoles = CartesianArray.load(
                survey_product_dir / params.window_multipole_file)
        else:
            window_multipoles = None

        cartesian_model = CartesianMultipoles(
            cartesian_data.attrs['wavenumbers'],
            params.redshift,
            cosmo=simulation_cosmo,
            growth_rate=growth_rate,
            mask_multipoles=mask_multipoles,
            window_multipoles=window_multipoles)

        if params.covariance_file is not None:
            covariance_estimator = CovarianceEstimator.load(
                survey_product_dir / params.covariance_file)
            mode_counts = None
        else:
            covariance_estimator = None
            mode_counts = cartesian_data.attrs['mode_counts']
    else:
        cartesian_data = None
        covariance_estimator = None
        mode_counts = None
        cartesian_model = None

    return LogLikelihood(spherical_pivot=spherical_pivot,
                         cartesian_pivot=cartesian_pivot,
                         spherical_data=spherical_data,
                         cartesian_data=cartesian_data,
                         covariance_estimator=covariance_estimator,
                         mode_counts=mode_counts,
                         base_spherical_model=spherical_model,
                         base_cartesian_model=cartesian_model,
                         nbar=params.density,
                         contrast=params.contrast,
                         tracer_p=params.tracer_p,
                         comm=comm)
        "rsd=False",
        "mask={}".format(MASK_TAG),
        "selection={}".format(SELECTION_TAG),
    ]))

    product_dir = data_dir / "processed" / "survey_products"
    couplings_file = "couplings-({}).npz".format(",".join([
        "rmax=500.0",
        "kmax={}".format(KCUT),
        "mask={}".format(MASK_TAG),
        "selection={}".format(SELECTION_TAG),
    ]))

    output_dir = data_dir / "raw" / "survey_validation"
    output_filename = "spherical-map-validation-({})".format(",".join([
        "scale=[None,0.04]", "rsd=False", "mask={}".format(MASK_TAG),
        "selection={}".format(SELECTION_TAG)
    ]))

    # Validate maps.
    cosmology = BaseModel(cosmo_file)

    model_params = dict(b_1=BIAS, f_nl=NG, nbar=DENSITY, contrast=CONTRAST)

    map_power, _, disc = extract_map_power(MAP_SERIALS)
    spherical_spectrum = predict_power_spectrum()
    figure = compare_power_spectrum()
    # pylint: disable=using-constant-test
    if False:
        figure.save(output_dir / (output_filename + '.pdf'))
def validate_fullsky_spherical_model(b_1=2.3415,
                                     f_nl=100.,
                                     nbar=2.5e-4,
                                     contrast=10.,
                                     use_shortcut=True):
    """Validate full-sky spherical modelling against baseline power
    spectrum model.

    Parameters
    ----------
    b_1, f_nl, nbar, contrast : float, optional
        Cosmological and model parameters to pass to the model.  Defaults
        are 2.3415, 100., 2.5e-4 and 10. respectively.
    use_shortcut : bool, optional
        If `True` (default), short cuts (Kronecker delta reduction in the
        absence of couplings and diagonalisation of correlator matrix)
        are used.

    """
    # External information.
    simulation_cosmo = BaseModel(cosmo_dir / cosmo_file)

    if use_shortcut:
        fullsky_couplings = None
        disc = DiscreteSpectrum(RMAX, 'dirichlet', KMAX)
    else:
        fullsky_couplings = Couplings.load(
            survey_product_dir /
            couplings_file.format(RMAX, KCUT, "1.0", "None"))
        disc = fullsky_couplings.disc

    # Spherical model.
    # pylint: disable=global-statement
    global spherical_model_fullsky
    spherical_model_fullsky = SphericalCorrelator(disc,
                                                  REDSHIFT,
                                                  cosmo=simulation_cosmo,
                                                  growth_rate=0.,
                                                  couplings=fullsky_couplings,
                                                  comm=comm)

    spherical_radial = spherical_model_fullsky.radialised_power(
        b_1=b_1, f_nl=f_nl, nbar=nbar, contrast=contrast)

    wavenumbers = spherical_radial['wavenumbers']
    spherical_spectrum = spherical_radial['mode_powers']

    # Cartesian model.
    cartesian_spectrum = modified_power_spectrum(
        b_1=b_1, f_nl=f_nl, cosmo=simulation_cosmo,
        redshift=REDSHIFT)(wavenumbers) + (1 + 1 / contrast) / nbar

    _ratios = spherical_spectrum / cartesian_spectrum - 1

    sns.set(style='ticks', font='serif')
    plt.figure("full sky spherical model validation")
    plt.loglog(wavenumbers, spherical_spectrum, label='spherical')
    plt.loglog(wavenumbers, cartesian_spectrum, ls='--', label='Cartesian')
    plt.xlabel(r"$k\ \  [h/\mathrm{{Mpc}}]$")
    plt.ylabel(r"$P(k)\ \ [(\mathrm{{Mpc}}/h)^3]$")
    plt.legend()
    plt.title("Full-sky spherical model validation")

    output_file = output_dir / output_filename.format("1.0", "None")
    plt.savefig(output_file.with_suffix(output_file.suffix + '.pdf'))

    return _ratios
def validate_spherical_correlator_model(b_1=1.,
                                        f_nl=0.,
                                        nbar=2.5e-4,
                                        contrast=10.):
    """Validate generic spherical correlator predictions against estimates
    from large sample sets.

    Parameters
    ----------
    b_1, f_nl, nbar, contrast : float, optional
        Cosmological and model parameters to pass to the model.  Defaults
        are 1., 0., 2.5e-4 and 10. respectively.

    """
    # External information.
    simulation_cosmo = BaseModel(cosmo_dir / cosmo_file)

    couplings = Couplings.load(
        survey_product_dir /
        couplings_file.format(RMAX, KCUT, MASK_TAG, SELECTION_TAG))

    spherical_correlation_estimate = covar_to_corr(
        np.load(raw_product_dir / corr_estimate_file, allow_pickle=True))

    # Spherical model.
    # pylint: disable=global-statement
    global spherical_model_generic
    spherical_model_generic = SphericalCorrelator(couplings.disc,
                                                  REDSHIFT,
                                                  cosmo=simulation_cosmo,
                                                  growth_rate=0.,
                                                  couplings=couplings,
                                                  comm=comm)

    spherical_correlation_model = covar_to_corr(
        spherical_model_generic.correlator_matrix("natural",
                                                  b_1=b_1,
                                                  f_nl=f_nl,
                                                  nbar=nbar,
                                                  contrast=contrast,
                                                  report_progress=True))

    _ratios = spherical_correlation_estimate / spherical_correlation_model - 1

    sns.set(style='ticks', font='serif')
    plt.figure("partial sky spherical model validation", figsize=(10, 2.75))
    plt.subplot2grid((1, 3), (0, 0))
    sns.heatmap(spherical_correlation_model.real,
                rasterized=True,
                square=True,
                center=0.,
                vmin=-1.,
                vmax=1.)
    plt.subplot2grid((1, 3), (0, 1))
    sns.heatmap(spherical_correlation_estimate.real,
                rasterized=True,
                square=True,
                center=0.,
                vmin=-1.,
                vmax=1.)
    plt.subplot2grid((1, 3), (0, 2))
    sns.heatmap(spherical_correlation_estimate.real -
                spherical_correlation_model.real,
                cmap='coolwarm',
                rasterized=True,
                square=True,
                center=0.)

    plt.subplots_adjust(wspace=0.4,
                        bottom=0.175,
                        top=0.825,
                        left=0.05,
                        right=0.95)

    output_file = output_dir / output_filename.format(MASK_TAG, SELECTION_TAG)
    plt.savefig(output_file.with_suffix(output_file.suffix + '.pdf'))

    return _ratios