Example #1
0
def shannon_diversity(data, bins=None, categorical=False, categories=None):
    """
    Calculates the Shannon\'s diversity index of data. Helper function for
    :py:class:`momepy.Shannon`.

    .. math::

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

    Formula adapted from https://gist.github.com/audy/783125

    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
        Shannon's diversity index

    See also
    --------
    momepy.Shannon : Calculates the Shannon's diversity index
    momepy.Simpson : Calculates the Simpson's diversity index
    momepy.simpson_diversity : Calculates the Simpson's diversity index
    """
    from math import log as ln

    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) * ln(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) for n in counts.values() if n != 0)
Example #2
0
def pretty_fisherjenks_sampled(y, k=5, digits=2):
    """
    Return pretty, rounded, Fisher Jenks Sampled classification schemes. Relies on mapclassify. 
    -----------
    Parameters:
    y: input vector
    k: number of classes
    digits: degree of rounding
    -----------
    Returns: mapclassify object
    """
    original = mapclassify.FisherJenksSampled(y, k=k)
    accuracies = (-(np.floor(np.log10(original.bins)) - digits)).astype(int)
    pretty_bins = [
        round(limit, accuracies[i]) for i, limit in enumerate(original.bins)
    ]
    return mapclassify.UserDefined(y, pretty_bins)
Example #3
0
def plot_world_map(df):
    """ Plot a world map from a data frame """

    cases_per_million = df['total_cases_per_million']
    scheme = mapclassify.UserDefined(
        cases_per_million, bins=[100, 500, 1000, 5000, 10000, 15000, 20000])

    gplt.choropleth(
        df,
        hue=cases_per_million,
        edgecolor='white',
        linewidth=1,
        scheme=scheme,
        cmap='Reds',
        legend=True,
        figsize=(12, 6),
    )

    plt.title("Total Confirmed COVID-19 Cases Per Million as of 07-09-2020")

    # Without this, the world map doesnt load
    plt.show()
def plot_choropleth(
    d,
    key,
    *,
    scheme=None,
    bin_uls=None,
    ax=None,
    cmap=mpl.cm.viridis_r,
    legend=True,
    legend_kws=None,
    legend_fmt=DEFAULT_LEGEND_FMT,
    plot_kws=None,
):
    if legend_kws is None:
        legend_kws = {}

    if plot_kws is None:
        plot_kws = {}

    if bin_uls is not None:
        assert scheme is None, "supply either scheme or bin_uls"
        scheme = mapclassify.UserDefined(d[key], bin_uls)

    assert scheme is not None

    if ax is None:
        fig, ax = plt.subplots()

    plot_data = d.assign(**{key: scheme.yb})
    plot_data.plot(key, cmap=cmap, ax=ax, linewidth=0, **plot_kws)

    if legend:
        handles = make_legend_handles(scheme, cmap, fmt=legend_fmt)
        ax.legend(handles=handles, **legend_kws)

    return ax
Example #5
0
        "data/world_data_administrative_boundaries_country_level.shp")
except Exception as e:
    world = gpd.read_file(
        "data/world_data_administrative_boundaries_country_level.zip")

# TODO: Make into gifs
# TODO: Make legend have "No Data" for 0

countries_gdf = countries_gdf[['GDLcode', 'geometry']]
countries_gdf.rename(columns={'GDLcode': 'GDLCODE'}, inplace=True)

countries_gdf = countries_gdf.merge(SHDI_first, on='GDLCODE', how='left')
countries_gdf['shdi'].fillna(-1, inplace=True)
scheme = mapclassify.UserDefined(countries_gdf['shdi'],
                                 bins=[
                                     0, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6,
                                     0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1.0
                                 ])

background_color = '#d1e5f0'
missing_color = 'grey'
fig, ax = plt.subplots(
    figsize=(20, 10),
    facecolor=background_color,
    subplot_kw={
        'projection': gcrs.Robinson(),
        # https://scitools.org.uk/cartopy/docs/latest/crs/projections.html
        'facecolor': background_color
    })
