Exemplo n.º 1
0
def stability(product_data, item, k):
    results = {}

    pci = np.array(product_data[item][years].dropna(how='any'))

    # convert to a code cell to generate a time series of the maps
    q10 = np.array([
        mapclassify.Quantiles(pci[:, years.index(year)], k=k).yb
        for year in years
    ]).transpose()
    m10 = giddy.markov.Markov(q10)

    results['q10'] = q10
    results['transitions'] = m10.transitions

    np.set_printoptions(3, suppress=True)
    results['stability'] = m10.p
    # The 5 diagonals are between 0.440-0.685 shows medium stability over time

    results['steady_state'] = m10.steady_state  #steady state distribution

    # Get first mean passage time: the average number of steps to go from a state/class to another state for the first time
    results['steps'] = giddy.ergodic.fmpt(m10.p)  #first mean passage time

    return (results)
  def plotAll(self,figname):
    fig = plt.figure(figsize=(15, 15))
    ax1 = plt.gca()
    
    tot_people =self._blocks["density"]
    scheme = mapclassify.Quantiles(tot_people, k=5) 
 
    geoplot.choropleth(
    self._blocks, hue=tot_people, scheme=scheme,
    cmap='Oranges', figsize=(12, 8), ax=ax1
    )
    plt.savefig('density-'+figname)

    fig = plt.figure(figsize=(15, 15))
    ax1 = plt.gca()

    ctx.plot_map(self._loc, ax=ax1)
    _c = ["red", "blue"]
    for i, _r in enumerate(self._roads):
      _r.plot(ax=ax1, facecolor='none', edgecolor=_c[i])
    self._blocks.plot(ax=ax1,facecolor='none', edgecolor="black")  
    

    plt.tight_layout()

    # Plot agents
    self.grid._agdf.plot(ax=ax1)
    plt.savefig('agents-'+figname)
Exemplo n.º 3
0
 def test___init__(self):
     import numpy as np
     f = ps.io.open(ps.examples.get_path('usjoin.csv'))
     pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
     q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
     m = Markov(q5)
     np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"),
                                          0.19758992000997844)
Exemplo n.º 4
0
 def test___init__(self):
     f = ps.io.open(ps.examples.get_path('usjoin.csv'))
     pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
     q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
     m = Markov(q5)
     res = np.array(
         [0.08988764, 0.21468144, 0.21125, 0.20194986, 0.07259074])
     np.testing.assert_array_almost_equal(prais(m.p), res)
def classify_y(y):
    # generating classification objects from the data
    q5 = mc.Quantiles(y, k=5)
    ei5 = mc.EqualInterval(y, k=5)
    mb5 = mc.MaximumBreaks(y, k=5)
    fits = [c.adcm for c in [q5, ei5, mb5]]
    print('Fits: ', fits)
    return fits
Exemplo n.º 6
0
    def test___init__(self):
        # markov = Markov(class_ids, classes)
        f = ps.io.open(ps.examples.get_path('usjoin.csv'))
        pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
        q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
        m = Markov(q5)
        expected = np.array([[729., 71., 1., 0., 0.], [72., 567., 80., 3., 0.],
                             [0., 81., 631., 86.,
                              2.], [0., 3., 86., 573., 56.],
                             [0., 0., 1., 57., 741.]])
        np.testing.assert_array_equal(m.transitions, expected)
        expected = np.array(
            [[0.91011236, 0.0886392, 0.00124844, 0., 0.],
             [0.09972299, 0.78531856, 0.11080332, 0.00415512, 0.],
             [0., 0.10125, 0.78875, 0.1075, 0.0025],
             [0., 0.00417827, 0.11977716, 0.79805014, 0.07799443],
             [0., 0., 0.00125156, 0.07133917, 0.92740926]])
        np.testing.assert_array_almost_equal(m.p, expected)
        expected = np.array(
            [0.20774716, 0.18725774, 0.20740537, 0.18821787, 0.20937187])
        np.testing.assert_array_almost_equal(m.steady_state, expected)

        expected = np.array(
            [[4.81354357, 11.50292712, 29.60921231, 53.38594954, 103.59816743],
             [42.04774505, 5.34023324, 18.74455332, 42.50023268, 92.71316899],
             [69.25849753, 27.21075248, 4.82147603, 25.27184624, 75.43305672],
             [84.90689329, 42.85914824, 17.18082642, 5.31299186, 51.60953369],
             [98.41295543, 56.36521038, 30.66046735, 14.21158356, 4.77619083]])
        np.testing.assert_array_almost_equal(m.fmpt, expected)

        expected = np.array(
            [11.125, 4.65806452, 4.73372781, 4.95172414, 13.77586207])
        np.testing.assert_array_almost_equal(m.sojourn_time, expected)

        m = Markov(q5[:, :2])
        expected = np.array([[10., 0., 0., 0., 0.], [0., 8., 1., 0., 0.],
                             [0., 1., 9., 1., 0.], [0., 0., 0., 8., 0.],
                             [0., 0., 0., 1., 9.]])
        np.testing.assert_array_equal(m.transitions, expected)
        expected = np.array([[1., 0., 0., 0., 0.],
                             [0., 0.88888889, 0.11111111, 0., 0.],
                             [0., 0.09090909, 0.81818182, 0.09090909, 0.],
                             [0., 0., 0., 1., 0.], [0., 0., 0., 0.1, 0.9]])
        np.testing.assert_array_almost_equal(m.p, expected)
        expected = np.array([[1., 0., 0., 0., 0.], [0., 0., 0., 1., 0.]])
        np.testing.assert_array_almost_equal(m.steady_state, expected)

        expected = np.array([[1., np.inf, np.inf, np.inf, np.inf],
                             [np.inf, np.inf, 9., 29., np.inf],
                             [np.inf, np.inf, np.inf, 20, np.inf],
                             [np.inf, np.inf, np.inf, 1., np.inf],
                             [np.inf, np.inf, np.inf, 10, np.inf]])
        np.testing.assert_array_almost_equal(m.fmpt, expected)

        expected = np.array([np.inf, 9., 5.5, np.inf, 10.])
        np.testing.assert_array_almost_equal(m.sojourn_time, expected)
