def _moran_loc_from_rose_calc(rose): """ Calculate esda.moran.Moran_Local values from pysal.explore.giddy.rose object """ old_state = np.random.get_state() moran_locy = Moran_Local(rose.Y[:, 0], rose.w) np.random.set_state(old_state) moran_locx = Moran_Local(rose.Y[:, 1], rose.w) np.random.set_state(old_state) return moran_locy, moran_locx
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 get_lisa(gdf, indicator, w): ''' This function takes a year and a variable and returns the all the quadrants for local moran analysis ... Args: ''' gdf = gdf.copy() quadrant_labels = {1: 'HH', 2: 'HL', 3: 'LL', 4: 'LH' } moran = Moran(gdf[indicator].values, w) moran_loc = Moran_Local(gdf[indicator].values, w, transformation="r") significant = pd.Series(moran_loc.p_sim < 0.05) quadrant = pd.Series(moran_loc.q) quadrant = quadrant.replace(quadrant_labels) quadrant[~significant] = 'Non-significant' gdf['lisa_cluster'] = quadrant return gdf
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_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(): link = examples.get_path('columbus.shp') df = gpd.read_file(link) 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_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, 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 get_lisa_legacy(path, indicator, w, grid_id): ''' This function takes a year and a variable and returns the all the quadrants for local moran analysis ... Args: ''' carto = gpd.read_file(path) moran = Moran(carto[indicator].values, w) moran_loc = Moran_Local(carto[indicator].values, w, transformation="r", permutations=99) carto['significant'] = moran_loc.p_sim < 0.05 carto['quadrant'] = moran_loc.q return {'gdf': carto, 'moran': moran, 'value': moran.I, 'significance': moran.p_sim, 'local': moran_loc}
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)
# Imports import matplotlib.pyplot as plt from pysal.lib.weights.contiguity import Queen from pysal.lib import examples import geopandas as gpd from pysal.explore.esda.moran import Moran_Local from pysal.viz.splot.esda import plot_local_autocorrelation # Data preparation and analysis link = examples.get_path('Guerry.shp') gdf = gpd.read_file(link) y = gdf['Donatns'].values w = Queen.from_dataframe(gdf) w.transform = 'r' moran_loc = Moran_Local(y, w) # Plotting with quadrant mask and region mask fig = plot_local_autocorrelation(moran_loc, gdf, 'Donatns', p=0.05, region_column='Dprtmnt', mask=['Ain'], quadrant=1) plt.show()