def test_plot_spatial_weights(): # get data gdf = gpd.read_file(examples.get_path('43MUE250GC_SIR.shp')) gdf.head() # calculate weights weights = Queen.from_dataframe(gdf) # plot weights fig, _ = plot_spatial_weights(weights, gdf) plt.close(fig) # calculate nonplanar_joins wnp = libpysal.weights.util.nonplanar_neighbors(weights, gdf) # plot new joins fig2, _ = plot_spatial_weights(wnp, gdf) plt.close(fig2) #customize fig3, _ = plot_spatial_weights(wnp, gdf, nonplanar_edge_kws=dict(color='#4393c3')) plt.close(fig3) # plot in existing figure fig4, axs = plt.subplots(1, 3) plot_spatial_weights(wnp, gdf, ax=axs[0]) plt.close(fig4) # uses a column as the index for spatial weights object weights_index = Queen.from_dataframe(gdf, idVariable="CD_GEOCMU") fig, _ = plot_spatial_weights(weights_index, gdf, indexed_on="CD_GEOCMU") plt.close(fig)
def test_plot_moran_simulation(): # Load data and apply statistical analysis gdf = _test_data() y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calc Global Moran w = Queen.from_dataframe(gdf) moran = Moran(y, w) # plot fig, _ = plot_moran_simulation(moran) plt.close(fig) # customize fig, _ = plot_moran_simulation(moran, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_plot_local_autocorrelation(): columbus = examples.load_example('Columbus') link_to_data = columbus.get_path('columbus.shp') df = gpd.read_file(link_to_data) y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) fig, _ = plot_local_autocorrelation(moran_loc, df, 'HOVAL', p=0.05) plt.close(fig) # also test with quadrant and mask fig, _ = plot_local_autocorrelation(moran_loc, df, 'HOVAL', p=0.05, region_column='POLYID', aspect_equal=False, mask=['1', '2', '3'], quadrant=1) plt.close(fig) # also test with quadrant and mask assert_raises(ValueError, plot_local_autocorrelation, moran_loc, df, 'HOVAL', p=0.05, region_column='POLYID', mask=['100', '200', '300'], quadrant=1)
def test_moran_loc_bv_scatterplot(): link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Univariate and Bivariate Moran moran_loc = Moran_Local(y, w) moran_loc_bv = Moran_Local_BV(x, y, w) # try with p value so points are colored fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv, p=0.05, aspect_equal=False) plt.close(fig) assert_raises(ValueError, _moran_loc_bv_scatterplot, moran_loc, p=0.5) assert_warns(UserWarning, _moran_loc_bv_scatterplot, moran_loc_bv, p=0.5, scatter_kwds=dict(c='r'))
def test_plot_moran_simulation(): # Load data and apply statistical analysis link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calc Global Moran w = Queen.from_dataframe(gdf) moran = Moran(y, w) # plot fig, _ = plot_moran_simulation(moran) plt.close(fig) # customize fig, _ = plot_moran_simulation(moran, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def _test_calc_moran_loc(gdf, var='HOVAL'): y = gdf[var].values w = Queen.from_dataframe(gdf) w.transform = 'r' moran_loc = Moran_Local(y, w) return moran_loc
def test_moran_loc_scatterplot(): df = _test_data_columbus() x = df['INC'].values y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) moran_bv = Moran_BV(x, y, w) # try without p value fig, _ = _moran_loc_scatterplot(moran_loc) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) # try with p value and zstandard=False fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05, zstandard=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) # try without p value and zstandard=False fig, _ = _moran_loc_scatterplot(moran_loc, zstandard=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) raises(ValueError, _moran_loc_scatterplot, moran_bv, p=0.5) warns(UserWarning, _moran_loc_scatterplot, moran_loc, p=0.5, scatter_kwds=dict(c='#4393c3'))
def test_moran_global_scatterplot(): # Load data and apply statistical analysis gdf = _test_data() y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calc Global Moran w = Queen.from_dataframe(gdf) moran = Moran(y, w) # plot fig, _ = _moran_global_scatterplot(moran) plt.close(fig) # customize fig, _ = _moran_global_scatterplot(moran, zstandard=False, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_plot_local_autocorrelation(): link = examples.get_path("columbus.shp") df = gpd.read_file(link) y = df["HOVAL"].values w = Queen.from_dataframe(df) w.transform = "r" moran_loc = esda.moran.Moran_Local(y, w) fig = plot_local_autocorrelation(moran_loc, df, "HOVAL")
def test_moran_scatterplot(): link = examples.get_path("columbus.shp") df = gpd.read_file(link) y = df["HOVAL"].values w = Queen.from_dataframe(df) w.transform = "r" moran_loc = esda.moran.Moran_Local(y, w) fig = moran_scatterplot(moran_loc, p=0.05)
def test_plot_choropleth(): link = examples.get_path("columbus.shp") df = gpd.read_file(link) w = Queen.from_dataframe(df) w.transform = "r" TOOLS = "tap,help" fig = plot_choropleth( df, "HOVAL", title="columbus", reverse_colors=True, tools=TOOLS )
def test_moran_global_scatterplot(): # Load data and apply statistical analysis guerry = examples.load_example('Guerry') link_to_data = guerry.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calc Global Moran w = Queen.from_dataframe(gdf) moran = Moran(y, w) # plot fig, _ = _moran_global_scatterplot(moran) plt.close(fig) # customize fig, _ = _moran_global_scatterplot(moran, zstandard=False, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_lisa_cluster(): link = examples.get_path("columbus.shp") df = gpd.read_file(link) y = df["HOVAL"].values w = Queen.from_dataframe(df) w.transform = "r" moran_loc = esda.moran.Moran_Local(y, w) TOOLS = "tap,reset,help" fig = lisa_cluster(moran_loc, df, p=0.05, tools=TOOLS)
def test_lisa_cluster(): link = examples.get_path('columbus.shp') df = gpd.read_file(link) y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) fig, _ = lisa_cluster(moran_loc, df) plt.close(fig)
def test_plot_choropleth(): link = examples.get_path('columbus.shp') df = gpd.read_file(link) w = Queen.from_dataframe(df) w.transform = 'r' TOOLS = "tap,help" fig = plot_choropleth(df, 'HOVAL', title='columbus', reverse_colors=True, tools=TOOLS)
def test_moran_bv_scatterplot(): gdf = _test_data() x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Bivariate Moran moran_bv = Moran_BV(x, y, w) # plot fig, _ = _moran_bv_scatterplot(moran_bv) plt.close(fig) # customize plot fig, _ = _moran_bv_scatterplot(moran_bv, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_moran_bv_scatterplot(): link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Bivariate Moran moran_bv = Moran_BV(x, y, w) # plot fig, _ = _moran_bv_scatterplot(moran_bv) plt.close(fig) # customize plot fig, _ = _moran_bv_scatterplot(moran_bv, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_moran_loc_bv_scatterplot(): link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Bivariate Moran moran_loc_bv = Moran_Local_BV(x, y, w) # try with p value so points are colored fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv, p=0.05) plt.close(fig)
def test_plot_moran_bv(): # Load data and calculate weights link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Bivariate Moran moran_bv = Moran_BV(x, y, w) # plot fig, _ = plot_moran_bv(moran_bv) plt.close(fig) # customize plot fig, _ = plot_moran_bv(moran_bv, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_moran_loc_scatterplot(): link = examples.get_path('columbus.shp') df = gpd.read_file(link) y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) # try with p value so points are colored fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05, fitline_kwds=dict(color='#4393c3')) plt.close(fig)
def test_moran_loc_scatterplot(): columbus = examples.load_example('Columbus') link_to_data = columbus.get_path('columbus.shp') df = gpd.read_file(link_to_data) x = df['INC'].values y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) moran_bv = Moran_BV(x, y, w) # try without p value fig, _ = _moran_loc_scatterplot(moran_loc) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05, aspect_equal=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) # try with p value and zstandard=False fig, _ = _moran_loc_scatterplot(moran_loc, p=0.05, zstandard=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) # try without p value and zstandard=False fig, _ = _moran_loc_scatterplot(moran_loc, zstandard=False, fitline_kwds=dict(color='#4393c3')) plt.close(fig) assert_raises(ValueError, _moran_loc_scatterplot, moran_bv, p=0.5) assert_warns(UserWarning, _moran_loc_scatterplot, moran_loc, p=0.5, scatter_kwds=dict(c='#4393c3'))
def test_plot_spatial_weights(): # get data gdf = gpd.read_file(examples.get_path('43MUE250GC_SIR.shp')) gdf.head() # calculate weights weights = Queen.from_dataframe(gdf) # plot weights fig, _ = plot_spatial_weights(weights, gdf) plt.close(fig) # calculate nonplanar_joins wnp = libpysal.weights.util.nonplanar_neighbors(weights, gdf) # plot new joins fig2, _ = plot_spatial_weights(wnp, gdf) plt.close(fig2) #customize fig3, _ = plot_spatial_weights(wnp, gdf, nonplanar_edge_kws=dict(color='#4393c3')) plt.close(fig3)
def _data_generation(): # get csv and shp shp_link = examples.get_path('us48.shp') df = gpd.read_file(shp_link) income_table = pd.read_csv(examples.get_path("usjoin.csv")) # calculate relative values for year in range(1969, 2010): income_table[str(year) + '_rel'] = ( income_table[str(year)] / income_table[str(year)].mean()) # merge gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name') # retrieve spatial weights and data for two points in time w = Queen.from_dataframe(gdf) w.transform = 'r' y1 = gdf['1969_rel'].values y2 = gdf['2000_rel'].values #calculate rose Object Y = np.array([y1, y2]).T rose = Rose(Y, w, k=5) return gdf, y1, rose
def test_moran_scatterplot(): gdf = _test_data() x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate `esda.moran` Objects moran = Moran(y, w) moran_bv = Moran_BV(y, x, w) moran_loc = Moran_Local(y, w) moran_loc_bv = Moran_Local_BV(y, x, w) # try with p value so points are colored or warnings apply fig, _ = moran_scatterplot(moran, p=0.05, aspect_equal=False) plt.close(fig) fig, _ = moran_scatterplot(moran_loc, p=0.05) plt.close(fig) fig, _ = moran_scatterplot(moran_bv, p=0.05) plt.close(fig) fig, _ = moran_scatterplot(moran_loc_bv, p=0.05) plt.close(fig)
def test_moran_loc_bv_scatterplot(): gdf = _test_data() x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate Univariate and Bivariate Moran moran_loc = Moran_Local(y, w) moran_loc_bv = Moran_Local_BV(x, y, w) # try with p value so points are colored fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv) plt.close(fig) # try with p value and different figure size fig, _ = _moran_loc_bv_scatterplot(moran_loc_bv, p=0.05, aspect_equal=False) plt.close(fig) raises(ValueError, _moran_loc_bv_scatterplot, moran_loc, p=0.5) warns(UserWarning, _moran_loc_bv_scatterplot, moran_loc_bv, p=0.5, scatter_kwds=dict(c='r'))
def test_moran_scatterplot(): link_to_data = examples.get_path('Guerry.shp') gdf = gpd.read_file(link_to_data) x = gdf['Suicids'].values y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' # Calculate `esda.moran` Objects moran = Moran(y, w) moran_bv = Moran_BV(y, x, w) moran_loc = Moran_Local(y, w) moran_loc_bv = Moran_Local_BV(y, x, w) # try with p value so points are colored or warnings apply fig, _ = moran_scatterplot(moran, p=0.05) plt.close(fig) fig, _ = moran_scatterplot(moran_loc, p=0.05) plt.close(fig) fig, _ = moran_scatterplot(moran_bv, p=0.05) plt.close(fig) fig, _ = moran_scatterplot(moran_loc_bv, p=0.05) plt.close(fig)
def get_neighbors_dict(self): neighborhoods = gpd.read_file("data/chicago/neighborhoods/geo_export_265e79d9-e1f1-47fc-856d-1c753cbbe120.shp") weights = Queen.from_dataframe(neighborhoods) # Try with Rook later wnp = libpysal.weights.util.nonplanar_neighbors(weights, neighborhoods) wdf = weights.to_adjlist(remove_symmetric=True) list_edges = [] dict_idx_edges = {} for rowid, row in wdf.iterrows(): tidx1 = int(row["focal"]) tidx2 = int(row["neighbor"]) idx1 = np.minimum(tidx1, tidx2) idx2 = np.maximum(tidx1, tidx2) list_edges.append((idx1, idx2)) dict_idx_edges[idx1] = dict_idx_edges.get(idx1, []) + [idx2] dict_idx_edges[idx2] = dict_idx_edges.get(idx2, []) + [idx1] return dict_idx_edges
def test_plot_local_autocorrelation(): link = examples.get_path('columbus.shp') df = gpd.read_file(link) y = df['HOVAL'].values w = Queen.from_dataframe(df) w.transform = 'r' moran_loc = Moran_Local(y, w) fig, _ = plot_local_autocorrelation(moran_loc, df, 'HOVAL', p=0.05) plt.close(fig) # also test with quadrant and mask fig, _ = plot_local_autocorrelation(moran_loc, df, 'HOVAL', p=0.05, region_column='POLYID', mask=['1', '2', '3'], quadrant=1) plt.close(fig)
def _data_generation(): from giddy.directional import Rose # get csv and shp shp_link = examples.get_path("us48.shp") df = gpd.read_file(shp_link) income_table = pd.read_csv(examples.get_path("usjoin.csv")) # calculate relative values for year in range(1969, 2010): income_table[str(year) + "_rel"] = (income_table[str(year)] / income_table[str(year)].mean()) # merge gdf = df.merge(income_table, left_on="STATE_NAME", right_on="Name") # retrieve spatial weights and data for two points in time w = Queen.from_dataframe(gdf) w.transform = "r" y1 = gdf["1969_rel"].values y2 = gdf["2000_rel"].values # calculate rose Object Y = np.array([y1, y2]).T rose = Rose(Y, w, k=5) return gdf, y1, rose
# -*- coding: utf-8 -*- # @Time : 2019/3/29 14:35 # @Author : Crsboom # @Email : [email protected] # @File : pysal_test.py # @Software: PyCharm from libpysal.weights.contiguity import Queen import libpysal from libpysal import examples import matplotlib.pyplot as plt import geopandas as gpd import random from pysal.viz.splot.libpysal import plot_spatial_weights gdf = gpd.read_file(r'C:\Users\RSGG\Desktop\123\city\Anhui.shp') gdf.head() weights = Queen.from_dataframe(gdf) plot_spatial_weights(weights, gdf) #plt.show() x = [random.randint(0, 100) for i in range(17)] gdf['test'] = x fig, ax = plt.subplots(figsize=(12, 10), subplot_kw={'aspect': 'equal'}) gdf.plot(column='test', scheme='Quantiles', k=5, cmap='GnBu', legend=True, ax=ax)