B
    dn                 @   sB   d Z ddlZddlZddlZddlmZ G dd deZdgZdS )z[
This modules provides classes for evaluating distributions based on a fixed
set of points
    N)VARARGS_DELIMc               @   sJ   e Zd ZdZdZdd ZdddZdd	 Zd
d Zdd Z	e
dd ZdS )FixedSamplesa  
    A distribution consisting of a collection of a large number of fixed points.
    Only these values can be drawn from, so the number of points may need to be
    large to properly reflect the paramter space. This distribution is intended
    to aid in using nested samplers for semi-abitrary or complicated
    distributions where it is possible to provide or draw samples but less
    straightforward to provide an analytic invcdf. This class numerically
    approximates the invcdf for 1 or 2 dimensional distributions
    (but no higher).

    Parameters
    ----------
    params :
        This of parameters this distribution should use
    samples : dict of arrays or FieldArray
        Sampled points of the distribution. May contain transformed parameters
        which are different from the original distribution. If so, an inverse
        mapping is provided to associate points with other parameters provided.
    Zfixed_samplesc             C   s   || _ || _| j|d  | _t| jd t| j | _| j | _| j| j | _tt	| jt| jksnt
t|dkrtdd S )Nr   g      ?   zBOnly one or two parameters supported for fixed sample distribution)paramssamplesp1lenfracargsortsortp1sortednumpyuniqueAssertionError
ValueError)selfr   r    r   m/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/distributions/fixedsamples.py__init__/   s    zFixedSamples.__init__   c                s.   t jjdtj|d  fddjD S )zDraw random valuer   )highsizec                s   i | ]}j |   |qS r   )r   ).0p)ir   r   r   
<dictcomp>A   s    z$FixedSamples.rvs.<locals>.<dictcomp>)r   randomrandintr   r   r   )r   r   kwdsr   )r   r   r   rvs>   s    zFixedSamples.rvsc             K   s  i }|| j d  }tt|t| j }|t| jkrDt| jd }|dk rPd}| j|  || j d < }t| j dkrz|S || j d  }t| j|d| j  }t| j|d| j  }||k r|| }}tj	| j
|| dd}	| j| j d  }
tj	|
|	 dd}| }tj	|| dd}tt|t| }|t|krLt|d }|dk rZd}|| || j d < tj	| j|	|  dd}|| || j d < |S )z(Map unit cube to parameters in the spacer   r   )Zndmin)r   introundr   r   r   r   searchsortedr	   arrayr   r   r
   )r   originalnewu1i1Zp1vu2lrregionp2Zp2parti2Zp1partr   r   r   cdfinvC   s:    

zFixedSamples.cdfinvc             K   s   |S )z' Apply boundary conditions (none here) r   )r   r   r   r   r   apply_boundary_conditionsk   s    z&FixedSamples.apply_boundary_conditionsc             K   s   dS )z$ Dummy function, not the actual pdf r   r   )r   r   r   r   r   __call__o   s    zFixedSamples.__call__c                s*  ddl m} ddlm}m}m} ddlm} |t|	|d||	|d|}	d
}
|||
d}t|d	kr|td
td |d jtt|	dfddjjD d
}|||dtdkr d |gd   G  fddd|}|||j< | S )ak   Return instance based on config file

        Return a new instance based on the config file. This will draw from
        a single distribution section provided in the config file and
        apply a single transformation section if desired. If a transformation
        is applied, an inverse mapping is also provided for use in the config
        file.
        r   )read_distributions_from_config)read_transforms_from_configapply_transformsBaseTransform)
transformssubnamezsample-sizez	{}_sample)sectionr   zLFixed sample distrubtion only supports a single distribution to sample from.z0Drawing samples for fixed sample distribution:%s)r   c                s   i | ]} | |qS r   r   )r   r   )r   r   r   r      s    z,FixedSamples.from_config.<locals>.<dictcomp>z{}_transformc                   s@   e Zd ZZjZjZd Z 	 Z
 e
 ZfddZdS )z'FixedSamples.from_config.<locals>.Thookr   c                s8   t j|j   fddjD }||S )Nc                s    i | ]}| j    |qS r   )r   )r   r   )idxr   r   r   r   r      s    zEFixedSamples.from_config.<locals>.Thook.transform.<locals>.<dictcomp>)r   r"   r   p1nameoutputsZformat_output)r   mapsout)r   )r8   r   r   	transform   s    z1FixedSamples.from_config.<locals>.Thook.transformN)__name__
__module____qualname__namer:   Z_inputsinputsZ_outputsr9   r
   r   r   r=   r   )r   r   r   r6   transr   r   Thook   s   rD   )Zpycbc.distributionsr1   Zpycbc.transformsr2   r3   r4   r5   splitr   Zget_opt_tagformatr   r   logginginfor   r    floatZdtypenamesrA   )clscpr7   tagr1   r2   r3   r4   Zglobal_transformsr   ZdistsecdistZtransecrD   r   )r   r   r   r6   rC   r   from_configs   s,    




zFixedSamples.from_configN)r   )r>   r?   r@   __doc__rA   r   r   r.   r/   r0   classmethodrO   r   r   r   r   r      s   
(r   )	rP   rG   r   Znumpy.randomZpycbcr   objectr   __all__r   r   r   r   <module>   s    