Debian Codegraph @

notebooks/wishart-supervised-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Supervised Wishart classification"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "This notebook shows how to apply the supervised Wishart classification to a PolSARpro NetCDF dataset."
15 ]
16 },
17 {
18 "cell_type": "markdown",
19 "metadata": {},
20 "source": [
21 "## Import packages and set directories"
22 ]
23 },
24 {
25 "cell_type": "code",
26 "execution_count": null,
27 "metadata": {},
28 "outputs": [],
29 "source": [
30 "import os \n",
31 "from pathlib import Path\n",
32 "import xarray as xr\n",
33 "from polsarpro.io import open_netcdf_beam\n",
34 "from polsarpro.classification import wishart_supervised\n",
35 "\n",
36 "# optional import for progress bar\n",
37 "from dask.diagnostics import ProgressBar\n",
38 "\n",
39 "# change to your data paths\n",
40 "# original dataset\n",
41 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
42 "output_dir = Path(\"/data/psp/res\")\n",
43 "\n",
44 "# Hand drawn regions\n",
45 "label_path = Path(\"../data/sample_labels_sf_alos1.nc\")\n"
46 ]
47 },
48 {
49 "cell_type": "markdown",
50 "metadata": {},
51 "source": [
52 "## Load data\n",
53 "\n",
54 "We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n",
55 "To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook."
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": null,
61 "metadata": {},
62 "outputs": [],
63 "source": [
64 "# uncomment to test on S matrix made with SNAP\n",
65 "S = open_netcdf_beam(input_alos_data)\n",
66 "\n",
67 "# initial regions labeled as {1...N}, 0 for unlabelled\n",
68 "labels = xr.open_dataarray(label_path)"
69 ]
70 },
71 {
72 "cell_type": "markdown",
73 "metadata": {},
74 "source": [
75 "## Apply the classification\n",
76 "\n",
77 "Let's apply the algorithm and write the result to a NetCDF file.\n",
78 "Optionally we can use a progress bar to monitor the progress of the computation.\n",
79 "\n",
80 "**Note:** Unlike the C implementation, this version does not require precomputed H/A/Alpha parameters. However, for backward compatibility, you may provide existing parameters via the h_a_alpha_result argument. In that case, the H/A/Alpha parameters may have been computed using a different boxcar window size than the one used in this function."
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": null,
86 "metadata": {},
87 "outputs": [],
88 "source": [
89 "# change to the name of your liking\n",
90 "file_out = output_dir / \"psp_wishart_super.nc\"\n",
91 "\n",
92 "# netcdf writer cannot overwrite\n",
93 "if os.path.isfile(file_out):\n",
94 " os.remove(file_out)\n",
95 "\n",
96 "with ProgressBar():\n",
97 " wishart_supervised(S, training_labels=labels, boxcar_size=[7, 7]).to_netcdf(file_out)"
98 ]
99 },
100 {
101 "cell_type": "markdown",
102 "metadata": {},
103 "source": [
104 "## Display outputs"
105 ]
106 },
107 {
108 "cell_type": "markdown",
109 "metadata": {},
110 "source": [
111 "We open the previously saved dataset:"
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": null,
117 "metadata": {},
118 "outputs": [],
119 "source": [
120 "res = xr.open_dataset(file_out)"
121 ]
122 },
123 {
124 "cell_type": "markdown",
125 "metadata": {},
126 "source": [
127 "Then we may plot the individual outputs:"
128 ]
129 },
130 {
131 "cell_type": "code",
132 "execution_count": null,
133 "metadata": {},
134 "outputs": [],
135 "source": [
136 "from matplotlib.colors import ListedColormap\n",
137 "\n",
138 "# Blue: sea, green: vegetation, orange: built-up\n",
139 "colors = [\"#387EB9\", \"#4EAF49\", \"#FF7F00\"]\n",
140 "# Create the discrete colormap\n",
141 "my_cmap = ListedColormap(colors)\n",
142 "\n",
143 "res.wishart_supervised_class.plot.imshow(cmap=my_cmap)"
144 ]
145 }
146 ],
147 "metadata": {
148 "kernelspec": {
149 "display_name": "psp",
150 "language": "python",
151 "name": "python3"
152 },
153 "language_info": {
154 "codemirror_mode": {
155 "name": "ipython",
156 "version": 3
157 },
158 "file_extension": ".py",
159 "mimetype": "text/x-python",
160 "name": "python",
161 "nbconvert_exporter": "python",
162 "pygments_lexer": "ipython3",
163 "version": "3.12.13"
164 }
165 },
166 "nbformat": 4,
167 "nbformat_minor": 2
168 }