plt.title('Human Development Index by World Subdivisions',
          fontdict={
Example #6
0
                return np.median(
                    np.load(
                        src /
                        f"YLL_{state}_{district}_phi{phi}_{vax_policy}.npz")
                    ['arr_0'])
            except FileNotFoundError:
                # return np.nan
                return 0

        districts = districts_to_run.copy()\
            .assign(YLL = districts_to_run.index.map(load_median_YLL))\
            .assign(YLL_per_mn = lambda df: df["YLL"]/(df["N_tot"]/1e6))

        fig, ax = plt.subplots(1, 1)
        scheme = mapclassify.UserDefined(
            districts.YLL_per_mn,
            [0, 125, 250, 400, 600, 900, 1200, 1600, 2500, 5000, 7500
             ])  # ~deciles
        scheme = mapclassify.UserDefined(
            districts.YLL_per_mn,
            [0, 600, 1200, 2000, 2400, 3000, 3600, 4000, 4500, 5550, 9000
             ])  # ~deciles
        districts["category"] = scheme.yb
        india.join(districts["category"].astype(int))\
            .drop(labels = "Andaman And Nicobar Islands")\
            .plot(
            column = "category",
            linewidth = 0.1,
            edgecolor = "k",
            ax = ax,
            legend = True,
            categorical = True,
import numpy as np
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.colors
import mapclassify

# get coefficient surface w_sh
legend_classes = mapclassify.NaturalBreaks(
    gwr_map_2.loc[(~gwr_map_2['w_sh'].isna()), 'w_sh'], k=5)

legend_classes = mapclassify.UserDefined(
    gwr_map_2.loc[(~gwr_map_2['w_sh'].isna()), 'w_sh'],
    bins=[-5.55, 0, 5.64, 8.93, 12.69])
mapping = dict([(i, s)
                for i, s in enumerate(legend_classes.get_legend_classes())])

# plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_axes([0, 0, 0.95, 0.95])
gwr_map_2.loc[(~gwr_map_2['w_sh'].isna())].assign(cl = legend_classes.yb).plot(column = 'cl', categorical = True,\
                                             k = 6, cmap = 'coolwarm', norm = matplotlib.colors.Normalize(-1.8,3.5),\
                                             linewidth = 0.3,\
                                            alpha = 0.7,\
                                            ax = ax, edgecolor = 'black', legend = True,\
                                            legend_kwds = {'loc':'lower left',\
                                                           'fontsize': 10,\
                                                            'frameon': False,\
                                                            'title': 'GWR coefficients\nw_sh\n',\
                                                            'title_fontsize': 12,\
                                                             'edgecolor': 'gray'})
Example #8
0
            if ltla_classifier_mode == 'manual':
                england.plot(column=c_date.strftime('bin_%m%d'),
                             ax=ax,
                             vmin=0,
                             vmax=len(ltla_classifier_bins) - 1,
                             cmap=colour_map,
                             zorder=z)
                z += 1
#                ltla_classifier_colours = ['#338ed7','#ddfffe','#d9fedf','#f3ff92','#ffa923','#e90418','#ad0620']
#                for count,cbin in enumerate(ltla_classifier_bins):
#                    england.plot(column=('bin%02d' % count) + c_date.strftime('%m%d'),ax=ax,vmin=0,vmax=1,facecolor=ltla_classifier_colours[count],zorder=z)
#                    z+=1
            else:
                #Classify the relevant data using mapclassify
                classified = mc.UserDefined(
                    england.get(c_date.strftime('ltla_%m%d')),
                    ltla_classifier_bins)
                classified.plot(england,
                                ax=ax,
                                cmap=colour_map,
                                border_width=0.1,
                                border_color='#EEEEEE22',
                                legend=True,
                                legend_kwds={'loc': 'center left'})
        if (plot_risk_weighted_ltla_binned):
            england.plot(column=c_date.strftime('rwbin_%m%d'),
                         ax=ax,
                         vmin=0,
                         vmax=len(ltla_classifier_bins) - 1,
                         cmap=colour_map,
                         zorder=z)
Example #9
0
def test_classify():
    # data
    link_to_data = examples.get_path('columbus.shp')
    gdf = gpd.read_file(link_to_data)
    x = gdf['HOVAL'].values

    # box_plot
    a = classify(x, 'box_plot') 
    b = mapclassify.BoxPlot(x)
    _assertions(a, b)

    # EqualInterval
    a = classify(x, "EqualInterval", k=3)
    b = mapclassify.EqualInterval(x, k=3)
    _assertions(a, b)

    # FisherJenks
    a = classify(x, "FisherJenks", k=3)
    b = mapclassify.FisherJenks(x, k=3)
    _assertions(a, b)

    
    a= classify(x, "FisherJenksSampled", k=3, pct_sampled=0.5, truncate=False)
    b = mapclassify.FisherJenksSampled(x, k=3, pct=0.5,truncate=False)
    _assertions(a, b)
    
    # headtail_breaks
    a = classify(x, 'headtail_breaks')
    b = mapclassify.HeadTailBreaks(x)
    _assertions(a, b)
    
    # quantiles
    a = classify(x, 'quantiles',k=3)
    b = mapclassify.Quantiles(x, k=3)
    _assertions(a, b)

    # percentiles
    a = classify(x, 'percentiles', pct=[25,50,75,100])
    b = mapclassify.Percentiles(x, pct=[25,50,75,100])
    _assertions(a, b)

    #JenksCaspall
    a = classify(x, 'JenksCaspall', k=3)
    b = mapclassify.JenksCaspall(x, k=3)
    _assertions(a, b)

    a = classify(x, 'JenksCaspallForced', k=3) 
    b = mapclassify.JenksCaspallForced(x, k=3)
    _assertions(a, b)
    
    a = classify(x, 'JenksCaspallSampled', pct_sampled=0.5)
    b = mapclassify.JenksCaspallSampled(x, pct=0.5)
    _assertions(a, b)
    

    # natural_breaks, max_p_classifier
    a = classify(x, 'natural_breaks')
    b = mapclassify.NaturalBreaks(x)
    _assertions(a, b)

    
    a = classify(x, 'max_p', k=3, initial=50)
    b = mapclassify.MaxP(x, k=3, initial=50)
    _assertions(a, b)
    

    # std_mean
    a = classify(x, 'std_mean', multiples=[-1,-0.5,0.5,1])
    b = mapclassify.StdMean(x, multiples=[-1,-0.5,0.5,1])
    _assertions(a, b)

    
    # user_defined
    a = classify(x, 'user_defined', bins=[20, max(x)]) 
    b = mapclassify.UserDefined(x, bins=[20, max(x)])
    _assertions(a, b)