def cal_localmoran(self, coors, value, K): kd = pysal.cg.kdtree.KDTree(np.array(coors)) # wnn2 = pysal.KNN(kd, 2) # weights K = int(K) weights = pysal.weights.Distance.KNN(kd, K) #local moran localm = pysal.Moran_Local(value, weights) std_val = np.std(value, ddof=1) avg_val = np.average(value) x_val = [] #标准化的观测值 for i in range(len(value)): guance_val = (value[i] - avg_val) / std_val x_val.append(guance_val) #neighbors neib = weights.neighbors y_val = [] for i in range(len(neib)): kneib = neib[i] kval = 0 for j in kneib: kval = kval + x_val[j] / K y_val.append(kval) sc.compute_initial_figure(x_val, y_val)
def compute(self): if not self.output: self.output = VectorFileIO(name='result', uri=self.get_outpath()) first_df = self.inputs[0].read() col = self.var_col adjust_by_col = self.adjust_by_col # filter out null fields or else weight functions won't work keep = first_df[col].notnull() filtered_df = first_df[keep].reset_index() # get Local Moran's I f = np.array(filtered_df[col]) w = wt.gpd_contiguity(filtered_df) if adjust_by_col: adjust_by = np.array(filtered_df[adjust_by_col]) lm = pysal.esda.moran.Moran_Local_Rate(e=f, b=adjust_by, w=w, permutations=9999) else: lm = pysal.Moran_Local(y=f, w=w, permutations=9999) sig = lm.p_sim < 0.05 filtered_df['lm_sig'] = sig filtered_df['lm_p_sim'] = lm.p_sim filtered_df['lm_q'] = lm.q filtered_df['lm_Is'] = lm.Is self.output.data = filtered_df self.output.write() logger.debug(self.output)
def calcLocalMoranI(self, mx, yVal): lm = pysal.Moran_Local(yVal, mx) LMI = lm.Is pVal = lm.p_sim Zi = lm.z_sim self.LocalMoranIgeojson = self.change2geojson(self.coords, LMI, Zi, pVal)
def precomputeLISA(self): """ """ n = len(self.data_sel_keys) progress_dlg = wx.ProgressDialog( "Progress", "Pre-computing LISAs with 499 permutations... ", maximum=n, parent=self, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE) progress_dlg.CenterOnScreen() moran_locals = [] try: # C++ DLL call from stars.core.LISAWrapper import call_lisa for i, data in enumerate(self.data_sel_values): progress_dlg.Update(i + 1) localMoran, sigLocalMoran, sigFlag, clusterFlag = call_lisa( data, str(self.weight_file), 499) ml = [localMoran, sigLocalMoran, sigFlag, clusterFlag] moran_locals.append(ml) except: # old for pysal for i, data in enumerate(self.data_sel_values): progress_dlg.Update(i + 1) localMoran = pysal.Moran_Local(data, self.weight, transformation="r", permutations=499) ml = [localMoran.Is, localMoran.p_sim, [], localMoran.q] moran_locals.append(ml) progress_dlg.Destroy() return moran_locals
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 lincs(wed, y, permutations=999, segment=False): if segment: # segment wed and y # get new wed and extract new y raise NotImplementedError w = networkw.w_links(wed) # lisa from PySAL lisa = ps.Moran_Local(y, w, permutations=permutations) return lisa
def __init__(self, w, z, y, floor, floor_variable, initial=100): lis = pysal.Moran_Local(y, w) ids = np.argsort(lis.Is) ids = ids[list(range(w.n - 1, -1, -1))] ids = ids.tolist() mp = Maxp.__init__(self, w, z, floor=floor, floor_variable=floor_variable, initial=initial, seeds=ids)
def process_LISA(parent, data, weight_path): """ """ progress_dlg = wx.ProgressDialog( "Progress", "Computing LISA with 499 permutations... ", maximum=2, parent=parent, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE) progress_dlg.CenterOnScreen() progress_dlg.Update(1) id_groups = [[] for i in range(6)] try: # call Geoda LISA DLL first from stars.core.LISAWrapper import call_lisa localMoran, sigLocalMoran, sigFlag, clusterFlag = call_lisa( data, str(weight_path), 499) # prepare drawing map # 0 not significant, 1 HH, 2 LL, 3 LH, 4 HL, 5 Neighborless lm_moran = np.array(localMoran) lm_p_sim = np.array(sigLocalMoran) lm_sig = np.array(sigFlag) lm_q = np.array(clusterFlag) for i, sig in enumerate(lm_sig): if sig > 0: id_groups[lm_q[i]].append(i) else: id_groups[0].append(i) except: # if DLL call is failed, try pysal LISA instead w = pysal.open(weight_path).read() lm = pysal.Moran_Local(data, w, transformation="r", permutations=499) for i, sig in enumerate(lm.p_sim): if sig < parent.siglevel: id_groups[lm.q[i]].append(i) else: id_groups[0].append(i) progress_dlg.Update(2) progress_dlg.Destroy() return id_groups
def lisa_mapa(variavel, shapefile, p_thres=0.05, **kws): w = ps.queen_from_shapefile(shapefile) lisa = ps.Moran_Local(variavel, w) fig = plt.figure(figsize=(9, 7)) shp = ps.open(shapefile) base = maps.map_poly_shp(shp) base = maps.base_lisa_cluster(base, lisa, p_thres=p_thres) base.set_edgecolor('1') base.set_linewidth(0.1) ax = maps.setup_ax([base], [shp.bbox]) boxes, labels = maps.lisa_legend_components(lisa, p_thres=p_thres) plt.legend(boxes, labels, fancybox=True, **kws) plt.show()
def main(): work_dir = r"D:\hcho_change\ROI\COI" os.chdir(work_dir) os.chdir("aod") files = os.listdir(os.getcwd()) os.chdir("..") # name = files[-1] for name in files: os.chdir("aod") print name f = gdal.Open(name) cols = f.RasterXSize rows = f.RasterYSize gt = f.GetGeoTransform() proj = f.GetProjection() array = f.ReadAsArray(0, 0, cols, rows) #预处理 array[np.where(~np.isfinite(array) == True)] = -1 array[np.where(array < 0)] = np.float("nan") #------ y, x = np.where(np.isfinite(array)) lats = np.array([gt[3] + gt[5] * i for i in y]) lons = np.array([gt[0] + gt[1] * i for i in x]) data = array[y, x] coor = np.vstack((lons, lats)).T w = pysal.knnW(coor, k=3) mr = pysal.Moran_Local(data, w).p_sim array[np.where(np.isfinite(array))] = mr array[np.where(~np.isfinite(array))] = 0. dr = gdal.GetDriverByName("GTiff") os.chdir("..") os.chdir("out") name = name[0:-4] + "moran.tif" ds = dr.Create(name, cols, rows, 1, gdal.GDT_Float32) ds.SetGeoTransform(gt) ds.SetProjection(proj) ds.GetRasterBand(1).WriteArray(array) os.chdir("..") print name + " has done."
def moran_local(t, attr, significance, num_ngbrs, permutations, geom_column, id_col, w_type): """ Moran's I implementation for PL/Python Andy Eschbacher """ # TODO: ensure that the significance output can be smaller that 1e-3 (0.001) # TODO: make a wishlist of output features (zscores, pvalues, raw local lisa, what else?) plpy.notice('** Constructing query') # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors qvals = { "id_col": id_col, "attr1": attr, "geom_col": geom_column, "table": t, "num_ngbrs": num_ngbrs } q = get_query(w_type, qvals) try: r = plpy.execute(q) plpy.notice('** Query returned with %d rows' % len(r)) except plpy.SPIError: plpy.notice('** Query failed: "%s"' % q) plpy.notice('** Exiting function') return zip([None], [None], [None], [None]) y = get_attributes(r, 1) w = get_weight(r, w_type) # calculate LISA values lisa = ps.Moran_Local(y, w) # find units of significance lisa_sig = lisa_sig_vals(lisa.p_sim, lisa.q, significance) plpy.notice('** Finished calculations') return zip(lisa.Is, lisa_sig, lisa.p_sim, w.id_order)
def _local_autocorrelation_method(self, method, star): if method.lower() == 'moran': method_fct = lambda *args, **kwargs: pysal.Moran_Local( *args, **kwargs) def get_stats(_object): return _object.Is elif method.lower() == 'getisord': method_fct = lambda *args, **kwargs: pysal.esda.getisord.G_Local( *args, **kwargs, star=True) def get_stats(_object): return _object.Gs else: raise ValueError( "no corresponding local autocorrelation function to {}".format( method)) return method_fct, get_stats
temp['Area'] = area temp['Category'] = item mi = pysal.Moran(data[area][item], weights[area]) temp['MI'] = mi.I temp['p_val'] = mi.p_sim moran = moran.append(temp) print(area, item) # LISA plot check = cp.copy(data[area]) check[item + '_std'] = (check[item] - check[item].mean()) / check[item].std() check['w_' + item + '_std'] = pysal.weights.lag_spatial( weights[area], check[item + '_std']) lisa = pysal.Moran_Local(check[item], weights[area]) # Break observations into significant or not check['significant'] = lisa.p_sim < 0.05 # Store the quadrant they belong to check['quadrant'] = lisa.q check.loc[check['significant'] == False, 'quadrant'] = 0 check['quadrant2'] = check['quadrant'].map({ 1: 'HH', 2: 'LH', 3: 'LL', 4: 'HL', 0: 'NS' }) check['col'] = check['quadrant'].map({ 1: 'red', 2: '#83cef4',
# In[13]: shp_link = ps.examples.get_path('pitts_review_census1.shp') # In[14]: print 'Reading from ', shp_link # In[16]: TotPop = np.array(ps.open(shp_link.replace('.shp', '.dbf')).by_col('TotPop')) # In[17]: w = ps.queen_from_shapefile(shp_link) lisa = ps.Moran_Local(TotPop, w, permutations=9999) # In[7]: maps.plot_lisa_cluster(shp_link, lisa, figsize=(9, 6)) # In[11]: orig_crs = ccrs.PlateCarree() projection = ccrs.LambertConformal() p_thres = 0.1 shp = ps.open(shp_link) polys = maps.map_poly_shp(shp) polys = maps.base_lisa_cluster(polys, lisa, p_thres=p_thres) polys.set_edgecolor('1')
# Global Moran's I mi = ps.Moran(y, w) mi.I mi.EI mi.p_norm # Geary's C gc = ps.Geary(y, w) gc.C gc.EC gc.z_norm # Local Moran's I lm = ps.Moran_Local(y, w) sigs = lm.p_sim for i in range(lm.n): if sigs[i] > 0.1: sigs[i] = 0 elif sigs[i] > 0.05: sigs[i] = 1 elif sigs[i] > 0.01: sigs[i] = 2 elif sigs[i] > 0.001: sigs[i] = 3 elif sigs[i] > 0.0: sigs[i] = 4 # plot significant autocorrelation maps.plot_choropleth(shp_link, np.array(sigs), type='equal_interval',
def processAlgorithm(self, progress): field = self.getParameterValue(self.FIELD) field = field[0:10] # try to handle Shapefile field length limit filename = self.getParameterValue(self.INPUT) layer = dataobjects.getObjectFromUri(filename) filename = dataobjects.exportVectorLayer(layer) provider = layer.dataProvider() fields = provider.fields() fields.append(QgsField('MORANS_P', QVariant.Double)) fields.append(QgsField('MORANS_Z', QVariant.Double)) fields.append(QgsField('MORANS_Q', QVariant.Int)) # quadrant fields.append(QgsField('MORANS_Q_S', QVariant.Int)) # significant quadrant fields.append(QgsField('MORANS_I', QVariant.Double)) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( fields, provider.geometryType(), layer.crs()) contiguity = self.getParameterValue(self.CONTIGUITY) if self.CONTIGUITY_OPTIONS[contiguity] == 'queen': print 'INFO: Local Moran\'s using queen contiguity' w = pysal.queen_from_shapefile(filename) elif self.CONTIGUITY_OPTIONS[contiguity] == 'rook': print 'INFO: Local Moran\'s using rook contiguity' w = pysal.rook_from_shapefile(filename) significance_level = self.getParameterValue(self.SIGNIFICANCE_LEVEL) if self.SIGNIFICANCE_OPTIONS[significance_level] == '90%': max_p = 0.10 elif self.SIGNIFICANCE_OPTIONS[significance_level] == '95%': max_p = 0.05 elif self.SIGNIFICANCE_OPTIONS[significance_level] == '99%': max_p = 0.01 print 'INFO: significance level ' + self.SIGNIFICANCE_OPTIONS[ significance_level] f = pysal.open( pysal.examples.get_path(filename.replace('.shp', '.dbf'))) y = np.array(f.by_col[str(field)]) lm = pysal.Moran_Local(y, w, transformation="r", permutations=999) # http://pysal.readthedocs.org/en/latest/library/esda/moran.html?highlight=local%20moran#pysal.esda.moran.Moran_Local # values indicate quadrat location 1 HH, 2 LH, 3 LL, 4 HL # http://www.biomedware.com/files/documentation/spacestat/Statistics/LM/Results/Interpreting_univariate_Local_Moran_statistics.htm # category - scatter plot quadrant - autocorrelation - interpretation # high-high - upper right (red) - positive - Cluster - "I'm high and my neighbors are high." # high-low - lower right (pink) - negative - Outlier - "I'm a high outlier among low neighbors." # low-low - lower left (med. blue) - positive - Cluster - "I'm low and my neighbors are low." # low-high - upper left (light blue) - negative - Outlier - "I'm a low outlier among high neighbors." # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/What_is_a_z_score_What_is_a_p_value/005p00000006000000/ # z-score (Standard Deviations) | p-value (Probability) | Confidence level # < -1.65 or > +1.65 | < 0.10 | 90% # < -1.96 or > +1.96 | < 0.05 | 95% # < -2.58 or > +2.58 | < 0.01 | 99% self.setOutputValue(self.P_SIM, str(lm.p_sim)) sig_q = lm.q * (lm.p_sim <= max_p) outFeat = QgsFeature() i = 0 for inFeat in processing.features(layer): inGeom = inFeat.geometry() outFeat.setGeometry(inGeom) attrs = inFeat.attributes() attrs.append(float(lm.p_sim[i])) attrs.append(float(lm.z_sim[i])) attrs.append(int(lm.q[i])) attrs.append(int(sig_q[i])) attrs.append(float(lm.Is[i])) outFeat.setAttributes(attrs) writer.addFeature(outFeat) i += 1 del writer
return shapefile def spatialcorrelation(data,column,filename): """ Performs a spatial correlation on the column and exports an html map of hot/cold spots :param data: Shapefile with data to be spatially correlated apppended :param column: Name of the parameter that map is made of :param filename: Output filename :return: Saves an html map in the static/Maps folder """ logger.info('Create ' + filename + ' map') W = ps.weights.Queen.from_dataframe(data) W.transform = 'r' moran = ps.Moran_Local(column.values, W, permutations=9999) sig = moran.p_sim < 0.05 hotspots = moran.q==1 * sig coldspots = moran.q==3 * sig hotcold = hotspots*1 + coldspots*2 if filename=='blank': hotcold = hotspots*0 hc_df = pd.DataFrame(hotcold) mapdata = data.join(hc_df) mapdata.rename(columns={0: 'type'}, inplace=True) style = pd.DataFrame({'type': [0,1,2], 'style': [ {'fillColor': '#e3dfd6', 'weight': .15, 'color': 'black'}, {'fillColor': '#dd3232', 'fillOpacity' : .55, 'weight': .25, 'color': 'black'}, {'fillColor': '#a2d0cf', 'fillOpacity' : .55, 'weight': .25, 'color': 'black'},
fields.append(QgsField('MORANS_Q', QVariant.Int)) fields.append(QgsField('MORANS_I', QVariant.Double)) fields.append(QgsField('MORANS_C', QVariant.Double)) writer = VectorWriter(morans_output, None, fields, provider.geometryType(), layer.crs()) if contiguity == 0: # queen print 'INFO: Local Moran\'s using queen contiguity' w = pysal.queen_from_shapefile(input) else: # 1 for rook print 'INFO: Local Moran\'s using rook contiguity' w = pysal.rook_from_shapefile(input) f = pysal.open(pysal.examples.get_path(input.replace('.shp', '.dbf'))) y = np.array(f.by_col[str(field)]) lm = pysal.Moran_Local(y, w, transformation="r", permutations=999) # http://pysal.readthedocs.org/en/latest/library/esda/moran.html?highlight=local%20moran#pysal.esda.moran.Moran_Local # values indicate quadrat location 1 HH, 2 LH, 3 LL, 4 HL # http://www.biomedware.com/files/documentation/spacestat/Statistics/LM/Results/Interpreting_univariate_Local_Moran_statistics.htm # category - scatter plot quadrant - autocorrelation - interpretation # high-high - upper right (red) - positive - Cluster - "I'm high and my neighbors are high." # high-low - lower right (pink) - negative - Outlier - "I'm a high outlier among low neighbors." # low-low - lower left (med. blue) - positive - Cluster - "I'm low and my neighbors are low." # low-high - upper left (light blue) - negative - Outlier - "I'm a low outlier among high neighbors." # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/What_is_a_z_score_What_is_a_p_value/005p00000006000000/ # z-score (Standard Deviations) | p-value (Probability) | Confidence level # < -1.65 or > +1.65 | < 0.10 | 90% # < -1.96 or > +1.96 | < 0.05 | 95%
from processing.core.VectorWriter import VectorWriter from qgis.core import * from PyQt4.QtCore import * layer = processing.getObject(input) provider = layer.dataProvider() fields = provider.fields() fields.append(QgsField('MORANS_P', QVariant.Double)) fields.append(QgsField('MORANS_Q', QVariant.Int)) writer = VectorWriter(morans_output, None, fields, provider.geometryType(), layer.crs()) w = pysal.rook_from_shapefile(input) f = pysal.open(pysal.examples.get_path(input.replace('.shp', '.dbf'))) y = np.array(f.by_col[str(field)]) lm = pysal.Moran_Local(y, w) print lm.p_sim print lm.q # github.com/pysal/pysal/blob/master/pysal/esda/moran.py # values indicate quadrat location 1 HH, 2 LH, 3 LL, 4 HL # http://www.biomedware.com/files/documentation/spacestat/Statistics/LM/Results/Interpreting_univariate_Local_Moran_statistics.htm # category - scatter plot quadrant - autocorrelation - interpretation # high-high - upper right (red) - positive - Cluster - "I'm high and my neighbors are high." # high-low - lower right (pink) - negative - Outlier - "I'm a high outlier among low neighbors." # low-low - lower left (med. blue) - positive - Cluster - "I'm low and my neighbors are low." # low-high - upper left (light blue) - negative - Outlier - "I'm a low outlier among high neighbors." outFeat = QgsFeature() i = 0 for inFeat in processing.features(layer): inGeom = inFeat.geometry()
def moranLocal(observations, w): m = pysal.Moran_Local(observations, w) return m.p_sim.mean(), m.Is, m.EI_sim
FROM msa_region p left join info_usa_alcohol_outlets a on (p.geoid = a.geo2000)""".replace( "\n", "") % ( m, a, a, ) print(counter, query) dataFrame = geopandas.GeoDataFrame.from_postgis(query, conn, geom_col='geometry') #Generate once per loop if counter == 0: weights = pysal.weights.Queen.from_dataframe(dataFrame) values = dataFrame['record'] lisa = pysal.Moran_Local(values, weights, permutations=999) #Attribute names get specified lisaDF = geopandas.pd.DataFrame({ a: values, 'lisa_%s' % (a, ): lisa.Is, 'p_value_%s' % (a, ): lisa.p_sim }) lisaDataFrames.append(lisaDF) lisaDataFrames.insert(0, dataFrame) regionDataFrame = geopandas.pd.concat(lisaDataFrames, axis=1) regionFrames.append(regionDataFrame) conn.close()
# %pip install matplotlib inline import matplotlib.pyplot as plt import pysal as ps import numpy as np import geopandas as gpd from pysal.contrib.viz import mapping as maps shp_link = ps.examples.get_path('NAT.shp') tx = gpd.read_file(shp_link) print('Reading from ', shp_link) hr90 = np.array(ps.open(shp_link.replace('.shp', '.dbf')).by_col('HR90')) w = ps.queen_from_shapefile(shp_link) lisa = ps.Moran_Local(hr90, w, permutations=9999) p_thres = 0.01 f, ax = plt.subplots(1, figsize=(9, 9)) # boxes, labels = maps.lisa_legend_components(lisa, p_thres=p_thres) # plt.legend(boxes, labels, loc='lower left', fancybox=True) tx.assign(cl=lisa).plot(column='cl', categorical=True, \ k=10, cmap='OrRd', linewidth=0.1, ax=ax, \ edgecolor='white', legend=True) ax.set_axis_off() plt.title('HR90 | LISA clusters | P-value = %.2f' % p_thres) plt.show()
def compute(self, vlayer, tfield, idvar, matType, progressBar): vlayer = qgis.utils.iface.activeLayer() idvar = self.idVariable.currentText() # print type(idvar) tfield = self.inField.currentText() # print type(tfield) provider = vlayer.dataProvider() allAttrs = provider.attributeIndexes() caps = vlayer.dataProvider().capabilities() start = 15.00 if caps & QgsVectorDataProvider.AddAttributes: if matType == "Rook": TestField = tfield[:7] + "_r" else: TestField = tfield[:7] + "_q" res = vlayer.dataProvider().addAttributes( [QgsField(TestField, QVariant.Double)]) wp = str(self.dic[str(self.inShape.currentText())]) if matType == "Rook": w = py.rook_from_shapefile(wp, idVariable=unicode(idvar)) else: w = py.queen_from_shapefile(wp, idVariable=unicode(idvar)) w1 = wp[:-3] + "dbf" db = py.open(w1) y = np.array(db.by_col[unicode(tfield)]) np.random.seed(12345) lm = py.Moran_Local(y, w) l = lm.p_sim # Replace insignificant values with the number 5: for i in range(len(l)): if l[i] > 0.05: l[i] = 5 # Replace the significant values with their quadrant: for i in range(len(l)): if l[i] <= 0.05: l[i] = lm.q[i] a = range(len(l)) l1 = np.array(l).flatten().tolist() d = dict(zip(a, l1)) fieldList = ftools_utils.getFieldList(vlayer) print len(fieldList) n = len(fieldList) - 1 add = 85.00 / len(l) print add vlayer.startEditing() for i in a: fid = int(i) # print fid vlayer.changeAttributeValue(fid, n, d[i]) start = start + add print start # print d[i] #index of the new added field vlayer.commitChanges() self.SAresult.setText( "Significance values have been added to your attribute table!") start = start + 1 progressBar.setValue(start)
# Initial LISA mapping setup orig_crs = ccrs.PlateCarree() projection = ccrs.LambertConformal() p_thres = 0.05 YEAR_LIST = ['1996', '2006', '2016'] YEAR_CODE = ['96', '06', '16'] EMP_LIST = ['Establishments', 'Employees', 'Employment Density', 'EmpCode'] EMP_CODE = ['Estab', 'Emp', 'EmpD', 'EmpCode'] print("Explanatory Statistics for Condos:") for i, yr in enumerate(YEAR_CODE): y = np.array(f.by_col['C{0}'.format(yr)]) mi = ps.Moran(y, w) lisa = ps.Moran_Local(y, w, permutations=9999) print("Global Moran I for {0}: {1:.5f}".format(YEAR_LIST[i], mi.I)) print("Moran I p-Value for {0}: {1:.5f}".format(YEAR_LIST[i], mi.p_norm)) polys = maps.map_poly_shp(shp) polys = maps.base_lisa_cluster(polys, lisa, p_thres=p_thres) polys.set_edgecolor('1') polys.set_linewidth(0.2) polys.set_transform(orig_crs) fig = plt.figure(figsize=(12, 8)) ax = plt.axes(projection=projection) extent = [shp.bbox[0], shp.bbox[2], shp.bbox[1], shp.bbox[3]] ax.set_extent(extent, crs=ccrs.PlateCarree()) ax.add_collection(polys) ax.outline_patch.set_visible(False)
file=output) if mir.p_z_sim < 0.05: print( 'Based on the z transformation of p value, the null hypothesis that responses are randomly distributed is rejected (p value<0.05).', file=output) else: print( 'Based on the z transformation of p value, the null hypothesis that responses are randomly distributed is not rejected (p value>0.05).', file=output) print(' ', file=output) # Local Indicators of Spatial Association (LISAs) for Moran’s I print('======= Local Morans I (LISA) - (Single Run) ========', file=output) lm = pysal.Moran_Local(response, w) print('Observed value for I: ', lm.Is, file=output) print("Estimated LISA values: ", lm.EI_sim, file=output) print('Calculated p value: ', lm.p_sim, file=output) lm = pysal.Moran_Local(response, w, permutations=9999) print(' ', file=output) print('======= Local Morans I (LISA) - (Permutations) ========', file=output) print("Number of LISA values (same as total number counties): ", len(lm.Is), file=output) print("Observed LISA values: ", lm.Is, file=output) print("Estimated LISA values: ", lm.EI_sim, file=output) print('Pseudo p-values for LISAs: ', lm.p_sim, file=output) print(' ', file=output)
data_df = eval(comboTable).copy() # data_df.drop(data_df.columns[0], axis=1, inplace = True) # needed for imported tables from csv data_df['FIPS'] = '1400000US' + data_df['FIPS'].astype(str) # this change is done to original table as well if not copy shp_df = pd.DataFrame(shape.records()) shp_df.columns = ['FIPS' if x == 1 else x for x in shp_df.columns] df = pd.merge(shp_df, data_df, how='left', on='FIPS') # http://pysal.readthedocs.io/en/latest/users/tutorials/autocorrelation.html#local-moran-s-i W = pysal.queen_from_shapefile(exportFolder + "tracts" + ".shp") # is shorter than census tables!!! string = ', '.join(str(e) for e in list(df.columns)) ent_cols = re.findall(r"RE\d\d|CE\d\d|IE\d\d", string) for col in ent_cols: y = np.array(df[col], dtype=float) y = np.nan_to_num(y) np.random.seed(12345) lm = pysal.Moran_Local(y, W) quadrants = [] i = 0 for val in lm.p_sim: if val < 0.05: quadrants.append(lm.q[i]) else: quadrants.append(0) i += 1 df[col + '_Moran'] = lm.Is df[col + '_Sig'] = lm.p_sim df[col + '_Quad'] = quadrants shape = shapefile.Reader(shp=shp, shx=shx, dbf=dbf) # needs to be reset w = shapefile.Writer(shape.shapeType)
xy = (((0, 0), (0, 0)), ((2, 1), (2, 1)), ((3, 1), (3, 1)), ((2, 5), (2, 5))) xy = np.array([[10, 30], [20, 20]]) markerobj = mpl.markers.MarkerStyle('o') path = markerobj.get_path().transformed( markerobj.get_transform()) scales = np.array([2, 2]) fig = plt.figure() ax = fig.add_subplot(111) pc = PathCollection((path,), scales, offsets=xy, \ facecolors='r', transOffset=mpl.transforms.IdentityTransform()) #pc.set_transform(mpl.transforms.IdentityTransform()) #_ = _add_axes2col(pc, [0, 0, 5, 5]) ax.add_collection(pc) fig.add_axes(ax) #ax = setup_ax([pc], ax) plt.show() ''' shp_link = ps.examples.get_path('columbus.shp') values = np.array( ps.open(ps.examples.get_path('columbus.dbf')).by_col('HOVAL')) w = ps.queen_from_shapefile(shp_link) lisa = ps.Moran_Local(values, w, permutations=999) fig1 = plot_lisa_cluster(shp_link, lisa) fig2 = plot_choropleth(shp_link, values, 'fisher_jenks') plt.show()
def act_on_msa(empShpOut_paths, thr=0.1, permutations=9999): ''' Perform operations required at the MSA level NOTE: besides returning `msa`, the method creates a shapefile and a .gal file for the MSA (if not present in `out_path`) and a shapefile with centers in `out_path` ... Arguments --------- msaEmpShp_path : tuple Parameters, including: * emp: DataFrame with MSA data * shp_path: None/str to shp with all tracts * out_path: str to out folder thr : float [Optional, default to 0.1] Significance level to consider center candidates permutations : int [Optional, default to 9999] Number of permutations to obtain pseudo-significance values Returns ------- msa : DataFrame Table with output information for tracts in msa. This includes: * dens_eb * lisa_i * lisa_p_sim * center_id ''' emp, shp_link, out_link = empShpOut_paths msa = emp['msa'].min() # get shape and W msa_shp_link = out_link + msa + '.shp' msa_gal_link = msa_shp_link.replace('.shp', '_queen.gal') try: fo = ps.open(msa_shp_link) fo.close() except: _ = clip_shp(shp_link, "GISJOIN", list(emp['GISJOIN'].values), \ msa_shp_link) try: w = ps.open(msa_gal_link).read() except: w = ps.queen_from_shapefile(msa_shp_link, "GISJOIN") fo = ps.open(msa_gal_link, 'w') fo.write(w) fo.close() print w.weights.keys()[:5] w.id_order = w.id_order print w.weights.keys()[:5] w.transform = 'R' print w.weights.keys()[:5] emp = emp.set_index('GISJOIN').reindex(ps.open(\ msa_shp_link.replace('.shp', '.dbf'))\ .by_col('GISJOIN'))\ .fillna(0) # get EB rate print w.weights.keys()[:5] eb = ps.esda.smoothing.Spatial_Empirical_Bayes(\ emp['emp'].values, emp['Shape_area'].values, w) emp['dens_eb'] = eb.r emp['dens_eb'] = emp['dens_eb'].fillna(0) #Avoid sliver problem # LISA lisa = ps.Moran_Local(emp['dens_eb'].values, w, permutations=permutations) lisa_pack = pd.DataFrame({'Is': lisa.Is, 'z_sim': lisa.z_sim, \ 'EI_sim': lisa.EI_sim, 'seI_sim': lisa.seI_sim, \ 'p_sim': lisa.p_sim, 'q': lisa.q}) lisa_pack = lisa_pack[['Is', 'z_sim', 'EI_sim', 'seI_sim', 'p_sim', 'q']] emp['lisa_i'] = lisa.Is emp['lisa_p_sim'] = lisa.p_sim emp['q'] = lisa.q emp['q'] = emp['q'].map({1: 'HH', 2: 'LH', 3: 'LL', 4: 'LH'}) # Center identification w.transform = 'O' c = CentFinder(lisa_pack.values, w, thr) emp['center_id'] = None for core in c.sClus: members = list(c.sClus[core]) emp.ix[members, 'center_id'] = core # Write out results if c.sClus: cent_shp_link = out_link + msa + '_cent.shp' ids_in_cent = list(emp[emp['center_id'].notnull()].index.values) _ = clip_shp(msa_shp_link, "GISJOIN", ids_in_cent, cent_shp_link) _ = df2dbf(emp.reindex(ps.open(cent_shp_link\ .replace('.shp', '.dbf')).by_col('GISJOIN')), cent_shp_link.replace('.shp', '.dbf')) emp.index.name = "GISJOIN" emp.to_csv(out_link + msa + '.csv') return emp
plt.xlabel('Percent of Deaths') plt.show() # In[6]: #Calculating Moran's I for the dataset that is being used #This Caluculates the Slope of the Red line, AKA the Moran's I value. Along with the seudo P-Value I_percent16 = ps.Moran(data.percent16.values, Queen) I_percent16.I, I_percent16.p_sim # In[7]: #Calculating the Local Autocorrelation Statistic #Autocorrelation is a characteristic of data in which the correlation between the values of the same variables is based on related objects. #The output depicts teh Moran's I of each county specified and their related P-Value LMo_percent16 = ps.Moran_Local(data.percent16.values, Queen) LMo_percent16.Is[0:50], LMo_percent16.p_sim[0:50] # In[8]: LMo_percent16 = ps.Moran_Local(data.percent16.values, Queen, permutations=9999) LMo_percent16.Is[0:50], LMo_percent16.p_sim[0:50] # In[9]: #TO dertermine which counties have significant data reading significant = LMo_percent16.p_sim < 0.05 hotspots = LMo_percent16.q == 1 * significant hotspots.sum() print('Siginicant Counities withing the United States', hotspots.sum())
zp = z > 0 lp = zl > 0 pp = zp * lp np = (1 - zp) * lp nn = (1 - zp) * (1 - lp) pn = zp * (1 - lp) return 1 * pp + 2 * np + 3 * nn + 4 * pn if __name__ == "__main__": w = ps.open(ps.examples.get_path("stl.gal")).read() f = ps.open(ps.examples.get_path("stl_hom.txt")) y = np.array(f.by_col['HR8893']) ###999### t1 = time.time() mi = ps.Moran_Local(y, w, 'r', 999) t2 = time.time() print "PySAL serial (999): ", t2-t1 t1 = time.time() mp_i = localmoran_mp(y, w, 'r', 999) t2 = time.time() print "PySAL MP (999): ", t2-t1 #assert(mi.q.all() == mp_i.all()) ###9999### t1 = time.time() mi = ps.Moran_Local(y, w, 'r', 9999) t2 = time.time() print "PySAL serial (9999): ", t2-t1 t1 = time.time() mp_i = localmoran_mp(y, w, 'r', 9999) t2 = time.time()