a
    �ù»jà%  ã                
   @   s0  d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZ 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mZmZ edddd�Zedddd�ZdZe e¡Z dej!fdd„Z"dd„ Z#dd„ Z$ee%edgdgeed
ddd�geeddd d�gd!œd"d#�dd"d$d%d!œd&d'„ƒZ&dS )(aõ  
=============================
Species distribution dataset
=============================

This dataset represents the geographic distribution of species.
The dataset is provided by Phillips et. al. (2006).

The two species are:

 - `"Bradypus variegatus"
   <http://www.iucnredlist.org/details/3038/0>`_ ,
   the Brown-throated Sloth.

 - `"Microryzomys minutus"
   <http://www.iucnredlist.org/details/13408/0>`_ ,
   also known as the Forest Small Rice Rat, a rodent that lives in Peru,
   Colombia, Ecuador, Peru, and Venezuela.

References
----------

`"Maximum entropy modeling of species geographic distributions"
<http://rob.schapire.net/papers/ecolmod.pdf>`_ S. J. Phillips,
R. P. Anderson, R. E. Schapire - Ecological Modelling, 190:231-259, 2006.
é    N)ÚBytesIO)ÚIntegralÚReal)ÚPathLikeÚmakedirsÚremove)Úexistsé   )ÚBunch)ÚIntervalÚvalidate_paramsé   )Úget_data_home)ÚRemoteFileMetadataÚ_fetch_remoteÚ_pkl_filepathzsamples.zipz.https://ndownloader.figshare.com/files/5976075Z@abb07ad284ac50d9e6d20f1c4211e0fd3c098f7f85955e89d321ee8efe37ac28)ÚfilenameÚurlZchecksumzcoverages.zipz.https://ndownloader.figshare.com/files/5976078Z@4d862674d72e79d6cee77e63b98651ec7926043ba7d39dcb31329cf3f6073807zspecies_coverage.pkzé   c                    sb   ‡ fdd„t |ƒD ƒ}dd„ ‰t‡fdd„|D ƒƒ}tjˆ |d�}t|d ƒ}|dkr^d||< |S )	zjLoad a coverage file from an open file object.

    This will return a numpy array of the given dtype
    c                    s   g | ]}ˆ   ¡ ‘qS © )Úreadline)Ú.0Ú_)ÚFr   úME:\soccer\ml_env\Lib\site-packages\sklearn/datasets/_species_distributions.pyÚ
<listcomp>H   ó    z"_load_coverage.<locals>.<listcomp>c                 S   s   |   ¡ d t|   ¡ d ƒfS )Nr   r   )ÚsplitÚfloat)Útr   r   r   Ú<lambda>I   r   z _load_coverage.<locals>.<lambda>c                    s   g | ]}ˆ |ƒ‘qS r   r   )r   Úline)Ú
make_tupler   r   r   J   r   ©Údtypes   NODATA_valueiñØÿÿ)ÚrangeÚdictÚnpÚloadtxtÚint)r   Úheader_lengthr$   ÚheaderÚMZnodatar   )r   r"   r   Ú_load_coverageC   s    r-   c                 C   s6   |   ¡  d¡ ¡  d¡}tj| dddd�}||j_|S )zÃLoad csv file.

    Parameters
    ----------
    F : file object
        CSV file open in byte mode.

    Returns
    -------
    rec : np.ndarray
        record array representing the data
    Úasciiú,r   z	S22,f4,f4)ZskiprowsÚ	delimiterr$   )r   ÚdecodeÚstripr   r'   r(   r$   Únames)r   r3   Zrecr   r   r   Ú	_load_csvS   s    r4   c                 C   s`   | j | j }|| j| j  }| j| j }|| j| j  }t ||| j¡}t ||| j¡}||fS )a%  Construct the map grid from the batch object

    Parameters
    ----------
    batch : Batch object
        The object returned by :func:`fetch_species_distributions`

    Returns
    -------
    (xgrid, ygrid) : 1-D arrays
        The grid corresponding to the values in batch.coverages
    )Úx_left_lower_cornerÚ	grid_sizeÚNxÚy_left_lower_cornerÚNyr'   Zarange)ÚbatchZxminZxmaxZyminZymaxZxgridZygridr   r   r   Úconstruct_gridsg   s    r;   ÚbooleanÚleft)Úclosedg        Zneither)Ú	data_homeÚdownload_if_missingÚ	n_retriesÚdelayT)Zprefer_skip_nested_validationé   g      ð?c                 C   s¶  t | ƒ} t| ƒst| ƒ tdddddd�}tj}t| tƒ}t|ƒ�s¨|sPtdƒ‚t	 
