B
    d1                 @   s,   d Z ddlZddlZdgZG dd dZdS )zF
This module implements functions and classes for spatial statistics.
    NRipleysKEstimatorc               @   s   e Zd ZdZd!ddZedd Zejdd Zedd	 Zejd
d	 Zedd Z	e	jdd Z	edd Z
e
jdd Z
edd Zejdd Zd"ddZdd Zdd Zd#ddZd$ddZd%dd ZdS )&r   a  
    Estimators for Ripley's K function for two-dimensional spatial data.
    See [1]_, [2]_, [3]_, [4]_, [5]_ for detailed mathematical and
    practical aspects of those estimators.

    Parameters
    ----------
    area : float
        Area of study from which the points where observed.
    x_max, y_max : float, float, optional
        Maximum rectangular coordinates of the area of study.
        Required if ``mode == 'translation'`` or ``mode == ohser``.
    x_min, y_min : float, float, optional
        Minimum rectangular coordinates of the area of study.
        Required if ``mode == 'variable-width'`` or ``mode == ohser``.

    Examples
    --------
    >>> import numpy as np
    >>> from matplotlib import pyplot as plt # doctest: +SKIP
    >>> from astropy.stats import RipleysKEstimator
    >>> z = np.random.uniform(low=5, high=10, size=(100, 2))
    >>> Kest = RipleysKEstimator(area=25, x_max=10, y_max=10,
    ... x_min=5, y_min=5)
    >>> r = np.linspace(0, 2.5, 100)
    >>> plt.plot(r, Kest.poisson(r)) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='none')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='translation')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='ohser')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='var-width')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='ripley')) # doctest: +SKIP

    References
    ----------
    .. [1] Peebles, P.J.E. *The large scale structure of the universe*.
       <https://ui.adsabs.harvard.edu/abs/1980lssu.book.....P>
    .. [2] Spatial descriptive statistics.
       <https://en.wikipedia.org/wiki/Spatial_descriptive_statistics>
    .. [3] Package spatstat.
       <https://cran.r-project.org/web/packages/spatstat/spatstat.pdf>
    .. [4] Cressie, N.A.C. (1991). Statistics for Spatial Data,
       Wiley, New York.
    .. [5] Stoyan, D., Stoyan, H. (1992). Fractals, Random Shapes and
       Point Fields, Akademie Verlag GmbH, Chichester.
    Nc             C   s"   || _ || _|| _|| _|| _d S )N)areax_maxy_maxx_miny_min)selfr   r   r   r   r    r	   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/stats/spatial.py__init__=   s
    zRipleysKEstimator.__init__c             C   s   | j S )N)_area)r   r	   r	   r
   r   D   s    zRipleysKEstimator.areac             C   s2   t |ttfr|dkr|| _ntd| dd S )Nr   z.area is expected to be a positive number. Got .)
isinstancefloatintr   
ValueError)r   valuer	   r	   r
   r   H   s    c             C   s   | j S )N)_y_max)r   r	   r	   r
   r   O   s    zRipleysKEstimator.y_maxc             C   s0   |d kst |ttfr|| _ntd|d S )Nz6y_max is expected to be a real number or None. Got {}.)r   r   r   r   r   format)r   r   r	   r	   r
   r   S   s    c             C   s   | j S )N)_x_max)r   r	   r	   r
   r   [   s    zRipleysKEstimator.x_maxc             C   s0   |d kst |ttfr|| _ntd|d S )Nz6x_max is expected to be a real number or None. Got {}.)r   r   r   r   r   r   )r   r   r	   r	   r
   r   _   s    c             C   s   | j S )N)_y_min)r   r	   r	   r
   r   g   s    zRipleysKEstimator.y_minc             C   s2   |d kst |ttfr|| _ntd| dd S )Nz+y_min is expected to be a real number. Got r   )r   r   r   r   r   )r   r   r	   r	   r
   r   k   s    c             C   s   | j S )N)_x_min)r   r	   r	   r
   r   r   s    zRipleysKEstimator.x_minc             C   s2   |d kst |ttfr|| _ntd| dd S )Nz+x_min is expected to be a real number. Got r   )r   r   r   r   r   )r   r   r	   r	   r
   r   v   s    nonec             C   s   | j |||dS )N)dataradiimode)evaluate)r   r   r   r   r	   r	   r
   __call__}   s    zRipleysKEstimator.__call__c             C   s   t |}tj||d  d dftjd}d}xPt|d D ]@}|| d }t|| ||d d   |||| < ||7 }q:W |S )N      )shapedtyper   )lennpzerosdoublerangeabs)r   r   nptsdiffkisizer	   r	   r
   _pairwise_diffs   s     (z!RipleysKEstimator._pairwise_diffsc             C   s   t j| | S )a  
        Evaluates the Ripley K function for the homogeneous Poisson process,
        also known as Complete State of Randomness (CSR).

        Parameters
        ----------
        radii : 1D array
            Set of distances in which Ripley's K function will be evaluated.

        Returns
        -------
        output : 1D array
            Ripley's K function evaluated at ``radii``.
        )r#   pi)r   r   r	   r	   r
   poisson   s    zRipleysKEstimator.poissonc             C   s   t | j|||dt j S )zs
        Evaluates the L function at ``radii``. For parameter description
        see ``evaluate`` method.
        )r   )r#   sqrtr   r.   )r   r   r   r   r	   r	   r
   	Lfunction   s    zRipleysKEstimator.Lfunctionc             C   s   | j |||d| S )zs
        Evaluates the H function at ``radii``. For parameter description
        see ``evaluate`` method.
        )r   )r1   )r   r   r   r   r	   r	   r
   	Hfunction   s    zRipleysKEstimator.Hfunctionc       $   	   C   s8  t |}|jd dks tdt|}t t|}|dkr| |}t |dddf |dddf }x(tt|D ]}||| k 	 ||< qzW | j
