Skip to content

Commit 56c3ebf

Browse files
authored
Correct examples in docstrings (#703)
* curvature * focal tools * proximity * viewshed * zonal tools
1 parent 9a5cf1e commit 56c3ebf

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

xrspatial/curvature.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def curvature(agg: xr.DataArray,
121121
.. sourcecode:: python
122122
123123
>>> import numpy as np
124+
>>> import dask.array as da
124125
>>> import xarray as xr
125126
>>> from xrspatial import curvature
126127
>>> flat_data = np.zeros((5, 5), dtype=np.float32)
@@ -187,7 +188,7 @@ def curvature(agg: xr.DataArray,
187188
cupy.asarray(concave_data),
188189
attrs={'res': (10, 10)}, name='concave_cupy_raster')
189190
>>> concave_curv = curvature(concave_raster)
190-
>>> print(type(concave_curv))
191+
>>> print(type(concave_curv.data))
191192
<class 'cupy.core.core.ndarray'>
192193
>>> print(concave_curv)
193194
<xarray.DataArray 'curvature' (dim_0: 5, dim_1: 5)>

xrspatial/focal.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ def mean(agg, passes=1, excludes=[np.nan], name='mean'):
193193
<class 'cupy.core.core.ndarray'>
194194
>>> print(mean_cupy)
195195
<xarray.DataArray 'mean' (dim_0: 5, dim_1: 5)>
196-
array([[0.47928994, 0.47928994, 0.47928994, 0.47928994, 0.47928994],
197-
[0.47928994, 0.47928994, 0.47928994, 0.47928994, 0.47928994],
198-
[0.47928994, 0.47928994, 0.47928994, 0.47928994, 0.47928994],
199-
[0.47928994, 0.47928994, 0.47928994, 0.47928994, 0.47928994],
200-
[0.47928994, 0.47928994, 0.47928994, 0.47928994, 0.47928994]])
196+
array([[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
197+
[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
198+
[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
199+
[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995],
200+
[0.47928995, 0.47928995, 0.47928995, 0.47928995, 0.47928995]])
201201
Dimensions without coordinates: dim_0, dim_1
202202
"""
203203

@@ -358,6 +358,7 @@ def apply(raster, kernel, func=_calc_mean, name='focal_apply'):
358358
[0., 1., 0.]])
359359
>>> # apply kernel mean by default
360360
>>> apply_mean_agg = apply(raster, kernel)
361+
>>> apply_mean_agg
361362
<xarray.DataArray 'focal_apply' (y: 4, x: 5)>
362363
array([[ 2. , 2.25 , 3.25 , 4.25 , 5.33333333],
363364
[ 5.25 , 6. , 7. , 8. , 8.75 ],
@@ -387,13 +388,14 @@ def apply(raster, kernel, func=_calc_mean, name='focal_apply'):
387388
])
388389
>>> @ngjit
389390
>>> def func(kernel_data):
390-
>>> weight = np.array([
391-
[0, 0.5, 0],
392-
[0, 1, 0.5],
393-
[0, 0.5, 0],
394-
])
395-
>>> return np.nansum(kernel_data * weight)
396-
391+
... weight = np.array([
392+
... [0, 0.5, 0],
393+
... [0, 1, 0.5],
394+
... [0, 0.5, 0],
395+
... ])
396+
... return np.nansum(kernel_data * weight)
397+
398+
>>> import dask.array as da
397399
>>> data_da = da.from_array(np.ones((6, 4), dtype=np.float64), chunks=(3, 2))
398400
>>> raster_da = xr.DataArray(data_da, dims=['y', 'x'], name='raster_da')
399401
>>> print(raster_da)
@@ -713,7 +715,7 @@ def hotspots(raster, kernel):
713715
>>> import numpy as np
714716
>>> import xarray as xr
715717
>>> from xrspatial.convolution import custom_kernel
716-
>>> kernel = custom_kernel(np.array([1, 1, 0]))
718+
>>> kernel = custom_kernel(np.array([[1, 1, 0]]))
717719
>>> data = np.array([
718720
... [0, 1000, 1000, 0, 0, 0],
719721
... [0, 0, 0, -1000, -1000, 0],

xrspatial/proximity.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ def manhattan_distance(x1: float, x2: float, y1: float, y2: float) -> float:
112112
>>> from xrspatial import manhattan_distance
113113
>>> point_a = (142.32, 23.23)
114114
>>> point_b = (312.54, 432.01)
115-
>>> # Calculate Euclidean Distance
115+
>>> # Calculate Manhattan Distance
116116
>>> dist = manhattan_distance(
117117
... point_a[0],
118118
... point_b[0],
119119
... point_a[1],
120120
... point_b[1])
121121
>>> print(dist)
122-
196075.9368
122+
579.0
123123
"""
124124

125125
x = x1 - x2
@@ -165,7 +165,7 @@ def great_circle_distance(
165165
>>> from xrspatial import great_circle_distance
166166
>>> point_a = (123.2, 82.32)
167167
>>> point_b = (178.0, 65.09)
168-
>>> # Calculate Euclidean Distance
168+
>>> # Calculate Great Circle Distance
169169
>>> dist = great_circle_distance(
170170
... point_a[0],
171171
... point_b[0],

xrspatial/viewshed.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ def viewshed(raster: xarray.DataArray,
16371637
array([[ 0, 0, 1, 0, 0],
16381638
[ 1, 3, 0, 0, 0],
16391639
[10, 2, 5, 2, -1],
1640-
[20, 1, 2, 9, 0]])
1640+
[11, 1, 2, 9, 0]])
16411641
Coordinates:
16421642
* y (y) float64 1.0 2.0 3.0 4.0
16431643
* x (x) float64 1.0 2.0 3.0 4.0 5.0

xrspatial/zonal.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,8 @@ def stats(
517517
stats() works with Dask with NumPy backed DataArray
518518
>>> import dask.array as da
519519
>>> import dask.array as da
520-
>>> values_dask = xr.DataArray(da.from_array(values, chunks=(3, 3)))
521-
>>> zones_dask = xr.DataArray(da.from_array(zones, chunks=(3, 3)))
522-
520+
>>> values_dask = xr.DataArray(da.from_array(values_data, chunks=(3, 3)))
521+
>>> zones_dask = xr.DataArray(da.from_array(zones_data, chunks=(3, 3)))
523522
>>> # Calculate Stats with dask backed xarray DataArrays
524523
>>> dask_stats_df = stats(zones=zones_dask, values=values_dask)
525524
>>> print(type(dask_stats_df))
@@ -530,20 +529,6 @@ def stats(
530529
1 10 27.0 49 5 675 14.21267 202.0 25
531530
2 20 72.0 94 50 1800 14.21267 202.0 25
532531
3 30 77.0 99 55 1925 14.21267 202.0 25
533-
534-
>>> # Custom Stats with dask backed xarray DataArrays
535-
>>> dask_custom_stats ={'double_sum': lambda val: val.sum()*2}
536-
>>> dask_custom_stats_df = stats(
537-
>>> zones=zones_dask, values=values_dask, stats_funcs=custom_stats
538-
>>> )
539-
>>> print(type(dask_custom_stats_df))
540-
<class 'dask.dataframe.core.DataFrame'>
541-
>>> print(dask_custom_stats_df.compute())
542-
zone double_sum
543-
0 0 1100
544-
1 10 1350
545-
2 20 3600
546-
3 30 3850
547532
"""
548533

549534
validate_arrays(zones, values)
@@ -968,10 +953,10 @@ def crosstab(
968953
.. sourcecode:: python
969954
970955
>>> import dask.array as da
971-
>>> values_dask = xr.DataArray(da.from_array(values, chunks=(3, 3)))
972-
>>> zones_dask = xr.DataArray(da.from_array(zones, chunks=(3, 3)))
973-
>>> df = crosstab(zones=zones_dask, values=values_dask)
974-
>>> print(df)
956+
>>> values_dask = xr.DataArray(da.from_array(values_data, chunks=(3, 3)))
957+
>>> zones_dask = xr.DataArray(da.from_array(zones_data, chunks=(3, 3)))
958+
>>> dask_df = crosstab(zones=zones_dask, values=values_dask)
959+
>>> print(dask_df)
975960
Dask DataFrame Structure:
976961
zone 0.0 10.0 20.0 30.0 40.0 50.0
977962
npartitions=5
@@ -981,7 +966,7 @@ def crosstab(
981966
4 ... ... ... ... ... ... ...
982967
5 ... ... ... ... ... ... ...
983968
Dask Name: astype, 1186 tasks
984-
>>> print(dask_df.compute)
969+
>>> print(dask_df.compute())
985970
zone 0.0 10.0 20.0 30.0 40.0 50.0
986971
0 0 1 0 0 0 0 0
987972
1 1 3 0 0 0 0 0
@@ -1120,6 +1105,7 @@ def apply(
11201105
11211106
>>> import numpy as np
11221107
>>> import xarray as xr
1108+
>>> from xrspatial.zonal import apply
11231109
>>> zones_val = np.array([
11241110
[1, 1, 0, 2],
11251111
[0, 2, 1, 2]])
@@ -1132,7 +1118,7 @@ def apply(
11321118
>>> apply(zones, agg, func)
11331119
>>> agg
11341120
array([[0, 0, 5, 0],
1135-
[3, 0, 0, 0]])
1121+
[3, np.nan, 0, 0]])
11361122
"""
11371123
if not isinstance(zones, xr.DataArray):
11381124
raise TypeError("zones must be instance of DataArray")
@@ -1276,6 +1262,7 @@ def suggest_zonal_canvas(
12761262
>>> from spatialpandas import GeoDataFrame
12771263
>>> import geopandas as gpd
12781264
>>> import datashader as ds
1265+
>>> from xrspatial.zonal import suggest_zonal_canvas
12791266
12801267
>>> df = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
12811268
>>> df = df.to_crs("EPSG:3857")
@@ -1298,12 +1285,16 @@ def suggest_zonal_canvas(
12981285
crs='Mercator',
12991286
min_pixels=min_pixels,
13001287
)
1288+
>>> height, width
1289+
(1537, 2376)
13011290
>>> cvs = ds.Canvas(x_range=x_range, y_range=y_range,
13021291
>>> plot_height=height, plot_width=width)
13031292
>>> spatial_df = GeoDataFrame(df, geometry='geometry')
13041293
>>> agg = cvs.polygons(spatial_df, 'geometry', agg=ds.max('id'))
13051294
>>> min_poly_id = df.area.argmin()
13061295
>>> actual_min_pixels = len(np.where(agg.data==min_poly_id)[0])
1296+
>>> actual_min_pixels
1297+
22
13071298
"""
13081299
full_xrange, full_yrange = get_full_extent(crs)
13091300
xmin, xmax = full_xrange

0 commit comments

Comments
 (0)