Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# define customization
scatter_kwds.setdefault('alpha', 0.6)
scatter_kwds.setdefault('s', 40)
fitline_kwds.setdefault('alpha', 0.9)
# get fig and ax
fig, ax = _create_moran_fig_ax(ax, figsize=(7,7))
# set labels
ax.set_xlabel('Attribute')
ax.set_ylabel('Spatial Lag')
ax.set_title('Moran BV Local Scatterplot')
# plot and set standards
lag = lag_spatial(moran_loc_bv.w, moran_loc_bv.zy)
fit = OLS(moran_loc_bv.zy[:, None], lag[:, None])
# v- and hlines
ax.axvline(0, alpha=0.5, color='k', linestyle='--')
ax.axhline(0, alpha=0.5, color='k', linestyle='--')
if p is not None:
fitline_kwds.setdefault('color', 'k')
scatter_kwds.setdefault('cmap', hmap)
scatter_kwds.setdefault('c', spots_bv)
ax.plot(lag, fit.predy, **fitline_kwds)
ax.scatter(moran_loc_bv.zx, fit.predy,
**scatter_kwds)
else:
scatter_kwds.setdefault('color', splot_colors['moran_base'])
fitline_kwds.setdefault('color', splot_colors['moran_fit'])
ax.plot(lag, fit.predy, **fitline_kwds)
ax.scatter(moran_loc_bv.zy, fit.predy, **scatter_kwds)
def __calc(self, z):
zl = lag_spatial(self.w, z)
bb = sum(z * zl) / 2.0
zw = 1 - z
zl = lag_spatial(self.w, zw)
ww = sum(zw * zl) / 2.0
bw = self.J - (bb + ww)
return (bb, ww, bw)
def __calc(self, z, op):
if op == 'c': # cross-product
zl = lag_spatial(self.w, z)
g = (z * zl).sum()
elif op == 's': # squared difference
zs = np.zeros(z.shape)
z2 = z ** 2
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zs[i] = sum([wij * (z2[i] - 2.0 * z[i] * z[
j] + z2[j]) for j, wij in zw])
g = zs.sum()
elif op == 'a': # absolute difference
zs = np.zeros(z.shape)
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
def calc(self, w, z):
zl = slag(w, z)
return self.n_1 * self.z * zl / self.den
def calc(self, w, zx, zy):
zly = slag(w, zy)
return self.n_1 * self.zx * zly / self.den
def __quads(self):
zl = slag(self.w, self.z)
zp = self.z > 0
lp = zl > 0
pp = zp * lp
np = (1 - zp) * lp
nn = (1 - zp) * (1 - lp)
pn = zp * (1 - lp)
self.q = self.quads[0] * pp + self.quads[1] * np + self.quads[2] * nn \
+ self.quads[3] * pn
def __init__(self, e, b, w):
if not w.id_order_set:
raise ValueError("w id_order must be set to align with the order of e and b")
else:
e = np.asarray(e).reshape(-1,1)
b = np.asarray(b).reshape(-1,1)
w.transform = 'b'
w_e, w_b = slag(w, e), slag(w, b)
self.r = (e + w_e) / (b + w_b)
w.transform = 'o'
scatter_kwds.setdefault('s', 40)
fitline_kwds.setdefault('alpha', 0.9)
fitline_kwds.setdefault('color', splot_colors['moran_fit'])
# get fig and ax
fig, ax = _create_moran_fig_ax(ax, figsize=(7,7))
# set labels
ax.set_xlabel('Attribute X')
ax.set_ylabel('Spatial Lag of Y')
ax.set_title('Bivariate Moran Scatterplot' +
' (' + str(round(moran_bv.I, 2)) + ')')
# plot and set standards
lag = lag_spatial(moran_bv.w, moran_bv.zy)
fit = OLS(moran_bv.zy[:, None], lag[:, None])
# plot
ax.scatter(moran_bv.zx, lag, **scatter_kwds)
ax.plot(lag, fit.predy, **fitline_kwds)
# v- and hlines
ax.axvline(0, alpha=0.5, color='k', linestyle='--')
ax.axhline(0, alpha=0.5, color='k', linestyle='--')
return fig, ax
def calc(self):
y = self.y
y2 = y * y
self.y_sum = y_sum = sum(y)
y2_sum = sum(y2)
if not self.star:
yl = 1.0 * slag(self.w, y)
ydi = y_sum - y
self.Gs = yl / ydi
N = self.n - 1
yl_mean = ydi / N
s2 = (y2_sum - y2) / N - (yl_mean) ** 2
else:
self.w.transform = "B"
yl = 1.0 * slag(self.w, y)
yl += y
if self.w_transform == "r":
yl = yl / (self.__getCardinalities() + 1.0)
self.Gs = yl / y_sum
N = self.n
yl_mean = y.mean()
s2 = y.var()
EGs_num, VGs_num = 1.0, 1.0
if self.w_transform == "b":
W = self.__getCardinalities()
W += self.star
EGs_num = W * 1.0
VGs_num = (W * (1.0 * N - W)) / (1.0 * N - 1)
self.EGs = (EGs_num * 1.0) / N