def run_adaptive_kernel(self, sfile, var): """ Invoked by main run method. """ if self.model.shapes.type == pysal.cg.Polygon: self.warn("The selected shapefile contains polygons and kernel \ weights can only be computed on points. " + "The centroids of the specified polygons will be used\ instead.") elif self.model.shapes.type != pysal.cg.Point: return self.warn("The selected shapefile does not contain points\ and kernel weights can only be computed on \ points.") kern = ['uniform', 'triangular', 'quadratic', 'quartic', 'gaussian'][ self.KFuncChoice.GetSelection()] k = int(self.KNumNeighSpin.GetValue()) if self.model.distMethod == 0: radius = None elif self.model.distMethod == 1: # 'Arc Distance (miles)' radius = pysal.cg.RADIUS_EARTH_MILES elif self.model.distMethod == 2: # 'Arc Distance (kilometers)' radius = pysal.cg.RADIUS_EARTH_KM print "Kernel on %s, k=%d, ids=%r, kernel=%s" % (sfile, k, var, kern) W = pysal.adaptive_kernelW_from_shapefile( sfile, k=k, function=kern, idVariable=var, radius=radius) W = pysal.weights.insert_diagonal(W, wsp=False) W.meta = {'shape file': sfile, 'id variable': var, 'method': 'adaptive kernel', 'method options': [kern, k]} if radius: W.meta['Sphere Radius'] = radius self.SetW(W)
def test_adaptive_kernelW_from_shapefile(self): kwa = pysal.adaptive_kernelW_from_shapefile( pysal.examples.get_path('columbus.shp')) self.assertEquals(kwa.weights[0], [1.0, 0.03178906767736345, 9.9999990066379496e-08]) np.testing.assert_array_almost_equal( kwa.bandwidth[:3], np.array([[0.59871832], [0.59871832], [0.56095647]]))
def test_adaptive_kernelW_from_shapefile(self): kwa = pysal.adaptive_kernelW_from_shapefile( pysal.examples.get_path('columbus.shp')) self.assertEquals(kwa.weights[0], [1.0, 0.03178906767736345, 9.9999990066379496e-08]) np.testing.assert_array_almost_equal(kwa.bandwidth[:3], np.array([[0.59871832], [0.59871832], [0.56095647]]))
def generateWeightsUsingShapefile(shapeFilePath, idVariable=None, weights=None, kind="queen", k=None, binary=False): # use weights from shapefile for purely geographic w = None if weights == None: if kind == "queen": w = pysal.queen_from_shapefile(shapeFilePath, idVariable=idVariable) if kind == "rook": w = pysal.rook_from_shapefile(shapeFilePath, idVariable=idVariable) if kind == "knn" and type(k) == int: w = pysal.knnW_from_shapefile(shapefile=shapeFilePath, k=k, idVariable=idVariable) if kind == "band": threshold = pysal.min_threshold_dist_from_shapefile( shapeFilePath, idVariable=idVariable) if binary == True: w = pysal.weights.DistanceBand.from_shapefile( shapeFilePath, threshold=threshold, binary=True, idVariable=idVariable) else: w = pysal.threshold_continuousW_from_shapefile( shapefile=shapeFilePath, threshold=threshold, idVariable=idVariable) if kind == "kernel": w = pysal.adaptive_kernelW_from_shapefile(shapeFilePath, diagonal=True, k=5, idVariable=idVariable) # else use user defined weights to create "space" instead of "place" else: if kind == "rook": w = pysal.rook_from_shapefile(shapeFilePath, idVariable=idVariable) if kind == "knn": w = pysal.knnW_from_shapefile(shapeFilePath, k=k, idVariable=idVariable) else: w = pysal.queen_from_shapefile(shapeFilePath, idVariable=idVariable) neighbors = w.neighbor_offsets w = pysal.W(neighbors, weights=weights) # row standardize the matrix. better to do it here and use it somewhere else. w.transform = 'r' if binary == True: w.transform = 'b' return w
def _kernel(arg_dict): """ General handler for building kernel based weights from shapefiles Examples -------- >>> w = wmd_reader('kernel.wmd') >>> w.n 49 >>> w.meta_data {'root': {u'input1': {u'data1': {u'type': u'shp', u'uri': u'../examples/columbus.shp'}}, u'weight_type': u'kernel', u'transform': u'O', u'parameters': {u'function': u'triangular', u'bandwidths': None, u'k': 2}}} """ input1 = arg_dict['input1']['data1'] uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() k = 2 bandwidths = None function = 'triangular' if 'parameters' in arg_dict: k = arg_dict['parameters'].get('k', k) # set default to 2 bandwidths = arg_dict['parameters'].get('bandwidths', bandwidths) function = arg_dict['parameters'].get('function', function) else: parameters = {} parameters['k'] = k parameters['bandwidths'] = bandwidths parameters['function'] = function arg_dict['parameters'] = parameters if weight_type == 'akernel': # adaptive kernel w = ps.adaptive_kernelW_from_shapefile(uri, bandwidths=bandwidths, k=k, function=function) elif weight_type == 'kernel': w = ps.kernelW_from_shapefile(uri, k=k, function=function) else: print "Unsupported kernel: ", weight_type return None w = WMD(w.neighbors, w.weights) w.meta_data = {} w.meta_data["input1"] = {"type": 'shp', 'uri': uri} w.meta_data["transform"] = w.transform w.meta_data["weight_type"] = weight_type w.meta_data['parameters'] = arg_dict['parameters'] return w
def test_adaptive_kernelW_from_shapefile(self): kwa = pysal.adaptive_kernelW_from_shapefile( pysal.examples.get_path('columbus.shp')) wds = {kwa.neighbors[0][i]: v for i, v in enumerate(kwa.weights[0])} self.assertEquals(wds, {0: 1.0, 2: 0.03178906767736345, 1: 9.9999990066379496e-08}) np.testing.assert_array_almost_equal(kwa.bandwidth[:3], np.array([[0.59871832], [0.59871832], [0.56095647]]))
def _kernel(arg_dict): """ General handler for building kernel based weights from shapefiles Examples -------- >>> w = wmd_reader('kernel.wmd') >>> w.n 49 >>> w.meta_data {'root': {u'input1': {u'data1': {u'type': u'shp', u'uri': u'../examples/columbus.shp'}}, u'weight_type': u'kernel', u'transform': u'O', u'parameters': {u'function': u'triangular', u'bandwidths': None, u'k': 2}}} """ input1 = arg_dict['input1']['data1'] uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() k = 2 bandwidths = None function = 'triangular' if 'parameters' in arg_dict: k = arg_dict['parameters'].get('k', k) # set default to 2 bandwidths = arg_dict['parameters'].get('bandwidths', bandwidths) function = arg_dict['parameters'].get('function', function) else: parameters = {} parameters['k'] = k parameters['bandwidths'] = bandwidths parameters['function'] = function arg_dict['parameters'] = parameters if weight_type == 'akernel': # adaptive kernel w = ps.adaptive_kernelW_from_shapefile(uri, bandwidths = bandwidths, k=k, function = function) elif weight_type == 'kernel': w = ps.kernelW_from_shapefile(uri, k=k, function = function) else: print "Unsupported kernel: ",weight_type return None w = WMD(w.neighbors, w.weights) w.meta_data = {} w.meta_data["input1"] = {"type": 'shp', 'uri':uri} w.meta_data["transform"] = w.transform w.meta_data["weight_type"] = weight_type w.meta_data['parameters'] = arg_dict['parameters'] return w
def test_adaptive_kernelW_from_shapefile(self): kwa = pysal.adaptive_kernelW_from_shapefile( pysal.examples.get_path('columbus.shp')) wds = {kwa.neighbors[0][i]: v for i, v in enumerate(kwa.weights[0])} self.assertEquals(wds, { 0: 1.0, 2: 0.03178906767736345, 1: 9.9999990066379496e-08 }) np.testing.assert_array_almost_equal( kwa.bandwidth[:3], np.array([[0.59871832], [0.59871832], [0.56095647]]))
def test_Kernel(self): kw = pysal.Kernel(self.points) self.assertEqual(kw.weights[0], [1.0, 0.50000004999999503, 0.44098306152674649]) kw15 = pysal.Kernel(self.points, bandwidth=15.0) self.assertEqual(kw15[0], { 0: 1.0, 1: 0.33333333333333337, 3: 0.2546440075000701 }) self.assertEqual(kw15.bandwidth[0], 15.) self.assertEqual(kw15.bandwidth[-1], 15.) bw = [25.0, 15.0, 25.0, 16.0, 14.5, 25.0] kwa = pysal.Kernel(self.points, bandwidth=bw) self.assertEqual(kwa.weights[0], [ 1.0, 0.59999999999999998, 0.55278640450004202, 0.10557280900008403 ]) self.assertEqual(kwa.neighbors[0], [0, 1, 3, 4]) self.assertEqual(kwa.bandwidth[0], 25.) self.assertEqual(kwa.bandwidth[1], 15.) self.assertEqual(kwa.bandwidth[2], 25.) self.assertEqual(kwa.bandwidth[3], 16.) self.assertEqual(kwa.bandwidth[4], 14.5) self.assertEqual(kwa.bandwidth[5], 25.) kwea = pysal.Kernel(self.points, fixed=False) self.assertEqual(kwea.weights[0], [1.0, 0.10557289844279438, 9.9999990066379496e-08]) l = kwea.bandwidth.tolist() self.assertEqual( l, [[11.180341005532938], [11.180341005532938], [20.000002000000002], [11.180341005532938], [14.142137037944515], [18.027758180095585]]) kweag = pysal.Kernel(self.points, fixed=False, function='gaussian') self.assertEqual( kweag.weights[0], [0.3989422804014327, 0.26741902915776961, 0.24197074871621341]) l = kweag.bandwidth.tolist() self.assertEqual( l, [[11.180341005532938], [11.180341005532938], [20.000002000000002], [11.180341005532938], [14.142137037944515], [18.027758180095585]]) kw = pysal.kernelW_from_shapefile(self.polyShp, idVariable='POLYID') self.assertEqual( set(kw.weights[1]), set([ 0.0070787731484506233, 0.2052478782400463, 0.23051223027663237, 1.0 ])) kwa = pysal.adaptive_kernelW_from_shapefile(self.polyShp) self.assertEqual(kwa.weights[0], [1.0, 0.03178906767736345, 9.9999990066379496e-08])
def test_Kernel(self): kw = pysal.Kernel(self.points) wds = {kw.neighbors[0][i]: v for i, v in enumerate(kw.weights[0])} self.assertEqual(wds, {0: 1, 1: 0.500000049999995, 3: 0.4409830615267465}) kw15 = pysal.Kernel(self.points, bandwidth=15.0) self.assertEqual(kw15[0], {0: 1.0, 1: 0.33333333333333337, 3: 0.2546440075000701}) self.assertEqual(kw15.bandwidth[0], 15.) self.assertEqual(kw15.bandwidth[-1], 15.) bw = [25.0, 15.0, 25.0, 16.0, 14.5, 25.0] kwa = pysal.Kernel(self.points, bandwidth=bw) wds = {kwa.neighbors[0][i]: v for i, v in enumerate(kwa.weights[0])} self.assertEqual(wds, {0: 1.0, 1: 0.59999999999999998, 3: 0.55278640450004202, 4: 0.10557280900008403}) self.assertEqual(kwa.bandwidth[0], 25.) self.assertEqual(kwa.bandwidth[1], 15.) self.assertEqual(kwa.bandwidth[2], 25.) self.assertEqual(kwa.bandwidth[3], 16.) self.assertEqual(kwa.bandwidth[4], 14.5) self.assertEqual(kwa.bandwidth[5], 25.) kwea = pysal.Kernel(self.points, fixed=False) wds = {kwea.neighbors[0][i]: v for i, v in enumerate(kw.weights[0])} self.assertEqual(kwea.weights[0], [1.0, 0.10557289844279438, 9.9999990066379496e-08]) l = kwea.bandwidth.tolist() np.allclose(l, [[11.180341005532938], [11.180341005532938], [20.000002000000002], [11.180341005532938], [14.142137037944515], [18.027758180095585]]) kweag = pysal.Kernel(self.points, fixed=False, function='gaussian') wds = {kweag.neighbors[0][i]: v for i, v in enumerate(kweag.weights[0])} self.assertEqual(wds, {0: 0.3989422804014327, 1: 0.26741902915776961, 3: 0.24197074871621341}) l = kweag.bandwidth.tolist() np.allclose(l, [[11.180341005532938], [11.180341005532938], [20.000002000000002], [11.180341005532938], [14.142137037944515], [18.027758180095585]]) kw = pysal.kernelW_from_shapefile(self.polyShp, idVariable='POLYID') wds = {kw.neighbors[1][i]: v for i, v in enumerate(kw.weights[1])} self.assertEqual(wds, {4: 0.0070787731484506233, 2: 0.2052478782400463, 3: 0.23051223027663237, 1: 1.0}) kwa = pysal.adaptive_kernelW_from_shapefile(self.polyShp) wds = {kwa.neighbors[0][i]: v for i, v in enumerate(kwa.weights[0])} self.assertEqual(wds, {0: 1.0, 2: 0.03178906767736345, 1: 9.9999990066379496e-08})
def run_adaptive_kernel(self, sfile, var): """ Invoked by main run method. """ if self.model.shapes.type == pysal.cg.Polygon: self.warn("The selected shapefile contains polygons and kernel \ weights can only be computed on points. " + "The centroids of the specified polygons will be used\ instead.") elif self.model.shapes.type != pysal.cg.Point: return self.warn("The selected shapefile does not contain points\ and kernel weights can only be computed on \ points.") kern = ['uniform', 'triangular', 'quadratic', 'quartic', 'gaussian'][self.KFuncChoice.GetSelection()] k = int(self.KNumNeighSpin.GetValue()) if self.model.distMethod == 0: radius = None elif self.model.distMethod == 1: # 'Arc Distance (miles)' radius = pysal.cg.RADIUS_EARTH_MILES elif self.model.distMethod == 2: # 'Arc Distance (kilometers)' radius = pysal.cg.RADIUS_EARTH_KM print "Kernel on %s, k=%d, ids=%r, kernel=%s" % (sfile, k, var, kern) W = pysal.adaptive_kernelW_from_shapefile(sfile, k=k, function=kern, idVariable=var, radius=radius) W = pysal.weights.insert_diagonal(W, wsp=False) W.meta = { 'shape file': sfile, 'id variable': var, 'method': 'adaptive kernel', 'method options': [kern, k] } if radius: W.meta['Sphere Radius'] = radius self.SetW(W)
X = [] X.append(db.by_col("INC")) X.append(db.by_col("CRIME")) #X = np.array(X).transpose() #X = np.append(X, y, axis=1) #np.column_stack([X, y]) #print(X) #def test(a, b, c): # d = locals() # return a + b + c, d #print(test(1, 2, 3)) w = pysal.weights.rook_from_shapefile(pysal.examples.get_path("columbus.shp")) gwk = pysal.adaptive_kernelW_from_shapefile(pysal.examples.get_path("columbus.shp")) import Regionalization #X = np.append(X, y, axis=1) #X.transpose() #Regionalization.generateRegions(w, observations=y) regimes = Regionalization.generateRegimes(w, X, y) #print(regimes, len(regimes)) X = np.array(X).transpose() y.shape = (len(hoval), 1) #print(len(X), len(y)) olsr = pysal.spreg.OLS_Regimes(y, X, w=w, regimes=regimes) print([olsr.multi[i].betas for i in range(0, len(olsr.multi))]) #ols = pysal.spreg.ols.OLS(y, X, w, spat_diag=True, moran=True, name_y='home value', name_x=['income', 'crime'], name_ds='columbus',gwk=gwk, robust='hac') #ols2 = pysal.spreg.ols.OLS(y, X, w, spat_diag=True, moran=True, name_y='home value', name_x=['income', 'crime'], name_ds='columbus') '''
x = np.zeros((len(ds), 1)) for i, c in enumerate(x_columns): data = ds.by_col(ds.header[c]) data = list(map(float, data)) data = np.array(data).reshape(len(ds), 1) logger.info('Variable #{} :'.format(i + 1) + ds.header[c]) x = np.hstack((x, data)) x = x[:, 1:] # Weights, row-standarsized w = ps.knnW_from_shapefile(shapefile, k=4, idVariable=None) w.transform = 'r' name_w = shapefile.split('/')[-1] # Kernel-weights kw = ps.adaptive_kernelW_from_shapefile(shapefile, k=12, idVariable=None) for name_y in names_y: y = ds.by_col(name_y) y = list(map(float, y)) y = np.array(y).reshape(len(ds), 1) logger.info('y = ' + name_y) ## For Spatial Lag Model -- Maximum Likelihood logger.info('Method = Spatial Lag Model -- Maximum Likelihood') ml_lag = ps.spreg.ML_Lag(y, x, w, name_y=name_y, name_ds=name_ds, name_w=name_w)
data = np.log(data) print('Variable #{} :'.format(i+1), 'log({})'.format(db.header[c])) else: print('Variable #{} :'.format(i+1), db.header[c]) x = np.hstack((x, data)) x = x[:, 1:] logfile = open('Results.log', 'a') # Weights, row-standarsized w = ps.knnW_from_shapefile('Listings shp/Tract_With_Airbnb.shp', k=4, idVariable=None) w.transform = 'r' # Kernel-weights kw = ps.adaptive_kernelW_from_shapefile('Listings shp/Tract_With_Airbnb.shp', k=12, idVariable=None) ## For OLS + White-test + Spatial Diagnostics ols_spat = ps.spreg.OLS(y, x, w=w, robust=None, gwk=kw, sig2n_k=True, nonspat_diag=False, spat_diag=True, moran=True, white_test=True, vm=False, name_y=None, name_x=None, name_w=None, name_gwk=None, name_ds=None) #print(ols_spat.summary) #logfile.write('\n'*2 + ols_spat.summary + '\n'*2) logfile.close() ## Langrange Multiplier error and lag values. vw = ps.spreg.diagnostics_sp.LMtests(ols_spat, w) print(vw) print(vw.lme) print(vw.lml)