def test_higher_order(self): w10 = pysal.lat2W(10, 10) w10_2 = pysal.higher_order(w10, 2) w10_20 = {2: 1.0, 11: 1.0, 20: 1.0} self.assertEquals(w10_20, w10_2[0]) w5 = pysal.lat2W() w50 = {1: 1.0, 5: 1.0} self.assertEquals(w50, w5[0]) w51 = {0: 1.0, 2: 1.0, 6: 1.0} self.assertEquals(w51, w5[1]) w5_2 = pysal.higher_order(w5, 2) w5_20 = {2: 1.0, 10: 1.0, 6: 1.0} self.assertEquals(w5_20, w5_2[0])
def _contiguity(arg_dict): """ General handler for building contiguity weights from shapefiles Examples -------- >>> w = wmd_reader('wrook1.wmd') >>> w.n 49 >>> w.meta_data {'root': {u'input1': {u'data1': {u'type': u'shp', u'uri': u'http://toae.org/pub/columbus.shp'}}, u'weight_type': u'rook', u'transform': u'O'}} """ input1 = arg_dict['input1'] for key in input1: input1 = input1[key] break uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() if weight_type == 'rook': w = ps.rook_from_shapefile(uri) elif weight_type == 'queen': w = ps.queen_from_shapefile(uri) else: print "Unsupported contiguity criterion: ", weight_type return None if 'parameters' in arg_dict: order = arg_dict['parameters'].get('order', 1) # default to 1st order lower = arg_dict['parameters'].get( 'lower', 0) # default to exclude lower orders if order > 1: w_orig = w w = ps.higher_order(w, order) if lower: for o in xrange(order - 1, 1, -1): w = ps.weights.w_union(ps.higher_order(w_orig, o), w) w = ps.weights.w_union(w, w_orig) parameters = arg_dict['parameters'] else: parameters = {'lower': 0, 'order': 1} 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'] = parameters return w
def _contiguity(arg_dict): """ General handler for building contiguity weights from shapefiles Examples -------- >>> w = wmd_reader('wrook1.wmd') >>> w.n 49 >>> w.meta_data {'root': {u'input1': {u'data1': {u'type': u'shp', u'uri': u'http://toae.org/pub/columbus.shp'}}, u'weight_type': u'rook', u'transform': u'O'}} """ input1 = arg_dict['input1'] for key in input1: input1 = input1[key] break uri = input1['uri'] weight_type = arg_dict['weight_type'] weight_type = weight_type.lower() if weight_type == 'rook': w = ps.rook_from_shapefile(uri) elif weight_type == 'queen': w = ps.queen_from_shapefile(uri) else: print "Unsupported contiguity criterion: ", weight_type return None if 'parameters' in arg_dict: order = arg_dict['parameters'].get('order', 1) # default to 1st order lower = arg_dict['parameters'].get('lower', 0) # default to exclude lower orders if order > 1: w_orig = w w = ps.higher_order(w, order) if lower: for o in xrange(order-1, 1, -1): w = ps.weights.w_union(ps.higher_order(w_orig, o), w) w = ps.weights.w_union(w, w_orig) parameters = arg_dict['parameters'] else: parameters = {'lower': 0, 'order': 1} 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'] = parameters return w
def buildWeights(self): """Performs Contiguity-based Weights Creation.""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### ssdo = self.ssdo isLowOrder = self.isLowOrder weightOrder = self.weightOrder #### Get Neighbor Dictionary for All Polygons #### master2Order = ssdo.master2Order polyNeighborDict = WU.polygonNeighborDict(self.inputFC, \ self.masterField, \ contiguityType=self.weightType) #### Assign empty list to polygons without neighbors #### if ssdo.numObs > len(polyNeighborDict): for masterKey in master2Order.keys(): if not polyNeighborDict.has_key(masterKey): polyNeighborDict[masterKey] = [] #### Convert DefaultDict to Real Dict ?#### if not self.idField: polyNeighborCopy = {} for key in polyNeighborDict.keys(): polyNeighborCopy[master2Order[key]] = [] for item in polyNeighborDict[key]: polyNeighborCopy[master2Order[key]].\ append(master2Order[item]) polyNeighborDict = polyNeighborCopy #### Create a PySAL W Object #### weightObj = W(polyNeighborDict) #### Building up Lower Order Spatial Weights #### if weightOrder > 1: ARCPY.SetProgressor("default", \ "Building up Lower Order Spatial Weights...") origWeight = weightObj weightObj = PYSAL.higher_order(weightObj, weightOrder) if isLowOrder: for order in xrange(weightOrder - 1, 1, -1): lowOrderW = PYSAL.higher_order(origWeight, order) weightObj = PYSAL.w_union(weightObj, lowOrderW) weightObj = PYSAL.w_union(weightObj, origWeight) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj
def buildWeights(self): """Performs Contiguity-based Weights Creation.""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### ssdo = self.ssdo isLowOrder = self.isLowOrder weightOrder = self.weightOrder #### Get Neighbor Dictionary for All Polygons #### master2Order = ssdo.master2Order polyNeighborDict = WU.polygonNeighborDict(self.inputFC, \ self.masterField, \ contiguityType=self.weightType) #### Assign empty list to polygons without neighbors #### if ssdo.numObs > len(polyNeighborDict): for masterKey in master2Order.keys(): if not polyNeighborDict.has_key(masterKey): polyNeighborDict[masterKey] = [] #### Convert DefaultDict to Real Dict ?#### if not self.idField: polyNeighborCopy = {} for key in polyNeighborDict.keys(): polyNeighborCopy[master2Order[key]] = [] for item in polyNeighborDict[key]: polyNeighborCopy[master2Order[key]].\ append(master2Order[item]) polyNeighborDict = polyNeighborCopy #### Create a PySAL W Object #### weightObj = W(polyNeighborDict) #### Building up Lower Order Spatial Weights #### if weightOrder > 1: ARCPY.SetProgressor("default", \ "Building up Lower Order Spatial Weights...") origWeight = weightObj weightObj = PYSAL.higher_order(weightObj, weightOrder) if isLowOrder: for order in xrange(weightOrder-1, 1, -1): lowOrderW = PYSAL.higher_order(origWeight, order) weightObj = PYSAL.w_union(weightObj, lowOrderW) weightObj = PYSAL.w_union(weightObj, origWeight) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj
def test_higher_order(self): weights = { 0: [1.0, 1.0, 1.0], 1: [1.0, 1.0, 1.0], 2: [1.0, 1.0, 1.0], 3: [1.0, 1.0, 1.0], 4: [1.0, 1.0, 1.0, 1.0], 5: [1.0, 1.0, 1.0], 6: [1.0, 1.0, 1.0], 7: [1.0, 1.0, 1.0], 8: [1.0, 1.0, 1.0] } neighbors = { 0: [4, 6, 2], 1: [3, 5, 7], 2: [8, 0, 4], 3: [7, 1, 5], 4: [8, 0, 2, 6], 5: [1, 3, 7], 6: [4, 0, 8], 7: [3, 1, 5], 8: [6, 2, 4] } wneighbs = { k: {neighb: weights[k][i] for i, neighb in enumerate(v)} for k, v in neighbors.iteritems() } w2 = pysal.higher_order(self.w3x3, 2) test_wneighbs = { k: {ne: w2.weights[k][i] for i, ne in enumerate(v)} for k, v in w2.neighbors.iteritems() } self.assertEqual(test_wneighbs, wneighbs)
def test_higher_order(self): weights = {0: [1.0, 1.0, 1.0], 1: [1.0, 1.0, 1.0], 2: [1.0, 1.0, 1.0], 3: [1.0, 1.0, 1.0], 4: [1.0, 1.0, 1.0, 1.0], 5: [1.0, 1.0, 1.0], 6: [1.0, 1.0, 1.0], 7: [1.0, 1.0, 1.0], 8: [1.0, 1.0, 1.0]} neighbors = {0: [2, 4, 6], 1: [3, 5, 7], 2: [0, 4, 8], 3: [1, 5, 7], 4: [0, 2, 6, 8], 5: [1, 3, 7], 6: [0, 4, 8], 7: [1, 3, 5], 8: [2, 4, 6]} w2 = pysal.higher_order(self.w3x3, 2) self.assertEqual(w2.neighbors, neighbors) self.assertEqual(w2.weights, weights)
def test_higher_order(self): weights = {0: [1.0, 1.0, 1.0], 1: [1.0, 1.0, 1.0], 2: [1.0, 1.0, 1.0], 3: [1.0, 1.0, 1.0], 4: [1.0, 1.0, 1.0, 1.0], 5: [1.0, 1.0, 1.0], 6: [1.0, 1.0, 1.0], 7: [1.0, 1.0, 1.0], 8: [1.0, 1.0, 1.0]} neighbors = {0: [4, 6, 2], 1: [3, 5, 7], 2: [8, 0, 4], 3: [7, 1, 5], 4: [8, 0, 2, 6], 5: [1, 3, 7], 6: [4, 0, 8], 7: [3, 1, 5], 8: [6, 2, 4]} w2 = pysal.higher_order(self.w3x3, 2) self.assertEqual(w2.neighbors, neighbors) self.assertEqual(w2.weights, weights)
def test_higher_order(self): weights = {0: [1.0, 1.0, 1.0], 1: [1.0, 1.0, 1.0], 2: [1.0, 1.0, 1.0], 3: [1.0, 1.0, 1.0], 4: [1.0, 1.0, 1.0, 1.0], 5: [1.0, 1.0, 1.0], 6: [1.0, 1.0, 1.0], 7: [1.0, 1.0, 1.0], 8: [1.0, 1.0, 1.0]} neighbors = {0: [4, 6, 2], 1: [3, 5, 7], 2: [8, 0, 4], 3: [7, 1, 5], 4: [8, 0, 2, 6], 5: [1, 3, 7], 6: [4, 0, 8], 7: [3, 1, 5], 8: [6, 2, 4]} wneighbs = {k: {neighb: weights[k][i] for i, neighb in enumerate(v)} for k, v in neighbors.iteritems()} w2 = pysal.higher_order(self.w3x3, 2) test_wneighbs = {k: {ne: weights[k][i] for i, ne in enumerate(v)} for k, v in w2.neighbors.iteritems()} self.assertEqual(test_wneighbs, wneighbs)
def _higher_order(arg_dict): wmd = arg_dict['wmd'] order = 2 if 'parameters' in arg_dict: order = arg_dict['parameters'].get('order', order) else: parameters = {} parameters['order'] = order arg_dict['parameters'] = parameters w = ps.higher_order(wmd, order) w = WMD(w.neighbors, w.weights) w.meta_data = {} w.meta_data['input1'] = arg_dict['input1'] w.meta_data['parameters'] = arg_dict['parameters'] return w
def queren(self): time_field = self.comboBox.currentText() lon_field = self.comboBox_2.currentText() lat_field = self.comboBox_3.currentText() val_field = self.comboBox_4.currentText() location_field = self.comboBox_5.currentText() l_val = int(self.textEdit_3.toPlainText()) m_val = int(self.textEdit_4.toPlainText()) p_val = int(self.textEdit_5.toPlainText()) q_val = int(self.textEdit_6.toPlainText()) K_val = int(self.textEdit.toPlainText()) diff_val = int(self.textEdit_7.toPlainText()) pre_val = int(self.textEdit_8.toPlainText()) db = pymysql.connect("localhost", "root", "960609", "biye", charset='utf8') # 使用cursor()方法获取操作游标 cursor = db.cursor() #获取地点字段 location_sql = 'select distinct ' + location_field + ' from AOI order by ' + location_field + ' asc' cursor.execute(location_sql) rows = cursor.fetchall() location_val = [] for (row, ) in rows: loca = row location_val.append(loca) #获取时间字段 time_sql = 'select distinct ' + time_field + ' from AOI' cursor.execute(time_sql) rows = cursor.fetchall() datetime_val = [] for (row, ) in rows: dt = str(row) datetime_val.append(dt) data = [] diff = [diff_val] #差分次数i #拼接时空数据 for i in range(len(datetime_val)): locaval_sql = 'select ' + val_field + ' from AOI where ' + time_field + ' = \'' + datetime_val[ i] + '\' order by ' + location_field + ' asc' cursor.execute(locaval_sql) rows = cursor.fetchall() lval = [] for (row, ) in rows: val = row lval.append(val) data.append(lval) ts = np.array(data) #计算差分 ts_diff = utils.set_stationary(ts, diff) #获取点的经纬度坐标 # 点的经度 lon_sql = 'select distinct ' + lon_field + ' from AOI order by ' + location_field + ' asc' cursor.execute(lon_sql) rows = cursor.fetchall() x_val = [] for (row, ) in rows: xval = row x_val.append(xval) # 点的纬度 lat_sql = 'select distinct ' + lat_field + ' from AOI order by ' + location_field + ' asc' cursor.execute(lat_sql) rows = cursor.fetchall() y_val = [] for (row, ) in rows: yval = row y_val.append(yval) #拼接点 point = [] for i in range(len(x_val)): coor = [] coor.append(x_val[i]) coor.append(y_val[i]) coortuple = tuple(coor) point.append(coortuple) #计算权重 points = np.array(point) kd = pysal.cg.kdtree.KDTree(points) # weights K = int(K_val) weights = pysal.weights.Distance.KNN(kd, K) #weights.transform='r' #计算滞后 w0 = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]] w = [weights] s_lag = max(l_val, p_val) for s in range(2, s_lag + 1): w_tmp = pysal.higher_order(weights, s) w.append(w_tmp) slw = [w0] for j in range(len(w)): w[j].transform = 'r' w_f = w[j].full() slw.append(w_f[0].tolist()) w_slag = np.array(slw) starma = starma_model.STARMA(m_val, q_val, ts_diff, w_slag) starma.fit() result = starma.predict(ts_diff, pre_val) self.table.show_table(result, location_val)
[0.1111111,0.1111111,0.1111111,0,0.1111111,0.1111111,0.1111111,0,0,0.1111111,0,0.1111111,0.1111111,0]] yy=[ [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], [1,2,3,4,5,6,7,8,9,10,11,12,13,14], ] npy=np.arange(196) npy.shape=(14,14) weight1=np.array(weight1) y1=pysal.lag_spatial(weight1,npy) print(y1) ''' w = pysal.lat2W(5, 5) w2 = pysal.higher_order(w, 2) print(w) print(w2)
def CreateWeights(shp_path, weights_name, w_unique_ID, weights_type, \ cont_type = None, cont_order = None, cont_ilo = None, \ dist_metric = None, dist_method = None, dist_value = None,\ kernel_type = None, kernel_nn = None): if shp_path == None or len(shp_path) == 0: raise "Shape file path cannot be empty." if weights_name == None or len(weights_name) == 0: raise "Weights name cannot be empty." weights_type = int(weights_type) if weights_type < 0 or weights_type > 2: raise "Weights Type is illegal." dbf_path = shp_path[:-3] + "dbf" dbf = po(dbf_path, 'r') for item in dbf.header: if item.lower() == w_unique_ID.lower(): w_unique_ID = item break if weights_type == 0: # contiguity-based weights | GAL file cont_type = int(cont_type) if cont_type == 0: # rook w = rook( shp_path, w_unique_ID ) else: w = queen( shp_path, w_unique_ID ) orig_w = w w_order = int(cont_order) if w_order > 1: w = higher_order(w, w_order) if cont_ilo == "true": for order in xrange(w_order -1, 1, -1): lowerOrderW = higher_order(oirg_w, order) w = w_union(w, lowerOrderW) w = w_union(w, orig_w) elif weights_type == 1: # distance-based weights | GAL file GWT file radius = None # If supplied arc_distances will be calculated p = 2 # Euclidean distance, 1: Manhanttan distance params = {'idVariable': w_unique_ID, 'radius': radius, 'p': p} dist_method = int(dist_method) if dist_method == 0: # kNN params['k'] = int(dist_value) w = knnW_from_shapefile( shp_path, **params ) print w elif dist_method == 1: # threshold dist_value = float(dist_value) w = threshold_binaryW_from_shapefile( shp_path, dist_value, **params ) elif dist_method == 2: # inverse_distance items = dist_value.split(",") dist_value = float(items[0]) power = 1 if len(items) <= 1 else float(items[1]) params['alpha'] = -1 * power w = threshold_continuousW_from_shapefile( shp_path, dist_value, **params ) elif weights_type == 2: #kernel-based weights | GWT file kerns = ['uniform', 'triangular', 'quadratic', 'quartic', 'gaussian'] kern = kerns[int(kernel_type)] k = int(kernel_nn) w = adaptive_kernelW_from_shapefile( shp_path, k = k, function = kern, \ idVariable = w_unique_ID) w = insert_diagonal( w, wsp = False ) method_options = [kern, k] #w_object = {'w' : w, 'shapefile' : shp_path, 'id' : w_unique_ID, 'method' : weights_type, 'method options' : method_options} #return w_object return w