Skip to content

Commit 4ee700b

Browse files
authored
More examples to user guide (#742)
* add zonal crosstab example * focal hotspots * more examples on multispectral tools * zonal crosstab example
1 parent f6c5164 commit 4ee700b

File tree

4 files changed

+2678
-44
lines changed

4 files changed

+2678
-44
lines changed

examples/user_guide/3_Zonal.ipynb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
"outputs": [],
158158
"source": [
159159
"from xrspatial import zonal_stats\n",
160+
"\n",
160161
"zones_agg.values = np.nan_to_num(zones_agg.values, copy=False).astype(int)\n",
161162
"zonal_stats(zones_agg, terrain)"
162163
]
@@ -191,6 +192,54 @@
191192
"source": [
192193
"Here the zones are defined by line segments, but they can be any spatial pattern or, more specifically, any region computable as a Datashader aggregate."
193194
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"## Zonal crosstab\n",
201+
"\n",
202+
"Zonal crosstab function can be used when we want to calculate cross-tabulated (categorical stats) areas between two datasets. As an example, assume we have gender data (male/female), and we want to see how all the gender groups distributed over all the zones defined above.\n",
203+
"\n",
204+
"First, let's create a mock dataset for gender: 1 for male, and 2 for female. "
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": null,
210+
"metadata": {},
211+
"outputs": [],
212+
"source": [
213+
"# define a mask where 0s are water, and 1s are land areas\n",
214+
"mask = terrain.data.astype(bool).astype(int)\n",
215+
"\n",
216+
"# gender data where 0s are nodata, 1s are male, and 2s are female\n",
217+
"# assume that the population only takes 10% overall, and the probability of male and female are equal.\n",
218+
"genders = np.random.choice([0, 1, 2], p=[0.9, 0.05, 0.05], size=zones_agg.shape) * mask\n",
219+
"genders_agg = xr.DataArray(genders, coords=terrain.coords, dims=terrain.dims)\n",
220+
"\n",
221+
"# visualize the results\n",
222+
"genders_shaded = shade(genders_agg, cmap=['white', 'blue', 'red'])\n",
223+
"stack(genders_shaded, terrain_shaded)"
224+
]
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"metadata": {},
229+
"source": [
230+
"Now, let's calculate the stats between the 2 datasets of genders and zones."
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": null,
236+
"metadata": {},
237+
"outputs": [],
238+
"source": [
239+
"from xrspatial import zonal_crosstab\n",
240+
"\n",
241+
"zonal_crosstab(zones=zones_agg, values=genders_agg, zone_ids=[11, 12, 13, 14, 15, 16], cat_ids=[1, 2])"
242+
]
194243
}
195244
],
196245
"metadata": {
@@ -209,7 +258,7 @@
209258
"name": "python",
210259
"nbconvert_exporter": "python",
211260
"pygments_lexer": "ipython3",
212-
"version": "3.9.10"
261+
"version": "3.9.14"
213262
}
214263
},
215264
"nbformat": 4,

examples/user_guide/4_Focal.ipynb

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,72 @@
201201
")\n",
202202
"stack(illuminated_shaded, sobel_terrain_shaded)"
203203
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {},
208+
"source": [
209+
"## Focal hotspots\n",
210+
"\n",
211+
"The focal hotspots tool helps identify significant hot and cold spots in a raster. A hot spot has a high value and be surrounded by neighbors with high values as well. Similarly, a cold spot has a low value and surrounded by other low value pixels. The neighborhood of a pixel is defined by an odd sized kernel.\n",
212+
"\n",
213+
"The result of the function is a data array with the following 7 values:\n",
214+
"- 90 for 90% confidence high value cluster\n",
215+
"- 95 for 95% confidence high value cluster\n",
216+
"- 99 for 99% confidence high value cluster\n",
217+
"- 90 for 90% confidence low value cluster\n",
218+
"- 95 for 95% confidence low value cluster\n",
219+
"- 99 for 99% confidence low value cluster\n",
220+
"- 0 for no significance\n",
221+
"\n",
222+
"In this example, let's see if there are any hot or cold spots in our elevation terrain."
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": null,
228+
"metadata": {},
229+
"outputs": [],
230+
"source": [
231+
"kernel = convolution.circle_kernel(cellsize_x, cellsize_y, 5*cellsize_x)\n",
232+
"kernel"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": null,
238+
"metadata": {},
239+
"outputs": [],
240+
"source": [
241+
"from xrspatial.focal import hotspots\n",
242+
"\n",
243+
"elevation_hotspots = hotspots(terrain, kernel)\n",
244+
"np.unique(elevation_hotspots.data)"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
"Visualize hotspots (if any) with Fuchsia color, and cold spots (if any) with Black color."
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": null,
257+
"metadata": {},
258+
"outputs": [],
259+
"source": [
260+
"# visualize the result\n",
261+
"hotspots_shaded = shade(elevation_hotspots, cmap=[\"gray\", \"black\", \"fuchsia\"], alpha=255, how=\"linear\")\n",
262+
"\n",
263+
"stack(hotspots_shaded, terrain_shaded)"
264+
]
204265
}
205266
],
206267
"metadata": {
207268
"kernelspec": {
208-
"display_name": "Python 3",
269+
"display_name": "Python 3 (ipykernel)",
209270
"language": "python",
210271
"name": "python3"
211272
},
@@ -219,7 +280,7 @@
219280
"name": "python",
220281
"nbconvert_exporter": "python",
221282
"pygments_lexer": "ipython3",
222-
"version": "3.8.5"
283+
"version": "3.9.14"
223284
}
224285
},
225286
"nbformat": 4,

0 commit comments

Comments
 (0)