a
    BCCf                      @   s   d dl Z d dlZd dlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ d dlmZmZ G d	d
 d
Zdd ZdddZdd ZdddZdS )    N)stats   )_get_pvalue	_rankdata)
_morestats)_broadcast_arrays_get_wilcoxon_distr)
_lazywhere_get_nanc                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )WilcoxonDistributionc                 C   s4   t |jtdd}|| _dd t |D | _d S )NFcopyc                 S   s   i | ]}|t |qS  r   ).0nir   r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/stats/_wilcoxon.py
<dictcomp>       z1WilcoxonDistribution.__init__.<locals>.<dictcomp>)npasarrayastypeintnunique_dists)selfr   r   r   r   __init__   s    zWilcoxonDistribution.__init__c                 C   s   | j | }|d |d   S Nr   r   sumr   kr   Zpmfsr   r   r   _cdf1   s    
zWilcoxonDistribution._cdf1c                 C   s   t j| jtgd||S N)Zotypes)r   	vectorizer#   floatr   r"   r   r   r   r   _cdf   s    zWilcoxonDistribution._cdfc                 C   s   | j | }||d   S )Nr   r!   r   r   r   _sf1   s    
zWilcoxonDistribution._sf1c                 C   s   t j| jtgd||S r$   )r   r%   r)   r&   r'   r   r   r   _sf   s    zWilcoxonDistribution._sfc                 C   s   | j | j d  d S )Nr      )r   r   r   r   r   mean!   s    zWilcoxonDistribution.meanc                 C   s8   t |jtdd}|  }t j|jt jd}|||fS )NFr   )dtype)r   r   r   r   r-   emptyshapefloat64r   r"   mnoutr   r   r   _prep$   s    zWilcoxonDistribution._prepc                    s8     |\}}}t||k| jf j fdddd S )Nc                    s   d  | d | S r   )r*   r"   r   r,   r   r   <lambda>-   r   z*WilcoxonDistribution.cdf.<locals>.<lambda>f2r   )r5   r
   r   r(   r2   r   r,   r   cdf*   s    
zWilcoxonDistribution.cdfc                    s8     |\}}}t||k| jf j fdddd S )Nc                    s   d  | d | S r   )r(   r6   r,   r   r   r7   2   r   z)WilcoxonDistribution.sf.<locals>.<lambda>r8   r   )r5   r
   r   r*   r2   r   r,   r   sf/   s    
zWilcoxonDistribution.sfN)__name__
__module____qualname__r   r#   r(   r)   r*   r-   r5   r:   r;   r   r   r   r   r      s   r   c              
   C   s  t |d }d}t |jt jr,|jdkr4t|d}zF|d u rRt | } | }nt| |f|d\} }| | }t ||d}W n0 t j	y }	 zt||	W Y d }	~	n
d }	~	0 0 d}|d ur| j
| |j
| krt|d}t |jt jr|t j}t |jt jst|t| }h d	}
d
|
 d}||
vrDt|ddh}d| d}||vrjt|t| }h d}d| d}||vrt|t|tjsh d}d| d}||vrt|t j|dkdd}t |dk}|dkr|j
d dkr|sd}nd}t |dk}|dkrN|dkrNd}tjddd |dkr|dv r||jkr|jdkr|jdkrtdd|j
d   k rdk rn n|dkrtjddd ||||||fS ) Nr   z`axis` must be an integer.r   z<`axis` must be compatible with the shape(s) of `x` (and `y`)axisz3`x` and `y` must have the same length along `axis`.z<`x` (and `y`, if provided) must be an array of real numbers.>   prattzsplitwilcoxz`zero_method` must be one of .TFz`correction` must be one of >   	two-sidedgreaterlessz`alternative` must be one of >   autoapproxexactz`method` must be one of z- or an instance of `stats.PermutationMethod`.rI   2   rK   rJ   z^Exact p-value calculation does not work if there are zeros. Switching to normal approximation.   )
stacklevel)rD   rB   r   zOzero_method 'wilcox' and 'pratt' do not work if x - y is zero for all elements.
   z/Sample size too small for normal approximation.)r   r   Z
issubdtyper.   integerndim
ValueErrorr   ZmoveaxisZ	AxisErrorr0   r   r1   Zfloatingstrlower
isinstancer   ZPermutationMethodr    anywarningswarnsize)xyzero_method
correctionalternativemethodr@   messagedeZzero_methodsZcorrectionsZalternativesmethodsn_zeroZ	has_zerosr   r   r   _wilcoxon_iv5   s    
 




,re   rD   c                 C   s  | dk}|dkr,| j d s"|  } tj| |< t| }tj|dd}| jd | }tt| ddd\}}tj| dk| dd}tj| dk | dd}	|d	krtj|| ddd
 }
||
7 }|	|
7 }	||d  d }||d  d| d  }|dkrD|jdd}|||d  d 8 }|||d  d| d  8 }d||j	dddf< |d | jdd}||d
 8 }t
|d }|| | }||	|||fS )Nr   rD   Z	WRITEABLErA   r?   ZaverageT)Zreturn_tiesrC   rM   g      ?g      ?g       @rB         )flagsr   r   nanisnanr    r0   r   absrV   sqrt)ra   r\   Zi_zerosZi_nanZn_nancountrtr_plusr_minusZr_zero_2r3   serd   Ztie_correctzr   r   r   _wilcoxon_statistic   s6    



rt   c                 C   s&   |dkrdS |dkrdS t | S d S )NrG   r   rH   rA   )r   sign)rs   r^   r   r   r   _correction_sign   s
    rv   TrF   rI   c                    s  t | | ||||}|\} }}}}|jdkrVt|}	tj|	|	d}
|dkrR|	|
_|
S t| \}}}}}|dkr|rt||}||d | 8 }t|t	j
|}n|dkrt|}|dkr||}n>|dkr||}n*dt|||| }t|dd	}n2t	j|f fd
dfddi| |ddj}|dkrNt||n|}|dkrr|dkrrt| n|}tj||d d}
|dkr|d |
_|
S )Nr   )	statisticpvaluerJ   g      ?rK   rH   rG   rM   r   c                    s   t |  d S )Nr   )rt   )ra   r\   r   r   r7      r   z_wilcoxon_nd.<locals>.<lambda>Zpermutation_typeZsamplesrA   )r^   r@   rF   r   )re   rY   r   r   ZWilcoxonResultZ
zstatisticrt   rv   r   r   Znormr   r:   r;   r   minimumZclipZpermutation_test_asdictrx   rk   )rZ   r[   r\   r]   r^   r_   r@   tempra   NaNresrp   rq   rr   rs   rm   ru   pdistrw   r   ry   r   _wilcoxon_nd   sJ    


$

r   )rD   )NrD   TrF   rI   r   )rW   numpyr   Zscipyr   Z	_stats_pyr   r    r   Z_axis_nan_policyr   Z
_hypotestsr	   Zscipy._lib._utilr
   r   r   re   rt   rv   r   r   r   r   r   <module>   s   )P
5	  