dtj| f ¡ tt| ||d	�}t |¡�H}|jD ]0}	t||	 ƒ}
d
|	v r¦t|
ƒ}d|	v r†t|
ƒ}q†W d  ƒ n1 sÌ0    Y  t|ƒ t	 
dtj| f ¡ tt| ||d	�}t |¡�Z}g }|jD ]0}	t||	 ƒ}
t	 d |	¡¡ | t|
ƒ¡ �qtj||d�}W d  ƒ n1 �sn0    Y  t|ƒ tf |||dœ|¤Ž}tj||dd� n
t |¡}|S )a  Loader for species distribution dataset from Phillips et. al. (2006).

    Read more in the :ref:`User Guide <species_distribution_dataset>`.

    Parameters
    ----------
    data_home : str or path-like, default=None
        Specify another download and cache folder for the datasets. By default
        all scikit-learn data is stored in '~/scikit_learn_data' subfolders.

    download_if_missing : bool, default=True
        If False, raise an OSError if the data is not locally available
        instead of trying to download the data from the source site.

    n_retries : int, default=3
        Number of retries when HTTP errors are encountered.

        .. versionadded:: 1.5

    delay : float, default=1.0
        Number of seconds between retries.

        .. versionadded:: 1.5

    Returns
    -------
    data : :class:`~sklearn.utils.Bunch`
        Dictionary-like object, with the following attributes.

        coverages : array, shape = [14, 1592, 1212]
            These represent the 14 features measured
            at each point of the map grid.
            The latitude/longitude values for the grid are discussed below.
            Missing data is represented by the value -9999.
        train : record array, shape = (1624,)
            The training points for the data.  Each point has three fields:

            - train['species'] is the species name
            - train['dd long'] is the longitude, in degrees
            - train['dd lat'] is the latitude, in degrees
        test : record array, shape = (620,)
            The test points for the data.  Same format as the training data.
        Nx, Ny : integers
            The number of longitudes (x) and latitudes (y) in the grid
        x_left_lower_corner, y_left_lower_corner : floats
            The (x,y) position of the lower-left corner, in degrees
        grid_size : float
            The spacing between points of the grid, in degrees

    Notes
    -----

    This dataset represents the geographic distribution of species.
    The dataset is provided by Phillips et. al. (2006).

    The two species are:

    - `"Bradypus variegatus"
      <http://www.iucnredlist.org/details/3038/0>`_ ,
      the Brown-throated Sloth.

    - `"Microryzomys minutus"
      <http://www.iucnredlist.org/details/13408/0>`_ ,
      also known as the Forest Small Rice Rat, a rodent that lives in Peru,
      Colombia, Ecuador, Peru, and Venezuela.

    References
    ----------

    * `"Maximum entropy modeling of species geographic distributions"
      <http://rob.schapire.net/papers/ecolmod.pdf>`_
      S. J. Phillips, R. P. Anderson, R. E. Schapire - Ecological Modelling,
      190:231-259, 2006.

    Examples
    --------
    >>> from sklearn.datasets import fetch_species_distributions
    >>> species = fetch_species_distributions()
    >>> species.train[:5]
    array([(b'microryzomys_minutus', -64.7   , -17.85  ),
           (b'microryzomys_minutus', -67.8333, -16.3333),
           (b'microryzomys_minutus', -67.8833, -16.3   ),
           (b'microryzomys_minutus', -67.8   , -16.2667),
           (b'microryzomys_minutus', -67.9833, -15.9   )],
          dtype=[('species', 'S22'), ('dd long', '<f4'), ('dd lat', '<f4')])

    For a more extended example,
    see :ref:`sphx_glr_auto_examples_applications_plot_species_distribution_modeling.py`
    g33333³WÀi¼  gfffffLÀi8  gš™™™™™©?)r5   r7   r8   r9   r6   z1Data not found and `download_if_missing` is Falsez&Downloading species data from %s to %s)ÚdirnamerA   rB   ÚtrainÚtestNz'Downloading coverage data from %s to %sz - converting {}r#   )Ú	coveragesrF   rE   é	   )Úcompress)r   r   r   r&   r'   Úint16r   ÚDATA_ARCHIVE_NAMEÚOSErrorÚloggerÚinfoÚSAMPLESr   r   ÚloadÚfilesr   r4   r   Ú	COVERAGESÚdebugÚformatÚappendr-   Zasarrayr
   ÚjoblibÚdump)r?   r@   rA   rB   Zextra_paramsr$   Úarchive_pathZsamples_pathÚXÚfZfhandlerE   rF   Zcoverages_pathrG   Zbunchr   r   r   Úfetch_species_distributions‚   sZ    iû

ÿ
(ÿÿ
.
r[   )'Ú__doc__ÚloggingÚior   Únumbersr   r   Úosr   r   r   Úos.pathr   rV   Únumpyr'   Úutilsr
   Zutils._param_validationr   r   Ú r   Z_baser   r   r   rO   rR   rK   Ú	getLoggerÚ__name__rM   rJ   r-   r4   r;   Ústrr[   r   r   r   r   Ú<module>   sN   ýý
üùû