d | ||d   }n|dkr~| |}t |dddf |dddf }| j| j |dddf  | j| j |dddf   }	x6tt|D ]&}||| k }
d|	 |
 	 ||< q2W | j
d ||d   d | }n|d	kr| |}t |dddf |dddf }| j
}t| j| j | j| j  | j| j | j| j  }|t||  }t || d |dk }t || |d  |t|d d k  ||k }t jd| dd|    || |  }dt d| |dk  d|  d||   }dt |||  ||  ||k |t|d d k   d|  d| |  | d||  |  }|t j ||dk |dk ||dk ||k  |||k  |t|d d k    }x6tt|D ]&}||| k }
d| |
 	 ||< qhW | j
d ||d   d | }n|d
krt t | j|dddf  | j|dddf  t |dddf | j |dddf | j }xtt|D ]}xt|D ]~}xvt|D ]j}||krLt|| ||  }t|| 	 }|||   k r|| k rLn n|| d ||< qLW q>W ||| k	 }|dks0|| | ||< q0W | j
| | }n2|dkr$t j||d  d t jd}t j||d  d t jd}xt|d D ]}t| j|| d  || d | j }t| j|| d  || d | j }|d|d  |d   d }|d d|d  |  d }|t |d |  |||< |t |d |  |||< qRW | |}t |dddf |dddf }|t ||k} dt t ||| t t |||  t j  }!ddt || |   t || |     t j  }"| |! |  |"  }#x.tt|D ]}||| k |# 	 ||< qW | j
d | ||d   }ntd| d|S )a  
        Evaluates the Ripley K estimator for a given set of values ``radii``.

        Parameters
        ----------
        data : 2D array
            Set of observed points in as a n by 2 array which will be used to
            estimate Ripley's K function.
        radii : 1D array
            Set of distances in which Ripley's K estimator will be evaluated.
            Usually, it's common to consider max(radii) < (area/2)**0.5.
        mode : str
            Keyword which indicates the method for edge effects correction.
            Available methods are 'none', 'translation', 'ohser', 'var-width',
            and 'ripley'.

            * 'none'
                this method does not take into account any edge effects
                whatsoever.
            * 'translation'
                computes the intersection of rectangular areas centered at
                the given points provided the upper bounds of the
                dimensions of the rectangular area of study. It assumes that
                all the points lie in a bounded rectangular region satisfying
                x_min < x_i < x_max; y_min < y_i < y_max. A detailed
                description of this method can be found on ref [4].
            * 'ohser'
                this method uses the isotropized set covariance function of
                the window of study as a weight to correct for
                edge-effects. A detailed description of this method can be
                found on ref [4].
            * 'var-width'
                this method considers the distance of each observed point to
                the nearest boundary of the study window as a factor to
                account for edge-effects. See [3] for a brief description of
                this method.
            * 'ripley'
                this method is known as Ripley's edge-corrected estimator.
                The weight for edge-correction is a function of the
                proportions of circumferences centered at each data point
                which crosses another data point of interest. See [3] for
                a detailed description of this method.

        Returns
        -------
        ripley : 1D array
            Ripley's K function estimator evaluated at ``radii``.
        r   r   zGdata must be an n by 2 array, where n is the number of observed points.r   Nr   g       @translationZohserz	var-widthripley)r    r!   g      ?g      ?zmode z is not implemented.)r#   Zasarrayr    r   r"   r$   r-   hypotr&   sumr   r   r   r   r   maxmathr0   r.   Zarcsinminimumr'   r%   minZonesZarccos)$r   r   r   r   r(   r4   r)   Z	distancesrZintersec_areaZdist_indicatorabxuvc1c2c3Zcov_funcZlt_distr+   jdistZlt_dist_sumZhor_distZver_distr*   Zmin_hor_distZmin_ver_diststartendZdist_indZw1Zw2weightr	   r	   r
   r      s    2

$

$"

$4&.dL"
22
$



$
$ 4zRipleysKEstimator.evaluate)NNNN)r   )r   )r   )r   )__name__
__module____qualname____doc__r   propertyr   setterr   r   r   r   r   r-   r/   r1   r2   r   r	   r	   r	   r
   r      s$   -



)rL   numpyr#   r8   __all__r   r	   r	   r	   r
   <module>   s   