Exemplo n.º 7
0
def draw_month(month):
    to_full_month = {
        'Jan': 'January',
        'Feb': 'February',
        'Mar': 'March',
        'Apr': 'April',
        'May': 'May',
        'Jun': 'June',
        'Jul': 'July'
    }
    frames = []
    for i in range(1, 32):
        day_str = str(i)
        if i < 10:
            day_str = '0' + str(i)
        if os.path.exists(month + ' ' + day_str + '.csv'):
            df1 = pd.read_csv(month + ' ' + day_str + '.csv',
                              header=None,
                              names=[
                                  'id', 'longitude', 'latitude', 'location',
                                  'created_at', 'lang'
                              ])
            frames.append(df1)
    df = pd.concat(frames)
    print(df.shape)
    mydict = dict(df.location.value_counts())
    df['notnan'] = df['location'].notna()
    df['count'] = df.apply(lambda x: mydict[x.location] if x.notnan else 1,
                           axis=1)
    df.drop_duplicates(subset='location', keep='first', inplace=True)
    gdf = gpd.GeoDataFrame(df,
                           geometry=gpd.points_from_xy(df.longitude,
                                                       df.latitude))

    scheme = mc.Quantiles(df['count'], k=5)
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

    ax = gplt.polyplot(
        world,
        edgecolor='white',
        facecolor='lightgray',
    )
    gplt.pointplot(gdf,
                   ax=ax,
                   hue='count',
                   cmap='Reds',
                   scale='count',
                   scheme=scheme,
                   legend=True,
                   legend_var='hue')
    ax.set_title('Discussion on Twitter, ' + to_full_month[month], fontsize=10)
    plt.savefig(month + '.png', dpi=1000)
def storeShape(siteName, col):
    #only for trip
    df = dataPrep(siteName, False)
    df = df.drop_duplicates(subset=['store', 'address'])
    df['avgRating'] = df['avgRating'].apply(
        lambda x: float(x.replace(",", ".")))

    #DATA READING efood
    #df = pd.read_csv(siteName+'.csv')
    #print(df.avgRating)
    df = df[(df.latitude < 39.7006) & (df.latitude > 39.6013) &
            (df.longitude < 20.9526) & (df.longitude > 20.7659)]
    print(df)
    #GEO DATA FRAME CREATION
    geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
    geo_df = gpd.GeoDataFrame(df, geometry=geometry)
    geo_df.set_crs(epsg=4326, inplace=True)
    #REGIONS LOAD
    regions = gpd.read_file('geo.geojson')
    #JOIN GEO_DATA WITH REGIONS
    print(geo_df)
    print(regions)
    merged_df = gpd.sjoin(geo_df, regions, how='inner', op='within')
    print(merged_df)
    temp = merged_df.groupby('index_right')[col].mean()
    print(temp)
    regions['oindex'] = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    print(regions)
    for_plot = regions.merge(temp,
                             how='left',
                             left_on='oindex',
                             right_on='index_right').dropna()
    print(for_plot)
    #PLOT
    f, ax = plt.subplots(1, figsize=(11, 9))
    #IMAGE
    BBox = (20.7693, 20.9526, 39.6013, 39.7278)
    ioa_im = plt.imread('map.png')
    ax.imshow(ioa_im, zorder=0, extent=BBox)
    #BINS
    bins = mapclassify.Quantiles(for_plot[col], k=4).bins
    #PLOT SETTINGS AND LABELS
    for_plot.plot(column=col,
                  ax=ax,
                  legend=True,
                  alpha=0.7,
                  edgecolor='black',
                  scheme="User_Defined",
                  classification_kwds=dict(bins=bins),
                  cmap='RdYlGn_r')
    plt.title(siteName + "'s " + col)
    plt.show()
