def test_lisa_cluster(): df = _test_data_columbus() moran_loc = _test_calc_moran_loc(df) fig, _ = lisa_cluster(moran_loc, df) plt.close(fig) # test LineStrings df_line = _test_LineString() moran_loc = _test_calc_moran_loc(df_line, var="Length") fig, _ = lisa_cluster(moran_loc, df_line) plt.close(fig)
def moran_inverse(file): # Read in shapefile df = file # df = gpd.read_file("C:\zoovision\data\Region1.shp") # print(df.dtypes) y = df['ind_100t'] # Calculate weight # First calculate minimum threshold distance to nearest neightbor thresh = ps.min_threshold_dist_from_shapefile( "C:\zoovision\data\Region1.shp") print(thresh) # thresh = 1.1 # weight based on fixed distance,(0 if not in threshold) w = ps.weights.DistanceBand.from_shapefile("C:\zoovision\data\Region1.shp", threshold=thresh, binary=False) # weight based on non fixed distance,(0 if not in threshold) # w = ps.weights.DistanceBand.from_shapefile("C:\zoovision\data\Region1.shp", binary=False) # transform r= standardize moran_loc = ps.Moran_Local(y, w, transformation="r", permutations=999) print(moran_loc.p_sim) fig, ax = lisa_cluster(moran_loc, df, p=0.05, figsize=(15, 10)) ax.set_title( "Local Indicators of Spatial Association ", fontsize=35) # plot_moran(moran_loc, zstandard=True, figsize=(10, 4))
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)
# Global spatial autocorrelation y = sa_df[week] moran = Moran(y, w) # Local spatial autocorrelation m_local = Moran_Local(y, w) lisa = m_local.Is # set CRS sa_df = sa_df.to_crs("EPSG:3857") #Plot map fig, ax = plt.subplots(figsize=(9,9)) lisa_cluster(m_local, sa_df, p=0.05, figsize = (9,9),ax=ax) description = 'Weekly Covid-19 Spatial Autocorrelation' info_text = 'Hot- and coldspots indicates clusters of high and low infection rates. \nDonuts are regions with low infection-rates sorrounded by areas with high infection-rates. \nDiamonds are regions with high infection-rates sorrounded by regions with low infection-rates' ax.set_title(str(week_name), fontdict={'fontsize': 22}, loc='left') ax.annotate(description, xy=(0.325, 0.140), size=14, xycoords='figure fraction') ax.annotate(info_text, xy=(0.325, 0.090), size=8, xycoords='figure fraction') #Legend items item1 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="red", markersize=12) item2 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="orange", markersize=12) item3 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="lightblue", markersize=12) item4 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="blue", markersize=12) plt.legend((item1, item2, item3, item4), ('Hotspot', 'Diamonds','Donuts','Coldspot')).set_bbox_to_anchor((0.240, 0.086)) #(x,y) plt.savefig(filepath, dpi=150)
def moran_gen(file): # Read in shapefile df = file # df = gpd.read_file(file) # print(df.dtypes) y = df['ind_100t'] # Calculate weight # First calculate minimum threshold distance to nearest neightbor thresh = ps.min_threshold_dist_from_shapefile( "C:\zoovision\data\Region1.shp") # thresh = 1 # print(thresh) # weight based on fixed distance, for binary(0 or 1 if within threshold) # arcgis_swm = ps.open('C:\zoovision\data\weightfiles\week1test.swm', 'r') # w = arcgis_swm.read() # arcgis_swm.close() # e = open('C:\zoovision\data\Region1_count.txt') # x = e.readlines() # print(x.head()) # gwt = ps.open('C:\zoovision\weights.gwt', 'r') # w = gwt.read() # gwt.close() # w = ps.open('C:\zoovision\data\Region1_count.txt', 'r', 'Region1_count').read() testfile = ps.open('C:\zoovision\data\Region1_count.txt', 'r', 'arcgis_text') testfile = ps.open('C:\zoovision\data\Region1_count.txt', 'r', 'arcgis_text') w = testfile.read() testfile.close() # testfile = ps.open('C:\zoovision\data\weightfiles\Region1_genweights.swm', 'r') # w = testfile.read() testfile.close() w.n # f = tempfile.NamedTemporaryFile(suffix='.txt') # fname = f.name # f.close() # o = ps.open(fname, 'w', 'Region1_count') # o.write(w) # o.close() # wnew = ps.open(fname, 'r', 'Region1_count').read() # wnew.pct_nonzero == w.pct_nonzero # os.remove(fname) # arcgis_txt.close() # w = ps.queen_from_shapefile("C:\zoovision\data\Region1.shp") # w = ps.weights.DistanceBand.from_shapefile("C:\zoovision\data\Region1.shp", threshold=thresh, binary=False) # print(tuple(w1)) # f = ps.open(ps.examples.get_path("stl_hom.txt")) # y = np.array(f.by_col['HR8893']) # w = ps.open(ps.examples.get_path("stl.gal")).read() # np.random.seed(12345) # moran_loc = ps.Moran_Local(y, w) # print(tuple(w)) # w2 = ps.lat2W(6, 4) # w = ps.w_union(w1, w2) # w = w1.multiply(w2) moran_loc = Moran_Local(y, w, transformation='r', permutations=999) # moran_loc = ps.Moran_Local(y, w, permutations=999) fig, ax = plt.subplots(figsize=(15, 10)) fig, ax = lisa_cluster(moran_loc, df, p=0.05, figsize=(15, 10)) ax.set_title( "Local Indicators of Spatial Association ", fontsize=35) # plot_moran(moran_loc, zstandard=True, figsize=(10, 4))
def moran_map(moran, dataset): chart = lisa_cluster(moran, dataset, p=0.05, figsize=(9,9)) fig = chart[0] st.pyplot(fig)