U
    !`9                     @   s  d dl Z d dl mZ d dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dl	mZ d d	l	mZ d d
l	mZ d dl	mZ d dl	mZ e
eeeeeedZdd Zee
ZeeZeeZeeZeeZeeZeeZdd Zejdddd Zdd ZdS )    N)environ)wraps)threadpool_limits)_openmp_effective_n_threads)fetch_20newsgroups)fetch_20newsgroups_vectorized)fetch_california_housing)fetch_covtype)fetch_kddcup99)fetch_olivetti_faces)
fetch_rcv1)fetch_20newsgroups_fxt!fetch_20newsgroups_vectorized_fxtfetch_california_housing_fxtfetch_covtype_fxtfetch_kddcup99_fxtfetch_olivetti_faces_fxtfetch_rcv1_fxtc                    s8   t dddk t fddtfddS )zAFetch dataset (download if missing and requested by environment).SKLEARN_SKIP_NETWORK_TESTS10c                     s8    |d< z| |W S  t k
r2   td Y nX d S )Ndownload_if_missing1test is enabled when SKLEARN_SKIP_NETWORK_TESTS=0)IOErrorpytestskip)argskwargs)r   f /lib/python3.8/site-packages/sklearn/conftest.pywrapped!   s
    z_fetch_fixture.<locals>.wrappedc                      s    S )Nr   r   )r!   r   r    <lambda>(       z _fetch_fixture.<locals>.<lambda>)r   getr   r   fixture)r   r   )r   r   r!   r    _fetch_fixture   s    r&   c                 C   s   t dddk}tjjdd}tt}t }|D ]@}t|ds@q0t|j}||@ }|sXq0|rf||O }q0|	| q0t dd}	|	dkr|r|D ]}
t|
   qd	S )
zCalled after collect is completed.

    Parameters
    ----------
    config : pytest config
    items : list of collected items
    r   r   r   r   )reasonfixturenamesZPYTEST_XDIST_WORKERZgw0N)
r   r$   r   Zmarkr   setdataset_fetchershasattrr(   Z
add_marker)ZconfigitemsZrun_network_testsZskip_networkZdataset_features_setZdatasets_to_downloaditemZitem_fixturesZdataset_to_fetchZ	worker_idnamer   r   r    pytest_collection_modifyitems6   s(    


r/   Zfunction)Zscopec                  c   s2   t d} | d t d}|V  |d dS )aA  Setup and teardown fixture for matplotlib.

    This fixture checks if we can import matplotlib. If not, the tests will be
    skipped. Otherwise, we setup matplotlib backend and close the figures
    after running the functions.

    Returns
    -------
    pyplot : module
        The ``matplotlib.pyplot`` module.
    
matplotlibZaggzmatplotlib.pyplotallN)r   ZimportorskipZuseclose)r0   pyplotr   r   r    r3   ^   s
    


r3   c                 C   sN   zt tjd }W n tk
r(   Y dS X t }t|| d}t|dd dS )zSet the number of openmp threads based on the number of workers
    xdist is using to prevent oversubscription.

    Parameters
    ----------
    item : pytest item
        item to be processed
    ZPYTEST_XDIST_WORKER_COUNTN   Zopenmp)Zuser_api)intosr   KeyErrorr   maxr   )r-   Zxdist_worker_countZopenmp_threadsZthreads_per_workerr   r   r    pytest_runtest_setupr   s    	r9   )r6   r   	functoolsr   r   Zthreadpoolctlr   Zsklearn.utils._openmp_helpersr   Zsklearn.datasetsr   r   r   r	   r
   r   r   r*   r&   r   r   r   r   r   r   r   r/   r%   r3   r9   r   r   r   r    <module>   sB   (