Exemplo n.º 9
0
 def test___init__(self):
     pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1])
     f = ps.io.open(ps.examples.get_path("usjoin.csv"))
     pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
     q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
     m = Markov(q5)
     np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"),
                                          0.19758992000997844)
     np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="D"),
                                          0.60684854623695594)
     np.testing.assert_array_almost_equal(
         markov_mobility(m.p, measure="L2"), 0.039782002308159647)
     np.testing.assert_array_almost_equal(
         markov_mobility(m.p, measure="B1", ini=pi), 0.2277675878319787)
     np.testing.assert_array_almost_equal(
         markov_mobility(m.p, measure="B2", ini=pi), 0.046366601194789261)
def GeoJson(siteName, lastYear):
    df = dataPrep(siteName, lastYear)
    #GEO DATA FRAME CREATION
    geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
    geo_df = gpd.GeoDataFrame(df, geometry=geometry)
    geo_df.set_crs(epsg=4326, inplace=True)
    #REGIONS LOAD
    regions = gpd.read_file('geo.geojson')
    print(regions)
    #JOIN GEO_DATA WITH REGIONS
    merged_df = gpd.sjoin(geo_df, regions, how='inner', op='within')
    temp = merged_df.groupby('index_right').store.count()
    regions['oindex'] = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    for_plot = regions.merge(temp,
                             how='left',
                             left_on='oindex',
                             right_on='index_right').fillna(0)
    #PLOT
    f, ax = plt.subplots(1, figsize=(11, 9))
    #IMAGE
    ioa_im = plt.imread('map.png')
    ax.imshow(ioa_im, zorder=0, extent=BBox)
    #BINS
    bins = mapclassify.Quantiles(for_plot['store'], k=8).bins
    for i in range(len(bins)):
        bins[i] = bins[i]
    #PLOT SETTINGS AND LABELS
    for_plot.plot(column='store',
                  ax=ax,
                  legend=True,
                  alpha=0.7,
                  edgecolor='black',
                  scheme="User_Defined",
                  classification_kwds=dict(bins=bins),
                  cmap='RdYlGn')
    #for_plot.plot(column = 'store', ax =ax, legend = True, alpha = 0.7, edgecolor = 'black', legend_kwds={'label': "Total number of reviews by shape"})
    if lastYear:
        plt.xlabel('From: 2020-Jan-1 To: 2020-Dec-31')
        plt.title(siteName + "'s reviews in one year period inside shapeboxes")
    else:
        plt.xlabel('From: ' + str(geo_df.date.min()) + ' to: ' +
                   str(geo_df.date.max()))
        plt.title(siteName + "'s reviews all data inside shapeboxes")
    plt.ylabel("Total number of reviews: " + str(int(for_plot.store.sum())))
    plt.show()
Exemplo n.º 11
0
def plot_vote(states, poll_avg):
    shapefile = 'cb_2018_us_state_5m/cb_2018_us_state_5m.shp'

    gdf = gpd.read_file(shapefile)[['NAME', 'geometry']]
    gdf.columns = ['name', 'geometry']

    poll_avg_df = pd.DataFrame({'name': states, 'poll_avg': poll_avg})

    data = gdf.merge(poll_avg_df, on='name', how='right')
    # data['poll_avg'].fillna(value=0)
    print(data)
    print(data['geometry'])

    scheme = mapclassify.Quantiles(data['poll_avg'], k=7)
    geoplot.choropleth(data, hue=data['poll_avg'], scheme=scheme, legend=True)
    plt.show()

    return
Exemplo n.º 12
0
def DMC(data_df):
    print("-" * 50)
    print(data_df.shape, data_df.columns)
    valArray = data_df['CasesWeekly'].groupby(
        level='Week Number').apply(lambda x: x.values.tolist()).values
    valArray = np.array([np.array(i) for i in valArray])
    # valArray=np.array([np.array(i) for i in valArray])
    # print(valArray.shape)
    valArray_fillNan = bfill(valArray.T).T
    valArray_fillNan[np.isnan(valArray_fillNan)] = 0
    # print(valArray_fillNan.shape)
    q5 = np.array([mc.Quantiles(y, k=5).yb
                   for y in valArray_fillNan]).transpose()
    # print(q5.shape)
    # print(q5)
    m5 = giddy.markov.Markov(q5)
    print('DMC-transitions:\n', m5.transitions)
    print('DMC-p:\n', m5.p)
    print('DMC-steady state\n', m5.steady_state)
    print('DMC-fmpt', giddy.ergodic.fmpt(m5.p))
