§
    ¨éUg0&  ã                   óš  — 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¬¦  «        Z eddd¬¦  «        ZdZ ej        e¦  «        Z dej!        fd„Z"d„ Z#d„ Z$ ee%edgdg eed
dd¬¦  «        g eeddd¬¦  «        g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.

Notes
-----

For an example of using this dataset, see
:ref:`examples/applications/plot_species_distribution_modeling.py
<sphx_glr_auto_examples_applications_plot_species_distribution_modeling.py>`.
é    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/5976075Ú@abb07ad284ac50d9e6d20f1c4211e0fd3c098f7f85955e89d321ee8efe37ac28)ÚfilenameÚurlÚchecksumzcoverages.zipz.https://ndownloader.figshare.com/files/5976078Ú@4d862674d72e79d6cee77e63b98651ec7926043ba7d39dcb31329cf3f6073807zspecies_coverage.pkzé   c                 óè   ‡ ‡— ˆ fd„t          |¦  «        D ¦   «         }d„ Št          ˆfd„|D ¦   «         ¦  «        }t          j        ‰ |¬¦  «        }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                 ó8   •— g | ]}‰                      ¦   «         ‘ŒS © )Úreadline)Ú.0Ú_ÚFs     €úf/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/datasets/_species_distributions.pyú
<listcomp>z"_load_coverage.<locals>.<listcomp>Q   s!   ø€ Ð9Ð9Ð9˜qˆa�jŠj‰lŒlÐ9Ð9Ð9ó    c                 ó„   — |                       ¦   «         d         t          |                       ¦   «         d         ¦  «        fS )Nr   r   )ÚsplitÚfloat)Úts    r    ú<lambda>z _load_coverage.<locals>.<lambda>R   s+   € ˜AŸGšG™IœI aœL­%°·²±	´	¸!´Ñ*=Ô*=Ð>€ r"   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   )r   ÚlineÚ
make_tuples     €r    r!   z"_load_coverage.<locals>.<listcomp>S   s#   ø€ Ð7Ð7Ð7¨�:�:˜dÑ#Ô#Ð7Ð7Ð7r"   ©Údtypes   NODATA_valueiñØÿÿ)ÚrangeÚdictÚnpÚloadtxtÚint)r   Úheader_lengthr,   ÚheaderÚMÚnodatar*   s   `     @r    Ú_load_coverager6   L   sŽ   øø€ ð
 :Ð9Ð9Ð9¥E¨-Ñ$8Ô$8Ð9Ñ9Ô9€FØ>Ð>€JÝÐ7Ð7Ð7Ð7°Ð7Ñ7Ô7Ñ8Ô8€Få
Œ
�1˜EÐ"Ñ"Ô"€AÝ�˜Ô(Ñ)Ô)€FØ�‚€Øˆˆ&‰	Ø€Hr"   c                 óæ   — |                       ¦   «                              d¦  «                             ¦   «                              d¦  «        }t	          j        | 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)ÚskiprowsÚ	delimiterr,   )r   ÚdecodeÚstripr$   r/   r0   r,   Únames)r   r>   Úrecs      r    Ú	_load_csvr@   \   s_   € ð �JŠJ‰LŒL×Ò Ñ(Ô(×.Ò.Ñ0Ô0×6Ò6°sÑ;Ô;€Eå
Œ*�Q ¨c¸Ð
EÑ
EÔ
E€CØ€C„I„OØ€Jr"   c                 óú   — | j         | j        z   }|| j        | j        z  z   }| j        | j        z   }|| j        | j        z  z   }t          j        ||| j        ¦  «        }t          j        ||| 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/   Úarange)ÚbatchÚxminÚxmaxÚyminÚymaxÚxgridÚygrids          r    Úconstruct_gridsrO   p   s~   € ð Ô$ u¤Ñ6€DØ�5”8˜eœoÑ-Ñ.€DØÔ$ u¤Ñ6€DØ�5”8˜eœoÑ-Ñ.€Dõ ŒI�d˜D %¤/Ñ2Ô2€EåŒI�d˜D %¤/Ñ2Ô2€Eà�5ˆ>Ðr"   ÚbooleanÚleft)Úclosedg        Úneither)Ú	data_homeÚdownload_if_missingÚ	n_retriesÚdelayT)Úprefer_skip_nested_validationé   g      ð?c                 ó  — t          | ¦  «        } t          | ¦  «        st          | ¦  «         t          ddddd¬¦  «        }t          j        }t          | t          ¦  «        }t          |¦  «        �sÿ|st          d¦  «        ‚t           
                    dt          j        ›d	| ›�¦  «         t          t          | ||¬
¦  «        }t	          j        |¦  «        5 }|j        D ]=}	t!          ||	         ¦  «        }
d|	v rt#          |
¦  «        }d|	v rt#          |
¦  «        }Œ>	 ddd¦  «         n# 1 swxY w Y   t%          |¦  «         t           
                    dt&          j        ›d	| ›�¦  «         t          t&          | ||¬
¦  «        }t	          j        |¦  «        5 }g }|j        D ]f}	t!          ||	         ¦  «        }
t                               d                     |	¦  «        ¦  «         |                     t/          |
¦  «        ¦  «         Œgt	          j        ||¬¦  «        }ddd¦  «         n# 1 swxY w Y   t%          |¦  «         t3          d|||dœ|¤Ž}t5          j        ||d¬¦  «         nt5          j        |¦  «        }|S )au  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.

    - For an example of using this dataset with scikit-learn, see
      :ref:`examples/applications/plot_species_distribution_modeling.py
      <sphx_glr_auto_examples_applications_plot_species_distribution_modeling.py>`.

    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')])
    g33333³WÀi¼  gfffffLÀi8  gš™™™™™©?)rB   rD   rE   rF   rC   z1Data not found and `download_if_missing` is FalsezDownloading species data from z to )ÚdirnamerV   rW   ÚtrainÚtestNzDownloading coverage data from z - converting {}r+   )Ú	coveragesr]   r\   é	   )Úcompressr   )r   r	   r   r.   r/   Úint16r   ÚDATA_ARCHIVE_NAMEÚOSErrorÚloggerÚinfoÚSAMPLESr   r   ÚloadÚfilesr   r@   r   Ú	COVERAGESÚdebugÚformatÚappendr6   Úasarrayr   ÚjoblibÚdump)rT   rU   rV   rW   Úextra_paramsr,   Úarchive_pathÚsamples_pathÚXÚfÚfhandler\   r]   Úcoverages_pathr^   Úbunchs                   r    Úfetch_species_distributionsrx   ‹   s  € õT ˜iÑ(Ô(€IÝ�)ÑÔð Ý�ÑÔÐõ
 Ø!ØØ"ØØðñ ô €Lõ ŒH€Eå  Õ,=Ñ>Ô>€Lå�,ÑÔñ "*Ø"ð 	OÝÐMÑNÔNÐNÝ�ŠˆÅÄÀÀÈYÈYÐWÑXÔXÐXÝ$Ý˜Y°)À5ð
ñ 
ô 
ˆõ ŒW�\Ñ"Ô"ð 	. aØ”Wð .ð .�Ý! ! A¤$™-œ-�Ø˜a�<�<Ý% gÑ.Ô.�EØ˜Q�;�;Ý$ WÑ-Ô-�Døð.ð	.ð 	.ð 	.ñ 	.ô 	.ð 	.ð 	.ð 	.ð 	.ð 	.ð 	.øøøð 	.ð 	.ð 	.ð 	.õ 	ˆ|ÑÔÐå�ŠˆÝ9B¼¸¸È	È	ÐRñ	
ô 	
ð 	
õ 'Ý˜y°IÀUð
ñ 
ô 
ˆõ ŒW�^Ñ$Ô$ð 	;¨ØˆIØ”Wð :ð :�Ý! ! A¤$™-œ-�Ý—’Ð/×6Ò6°qÑ9Ô9Ñ:Ô:Ð:Ø× Ò ¥°Ñ!8Ô!8Ñ9Ô9Ð9Ð9Ýœ
 9°EÐ:Ñ:Ô:ˆIð	;ð 	;ð 	;ñ 	;ô 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;øøøð 	;ð 	;ð 	;ð 	;õ 	ˆ~ÑÔÐåÐR 	°¸EÐRÐRÀ\ÐRÐRˆÝŒ�E˜<°!Ð4Ñ4Ô4Ð4Ð4å”˜LÑ)Ô)ˆà€Ls&   ÃAD-Ä-D1Ä4D1ÆBH0È0H4È7H4)'Ú__doc__ÚloggingÚior   Únumbersr   r   Úosr   r   r   Úos.pathr	   rn   Únumpyr/   Úutilsr   Úutils._param_validationr   r   Ú r   Ú_baser   r   r   rf   ri   rb   Ú	getLoggerÚ__name__rd   ra   r6   r@   rO   Ústrrx   r   r"   r    ú<module>r‡      s'  ðð ð  ðN €€€Ø Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð à Ð Ð Ð Ð Ð Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ð Ð Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cð Ð
ØØ8ØOðñ ô €ð ÐØØ8ØOðñ ô €	ð +Ð ð 
ˆÔ	˜8Ñ	$Ô	$€ð %&¨R¬Xð ð ð ð ð ð ð ð(ð ð ð6 €à˜8 TÐ*Ø )˜{Ø�h˜x¨¨D¸Ð@Ñ@Ô@ÐAØ�(˜4  d°9Ð=Ñ=Ô=Ð>ð	ð ð #'ðñ ô ð ØØØ
ðWð Wð Wð Wñô ðWð Wð Wr"   