B
    d	                 @   sH   d Z ddlZddlmZ ddlmZ ddlmZ dddZdddZ	dS )z
This module provides functions for drawing samples from a standalone .ini file
in a Python script, rather than in the command line.
    N)InterpolatingConfigParser)
transforms)distributionspriorc             C   sh   t j| |ddd\}}t| dr2t| d}nd}t j| ||d}t | |}t j|f|d|iS )a  Loads a prior distribution from the given config file.

    Parameters
    ----------
    cp : pycbc.workflow.WorkflowConfigParser
        The config file to read.
    sections : list of str, optional
        The sections to retrieve the prior from. If ``None`` (the default),
        will look in sections starting with 'prior'.

    Returns
    -------
    distributions.JointDistribution
        The prior distribution.
    variable_paramsstatic_params)prior_sectionZvargs_sectionZsargs_sectionwaveform_transformsN)r   Zstatic_argsconstraints)	r   Zread_params_from_configanyget_subsectionsr   read_transforms_from_configZread_constraints_from_configZread_distributions_from_configZJointDistribution)cpr   r   r   r	   r
   dists r   f/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/distributions/utils.pyprior_from_config#   s    
r      M c             C   sr   t j| t }t| d}|| |  t|d}|jt	|d}t
|drnt|d}t||}|S )a   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'])
    r)r   )sizer	   )nprandomseedr   open	read_filecloser   Zrvsintr   r   r   r   Zapply_transforms)pathnumr   Zconfig_parserfileZprior_distsZsamplesr	   r   r   r   draw_samples_from_configM   s    %


r!   )r   )r   r   )
__doc__numpyr   Zpycbc.types.configr   Zpycbcr   r   r   r!   r   r   r   r   <module>   s   
*