Exemplo n.º 13
0
 def test___init__(self):
     # markov = Markov(class_ids, classes)
     f = ps.io.open(ps.examples.get_path('usjoin.csv'))
     pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
     q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
     m = Markov(q5)
     expected = np.array([[729., 71., 1., 0., 0.], [72., 567., 80., 3., 0.],
                          [0., 81., 631., 86.,
                           2.], [0., 3., 86., 573., 56.],
                          [0., 0., 1., 57., 741.]])
     np.testing.assert_array_equal(m.transitions, expected)
     expected = np.array(
         [[0.91011236, 0.0886392, 0.00124844, 0., 0.],
          [0.09972299, 0.78531856, 0.11080332, 0.00415512, 0.],
          [0., 0.10125, 0.78875, 0.1075, 0.0025],
          [0., 0.00417827, 0.11977716, 0.79805014, 0.07799443],
          [0., 0., 0.00125156, 0.07133917, 0.92740926]])
     np.testing.assert_array_almost_equal(m.p, expected)
     expected = np.array(
         [0.20774716, 0.18725774, 0.20740537, 0.18821787, 0.20937187])
     np.testing.assert_array_almost_equal(m.steady_state, expected)
Exemplo n.º 14
0
def draw_per_day(date):
    if os.path.exists(date + '.csv') is False:
        return
    print(date)
    df = pd.read_csv(date + '.csv',
                     header=None,
                     names=[
                         'id', 'longitude', 'latitude', 'location',
                         'created_at', 'lang'
                     ])
    print(date, df.shape)
    # assign count value
    mydict = dict(df.location.value_counts())

    df['notnan'] = df['location'].notna()
    df['count'] = df.apply(lambda x: mydict[x.location] if x.notnan else 1,
                           axis=1)
    df.drop_duplicates(subset='location', keep='first', inplace=True)
    gdf = gpd.GeoDataFrame(df,
                           geometry=gpd.points_from_xy(df.longitude,
                                                       df.latitude))

    scheme = mc.Quantiles(df['count'], k=5)
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

    ax = gplt.polyplot(
        world,
        edgecolor='white',
        facecolor='lightgray',
    )
    gplt.pointplot(gdf,
                   ax=ax,
                   hue='count',
                   cmap='Reds',
                   scale='count',
                   scheme=scheme,
                   legend=True,
                   legend_var='hue')
    ax.set_title('Discussion on Twitter, ' + date, fontsize=10)
    plt.savefig(date + '.png', dpi=1000)
Exemplo n.º 15
0
def mobilityOf_Values(data_df, zipPolygon):
    caseWeekly_unstack = data_df['CasesWeekly'].unstack(level=0)

    zip_codes = gpd.read_file(zipPolygon)
    data_df_zipGPD = zip_codes.merge(caseWeekly_unstack,
                                     left_on='zip',
                                     right_on=caseWeekly_unstack.index)
    # print(data_df_zipGPD)
    W = ps.lib.weights.Queen(data_df_zipGPD.geometry)
    W.transform = 'R'
    weeks = idx_weekNumber = data_df.index.get_level_values('Week Number')
    weeks = np.unique(weeks)
    valArray = data_df_zipGPD[weeks].to_numpy()
    valArray_fillNan = bfill(valArray).T
    valArray_fillNan[np.isnan(valArray_fillNan)] = 0
    # print(valArray_fillNan,valArray_fillNan.shape)

    q5 = np.array([mc.Quantiles(y).yb for y in valArray_fillNan]).transpose(
    )  #each row represents an state's income time series 1929-2010
    m = markov.Markov(q5)
    print(m.p)
    #1. Shorrock1’s mobility measure
    print("Shorrock1’s mobility measure:",
          mobility.markov_mobility(m.p, measure="P"))
    #2. Shorroks2’s mobility measure
    print("Shorroks2’s mobility measure:",
          mobility.markov_mobility(m.p, measure="D"))
    #3. Sommers and Conlisk’s mobility measure
    print("Sommers and Conlisk’s mobility measure:",
          mobility.markov_mobility(m.p, measure="L2"))
    #4. Bartholomew1’s mobility measure
    pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1])
    print("Bartholomew1’s mobility measure:",
          mobility.markov_mobility(m.p, measure="B1", ini=pi))
    #5. Bartholomew2’s mobility measure
    pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1])
    print("Bartholomew2’s mobility measure:",
          mobility.markov_mobility(m.p, measure="B2", ini=pi))
Exemplo n.º 16
0
def main():
    data_dir = "./data/au-cities.csv"
    world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    cities = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))

    print(world.head())
    ax = gp.polyplot(world, projection = gp.crs.Orthographic())
    ax.outline_patch.set_visible(True)

    #Graphs a choropleth of gdp / population
    gdp_per_person = world["gdp_md_est"] / world["pop_est"]
    scheme = mapclassify.Quantiles(gdp_per_person, k = 5)
    gp.choropleth(world, hue = gdp_per_person, scheme = scheme, 
    cmap = "Greens")
    print(world.head())

    #Graphs population size by establishing area size 
    #to the African continent
    africa = world[world.continent == "Africa"] 
    ax = gp.cartogram(africa, scale = "pop_est", limits = (0.2, 1), 
    edgecolor = "black")
    gp.polyplot(africa, edgecolor = "black", ax = ax)

    plt.show()
