libpysal.weights.da2WSP¶
- libpysal.weights.da2WSP(da, criterion='queen', z_value=None, coords_labels={}, k=1, include_nodata=False, n_jobs=1)[source]¶
Create a WSP object from xarray.DataArray with an additional attribute index containing coordinate values of the raster in the form of Pandas.Index/MultiIndex.
- Parameters:
- da
xarray.DataArray
Input 2D or 3D DataArray with shape=(z, y, x)
- criterion{“rook”, “queen”}
Type of contiguity. Default is queen.
- z_valueint/string/float
Select the z_value of 3D DataArray with multiple layers.
- 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. coords_labels = {“y_label”: “latitude”, “x_label”: “longitude”, “z_label”: “year”} Default is {} empty dictionary.
- k
int
Order of contiguity, this will select all neighbors upto kth order. Default is 1.
- include_nodatabool
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.
- da
- Returns:
- wsp
libpysal.weights.WSP
instance of spatial weights class WSP with an index attribute
- wsp
See also
Notes
Lower order contiguities are also selected.
Returned object contains index attribute that includes a Pandas.MultiIndex object from the DataArray.
Examples
>>> 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 10 >>> pct_sp = wsp.sparse.nnz *1. / wsp.n**2 >>> "%.3f"%pct_sp '0.300' >>> print(wsp.sparse[4].todense()) [[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'])