def test___init__(self): import pysal f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) pci = pci.transpose() rpci = pci / (pci.mean(axis=0)) w = pysal.open(pysal.examples.get_path("states48.gal")).read() w.transform = 'r' sm = pysal.Spatial_Markov(rpci, w, fixed=True, k=5) S = np.array( [[0.43509425, 0.2635327, 0.20363044, 0.06841983, 0.02932278], [0.13391287, 0.33993305, 0.25153036, 0.23343016, 0.04119356], [0.12124869, 0.21137444, 0.2635101, 0.29013417, 0.1137326], [0.0776413, 0.19748806, 0.25352636, 0.22480415, 0.24654013], [0.01776781, 0.19964349, 0.19009833, 0.25524697, 0.3372434]]) np.testing.assert_array_almost_equal(S, sm.S)
def markov(observations, w=None, numQuints=5, method="regular"): result = None s = None if method == "regular": # non spatial analysis quintiles = np.array([pysal.Quantiles(y, k=numQuints).yb for y in observations]).transpose() result = pysal.Markov(quintiles) #s = result.steady_state else: observations = observations.transpose() if method == "spatial": # standardize observations for smoother calculations: observations = observations / (observations.mean(axis=0)) result = pysal.Spatial_Markov(observations, w, fixed=True, k=numQuints) #s = result.S else: # method == lisa result = pysal.LISA_Markov(observations, w) #s = result.steady_state return result.transitions, result.p, s, pysal.ergodic.fmpt(result.p)
def test_chi2(self): import pysal f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) pci = pci.transpose() rpci = pci / (pci.mean(axis=0)) w = pysal.open(pysal.examples.get_path("states48.gal")).read() w.transform = 'r' sm = pysal.Spatial_Markov(rpci, w, fixed=True, k=5) chi = np.matrix([[4.06139105e+01, 6.32961385e-04, 1.60000000e+01], [5.55485793e+01, 2.88879565e-06, 1.60000000e+01], [1.77772638e+01, 3.37100315e-01, 1.60000000e+01], [4.00925436e+01, 7.54729084e-04, 1.60000000e+01], [4.68588786e+01, 7.16364084e-05, 1.60000000e+01]]).getA() obs = np.matrix(sm.chi2).getA() np.testing.assert_array_almost_equal(obs, chi) obs = np.matrix([[4.61209613e+02, 0.00000000e+00, 4.00000000e+00], [1.48140694e+02, 0.00000000e+00, 4.00000000e+00], [6.33129261e+01, 5.83089133e-13, 4.00000000e+00], [7.22778509e+01, 7.54951657e-15, 4.00000000e+00], [2.32659201e+02, 0.00000000e+00, 4.00000000e+00]]) np.testing.assert_array_almost_equal(obs.getA(), np.matrix(sm.shtest).getA())
def test_chi2(self): import pysal f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) pci = pci.transpose() rpci = pci / (pci.mean(axis=0)) w = pysal.open(pysal.examples.get_path("states48.gal")).read() w.transform = 'r' sm = pysal.Spatial_Markov(rpci, w, fixed=True, k=5) chi = np.matrix([[4.05598541e+01, 6.44644317e-04, 1.60000000e+01], [5.54751974e+01, 2.97033748e-06, 1.60000000e+01], [1.77528996e+01, 3.38563882e-01, 1.60000000e+01], [4.00390961e+01, 7.68422046e-04, 1.60000000e+01], [4.67966803e+01, 7.32512065e-05, 1.60000000e+01]]).getA() obs = np.matrix(sm.chi2).getA() np.testing.assert_array_almost_equal(obs, chi) obs = np.matrix([[4.61209613e+02, 0.00000000e+00, 4.00000000e+00], [1.48140694e+02, 0.00000000e+00, 4.00000000e+00], [6.33129261e+01, 5.83089133e-13, 4.00000000e+00], [7.22778509e+01, 7.54951657e-15, 4.00000000e+00], [2.32659201e+02, 0.00000000e+00, 4.00000000e+00]]) np.testing.assert_array_almost_equal(obs.getA(), np.matrix(sm.shtest).getA())
print(m5.transitions) print(m5.p) print(m5.steady_state) print(pysal.ergodic.fmpt(m5.p)) ################################################################# Spatial Markov fpci = pci.transpose() / (pci.transpose().mean(axis=0)) print(fpci) w = pysal.open( r'C:\Anaconda\Lib\site-packages\pysal\examples\us_income\states48.gal' ).read() w.transform = 'r' sm = pysal.Spatial_Markov(fpci, w, fixed=True, k=5) for p in sm.p: print(p) for f in sm.F: print(f) ################################################################# LISA Markov lm = pysal.LISA_Markov(pci.transpose(), w) print(lm.classes) # the estimated transition probability matrix print(lm.transitions) print(lm.p) print(lm.steady_state) print(pysal.ergodic.fmpt(lm.p))
def spatial_trend(self, subquery, time_cols, num_classes=7, w_type='knn', num_ngbrs=5, permutations=0, geom_col='the_geom', id_col='cartodb_id'): """ Predict the trends of a unit based on: 1. history of its transitions to different classes (e.g., 1st quantile -> 2nd quantile) 2. average class of its neighbors Inputs: @param subquery string: e.g., SELECT the_geom, cartodb_id, interesting_time_column FROM table_name @param time_cols list of strings: list of strings of column names @param num_classes (optional): number of classes to break distribution of values into. Currently uses quantile bins. @param w_type string (optional): weight type ('knn' or 'queen') @param num_ngbrs int (optional): number of neighbors (if knn type) @param permutations int (optional): number of permutations for test stats @param geom_col string (optional): name of column which contains the geometries @param id_col string (optional): name of column which has the ids of the table Outputs: @param trend_up float: probablity that a geom will move to a higher class @param trend_down float: probablity that a geom will move to a lower class @param trend float: (trend_up - trend_down) / trend_static @param volatility float: a measure of the volatility based on probability stddev(prob array) """ if len(time_cols) < 2: plpy.error('More than one time column needs to be passed') params = { "id_col": id_col, "time_cols": time_cols, "geom_col": geom_col, "subquery": subquery, "num_ngbrs": num_ngbrs } result = self.data_provider.get_markov(w_type, params) # build weight weights = pu.get_weight(result, w_type) weights.transform = 'r' # prep time data t_data = get_time_data(result, time_cols) sp_markov_result = ps.Spatial_Markov(t_data, weights, k=num_classes, fixed=False, permutations=permutations) # get lag classes lag_classes = ps.Quantiles(ps.lag_spatial(weights, t_data[:, -1]), k=num_classes).yb # look up probablity distribution for each unit according to class and # lag class prob_dist = get_prob_dist(sp_markov_result.P, lag_classes, sp_markov_result.classes[:, -1]) # find the ups and down and overall distribution of each cell trend_up, trend_down, trend, volatility = get_prob_stats( prob_dist, sp_markov_result.classes[:, -1]) # output the results return zip(trend, trend_up, trend_down, volatility, weights.id_order)
rpiD = np.array([fd.by_col[str(y)] for y in yr_month]) rpiS = np.array([fs.by_col[str(y)] for y in yr_month]) rpiT = np.array([ft.by_col[str(y)] for y in yr_month]) rpiC = np.array([fc.by_col[str(y)] for y in yr_month]) rpiD = rpiD.transpose() rpiS = rpiS.transpose() rpiT = rpiT.transpose() rpiC = rpiC.transpose() # Spatial weights using a Rook-based contiguity method w = ps.weights.Rook.from_shapefile('../TREB-Zones-Dissolve.shp') w.transform = 'r' # Spatial Markov with 5 classes for housing price (Detached, Semi-Detached, Townhouse, Condo) smD = ps.Spatial_Markov(rpiD, w, fixed=True, k=5) smS = ps.Spatial_Markov(rpiS, w, fixed=True, k=5) smT = ps.Spatial_Markov(rpiT, w, fixed=True, k=5) smC = ps.Spatial_Markov(rpiC, w, fixed=True, k=5) # Print results for each structure class # Results for Detached Homes print("Results for Detached Home Analysis:") # Pooled over space and time global transition probabilities print("Global Transition Probablilities:") print(smD.p) # Transition probabilities given condition of neighbours print("Conditioned Transition Probablilities:") for p in smD.P: print(p) print("Steady State Long Run Probabilities:")
return zip([None], [None], [None], [None], [None]) ## build weight weights = pu.get_weight(query_result, w_type) weights.transform = 'r' ## prep time data t_data = get_time_data(query_result, time_cols) plpy.debug('shape of t_data %d, %d' % t_data.shape) plpy.debug('number of weight objects: %d, %d' % (weights.sparse).shape) plpy.debug('first num elements: %f' % t_data[0, 0]) sp_markov_result = ps.Spatial_Markov(t_data, weights, k=num_classes, fixed=False, permutations=permutations) ## get lag classes lag_classes = ps.Quantiles(ps.lag_spatial(weights, t_data[:, -1]), k=num_classes).yb ## look up probablity distribution for each unit according to class and lag class prob_dist = get_prob_dist(sp_markov_result.P, lag_classes, sp_markov_result.classes[:, -1]) ## find the ups and down and overall distribution of each cell trend_up, trend_down, trend, volatility = get_prob_stats( prob_dist, sp_markov_result.classes[:, -1])
def accept(self): if self.ui.savedshpradio.isChecked(): #when selecting saved shp openfile=str(self.ui.inputline.text()) #make a string of saved file savefile = str(self.ui.saveoutputline.text()) #this will be a string like "c:\output.(.csv)" weightsfile=str(self.ui.inputweightsline.text()) if self.ui.matrixcheckbox.checkState(): #run spatial Matrix f=pysal.open(openfile) #read a shp file, not need to read w=pysal.open(weightsfile).read() #read a weights file #opendbf=openfile[:-3] + "dbf" #open the same file only with dbf #f_dbf = pysal.open(opendbf) #read the dbf attribute file fileheader=f.header #select a column and let it function columnindex1=int(self.ui.startcombobox.currentText()) #when select a column columnindex2=int(self.ui.endcombobox.currentText())+1 #avoid random selection? #change into array, by_col function is only for dbf file pci=np.array([f.by_col[str(y)] for y in range(columnindex1, columnindex2)]) #only number? by_col works for dbf, but the sample data use csv? #q5 = np.array([pysal.Quantiles(y).yb for y in pci]) #map classification? pci=pci.transpose() rpci = pci / (pci.mean(axis = 0)) #standardization w.transform='r' sm=pysal.Spatial_Markov(rpci, w, fixed=True, k=5) #what did k mean? does it equal to quantile? #results transition_matrix=sm.p #numpy.matrixlib.defmatrix.matrix results = "\n".join([ "\t".join(map(str,row)) for row in transition_matrix]) #results=repr(transition_matrix).replace('matrix',' ') #results=' '+''.join([ c for c in s if c not in ('(', ')','[',']',',')]) output=pysal.open(savefile,'w') output.write(results) output.close if self.ui.probabilitiescheckbox.checkState(): for p in sm.P: transition_probabilities=p #results=repr(transition_probabilities).replace('matrix',' ') #results=' '+''.join([ c for c in s if c not in ('(', ')','[',']',',')]) results = "\n".join([ "\t".join(map(str,row)) for row in transition_probabilities]) output=pysal.open(savefile,'w') output.write(results) output.close #else: # pass elif self.ui.steadystatecheckbox.checkState(): steady_state_distribution=sm.S #results=repr(Steady_State_Distribution).replace('matrix',' ') #results=' '+''.join([ c for c in s if c not in ('(', ')','[',']',',')]) results = "\n".join([ "\t".join(map(str,row)) for row in steady_state_distribution]) output=pysal.open(savefile,'w') output.write(results) output.close #else: #pass elif self.ui.firstcheckbox.checkState(): for f in sm.F: first_mean_passage_time=f #resultss=repr(first_mean_passage_time).replace('matrix',' ') #results=' '+''.join([ c for c in s if c not in ('(', ')','[',']',',')]) results = "\n".join([ "\t".join(map(str,row)) for row in first_mean_passage_time]) output=pysal.open(savefile,'w') output.write(results) output.close else: pass else: pass elif self.ui.activecombobox.isChecked(): #when selecting active shp and then import pysal layer = self.layers[self.ui.activecombobox.currentIndex()] #select a shp layer savefile = str(self.ui.outputline.text()) weightsfile=str(self.ui.Inputweightsline.text()) pass #if #f=pysal.open() #calculate Moran's I and other value, but do not know how to get the file path from active layers? #else: # return self.close() #close the dialog window