Exemplo n.º 17
0
def geospatial_viz(geo_data_url,
                   point_data_url=None,
                   att_var=None,
                   map_type=None):
    '''
    function to visualize the attribute information in map. (eg, population in states)
    geo_att_data: geodataframe that contains both geometry and attributes info
    att_var: the attributes to be visualized in the map
    map_type: string, the type of map to be viz. pointplot, choropleth, voronoi
    
    if point_data = None, att_var must be from geo_data
    
    '''
    geo_data = gpd.read_file(geo_data_url)
    print(geo_data.head())

    if point_data_url == 'No point attribute data':
        if att_var is None:
            ax = gplt.polyplot(geo_data, figsize=(10, 5))
            ax.set_title('plain map of continental USA', fontsize=16)
        else:
            if map_type == 'choropleth':
                scheme = mc.FisherJenks(geo_data[att_var], k=5)
                labels = scheme.get_legend_classes()
                ax = gplt.polyplot(geo_data, projection=gcrs.AlbersEqualArea())
                gplt.choropleth(geo_data,
                                hue=att_var,
                                edgecolor='white',
                                linewidth=1,
                                cmap='Blues',
                                legend=True,
                                scheme=scheme,
                                legend_labels=labels,
                                ax=ax)
                ax.set_title('{} in the continental US'.format(att_var),
                             fontsize=16)

            if map_type == "cartogram":
                gplt.cartogram(geo_data,
                               scale=att_var,
                               edgecolor='black',
                               projection=gcrs.AlbersEqualArea())

    else:
        point_data = gpd.read_file(point_data_url)
        scheme = mc.Quantiles(point_data[att_var], k=5)
        labels = scheme.get_legend_classes()

        if map_type == 'pointplot':
            if isinstance(point_data.geometry[0],
                          shapely.geometry.point.Point):
                ax = gplt.polyplot(geo_data,
                                   edgecolor='white',
                                   facecolor='lightgray',
                                   figsize=(12, 8)
                                   #projection = gcrs.AlbersEqualArea()
                                   )
                gplt.pointplot(point_data,
                               ax=ax,
                               hue=att_var,
                               cmap='Blues',
                               scheme=scheme,
                               scale=att_var,
                               legend=True,
                               legend_var='scale',
                               legend_kwargs={"loc": 'lower right'},
                               legend_labels=labels)
                ax.set_title(
                    'Cities in the continental US, by population 2010',
                    fontsize=16)
            else:
                print('Geometry data type not valid')

        if map_type == "voronoi":
            # check uniqueness of coordinates
            duplicates = point_data.geometry.duplicated()
            point_data_unique = point_data[-duplicates]
            proj = gplt.crs.AlbersEqualArea(central_longitude=-98,
                                            central_latitude=39.5)

            ax = gplt.voronoi(point_data_unique,
                              hue=att_var,
                              clip=geo_data,
                              projection=proj,
                              cmap='Blues',
                              legend=True,
                              edgecolor="white",
                              linewidth=0.01)

            gplt.polyplot(geo_data,
                          ax=ax,
                          extent=geo_data.total_bounds,
                          edgecolor="black",
                          linewidth=1,
                          zorder=1)
            plt.title("{} in US cities".format(att_var), fontsize=16)
Exemplo n.º 18
0
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mapclassify as mc

continental_usa_cities = gpd.read_file(gplt.datasets.get_path('usa_cities'))
continental_usa_cities = continental_usa_cities.query('STATE not in ["AK", "HI", "PR"]')
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))
scheme = mc.Quantiles(continental_usa_cities['POP_2010'], k=5)

