-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #652 from jGaboardi/lint_format_weights_part_4
lint & format `weights/*.py part 4
- Loading branch information
Showing
4 changed files
with
68 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
""" | ||
Spatial lag operations. | ||
""" | ||
__author__ = "Sergio J. Rey <[email protected]>, David C. Folch <[email protected]>, Levi John Wolf <[email protected]" | ||
__author__ = ( | ||
"Sergio J. Rey <[email protected]>, " | ||
"David C. Folch <[email protected]>, " | ||
"Levi John Wolf <[email protected]" | ||
) | ||
|
||
__all__ = ["lag_spatial", "lag_categorical"] | ||
|
||
import numpy as np | ||
|
@@ -16,21 +21,18 @@ def lag_spatial(w, y): | |
Parameters | ||
---------- | ||
w : W | ||
libpysal spatial weightsobject | ||
y : array | ||
numpy array with dimensionality conforming to w (see examples) | ||
Returns | ||
------- | ||
wy : array | ||
array of numeric values for the spatial lag | ||
Examples | ||
-------- | ||
Setup a 9x9 binary spatial weights matrix and vector of data; compute the | ||
spatial lag of the vector. | ||
|
@@ -50,7 +52,6 @@ def lag_spatial(w, y): | |
array([2. , 2. , 3. , 3.33333333, 4. , | ||
4.66666667, 5. , 6. , 6. ]) | ||
Explicitly define data vector as 9x1 and recompute the spatial lag | ||
>>> y.shape = (9, 1) | ||
|
@@ -66,7 +67,6 @@ def lag_spatial(w, y): | |
[6. ], | ||
[6. ]]) | ||
Take the spatial lag of a 9x2 data matrix | ||
>>> yr = np.arange(8, -1, -1) | ||
|
@@ -83,7 +83,6 @@ def lag_spatial(w, y): | |
[5. , 3. ], | ||
[6. , 2. ], | ||
[6. , 2. ]]) | ||
""" | ||
return w.sparse * y | ||
|
||
|
@@ -97,7 +96,6 @@ def lag_categorical(w, y, ties="tryself"): | |
Parameters | ||
---------- | ||
w : W | ||
PySAL spatial weightsobject | ||
y : iterable | ||
|
@@ -111,6 +109,7 @@ def lag_categorical(w, y, ties="tryself"): | |
and break a tie. If this does not resolve the tie, | ||
a winner is chosen randomly. To just use random choice to | ||
break ties, pass "random" instead. | ||
Returns | ||
------- | ||
an (n x k) column vector containing the most common neighboring observation | ||
|
@@ -124,7 +123,6 @@ def lag_categorical(w, y, ties="tryself"): | |
Examples | ||
-------- | ||
Set up a 9x9 weights matrix describing a 3x3 regular lattice. Lag one list of | ||
categorical variables with no ties. | ||
|
@@ -141,7 +139,9 @@ def lag_categorical(w, y, ties="tryself"): | |
>>> yvect = np.array(y).reshape(9,1) | ||
>>> yvect_l = libpysal.weights.lag_categorical(w,yvect) | ||
>>> check = np.array( [ [i] for i in ['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b']] ) | ||
>>> check = np.array( | ||
... [ [i] for i in ['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b']] | ||
... ) | ||
>>> np.array_equal(yvect_l, check) | ||
True | ||
|
@@ -153,14 +153,12 @@ def lag_categorical(w, y, ties="tryself"): | |
>>> check = np.array([['b', 'd'], ['a', 'c'], ['b', 'c'], ['c', 'd'], ['b', 'd'], ['c', 'c'], ['b', 'd'], ['c', 'd'], ['b', 'c']]) | ||
>>> np.array_equal(check, ym_lag) | ||
True | ||
""" | ||
""" # noqa E501 | ||
if isinstance(y, list): | ||
y = np.array(y) | ||
orig_shape = y.shape | ||
if len(orig_shape) > 1: | ||
if orig_shape[1] > 1: | ||
return np.vstack([lag_categorical(w, col) for col in y.T]).T | ||
if len(orig_shape) > 1 and orig_shape[1] > 1: | ||
return np.vstack([lag_categorical(w, col) for col in y.T]).T | ||
y = y.flatten() | ||
output = np.zeros_like(y) | ||
labels = np.unique(y) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,16 @@ | |
""" | ||
|
||
# ruff: noqa: N802, N803, N999 | ||
|
||
__author__ = "Taylor Oshan <[email protected]> " | ||
|
||
from collections import OrderedDict | ||
|
||
from scipy.sparse import kron | ||
from .weights import W, WSP | ||
|
||
from .distance import DistanceBand | ||
from collections import OrderedDict | ||
from .weights import WSP, W | ||
|
||
|
||
def ODW(Wo, Wd, transform="r", silence_warnings=True): | ||
|
@@ -22,22 +26,19 @@ def ODW(Wo, Wd, transform="r", silence_warnings=True): | |
---------- | ||
Wo : W object for origin locations | ||
o x o spatial weight object amongst o origins | ||
Wd : W object for destination locations | ||
d x d spatial weight object amongst d destinations | ||
transform : Transformation for standardization of final OD spatial weight; default | ||
is 'r' for row standardized | ||
Returns | ||
------- | ||
W : spatial contiguity W object for assocations between flows | ||
ww : spatial contiguity W object for assocations between flows | ||
o*d x o*d spatial weight object amongst o*d flows between o | ||
origins and d destinations | ||
Examples | ||
-------- | ||
>>> import libpysal | ||
>>> O = libpysal.weights.lat2W(2,2) | ||
>>> D = libpysal.weights.lat2W(2,2) | ||
|
@@ -49,33 +50,32 @@ def ODW(Wo, Wd, transform="r", silence_warnings=True): | |
>>> OD.full()[0][0] | ||
array([0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. , 0.25, 0.25, | ||
0. , 0. , 0. , 0. , 0. ]) | ||
""" | ||
if Wo.transform != "b": | ||
try: | ||
Wo.tranform = "b" | ||
except: | ||
except: # noqa E722 | ||
raise AttributeError( | ||
"Wo is not binary and cannot be transformed to " | ||
"binary. Wo must be binary or suitably transformed to binary." | ||
) | ||
) from None | ||
if Wd.transform != "b": | ||
try: | ||
Wd.tranform = "b" | ||
except: | ||
except: # noqa E722 | ||
raise AttributeError( | ||
"Wd is not binary and cannot be transformed to " | ||
"binary. Wd must be binary or suitably transformed to binary." | ||
) | ||
Wo = Wo.sparse | ||
Wo.eliminate_zeros() | ||
Wd = Wd.sparse | ||
Wd.eliminate_zeros() | ||
Ww = kron(Wo, Wd, format="csr") | ||
Ww.eliminate_zeros() | ||
Ww = WSP(Ww).to_W(silence_warnings=silence_warnings) | ||
Ww.transform = transform | ||
return Ww | ||
) from None | ||
wo = Wo.sparse | ||
wo.eliminate_zeros() | ||
wd = Wd.sparse | ||
wd.eliminate_zeros() | ||
ww = kron(wo, wd, format="csr") | ||
ww.eliminate_zeros() | ||
ww = WSP(ww).to_W(silence_warnings=silence_warnings) | ||
ww.transform = transform | ||
return ww | ||
|
||
|
||
def netW(link_list, share="A", transform="r", **kwargs): | ||
|
@@ -88,19 +88,22 @@ def netW(link_list, share="A", transform="r", **kwargs): | |
link_list : list | ||
of tuples where each tuple is of the form (o,d) where o is an | ||
origin id and d is a destination id | ||
share : string | ||
denoting how to define the nodal relationship used to determine neighboring edges; defualt is 'A' for any shared nodes between two network edges; options include: O a shared origin node; D a shared destination node; OD; a shared origin or a shared destination node; C a shared node that is the destination of the first edge and the origin of the second edge - i.e., a directed chain is formed moving from edge one to edge two. | ||
denoting how to define the nodal relationship used to determine | ||
neighboring edges; defualt is 'A' for any shared nodes between two | ||
network edges; options include: O a shared origin node; D a shared | ||
destination node; OD; a shared origin or a shared destination node; | ||
C a shared node that is the destination of the first edge and the | ||
origin of the second edge - i.e., a directed chain is formed moving | ||
from edge one to edge two. | ||
transform : Transformation for standardization of final OD spatial weight; default | ||
is 'r' for row standardized | ||
**kwargs : keyword arguments | ||
optional arguments for :class:`pysal.weights.W` | ||
Returns | ||
------- | ||
W : nodal contiguity W object for networkd edges or flows | ||
net_w : nodal contiguity W object for networkd edges or flows | ||
W Object representing the binary adjacency of the network edges | ||
given a definition of nodal relationshilibpysal.weights.spintW. | ||
|
@@ -117,7 +120,6 @@ def netW(link_list, share="A", transform="r", **kwargs): | |
>>> any_common = libpysal.weights.netW(links, share='A') | ||
>>> any_common.neighbors[('a', 'b')] | ||
[('a', 'c'), ('a', 'd'), ('c', 'b'), ('c', 'a')] | ||
""" | ||
neighbors = {} | ||
neighbors = OrderedDict() | ||
|
@@ -151,9 +153,9 @@ def netW(link_list, share="A", transform="r", **kwargs): | |
raise AttributeError( | ||
"Parameter 'share' must be 'O', 'D'," " 'OD', or 'C'" | ||
) | ||
netW = W(neighbors, **kwargs) | ||
netW.tranform = transform | ||
return netW | ||
net_w = W(neighbors, **kwargs) | ||
net_w.tranform = transform | ||
return net_w | ||
|
||
|
||
def vecW( | ||
|
@@ -166,8 +168,8 @@ def vecW( | |
alpha=-1.0, | ||
binary=True, | ||
ids=None, | ||
build_sp=False, | ||
**kwargs | ||
build_sp=False, # noqa ARG001 | ||
**kwargs, | ||
): | ||
""" | ||
Distance-based spatial weight for vectors that is computed using a | ||
|
@@ -198,7 +200,6 @@ def vecW( | |
distance decay parameter for weight (default -1.0) | ||
if alpha is positive the weights will not decline with | ||
distance. If binary is True, alpha is ignored | ||
ids : list | ||
values to use for keys of the neighbors and weights dicts | ||
build_sp : boolean | ||
|
@@ -209,10 +210,9 @@ def vecW( | |
**kwargs : keyword arguments | ||
optional arguments for :class:`pysal.weights.W` | ||
Returns | ||
------- | ||
W : DistanceBand W object that uses 4-dimenional distances between | ||
w : DistanceBand W object that uses 4-dimenional distances between | ||
vectors origin and destination coordinates. | ||
Examples | ||
|
@@ -228,20 +228,19 @@ def vecW( | |
>>> W2 = libpysal.weights.vecW(x1, y2, x1, y2, threshold=8.5) | ||
>>> list(W2.neighbors[0]) | ||
[1, 2] | ||
""" | ||
data = list(zip(origin_x, origin_y, dest_x, dest_y)) | ||
W = DistanceBand( | ||
data = list(zip(origin_x, origin_y, dest_x, dest_y, strict=True)) | ||
w = DistanceBand( | ||
data, | ||
threshold=threshold, | ||
p=p, | ||
binary=binary, | ||
alpha=alpha, | ||
ids=ids, | ||
build_sp=False, | ||
**kwargs | ||
**kwargs, | ||
) | ||
return W | ||
return w | ||
|
||
|
||
def mat2L(edge_matrix): | ||
|
@@ -261,7 +260,6 @@ def mat2L(edge_matrix): | |
edge_list : list | ||
of tuples where each tuple is of the form (o,d) where o is an | ||
origin id and d is a destination id | ||
""" | ||
if len(edge_matrix.shape) != 2: | ||
raise AttributeError( | ||
|
Empty file.
Oops, something went wrong.