Skip to content

Commit

Permalink
Merge branch 'master' into lps_weights_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi authored Jan 31, 2021
2 parents 8f94406 + 2694f93 commit 7ebc25e
Show file tree
Hide file tree
Showing 5 changed files with 840 additions and 37 deletions.
11 changes: 11 additions & 0 deletions docsrc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ spint Weights
libpysal.weights.ODW
libpysal.weights.vecW

Weights tools to interface with rasters
+++++++++++++++++++++++++++++++++++++++

.. autosummary::
:toctree: generated/

libpysal.weights.da2W
libpysal.weights.da2WSP
libpysal.weights.w2da
libpysal.weights.wsp2da
libpysal.weights.testDataArray

Weights Util Classes and Functions
++++++++++++++++++++++++++++++++++
Expand Down
754 changes: 754 additions & 0 deletions docsrc/notebooks/Raster_awareness_API.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libpysal/weights/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
WSP2W,
)

from .raster import _da_checker

import copy
from warnings import warn as Warn
from scipy.spatial import distance_matrix
Expand Down
53 changes: 36 additions & 17 deletions libpysal/weights/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,37 @@ def da2W(
Examples
--------
>>> from libpysal.raster import da2W, testDataArray
>>> from libpysal.weights.raster import da2W, testDataArray
>>> da = testDataArray().rename(
{'band': 'layer', 'x': 'longitude', 'y': 'latitude'})
>>> da.dims
('layer', 'latitude', 'longitude')
>>> da.shape
(3, 4, 4)
>>> da.coords
Coordinates:
* layer (layer) int64 1 2 3
* latitude (latitude) float64 90.0 30.0 -30.0 -90.0
* longitude (longitude) float64 -180.0 -60.0 60.0 180.0
>>> da.attrs
{'nodatavals': (-32768.0,)}
>>> coords_labels = {
"z_label": "layer",
"y_label": "latitude",
"x_label": "longitude"
}
>>> w = da2W(da, layer=2, coords_labels=coords_labels)
>>> da.shape
(3, 4, 4)
>>> w = da2W(da, z_value=2, coords_labels=coords_labels)
>>> "%.3f"%w.pct_nonzero
'28.571'
>>> w[3] == {5: 1, 4: 1, 2: 1}
True
>>> w[6] == {5: 1}
'30.000'
>>> w[(2, 90.0, 180.0)] == {(2, 90.0, 60.0): 1, (2, 30.0, 180.0): 1}
True
>>> len(w.index)
7
10
>>> w.index[:2]
MultiIndex([(2, 90.0, 60.0),
(2, 90.0, 180.0)],
names=['layer', 'latitude', 'longitude'])
See Also
--------
Expand Down Expand Up @@ -171,28 +181,37 @@ def da2WSP(
Examples
--------
>>> from libpysal.raster import da2WSP, testDataArray
>>> from libpysal.weights.raster import da2WSP, testDataArray
>>> da = testDataArray().rename(
{'band': 'layer', 'x': 'longitude', 'y': 'latitude'})
>>> da.dims
('layer', 'latitude', 'longitude')
>>> da.shape
(3, 4, 4)
>>> da.coords
Coordinates:
* layer (layer) int64 1 2 3
* latitude (latitude) float64 90.0 30.0 -30.0 -90.0
* longitude (longitude) float64 -180.0 -60.0 60.0 180.0
>>> da.attrs
{'nodatavals': (-32768.0,)}
>>> coords_labels = {
"z_label": "layer",
"y_label": "latitude",
"x_label": "longitude"
}
>>> wsp = da2WSP(da, z_value=2, coords_labels=coords_labels)
>>> wsp.n
7
10
>>> pct_sp = wsp.sparse.nnz *1. / wsp.n**2
>>> "%.3f"%pct_sp
'0.286'
'0.300'
>>> print(wsp.sparse[4].todense())
[[0 0 0 1 0 1 0]]
>>> len(w.index)
7
[[0 0 1 0 0 1 1 1 0 0]]
>>> wsp.index[:2]
MultiIndex([(2, 90.0, 60.0),
(2, 90.0, 180.0)],
names=['layer', 'latitude', 'longitude'])
See Also
--------
Expand Down Expand Up @@ -308,7 +327,7 @@ def w2da(data, w, attrs={}, coords=None):
>>> da = testDataArray()
>>> da.shape
(3, 4, 4)
>>> w = da2W(da, layer=2)
>>> w = da2W(da, z_value=2)
>>> data = np.random.randint(0, 255, len(w.index))
>>> da1 = w2da(data, w)
Expand Down Expand Up @@ -351,7 +370,7 @@ def wsp2da(data, wsp, attrs={}, coords=None):
>>> da = testDataArray()
>>> da.shape
(3, 4, 4)
>>> wsp = da2WSP(da, layer=2)
>>> wsp = da2WSP(da, z_value=2)
>>> data = np.random.randint(0, 255, len(wsp.index))
>>> da1 = w2da(data, wsp)
Expand Down
57 changes: 38 additions & 19 deletions notebooks/Raster_awareness_API.ipynb

Large diffs are not rendered by default.

0 comments on commit 7ebc25e

Please sign in to comment.