ax = gplt.polyplot(
    contiguous_usa,
    zorder=-1,
    linewidth=1,
    projection=gcrs.AlbersEqualArea(),
    edgecolor='white',
    facecolor='lightgray',
    figsize=(8, 12)
)
gplt.pointplot(
    continental_usa_cities,
    scale='POP_2010',
    limits=(2, 30),
    hue='POP_2010',
    cmap='Blues',
    scheme=scheme,
    legend=True,
    legend_var='scale',
    legend_values=[8000000, 2000000, 1000000, 100000],
    legend_labels=['8 million', '2 million', '1 million', '100 thousand'],
Exemplo n.º 19
0
    ##
    with open(stats_file) as f:
        stats = json.load(f)
        # Iterate over each country
        for country, stat in stats.items():
            geo_country = geo_data[geo_data["name"].str.lower() ==
                                   country]  # Finds the corresponding geo data
            # Obtain all the countries that receive litter from it
            countries, tons, percs = getCountriesStats(stat)
            geo_from_countries = geo_data[geo_data["name"].str.lower().isin(
                countries)]  # Finds the corresponding geo data
            # gplt.polyplot(geo_country, figsize=(8, 4))
            # gplt.polyplot(geo_from_countries, figsize=(8, 4))
            # Note: this code sample requires gplt>=0.4.0.
            if len(tons) > 0 and len(geo_from_countries) == len(tons):
                scheme = mapclassify.Quantiles(tons, k=min(5, len(tons)))
                ax = gplt.polyplot(geo_data, figsize=(8, 4))
                # ax = gplt.webmap(geo_data, projection=gcrs.WebMercator())
                ax2 = gplt.choropleth(geo_from_countries,
                                      hue=tons,
                                      scheme=scheme,
                                      cmap='Greens',
                                      ax=ax)
                # gplt.choropleth(geo_country, hue=[1],  cmap='gray', ax=ax2)
                plt.title(country.capitalize())
                plt.show()
            else:
                print(F"------------- failed ------------")
                print(list(geo_from_countries["name"]))
                print(countries)
Exemplo n.º 20
0
        try:
            raysWithBuildings = gp.sjoin(rays, polys[j], op="intersects")
        except:
            print(
                "An exception has occured: there were not enough bins which contained buildings"
            )
            continue
        rays = rays.drop(raysWithBuildings.index.values.tolist())
        tree_list = list(rays['geometry']) + list(street['geometry'])
        strtree = STRtree(tree_list)
        pbf.accumulate_counts(strtree, street, 7)
        for k in street.index:
            grids[j].at[k, 'count'] = street.at[k, 'count']
        if i < 10:  # saves images of the plots of data for first 10 iterations
            ax = x.plot()
            scheme = mc.Quantiles(street['count'], k=20)
            gplt.choropleth(street,
                            ax=ax,
                            hue='count',
                            legend=True,
                            scheme=scheme,
                            cmap="jet",
                            legend_kwargs={'bbox_to_anchor': (1, 0.9)})
            plt.savefig('../datasets_and_generators/ANN_trainimages/x_' +
                        str(i) + '.png')
            plt.close()
    '''
    part 4: concatenates data to MASTER training data files 

    ATTENTION: when changing this file DO NOT immediately concatenate
    generated data to MASTER files, use a temporary json file by changing 
Exemplo n.º 21
0
"""
=====
PySAL
=====

This example shows how to read a shapefile with PySAL,
the Python Spatial Analysis Library, and convert it
to a NetworkX graph....
"""

import libpysal
import geopandas as gpd
import mapclassify as mc
import matplotlib.pyplot as plt
import networkx as nx

# %%
# TODO

columbus = gpd.read_file(libpysal.examples.get_path("columbus.shp"))
q5 = mc.Quantiles(columbus.CRIME, k=5)
q5.plot(columbus, axis_on=False, cmap="Blues")
plt.show()
Exemplo n.º 22
0
This example plots passenger volumes for commercial flights out of Los Angeles International
Airport. Some globe-modification options available in ``cartopy`` are demonstrated. Visit
`the cartopy docs <http://scitools.org.uk/cartopy/docs/latest/matplotlib/feature_interface.html>`_
for more information.
"""

import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import cartopy
import mapclassify as mc

la_flights = gpd.read_file(gplt.datasets.get_path('la_flights'))
scheme = mc.Quantiles(la_flights['Passengers'], k=5)

f, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={
    'projection': gcrs.Orthographic(central_latitude=40.7128, central_longitude=-74.0059)
})
plt.suptitle('Popular Flights out of Los Angeles, 2016', fontsize=16)
plt.subplots_adjust(top=0.95)

ax = gplt.sankey(
    la_flights, scale='Passengers', hue='Passengers', cmap='Purples', scheme=scheme, ax=axarr[0][0]
)
ax.set_global()
ax.outline_patch.set_visible(True)
ax.coastlines()

ax = gplt.sankey(
W = libpysal.weights.Queen.from_dataframe(london_shp)
W.transform = 'r'

pci = np.array([f.by_col[str(y)] for y in range(2007, 2018)])
pci.shape
pci = pci.T
pci.shape

cnames = f.by_col('MSOA11CD')
cnames[:10]

# join shape data to emissions
london = london_shp[['geometry']].join(london_msoa)

# 2007
pci07 = mapclassify.Quantiles(pci[:, 0], k=5)

f, ax = plt.subplots(1, figsize=(10, 5))
london.assign(cl=pci07.yb + 1).plot(column='cl',
                                    categorical=True,
                                    k=5,
                                    cmap='Greens',
                                    linewidth=0.1,
                                    ax=ax,
                                    edgecolor='grey',
                                    legend=True)
ax.set_axis_off()
plt.title('Per Capita Emissions 2007 Quintiles')
plt.show()

# 2017
Exemplo n.º 24
0
import matplotlib.pyplot as plt
import geopandas
import equal_area_breaks
import mapclassify

from demo_utils import *

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world = world[(world.pop_est > 0) & (world.name != "Antarctica")]
world['gdp_per_cap'] = 1000000 * (world.gdp_md_est / world.pop_est)
world = world.to_crs({'proj': 'wintri'})
world['area'] = world.geometry.area

data_col = 'gdp_per_cap'
cmap = cmap_yl_or_rd()

fig, axes = plt.subplots(nrows=2, ncols=2)

plot_classes(world, mapclassify.Quantiles(world[data_col]), fig, axes[0, 0],
             cmap)
plot_classes(world, mapclassify.EqualInterval(world[data_col]), fig,
             axes[1, 0], cmap)
plot_classes(world, mapclassify.NaturalBreaks(world[data_col]), fig,
             axes[0, 1], cmap)
plot_classes(
    world, equal_area_breaks.Equal_Area_DP(world[data_col],
                                           area=world['area']), fig,
    axes[1, 1], cmap)

plt.show()
Exemplo n.º 25
0
def test_Sequence_equal():
    '''
    2. Testing on sequences of equal length.
    '''

    seq1 = 'ACGGTAG'
    seq2 = 'CCTAAGA'
    seq3 = 'CCTAAGC'

    # 2.1 Calculating "hamming" distance will not fail on equal sequences

    seqAna = Sequence([seq1, seq2, seq3], dist_type="hamming")
    seq_dis_mat = np.array([[0., 6., 6.], [6., 0., 1.], [6., 1., 0.]])
    np.testing.assert_allclose(seqAna.seq_dis_mat, seq_dis_mat)

    # 2.2 User-defined substitution cost matrix and indel cost (distance
    #     between different types is always 1 and indel cost is 2 or larger) -
    #     give the same sequence distance matrix as "hamming" distance
    subs_mat = np.array([[0., 1., 1., 1.], [1., 0., 1., 1.], [1., 1., 0., 1.],
                         [1., 1., 1., 0.]])
    indel = 2
    seqAna = Sequence([seq1, seq2, seq3], subs_mat=subs_mat, indel=indel)
    seq_dis_mat = np.array([[0., 6., 6.], [6., 0., 1.], [6., 1., 0.]])
    np.testing.assert_allclose(seqAna.seq_dis_mat, seq_dis_mat)

    # 2.3 User-defined substitution cost matrix and indel cost (distance
    #     between different types is always 1 and indel cost is 1) - give a
    #     slightly different sequence distance matrix from "hamming" distance since
    #     insertion and deletion is happening
    indel = 1
    seqAna = Sequence([seq1, seq2, seq3], subs_mat=subs_mat, indel=indel)
    seq_dis_mat = np.array([[0., 5., 5.], [5., 0., 1.], [5., 1., 0.]])
    np.testing.assert_allclose(seqAna.seq_dis_mat, seq_dis_mat)

    f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
    pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
    q5 = np.array([mc.Quantiles(y, k=5).yb for y in pci]).transpose()
    seqs = q5[:5, :]  #only look at the first 5 sequences

    #"hamming"
    seq_hamming = Sequence(seqs, dist_type="hamming")
    seq_dis_mat = np.array([[0., 75., 7., 81., 81.], [75., 0., 80., 81., 65.],
                            [7., 80., 0., 81., 81.], [81., 81., 81., 0., 69.],
                            [81., 65., 81., 69., 0.]])
    np.testing.assert_allclose(seq_hamming.seq_dis_mat, seq_dis_mat)

    #"interval"
    seq_interval = Sequence(seqs, dist_type="interval")
    seq_dis_mat = np.array([[0., 123., 7., 308., 230.],
                            [123., 0., 130., 185., 109.],
                            [7., 130., 0., 315., 237.],
                            [308., 185., 315., 0., 90.],
                            [230., 109., 237., 90., 0.]])
    np.testing.assert_allclose(seq_interval.seq_dis_mat, seq_dis_mat)

    #"arbitrary"
    seq_arbitrary = Sequence(seqs, dist_type="arbitrary")
    seq_dis_mat = np.array([[0., 37.5, 3.5, 40.5, 40.5],
                            [37.5, 0., 40., 40.5, 32.5],
                            [3.5, 40., 0., 40.5, 40.5],
                            [40.5, 40.5, 40.5, 0., 34.5],
                            [40.5, 32.5, 40.5, 34.5, 0.]])
    np.testing.assert_allclose(seq_arbitrary.seq_dis_mat, seq_dis_mat)

    # "markov"
    seq_markov = Sequence(seqs, dist_type="markov")
    seq_dis_mat = np.array(
        [[0., 72.69026039, 6.44797042, 81., 81.],
         [72.69026039, 0., 77.55529756, 80.91834743, 58.93609228],
         [6.44797042, 77.55529756, 0., 81., 81.],
         [81., 80.91834743, 81., 0., 63.14216005],
         [81., 58.93609228, 81., 63.14216005, 0.]])
    np.testing.assert_allclose(seq_markov.seq_dis_mat, seq_dis_mat)

    # "tran"
    seq_tran = Sequence(seqs, dist_type="tran")
    seq_dis_mat = np.array([[0., 23., 8., 10., 30.], [23., 0., 17., 20., 33.],
                            [8., 17., 0., 6., 24.], [10., 20., 6., 0., 27.],
                            [30., 33., 24., 27., 0.]])
    np.testing.assert_allclose(seq_tran.seq_dis_mat, seq_dis_mat)
Exemplo n.º 26
0
"""

import pandas as pd
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mapclassify as mc

# load the data
obesity_by_state = pd.read_csv(gplt.datasets.get_path('obesity_by_state'),
                               sep='\t')
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))
contiguous_usa['Obesity Rate'] = contiguous_usa['state'].map(
    lambda state: obesity_by_state.query("State == @state").iloc[0]['Percent'])
scheme = mc.Quantiles(contiguous_usa['Obesity Rate'], k=5)

ax = gplt.cartogram(contiguous_usa,
                    scale='Obesity Rate',
                    limits=(0.75, 1),
                    projection=gcrs.AlbersEqualArea(central_longitude=-98,
                                                    central_latitude=39.5),
                    hue='Obesity Rate',
                    cmap='Reds',
                    scheme=scheme,
                    linewidth=0.5,
                    legend=True,
                    legend_kwargs={'loc': 'lower right'},
                    legend_var='hue',
                    figsize=(8, 12))
gplt.polyplot(contiguous_usa, facecolor='lightgray', edgecolor='None', ax=ax)
Exemplo n.º 27
0
def gpd_plot(shapefile, data):
    us = gpd.read_file(shapefile)
    pop_density = data['2019pop'] / data['LandArea']
    scheme = mapclassify.Quantiles(pop_density, k=10)
    geoplot.choropleth(us, hue=pop_density, scheme=scheme, cmap='Greens')
    plt.show()
Exemplo n.º 28
0
              legend=True)

#%%

#well-known styles
#geoplot for Equal intervals
gplt.choropleth(covidMap,
                hue='CasesPer100k',
                scheme=mc.EqualInterval(covidMap.CasesPer100k, k=5),
                cmap='OrRd',
                legend=True)
#%%
#geoplot for Quantiles
gplt.choropleth(covidMap,
                hue='CasesPer100k',
                scheme=mc.Quantiles(covidMap.CasesPer100k, k=5),
                cmap='OrRd',
                legend=True)

#%%
#optimization styles in geopandas
covidMap.plot(column='CasesPer100k',
              scheme='FisherJenks',
              k=5,
              cmap='OrRd',
              legend=True)
#%%
covidMap.plot(column='CasesPer100k',
              scheme='HeadTailBreaks',
              cmap='OrRd',
              legend=True)
Exemplo n.º 29
0
#
# (Example from geoplots gallery)

# %% Collapsed="false"
cali = gpd.read_file(gplt.datasets.get_path('california_congressional_districts'))
cali['area'] =cali.geometry.area

proj=gcrs.AlbersEqualArea(central_latitude=37.16611, central_longitude=-119.44944)
fig, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={'projection': proj})

gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=None, ax=axarr[0][0]
)
axarr[0][0].set_title('scheme=None', fontsize=18)

scheme = mc.Quantiles(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[0][1]
)
axarr[0][1].set_title('scheme="Quantiles"', fontsize=18)

scheme = mc.EqualInterval(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[1][0]
)
axarr[1][0].set_title('scheme="EqualInterval"', fontsize=18)

scheme = mc.FisherJenks(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[1][1]
)
Exemplo n.º 30
0
uk_avg[['grains', 'other food', 'meat', 'dairy and eggs', 'fruit and veg', 'drinks']].plot()

uk_avg[['private transport (land)', 'public transport (land and water)', 'air transport']].plot()


for item in ['other food', 'meat', 'dairy and eggs', 'clothing', 'home energy', 'private transport (land)', 'public transport (land and water)', 'air transport', 'total_ghg']:
    g=sns.FacetGrid(new_cat_all, row='year', col='RGN11NM')
    g.map(sns.scatterplot, 'Income anonymised', item)
    plt.savefig(eval("r'" + data_directory + "/Spatial_Emissions/outputs/facets/UK_regions_scatter_" + item + ".png'"))


# by income decile
new_cat_all = pd.DataFrame(columns=new_cat[2017].columns)
for year in range(2007, 2018):
    temp = cp.copy(new_cat[year]); temp['year'] = year
    temp['q10'] = temp['Income anonymised'].map(mapclassify.Quantiles(temp['Income anonymised'], k=10))
    temp['q10'] = [x[0] for x in temp['q10']]
    new_cat_all = new_cat_all.append(temp)

uk_avg = cp.copy(new_cat_all)
uk_avg[keep] = uk_avg[keep].apply(lambda x: x*uk_avg['population'])
uk_avg = uk_avg.groupby(['year', 'q10']).sum()
uk_avg[keep] = uk_avg[keep].apply(lambda x: x/uk_avg['population'])


categories = ['grains', 'other food', 'meat', 'dairy and eggs', 'fruit and veg', 'drinks']
data = uk_avg[categories].stack().reset_index(); data.columns = ['year', 'q10', 'category', 'ghg']; data['col'] = 'col'

g = sns.FacetGrid(data, row='col', col='q10', hue='category')
g.map(sns.lineplot, 'year', 'ghg')