Skip to content

Commit

Permalink
Merge pull request #343 from MgeeeeK/rastint_opti
Browse files Browse the repository at this point in the history
[WIP]: Optimized raster-based weights builder
  • Loading branch information
darribas committed Jan 25, 2021
2 parents 1cd5cfb + db8b7b9 commit 66f66aa
Show file tree
Hide file tree
Showing 7 changed files with 743 additions and 974 deletions.
2 changes: 2 additions & 0 deletions ci/38.yaml
Expand Up @@ -15,4 +15,6 @@ dependencies:
# optional
- geopandas>=0.7.0
- numba
- xarray
- joblib
- zstd
86 changes: 70 additions & 16 deletions libpysal/weights/contiguity.py
Expand Up @@ -183,9 +183,21 @@ def from_dataframe(
)

@classmethod
def from_xarray(cls, da, z_value=None, coords_labels={}, sparse=False, **kwargs):
def from_xarray(
cls,
da,
z_value=None,
coords_labels={},
k=1,
include_nodata=False,
n_jobs=1,
sparse=True,
**kwargs,
):
"""
Construct a weights object from a xarray.DataArray.
Construct a weights object from a xarray.DataArray with an additional
attribute index containing coordinate values of the raster
in the form of Pandas.Index/MultiIndex.
Parameters
----------
Expand All @@ -196,27 +208,42 @@ def from_xarray(cls, da, z_value=None, coords_labels={}, sparse=False, **kwargs)
coords_labels : dictionary
Pass dimension labels for coordinates and layers if they do not
belong to default dimensions, which are (band/time, y/lat, x/lon)
e.g. dims = {"y_label": "latitude", "x_label": "longitude", "z_label": "year"}
e.g. coords_labels = {"y_label": "latitude", "x_label": "longitude", "z_label": "year"}
Default is {} empty dictionary.
sparse : boolean
type of weight object. Default is False. For sparse, sparse = True
type of weight object. Default is True. For libpysal.weights.W, sparse = False
k : int
Order of contiguity, this will select all neighbors upto kth order.
Default is 1.
include_nodata : boolean
If True, missing values will be assumed as non-missing when
selecting higher_order neighbors, Default is False
n_jobs : int
Number of cores to be used in the sparse weight construction. If -1,
all available cores are used. Default is 1.
**kwargs : keyword arguments
optional arguments passed when sparse = False
Returns
-------
w : libpysal.weights.W/libpysal.weights.WSP
instance of spatial weights class W or WSP
instance of spatial weights class W or WSP with an index attribute
Notes
-----
1. Lower order contiguities are also selected.
2. Returned object contains `index` attribute that includes a
`Pandas.MultiIndex` object from the DataArray.
See Also
--------
:class:`libpysal.weights.weights.W`
:class:`libpysal.weights.weights.WSP`
:class:`libpysal.weights.weights.WSP`
"""
if sparse:
w = da2WSP(da, "rook", z_value, coords_labels)
w = da2WSP(da, "rook", z_value, coords_labels, k, include_nodata)
else:
w = da2W(da, "rook", z_value, coords_labels, **kwargs)
w = da2W(da, "rook", z_value, coords_labels, k, include_nodata, **kwargs)
return w


Expand Down Expand Up @@ -386,9 +413,21 @@ def from_dataframe(cls, df, geom_col=None, **kwargs):
return w

@classmethod
def from_xarray(cls, da, z_value=None, coords_labels={}, sparse=False, **kwargs):
def from_xarray(
cls,
da,
z_value=None,
coords_labels={},
k=1,
include_nodata=False,
n_jobs=1,
sparse=True,
**kwargs,
):
"""
Construct a weights object from a xarray.DataArray.
Construct a weights object from a xarray.DataArray with an additional
attribute index containing coordinate values of the raster
in the form of Pandas.Index/MultiIndex.
Parameters
----------
Expand All @@ -399,27 +438,42 @@ def from_xarray(cls, da, z_value=None, coords_labels={}, sparse=False, **kwargs)
coords_labels : dictionary
Pass dimension labels for coordinates and layers if they do not
belong to default dimensions, which are (band/time, y/lat, x/lon)
e.g. dims = {"y_label": "latitude", "x_label": "longitude", "z_label": "year"}
e.g. coords_labels = {"y_label": "latitude", "x_label": "longitude", "z_label": "year"}
Default is {} empty dictionary.
sparse : boolean
type of weight object. Default is False. For sparse, sparse = True
type of weight object. Default is True. For libpysal.weights.W, sparse = False
k : int
Order of contiguity, this will select all neighbors upto kth order.
Default is 1.
include_nodata : boolean
If True, missing values will be assumed as non-missing when
selecting higher_order neighbors, Default is False
n_jobs : int
Number of cores to be used in the sparse weight construction. If -1,
all available cores are used. Default is 1.
**kwargs : keyword arguments
optional arguments passed when sparse = False
Returns
-------
w : libpysal.weights.W/libpysal.weights.WSP
instance of spatial weights class W or WSP
instance of spatial weights class W or WSP with an index attribute
Notes
-----
1. Lower order contiguities are also selected.
2. Returned object contains `index` attribute that includes a
`Pandas.MultiIndex` object from the DataArray.
See Also
--------
:class:`libpysal.weights.weights.W`
:class:`libpysal.weights.weights.WSP`
:class:`libpysal.weights.weights.WSP`
"""
if sparse:
w = da2WSP(da, "queen", z_value, coords_labels)
w = da2WSP(da, "queen", z_value, coords_labels, k, include_nodata)
else:
w = da2W(da, "queen", z_value, coords_labels, **kwargs)
w = da2W(da, "queen", z_value, coords_labels, k, include_nodata, **kwargs)
return w


Expand Down

0 comments on commit 66f66aa

Please sign in to comment.