Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_weight(query_res, w_type='knn', num_ngbrs=5):
"""
Construct PySAL weight from return value of query
@param query_res dict-like: query results with attributes and neighbors
"""
neighbors = {x['id']: x['neighbors'] for x in query_res}
print('len of neighbors: %d' % len(neighbors))
built_weight = ps.W(neighbors)
built_weight.transform = 'r'
return built_weight
time_gstar_z = dict()
if self.rdo_createweight.GetValue():
if len(self.txt_p_neighbors.GetValue()):
self.n_p_neighbors = int(self.txt_p_neighbors.GetValue())
if len(self.txt_f_neighbors.GetValue()):
self.n_f_neighbors = int(self.txt_f_neighbors.GetValue())
if self.n_p_neighbors ==0 and self.n_f_neighbors==0:
return False
n_previous = self.n_p_neighbors
n_future = self.n_f_neighbors
tneighbors = self.create_time_w(n_previous, n_future)
tweights = pysal.W(tneighbors)
dlg = wx.FileDialog(
self.main, message="Save the time weights file as ...",
defaultFile=self.shape_name +'.time.gal',
wildcard="Weights file (*.gal)|*.gal|All files (*.*)|*.*",
style=wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
save_weight_path = dlg.GetPath()
o = pysal.open(save_weight_path,'w')
o.write(tweights)
o.close()
dlg.Destroy()
self.weight_path = save_weight_path
self.dialog.Destroy()
return self.weight_path
self.dialog.Destroy()
return False
d = dict([(i,[]) for i in id_order])
for pair in sk:
k, v = pair
k = id_order[k]
v = id_order[v]
d[k].append(v)
return pysal.W(neighbors=d)
else:
d = {}
for pair in sk:
k, v = pair
if k in d:
d[k].append(v)
else:
d[k] = [v]
return pysal.weights.WSP(pysal.W(neighbors=d).sparse)
def spw_from_shapefile(shapefile, idVariable=None):
polygons = pysal.open(shapefile,'r').read()
polygons = map(shapely.geometry.asShape,polygons)
perimeters = [p.length for p in polygons]
Wsrc = pysal.rook_from_shapefile(shapefile)
new_weights = {}
for i in Wsrc.neighbors:
a = polygons[i]
p = perimeters[i]
new_weights[i] = [a.intersection(polygons[j]).length/p for j in Wsrc.neighbors[i]]
return pysal.W(Wsrc.neighbors,new_weights)
"""
nodes = self.node_edge.keys()
neighbors = {}
for node in nodes:
lnks = self.enum_links_node(node)
# put i,j s.t. i < j
lnks = [tuple(sorted(lnk)) for lnk in lnks]
for comb in combinations(range(len(lnks)), 2):
l, r = comb
if lnks[l] not in neighbors:
neighbors[lnks[l]] = []
neighbors[lnks[l]].append(lnks[r])
if lnks[r] not in neighbors:
neighbors[lnks[r]] = []
neighbors[lnks[r]].append(lnks[l])
return ps.W(neighbors)
near.remove(node)
neighbors[ids[node]] = near
for end in near:
path = [end]
previous = pred[end]
while previous != node:
path.append(previous)
end = previous
previous = pred[end]
path.append(node)
cum_cost = 0
for p in range(len(path) - 1):
cum_cost += cost[(path[p], path[p + 1])]
wt.append(cum_cost ** alpha)
weights[ids[node]] = wt
return ps.W(neighbors, weights, ids)
'''
if m.shape[0] != m.shape[1]:
raise ValueError('Your array is not square')
neighbors, weights = {}, {}
for i in xrange(m.shape[0]):
# for i, row in enumerate(m):
row = m[i]
if ids:
i = ids[i]
ngh = list(row.nonzero()[0])
weights[i] = list(row[ngh])
ngh = list(ngh)
if ids:
ngh = [ids[j] for j in ngh]
neighbors[i] = ngh
return pysal.W(neighbors, weights, id_order=ids)
def gpd_contiguity(gpdf):
"""
Contiguity weights
https://github.com/pysal/pysal/blob/master/pysal/weights/_contW_binning.py
"""
polygons = gpd_polygons(gpdf)
neighbors = pysal.weights.\
Contiguity.ContiguityWeightsPolygons(polygons).w
# neighbors = pysal.weights.\
# Contiguity.ContiguityWeightsPolygons(polygons, 2).w # for rook
return pysal.W(neighbors)
wj = w**j
rj, cj = wj.nonzero()
sj = set(zip(rj, cj))
sk.difference_update(sj)
if not diagonal:
sk = set([(i,j) for i,j in sk if i!=j])
if id_order:
d = dict([(i,[]) for i in id_order])
for pair in sk:
k, v = pair
k = id_order[k]
v = id_order[v]
d[k].append(v)
return pysal.W(neighbors=d)
else:
d = {}
for pair in sk:
k, v = pair
if k in d:
d[k].append(v)
else:
d[k] = [v]
return pysal.weights.WSP(pysal.W(neighbors=d).sparse)