o
    5ήc                     @   s  d dl Z d dlmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZmZmZmZmZ d dl	mZ d dl	mZ dZee d dZed	d
dgZedg dZedg dZejerkeeeddZne  Zded< e!dg  e!dg  e!dg  d\Z"Z#de_$g dZ%g dZ&g dZ'dd e (e%e&e'D Z)d\Z*Z+dd e,d D Z-d!d" Z.d#d$ Z/d%d& Z0d'd( Z1d)d* Z2d+d, Z3d-d. Z4d>d/d0Z5d1d2 Z6d3d4 Z7d5d6 Z8d7d8 Z9d9d: Z:d;d< Z;e<d=kre;  dS dS )?    N)
namedtuple)Pool	cpu_count)gammapisqrtquadinfmpfmp)npdf)ncdfz&data/studentized_range_mpmath_ref.json   MPResultsrc_case	mp_resultCdfCase)qkvexpected_atolexpected_rtol
MomentCase)mr   r   r   r   rmodez]!!!!!! THIS FILE WAS AUTOGENERATED BY RUNNING `python studentized_range_mpmath_ref.py` !!!!!!COMMENTcdf_datapdf_datamoment_data)dy=r!      )g?r      
   )   r$      )r%   r$   r&   2   d   x   c                 C   s"   g | ]}t g |ttR  qS  )r   general_atolgeneral_rtol).0caser*   r*   U/tmp/pip-target-vg8gfxp4/lib/python/scipy/stats/tests/studentized_range_mpmath_ref.py
<listcomp>A   s    r0   )&.>r1   c                 C   s   g | ]
}t |d dttqS )r%   r$   )r   mom_atolmom_rtol)r-   ir*   r*   r/   r0   K   s       c                  C   sB   t tdd} tjt| dd W d   dS 1 sw   Y  dS )z3Writes the current res_dict to the target JSON filewr      )indentN)openresults_filepathjsondumpres_dict)fr*   r*   r/   
write_dataQ   s   "r?   c                 C   s   t |  S )zConverts a namedtuple to a dict)dict_asdict)named_tupler*   r*   r/   to_dictW   s   rC   c                 C   s   t | jt| jdS )z;Formats an MPResult namedtuple into a dict for JSON dumping)r   r   )rC   r   floatr   )r   r*   r*   r/   mp_res_to_dict\   s   rE   c                    h   t t t fdd  fddfdd}t|dtgt tgdd	d
}|S )z7Straightforward implementation of studentized range CDFc                    s(   t |t||   t|  d   S Nr   phiPhiszr   r   r*   r/   innerj   s   (zcdf_mp.<locals>.innerc                    s&   | d  t t|    | | S rG   rI   r   rK   rO   nur*   r/   outerm   s   &zcdf_mp.<locals>.outerc                    sB   t dt   d   td dd d    | | S Nr7   r   r   r   r   rK   r   rR   rS   r*   r/   wholep   s
   zcdf_mp.<locals>.wholer   gauss-legendrer$   method	maxdegreer
   r   r	   r   r   rR   rW   resr*   rO   r   rR   rS   r   r/   cdf_mpf      r`   c                    rF   )z7Straightforward implementation of studentized range PDFc                    s8   t ||   t | t||   t|  d   S Nr7   rH   rK   rN   r*   r/   rO   }      8zpdf_mp.<locals>.innerc                    s"   |  t t|    | | S NrP   rK   rQ   r*   r/   rS      s   "zpdf_mp.<locals>.outerc                    sJ   t dt    d  d   td dd d    | | S rT   rU   rK   rV   r*   r/   rW      s
   "zpdf_mp.<locals>.wholer   rX   r$   rY   r\   r]   r*   r_   r/   pdf_mpy   ra   re   c                    sz   t t t fdd  fddfddfdd}t|d	tgd	tgt tgd
dd}|S )z.Implementation of the studentized range momentc                    s8   t || |  t | t|| |  t|  d   S rb   rH   r   rL   rM   )r   r*   r/   rO      rc   zmoment_mp.<locals>.innerc                    s$   | t t|   | || S rd   rP   rf   rQ   r*   r/   rS      s   $zmoment_mp.<locals>.outerc                    sL   t dt    d  d   td dd d    | || S rT   rU   rf   rV   r*   r/   pdf   s
   "
zmoment_mp.<locals>.pdfc                    s   |   | || S rd   r*   rf   )r   rg   r*   r/   rW      s   zmoment_mp.<locals>.wholer   rX   r$   rY   r\   )r   r   rR   rW   r^   r*   )rO   r   r   rR   rS   rg   r/   	moment_mp   s   rh   c                    sD   | t vrt|  dt| tt fddt |  }t|dkS )znSearches the results dict for a result in the set that matches a case.
    Returns True if such a case exists.z not present in data structure!c                    s   | d  kS )Nr   r*   )r^   	case_dictr*   r/   <lambda>   s    zresult_exists.<locals>.<lambda>r   )r=   
ValueErrorrC   listfilterlen)set_keyr.   existing_resr*   ri   r/   result_exists   s   
rr   c                 C   sL   t  }|| }td|d  d| dt  |  d ||tt| |fS )zARuns the single passed case, returning an mp dictionary and indexz	Finished r   /z in batch. (Took zs))timeperf_counterprintrE   r   )r.   
run_lambdarp   indextotal_casest_startr^   r*   r*   r/   run   s   

r{   c                 C   s$   | \}}}t | || t  dS )zSA callback for completed jobs. Inserts and writes a calculated result
     to file.N)r=   insertr?   )r^   rx   rp   result_dictr*   r*   r/   write_result   s   

r~   c                    s~    fddt  D }tt t|  dt  d g }tt}|D ]}||jt|td q'|	  |
  dS )z)Runs an array of cases and writes to filec                    s,   g | ]\}}t |s||t fqS r*   )rr   ro   )r-   rx   r.   casesrw   rp   r*   r/   r0      s    zrun_cases.<locals>.<listcomp>rs   z? cases won't be calculated because their results already exist.)argscallbackN)	enumeraterv   ro   r   	num_poolsappendapply_asyncr{   r~   closejoin)r   rw   rp   job_argjobspoolr.   r*   r   r/   	run_cases   s   $r   c                 C      t | j| j| jS rd   )re   r   r   r   r.   r*   r*   r/   run_pdf      r   c                 C   r   rd   )r`   r   r   r   r   r*   r*   r/   run_cdf   r   r   c                 C   r   rd   )rh   r   r   r   r   r*   r*   r/   
run_moment   r   r   c                  C   s   t  } dtt tt }td| d tdtt d tttd tdtt d tttd	 td
tt d ttt	d tdt  |   d d S )Nr7   zProcessing z test caseszRunning 1st batch (z' PDF cases). These take about 30s each.r   zRunning 2nd batch (z' CDF cases). These take about 30s each.r   zRunning 3rd batch (zH moment cases). These take about anywhere from a few hours to days each.r    zTest data generated in rL   )
rt   ru   ro   cdf_pdf_casesmoment_casesrv   r   r   r   r   )rz   ry   r*   r*   r/   main   s   r   __main__)r   r   )=	itertoolscollectionsr   r;   rt   osmultiprocessingr   r   mpmathr   r   r   r   r	   r
   r   r   rI   r   rJ   r:   maxr   r   r   r   pathisfileloadr9   r=   r@   
setdefaultr+   r,   dpscp_qcp_kcp_nuproductr   r2   r3   ranger   r?   rC   rE   r`   re   rh   rr   r{   r~   r   r   r   r   r   __name__r*   r*   r*   r/   <module>   sl   $


