Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _test():
import doctest
start_suppress = np.get_printoptions()['suppress']
np.set_printoptions(suppress=True)
doctest.testmod()
np.set_printoptions(suppress=start_suppress)
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db = pysal.open(pysal.examples.get_path("columbus.dbf"), 'r')
y_var = 'CRIME'
y = np.array([db.by_col(y_var)]).reshape(49, 1)
x_var = ['INC']
x = np.array([db.by_col(name) for name in x_var]).T
yd_var = ['HOVAL']
yd = np.array([db.by_col(name) for name in yd_var]).T
q_var = ['DISCBD']
q = np.array([db.by_col(name) for name in q_var]).T
w = pysal.rook_from_shapefile(pysal.examples.get_path("columbus.shp"))
w.transform = 'r'
model = GM_Lag(
y, x, yd, q, w=w, spat_diag=True, name_y=y_var, name_x=x_var,
name_yend=yd_var, name_q=q_var, name_ds='columbus', name_w='columbus.gal')
print model.summary
def _test():
import doctest
doctest.testmod()
if __name__ == '__main__':
_test()
import numpy as np
import pysal
db=pysal.open("examples/columbus.dbf","r")
y = np.array(db.by_col("CRIME"))
y = np.reshape(y, (49,1))
X = []
X.append(db.by_col("INC"))
X = np.array(X).T
yd = []
yd.append(db.by_col("HOVAL"))
yd = np.array(yd).T
# instrument for HOVAL with DISCBD
q = []
q.append(db.by_col("DISCBD"))
q = np.array(q).T
reg = BaseTSLS(y, X, yd, q=q, robust='white')
print reg.betas
print reg.vm
#x_var = ['smsa']
x_var = ['smsa','UN']
x = np.array([db.by_col(name) for name in x_var]).T
#yend_var = ['UN']
#yend = np.array([db.by_col(name) for name in yend_var]).T
#q_var = ['UN']
#q = np.array([db.by_col(name) for name in q_var]).T
eq_var = 'time'
equationID = db.by_col(eq_var)
w = pysal.open(pysal.examples.get_path("swohio.gal"),'r').read()
w.transform = 'r'
#sur1 = ThreeSLS(y, x, equationID, yend, q, name_x=x_var, name_yend=yend_var, name_q=q_var)
sur1 = ThreeSLS(y, x, equationID, name_x=x_var, name_equationID=eq_var)
print sur1.summary
"""
db = pysal.open(pysal.examples.get_path("NAT.dbf"),'r')
y_var = ['HR80','HR90']
y = np.vstack((np.array([db.by_col(r)]).T for r in y_var))
y_name = 'HR'
x_var = ['PS80','UE80','PS90','UE90']
x = np.array([db.by_col(name) for name in x_var]).T
x = np.vstack((x[:,0:2],x[:,2:4]))
x_name = ['PS','UE']
yd_var = ['RD80','RD90']
yd = np.vstack((np.array([db.by_col(r)]).T for r in yd_var))
yd_name = ['RD']
q_var = ['FP79','FP89']
q = np.vstack((np.array([db.by_col(r)]).T for r in q_var))
q_name = ['FP']
eq_name = 'Year'
eq_ID = [1980]*3085 + [1990]*3085
w = pysal.rook_from_shapefile(pysal.examples.get_path("NAT.shp"))
def OnSaveQueryToDBF(self, event):
"""
"""
dlg = wx.FileDialog(
self, message="Save query as a dbf file...", defaultDir=os.getcwd(),
defaultFile='%s.dbf' % (self.points_data.name + '_query'),
wildcard="dbf file (*.dbf)|*.dbf|All files (*.*)|*.*",
style=wx.SAVE
)
if dlg.ShowModal() != wx.ID_OK:
return
path = dlg.GetPath()
dbf = self.points_data.dbf
newDBF= pysal.open(path,'w')
newDBF.header = dbf.header
newDBF.field_spec = dbf.field_spec
newDBF.header.append('ISQUERYITEM')
newDBF.field_spec.append(('N',4,0))
for i,row in enumerate(dbf):
if i in self.current_selected:
row.append(1)
newDBF.write(row)
newDBF.close()
self.ShowMsgBox("Query results have been saved in column 'ISQUERYITEM' of dbf file.",
mtype='Information',
micon=wx.ICON_INFORMATION)
def writeMeta(self, metaData, out_file):
shp = pysal.open(out_file, 'w')
dbf = pysal.open(out_file[:-3] + 'dbf', 'w')
dbf.header = ['LENGTH', 'NO_PNTS', 'INITIAL_CENTER']
dbf.field_spec = [('N',9,0)]*2 + [('L',1,0)]
for link in metaData:
vertices = list(link)
vertices = [pysal.cg.Point(v) for v in vertices]
shp.write(pysal.cg.Chain(vertices))
dbf.write(metaData[link])
shp.close()
dbf.close()
newvertices = s.vertices[:-1]
for v in newvertices:
vertices[v].add(i)
w = collections.defaultdict(set)
for neighbors in vertices.values():
for neighbor in neighbors:
w[neighbor] = w[neighbor] | neighbors
return w
#####################################################
# obtain a list for the states for each congressional district
cdistricts_dbf = "/Users/avelez/Documents/MGGG_UROP/state-adjacency-graphs/adjacency_graphs_all/MA_case/cb_2013_us_cd113_500k/cb_2013_us_cd113_500k.dbf"
cd_dbf = ps.open(cdistricts_dbf)
STATE_LIST = cd_dbf.by_col_array('STATEFP')
DISTRICT_LIST = [x for x in ps.open(cdistricts_file)]
GEOID_LIST = cd_dbf.by_col_array('GEOID')
# print(dbf.by_col_array('STATEFP'))
# create a dictionaries
# for each state, map to a list of its districts (as PySAL Polygon objects)
# for each district, map to its GEOID
STATE_TO_DISTRICTS = {}
DISTRICT_TO_GEOID = {}
for i in range(len(DISTRICT_LIST)):
state = STATE_LIST[i][0]
district_polygon = DISTRICT_LIST[i]
if state not in STATE_TO_DISTRICTS:
STATE_TO_DISTRICTS[state]=[]
STATE_TO_DISTRICTS[state].append(district_polygon)
DISTRICT_TO_GEOID[district_polygon] = GEOID_LIST[i][0]
def __init__(self, *args, **kwargs):
self._varName = 'Unknown'
args = args[:2]
FileIO.FileIO.__init__(self, *args, **kwargs)
self.file = pysal.open(self.dataPath, self.mode)
def ShowModal(self, dbf_path):
"""
Display the Dialog and add the id var.
"""
self.db = None
self.dbf_path = dbf_path
if os.path.exists(dbf_path):
self.db = pysal.open(dbf_path, 'r')
self.existingVarsListBox.Clear()
self.existingVarsListBox.InsertItems(self.db.header, 0)
self.idVarName.SetValue('POLY_ID')
return xrcAddIDVar.ShowModal(self)
else:
raise ValueError("Invalid DBF File")
# new approach using dtypes.name to avoid numpy name issue in type
type2spec = {'int': ('N', 20, 0),
'int8': ('N', 20, 0),
'int16': ('N', 20, 0),
'int32': ('N', 20, 0),
'int64': ('N', 20, 0),
'float': ('N', 36, 15),
'float32': ('N', 36, 15),
'float64': ('N', 36, 15),
'str': ('C', 14, 0),
'object': ('C', 14, 0),
'category': ('C', 14, 0)
}
types = [df[i].dtypes.name for i in df.columns]
specs = [type2spec[t] for t in types]
db = ps.open(dbf_path, 'w')
db.header = list(df.columns)
db.field_spec = specs
for i, row in df.T.iteritems():
db.write(row)
db.close()
return dbf_path
if hasattr(_basegeom, method):
locals()[method].__doc__ = getattr(_basegeom,method).__doc__
if __name__=='__main__':
#step 0, create 2 points
pt1 = pysal.cg.shapes.Point((0,0))
pt2 = pysal.cg.shapes.Point((10,10))
o = pysal.open('step0.shp','w')
o.write(pt1)
o.write(pt2)
o.close()
#step 1, buffer 2 points
b1 = buffer(pt1,10)
b2 = buffer(pt2,10)
o = pysal.open('step1.shp','w')
o.write(b1)
o.write(b2)
o.close()
#step 2, intersect 2 buffers
i = intersection(b1,b2)
o = pysal.open('step2.shp','w')
o.write(i)
o.close()
#step 3, union 2 buffers
u = union(b1, b2)
o = pysal.open('step3.shp','w')
o.write(u)
o.close()