Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
header = self.file.readline().strip().split()
header_n = len(header)
n = int(header[0])
if header_n > 1:
n = int(header[1])
w = {}
typ = self.data_type
for i in range(n):
id, n_neighbors = self.file.readline().strip().split()
id = typ(id)
n_neighbors = int(n_neighbors)
neighbors_i = map(typ, self.file.readline().strip().split())
neighbors[id] = neighbors_i
ids.append(id)
self.pos += 1
return W(neighbors, id_order=ids)
for i in xrange(no_obs):
origin, no_nghs = tuple(unpack('<2l', self.file.read(8)))
neighbors[origin] = []
weights[origin] = []
if no_nghs > 0:
neighbors[origin] = list(unpack('<%il' %
no_nghs, self.file.read(4 * no_nghs)))
if fixedWeights:
weights[origin] = list(unpack('
Read in the newly created dat file
>>> wnew = pysal.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):
self._writelines(obj)
else:
raise TypeError("Expected a pysal weights object, got: %s" % (
type(obj)))
if id_type == 'string':
ids = ['id' + str(i) for i in ids]
elif id_type == 'float':
ids = [i * 1. for i in ids]
if id_type == 'string' or id_type == 'float':
id_dict = dict(zip(range(n), ids))
alt_w = {}
alt_weights = {}
for i in w:
values = [id_dict[j] for j in w[i]]
key = id_dict[i]
alt_w[key] = values
alt_weights[key] = weights[i]
w = alt_w
weights = alt_weights
return pysal.weights.W(w, weights, ids=ids, id_order=ids[:])
>>> wnew = pysal.open(fname,'r','geobugs_text').read()
WARNING: there are 3 disconnected observations
Island ids: [6, 8, 11]
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):
cardinalities, neighbors, weights = [], [], []
for i in obj.id_order:
cardinalities.append(obj.cardinalities[i])
neighbors.extend(obj.neighbors[i])
weights.extend(obj.weights[i])
self.file.write('list(')
self.file.write('num=c(%s),' % ','.join(map(str, cardinalities)))
self.file.write('adj=c(%s),' % ','.join(map(str, neighbors)))
self.file.write('sumNumNeigh=%i)' % sum(cardinalities))
self.pos += 1
else:
raise TypeError("Expected a pysal weights object, got: %s" % (
type(obj)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
>>> regimes = ['n','n','s','s','e','e','w','w','e']
>>> n = len(regimes)
>>> w = block_weights(regimes)
>>> w.neighbors
{0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8], 6: [7], 7: [6], 8: [4, 5]}
"""
rids = np.unique(regimes)
neighbors = {}
NPNZ = np.nonzero
regimes = np.array(regimes)
for rid in rids:
members = NPNZ(regimes == rid)[0]
for member in members:
neighbors[member] = members[NPNZ(members != member)[0]].tolist()
w = pysal.weights.W(neighbors)
if ids is not None:
w.remap_ids(ids)
if sparse:
w = pysal.weights.WSP(w.sparse, id_order=ids)
return w
raise Exception("w must be a spatial weights object")
new_neigh = {}
new_weights = {}
for key, value in w.neighbors.iteritems():
new_values = [old2new[i] for i in value]
new_key = old2new[key]
new_neigh[new_key] = new_values
new_weights[new_key] = copy.copy(w.weights[key])
if id_order:
return pysal.weights.W(new_neigh, new_weights, id_order)
else:
if w.id_order:
id_order = [old2new[i] for i in w.id_order]
return pysal.weights.W(new_neigh, new_weights, id_order)
else:
return pysal.weights.W(new_neigh, new_weights)
self._cache = {}
mtype = kwargs.pop('mtype', 'OLS')
self._mtype = mtype
self._mfunc = sr._everything[mtype]
self._fit = kwargs.pop('fit', True)
if isinstance(args[0], str):
formula = args[0]
data = kwargs.pop('data')
matrices = pandashandler(formula, data)
elif 'formula' in kwargs.keys() and 'data' in kwargs.keys():
formula = kwargs.pop('formula')
data = kwargs.pop('data')
matrices = pandashandler(formula, data)
else:
matrices = [arg for arg in args if not isinstance(arg, W)]
args = matrices + [arg for arg in args if isinstance(arg, W)]
if self._fit:
self._called = self._mfunc(*args, **kwargs)
for name in dir(self._called):
try:
exec('self.{n} = self._called.{n}'.format(n=name))
except:
print("Assigning {a} from {s} to {d} failed!".format(a=name,
s=self._called,
d=self))
if id_type == 'string':
ids = ['id' + str(i) for i in ids]
elif id_type == 'float':
ids = [i * 1. for i in ids]
if id_type == 'string' or id_type == 'float':
id_dict = dict(zip(range(n), ids))
alt_w = {}
alt_weights = {}
for i in w:
values = [id_dict[j] for j in w[i]]
key = id_dict[i]
alt_w[key] = values
alt_weights[key] = weights[i]
w = alt_w
weights = alt_weights
return pysal.weights.W(w, weights, ids=ids, id_order=ids[:])
self._mtype = mtype
self._mfunc = sr._everything[mtype]
self._fit = kwargs.pop('fit', True)
if isinstance(args[0], str):
formula = args[0]
data = kwargs.pop('data')
matrices = pandashandler(formula, data)
elif 'formula' in kwargs.keys() and 'data' in kwargs.keys():
formula = kwargs.pop('formula')
data = kwargs.pop('data')
matrices = pandashandler(formula, data)
else:
matrices = [arg for arg in args if not isinstance(arg, W)]
args = matrices + [arg for arg in args if isinstance(arg, W)]
if self._fit:
self._called = self._mfunc(*args, **kwargs)
for name in dir(self._called):
try:
exec('self.{n} = self._called.{n}'.format(n=name))
except:
print("Assigning {a} from {s} to {d} failed!".format(a=name,
s=self._called,
d=self))