(data, bins=None, categorical=False, categories=None)
return sum(p(n, N) ** 2 for n in counts.values() if n != 0)
Calculates the Simpson\'s diversity index of data. Helper function for
    :py:class:`momepy.Simpson`.

    .. math::

        \\lambda=\\sum_{i=1}^{R} p_{i}^{2}

    Formula adapted from https://gist.github.com/martinjc/f227b447791df8c90568.

    Parameters
    ----------
    data : GeoDataFrame
        GeoDataFrame containing morphological tessellation
    bins : array, optional
        array of top edges of classification bins. Result of binnng.bins.
    categorical : bool (default False)
        treat values as categories (will not use ``bins``)
    categories : list-like (default None)
        list of categories

    Returns
    -------
    float
        Simpson's diversity index

    See also
    --------
    momepy.Simpson : Calculates the Simpson\'s diversity index
Calculates the Simpson\'s diversity index of data. Helper function for
    :py:class:`momepy.Simpson`.
Calculates the Simpson \ s diversity index of data . Helper function for : py : class : momepy . Simpson .
def simpson_diversity(data, bins=None, categorical=False, categories=None):
    """
    Calculates the Simpson\'s diversity index of data. Helper function for
    :py:class:`momepy.Simpson`.

    .. math::

        \\lambda=\\sum_{i=1}^{R} p_{i}^{2}

    Formula adapted from https://gist.github.com/martinjc/f227b447791df8c90568.

    Parameters
    ----------
    data : GeoDataFrame
        GeoDataFrame containing morphological tessellation
    bins : array, optional
        array of top edges of classification bins. Result of binnng.bins.
    categorical : bool (default False)
        treat values as categories (will not use ``bins``)
    categories : list-like (default None)
        list of categories

    Returns
    -------
    float
        Simpson's diversity index

    See also
    --------
    momepy.Simpson : Calculates the Simpson\'s diversity index
    """
    if not categorical:
        try:
            import mapclassify as mc
        except ImportError:
            raise ImportError("The 'mapclassify' package is required")

    def p(n, N):
        """Relative abundance"""
        if n == 0:
            return 0
        return float(n) / N

    if categorical:
        counts = data.value_counts().to_dict()
        for c in categories:
            if c not in counts.keys():
                counts[c] = 0
    else:
        sample_bins = mc.UserDefined(data, bins)
        counts = dict(zip(bins, sample_bins.counts))

    N = sum(counts.values())

    return sum(p(n, N) ** 2 for n in counts.values() if n != 0)
def simpson_diversity ( data , bins = None , categorical = False , categories = None ) : if not categorical : try : import mapclassify as mc except ImportError : raise ImportError ( "The 'mapclassify' package is required" ) def p ( n , N ) : """Relative abundance""" if n == 0 : return 0 return float ( n ) / N if categorical : counts = data . value_counts ( ) . to_dict ( ) for c in categories : if c not in counts . keys ( ) : counts [ c ] = 0 else : sample_bins = mc . UserDefined ( data , bins ) counts = dict ( zip ( bins , sample_bins . counts ) ) N = sum ( counts . values ( ) ) return sum ( p ( n , N ) ** 2 for n in counts . values ( ) if n != 0 )