Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if id_order:
self.n = len(id_order)
self.shp = os.path.split(self.dataPath)[1].split('.')[0]
self.id_var = id_var
weights, neighbors = self._readlines(id_type)
for k in neighbors:
if k in neighbors[k]:
k_index = neighbors[k].index(k)
if weights[k][k_index] == 0.0:
del neighbors[k][k_index]
del weights[k][k_index]
self.pos += 1
return W(neighbors, weights)
Read in the newly created text file
>>> wnew = pysal.lib.io.open(fname,'r').read()
Compare values from old to new
>>> wnew.pct_nonzero == w.pct_nonzero
True
Clean up temporary file created for this example
>>> os.remove(fname)
"""
self._complain_ifclosed(self.closed)
if issubclass(type(obj), W):
f = self.file
n = obj.n
if n > 256:
raise ValueError('WK1 file format supports only up to 256 observations.')
pack = struct.pack
f.write(pack('<6B', 0, 0, 2, 0, 6, 4))
f.write(pack('<6H', 6, 8, 0, 0, n, n))
f.write(pack('<2H6B', 150, 6, 0, 0, 0, 0, 0, 0))
f.write(pack('<2H1B', 47, 1, 0))
f.write(pack('<2H1b', 2, 1, 0))
f.write(pack('<2H1b', 3, 1, 0))
f.write(pack('<2H1b', 4, 1, 0))
f.write(pack('<2H1b', 5, 1, 0))
f.write(pack('<2H1b', 49, 1, 1))
f.write(pack('<4H2b13H', 7, 32, 0, 0, 113, 0, 10,
n, n, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0))
for orderID in weightDict.keys():
if len(weightDict[orderID]) > 0:
sumWeight = 0.0
for item in weightDict[orderID]:
sumWeight += item
weightArray = 1.0 * NUM.array(weightDict[orderID])/sumWeight
weightDict[orderID] = weightArray.tolist()
fi.close()
if returnWeightFileType(weightsFile) == 'GWT' and master2Order:
for neighKey in master2Order.keys():
orderID = int(master2Order[int(neighKey)])
if orderID not in neighDict:
neighDict[orderID] = []
weightDict[orderID] = []
w = W(neighDict, weightDict)
if inType == ".GAL":
w.transform = 'r'
w._varName = uid
return w
id_order = []
weights, neighbors = {}, {}
l = line1
for i in range(n):
obs, ngh, wgt = line2wgt(l)
id_order.append(obs)
neighbors[obs] = ngh
weights[obs] = wgt
l = self.file.readline()
if matrix_form:
for obs in neighbors:
neighbors[obs] = [id_order[ngh] for ngh in neighbors[obs]]
self.pos += 1
return W(neighbors, weights)
4.73469387755102
Get neighbor distances for a single observation
>>> w[1] == dict({2.0: 0.3333, 5.0: 0.3333, 6.0: 0.3333})
True
"""
if self.pos > 0:
raise StopIteration
id_type = float
weights, neighbors = self._readlines(id_type)
self.pos += 1
return W(neighbors, weights)
elif distanceType.upper() == DISTTYPE[1]:
weightObj = WEIGHTS.KNN(dataArray, knnNum)
elif distanceType.upper() == DISTTYPE[2]:
alpha = -1 * self.inverseDist
weightObj = WEIGHTS.DistanceBand(\
dataArray, threshold, alpha=alpha)
#### Re-Create WeightObj for NOT 0-based idField ####
if idField:
if ssdo.master2Order.keys() != ssdo.master2Order.values():
o2M = ssdo.order2Master
neighborDict = {o2M[oid] : [o2M[nid] for nid in nbrs] \
for oid,nbrs in weightObj.neighbors.items()}
weightDict = {o2M[oid] : weights \
for oid, weights in weightObj.weights.items()}
weightObj = WEIGHTS.W(neighborDict, weightDict)
#### Save weightObj Class Object for Writing Result ####
self.weightObj = weightObj
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 = WEIGHTS.W(polyNeighborDict)
#### Building up Lower Order Spatial Weights ####
if weightOrder > 1:
ARCPY.SetProgressor("default", \
"Building up Lower Order Spatial Weights...")
origWeight = weightObj
weightObj = WEIGHTS.higher_order(weightObj, weightOrder)
if isLowOrder:
for order in range(weightOrder-1, 1, -1):
lowOrderW = WEIGHTS.higher_order(origWeight, order)
weightObj = WEIGHTS.w_union(weightObj, lowOrderW)
weightObj = WEIGHTS.w_union(weightObj, origWeight)
#### Save weightObj Class Object for Writing Result ####
self.weightObj = weightObj
# Unstandardize if Necessary
nhWeight = nhWeight * sumUnstandard[0]
weights.append(nhWeight)
# Re-Standardize
if nhIDs:
weights = NUM.array(weights)
if rowStandard:
weights = (1.0 / weights.sum()) * weights
# Add To Dict Structures
neighs[orderID] = nhIDs
w[orderID] = weights
swm.close()
wobj = W(neighs, w)
wobj._varName = swm.masterField
return wojb