Beispiel #1
0
def initial_dist_from_config(cp, variable_params):
    r"""Loads a distribution for the sampler start from the given config file.

    A distribution will only be loaded if the config file has a [initial-\*]
    section(s).

    Parameters
    ----------
    cp : Config parser
        The config parser to try to load from.
    variable_params : list of str
        The variable parameters for the distribution.

    Returns
    -------
    JointDistribution or None :
        The initial distribution. If no [initial-\*] section found in the
        config file, will just return None.
    """
    if len(cp.get_subsections("initial")):
        logging.info("Using a different distribution for the starting points "
                     "than the prior.")
        initial_dists = distributions.read_distributions_from_config(
            cp, section="initial")
        constraints = distributions.read_constraints_from_config(
            cp, constraint_section="initial_constraint")
        init_dist = distributions.JointDistribution(
            variable_params, *initial_dists, **{"constraints": constraints})
    else:
        init_dist = None
    return init_dist
Beispiel #2
0
def draw_samples_from_config(path, num=1, seed=150914):
    r""" Generate sampling points from a standalone .ini file.

    Parameters
    ----------
    path : str
        The path to the .ini file.
    num : int
        The number of samples.
    seed: int
        The random seed for sampling.

    Returns
    --------
    samples : pycbc.io.record.FieldArray
        The parameter values and names of sample(s).

    Examples
    --------
    Draw a sample from the distribution defined in the .ini file:

    >>> import numpy as np
    >>> from pycbc.distributions.utils import draw_samples_from_config

    >>> # A path to the .ini file.
    >>> CONFIG_PATH = "./pycbc_bbh_prior.ini"
    >>> random_seed = np.random.randint(low=0, high=2**32-1)
    >>> sample = draw_samples_from_config(
    >>>          path=CONFIG_PATH, num=1, seed=random_seed)

    >>> # Print all parameters.
    >>> print(sample.fieldnames)
    >>> print(sample)
    >>> # Print a certain parameter, for example 'mass1'.
    >>> print(sample[0]['mass1'])
    """

    np.random.seed(seed)

    # Initialise InterpolatingConfigParser class.
    config_parser = InterpolatingConfigParser()
    # Read the file
    file = open(path, 'r')
    config_parser.read_file(file)
    file.close()

    # Get the vairable arguments from the .ini file.
    variable_args, _ = distributions.read_params_from_config(
        config_parser, prior_section='prior', vargs_section='variable_params')
    constraints = distributions.read_constraints_from_config(config_parser)

    if any(config_parser.get_subsections('waveform_transforms')):
        waveform_transforms = transforms.read_transforms_from_config(
            config_parser, 'waveform_transforms')
    else:
        waveform_transforms = None

    # Get prior distribution for each variable parameter.
    dists = distributions.read_distributions_from_config(config_parser)

    # Construct class that will draw the samples.
    randomsampler = distributions.JointDistribution(
        variable_args, *dists, **{"constraints": constraints})

    # Draw samples from prior distribution.
    samples = randomsampler.rvs(size=int(num))

    # Apply parameter transformation.
    if waveform_transforms is not None:
        samples = transforms.apply_transforms(samples, waveform_transforms)
    else:
        pass

    return samples