U
    Ãmœd  ã                   @   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© úO/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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 )zºp-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 )z�create a (n_levels, n_levels) array with corrected p_values

        this needs to improve, similar to R pairwise output
        )r.   r   Z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 )z•returns 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   Únumpyr   Zstatsmodels.tools.testingr   r   r#   r
   r
   r
   r   Ú<module>   s
   