Calculates an empirical species area or endemics area relationship

    Parameters
    ----------
    {0}
    divs : str
        Description of how to divide x_col and y_col. See notes.
    ear : bool
        If True, calculates an endemics area relationship

    Returns
    -------
    {1} Result has 5 columns; div, x, and y; that give the ID for the
    division given as an argument, fractional area, and the mean species
    richness at that division.

    Notes
    -----
    {2}

    For the SAR and EAR, cols must also contain x_col and y_col, giving the x
    and y dimensions along which to grid the patch.

    {3}

    {4}

    Examples
    --------

    {5}

    >>> # Get the SAR at the full area (1,1), 1 x 2 division,
    >>> # 2 x 1 division, 2 x 2 division, 2 x 4 division, 4 x 2 division, and
    >>> # 4 x 4 division
    >>> sar = meco.empirical.sar(pat,
                cols='spp_col:spp; count_col:count; x_col:row; y_col:column',
                splits="",
                divs="1,1; 1,2; 2,1; 2,2; 2,4; 4,2; 4,4")

    >>> sar[0][1]
       div  n_individs    n_spp   x        y
    0  1,1   2445.0000  24.0000  16  24.0000
    1  1,2   1222.5000  18.5000   8  18.5000
    2  2,1   1222.5000  17.0000   8  17.0000
    3  2,2    611.2500  13.5000   4  13.5000
    4  2,4    305.6250  10.1250   2  10.1250
    5  4,2    305.6250  10.5000   2  10.5000
    6  4,4    152.8125   7.5625   1   7.5625

    The column div gives the divisions specified in the function call. The
    column n_individs specifies the average number of individuals across the
    cells made from the given division. n_spp gives the average species across
    the cells made from the given division. x gives the absolute area of a
    cell for the given division. y gives the same information as n_spp and is
    included for easy plotting.

    See http://www.macroeco.org/tutorial_macroeco.html for additional
    examples and explanation

def sar(patch, cols, splits, divs, ear=False):
    """
    Calculates an empirical species area or endemics area relationship

    Parameters
    ----------
    {0}
    divs : str
        Description of how to divide x_col and y_col. See notes.
    ear : bool
        If True, calculates an endemics area relationship

    Returns
    -------
    {1} Result has 5 columns; div, x, and y; that give the ID for the
    division given as an argument, fractional area, and the mean species
    richness at that division.

    Notes
    -----
    {2}

    For the SAR and EAR, cols must also contain x_col and y_col, giving the x
    and y dimensions along which to grid the patch.

    {3}

    {4}

    Examples
    --------

    {5}

    >>> # Get the SAR at the full area (1,1), 1 x 2 division,
    >>> # 2 x 1 division, 2 x 2 division, 2 x 4 division, 4 x 2 division, and
    >>> # 4 x 4 division
    >>> sar = meco.empirical.sar(pat,
                cols='spp_col:spp; count_col:count; x_col:row; y_col:column',
                splits="",
                divs="1,1; 1,2; 2,1; 2,2; 2,4; 4,2; 4,4")

    >>> sar[0][1]
       div  n_individs    n_spp   x        y
    0  1,1   2445.0000  24.0000  16  24.0000
    1  1,2   1222.5000  18.5000   8  18.5000
    2  2,1   1222.5000  17.0000   8  17.0000
    3  2,2    611.2500  13.5000   4  13.5000
    4  2,4    305.6250  10.1250   2  10.1250
    5  4,2    305.6250  10.5000   2  10.5000
    6  4,4    152.8125   7.5625   1   7.5625

    The column div gives the divisions specified in the function call. The
    column n_individs specifies the average number of individuals across the
    cells made from the given division. n_spp gives the average species across
    the cells made from the given division. x gives the absolute area of a
    cell for the given division. y gives the same information as n_spp and is
    included for easy plotting.

    See http://www.macroeco.org/tutorial_macroeco.html for additional
    examples and explanation

    """

    def sar_y_func(spatial_table, all_spp):
        return np.mean(spatial_table['n_spp'])

    def ear_y_func(spatial_table, all_spp):
        endemic_counter = 0
        for spp in all_spp:
            spp_in_cell = [spp in x for x in spatial_table['spp_set']]
            spp_n_cells = np.sum(spp_in_cell)
            if spp_n_cells == 1:  # If a spp is in only 1 cell, endemic
                endemic_counter += 1
        n_cells = len(spatial_table)
        return endemic_counter / n_cells # mean endemics / cell

    if ear:
        y_func = ear_y_func
    else:
        y_func = sar_y_func

    return _sar_ear_inner(patch, cols, splits, divs, y_func)