U
    Kvf                     @   sF   d Z ddlmZ ddlZddlmZ G dd deZG dd dZdS )	zgBase classes for statistical test results

Created on Mon Apr 22 14:03:21 2013

Author: Josef Perktold
    )lzipN)Holderc                       sD   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZ  Z	S )HolderTuplez Holder class with indexing

    Nc                    sF   t t jf | |d k	r4t fdd|D  _n j jf _d S )Nc                 3   s   | ]}t  |V  qd S N)getattr).0Zattself :/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/stats/base.py	<genexpr>   s     z'HolderTuple.__init__.<locals>.<genexpr>)superr   __init__tupleZ	statisticZpvalue)r	   tuple_kwds	__class__r   r   r      s    zHolderTuple.__init__c                 c   s   | j E d H  d S r   r   r   r
   r
   r   __iter__   s    zHolderTuple.__iter__c                 C   s
   | j | S r   r   )r	   idxr
   r
   r   __getitem__   s    zHolderTuple.__getitem__c                 C   s
   t | jS r   )lenr   r   r
   r
   r   __len__   s    zHolderTuple.__len__c                 C   s   t jt| j|dS )N)dtype)npZasarraylistr   )r	   r   r
   r
   r   	__array__"   s    zHolderTuple.__array__)N)N)
__name__
__module____qualname____doc__r   r   r   r   r   __classcell__r
   r
   r   r   r      s   r   c                   @   s<   e Zd ZdZdddZdddZdd	 Zd
d Zdd ZdS )AllPairsResultsa  Results class for pairwise comparisons, based on p-values

    Parameters
    ----------
    pvals_raw : array_like, 1-D
        p-values from a pairwise comparison test
    all_pairs : list of tuples
        list of indices, one pair for each comparison
    multitest_method : str
        method that is used by default for p-value correction. This is used
        as default by the methods like if the multiple-testing method is not
        specified as argument.
    levels : {list[str], None}
        optional names of the levels or groups
    n_levels : None or int
        If None, then the number of levels or groups is inferred from the
        other arguments. It can be explicitly specified, if the inferred
        number is incorrect.

    Notes
    -----
    This class can also be used for other pairwise comparisons, for example
    comparing several treatments to a control (as in Dunnet's test).

    hsNc                    sj   || _ || _|d kr&t|d | _n|| _|| _ | _ d krRdd |D | _n fdd|D | _d S )N   c                 S   s   g | ]}d |f qS )z%rr
   r   pairsr
   r
   r   
<listcomp>N   s     z,AllPairsResults.__init__.<locals>.<listcomp>c                    s(   g | ] }d  |d   |d  f qS )z%s-%sr   r%   r
   r&   levelsr
   r   r(   P   s   
)	pvals_raw	all_pairsr   maxn_levelsmultitest_methodr*   all_pairs_names)r	   r+   r,   r/   r*   r.   r
   r)   r   r   A   s    
zAllPairsResults.__init__c                 C   s4   ddl m  m} |dkr | j}|j| j|dd S )zp-values corrected for multiple testing problem

        This uses the default p-value correction of the instance stored in
        ``self.multitest_method`` if method is None.

        r   N)methodr%   )statsmodels.stats.multiteststats	multitestr/   Zmultipletestsr+   )r	   r1   smtr
   r
   r   pval_correctedT   s    zAllPairsResults.pval_correctedc                 C   s   |   S r   )summaryr   r
   r
   r   __str__a   s    zAllPairsResults.__str__c                 C   s*   | j }t||f}|  |t| j < |S )zcreate a (n_levels, n_levels) array with corrected p_values

        this needs to improve, similar to R pairwise output
        )r.   r   zerosr6   r   r,   )r	   kZ	pvals_matr
   r
   r   
pval_tabled   s    zAllPairsResults.pval_tablec                 C   sz   ddl m  m} tdd | jD }d|j| j  }|dd|d d	   d
 7 }|ddd t| j| 	 D 7 }|S )zreturns text summarizing the results

        uses the default pvalue correction of the instance stored in
        ``self.multitest_method``
        r   Nc                 s   s   | ]}t |V  qd S r   )r   )r   ssr
   r
   r   r   v   s     z*AllPairsResults.summary.<locals>.<genexpr>z0Corrected p-values using %s p-value correction

ZPairs    r%   z	p-values

c                 s   s   | ]\}}d ||f V  qdS )z	%s  %6.4gNr
   )r   r'   pvr
   r
   r   r   {   s     )
r2   r3   r4   r-   r0   Zmultitest_methods_namesr/   joinzipr6   )r	   r5   maxleveltextr
   r
   r   r7   o   s    

zAllPairsResults.summary)r$   NN)N)	r   r   r    r!   r   r6   r8   r;   r7   r
   r
   r
   r   r#   &   s       

r#   )	r!   Zstatsmodels.compat.pythonr   Znumpyr   Zstatsmodels.tools.testingr   r   r#   r
   r
   r
   r   <module>   s
   