Debian Codegraph @

notebooks/wishart-h-a-alpha-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Wishart H / A / Alpha classification"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "This notebook shows how to apply the Wishart unsupervised 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_h_a_alpha\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 ]
44 },
45 {
46 "cell_type": "markdown",
47 "metadata": {},
48 "source": [
49 "## Load data\n",
50 "\n",
51 "We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n",
52 "To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook."
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": null,
58 "metadata": {},
59 "outputs": [],
60 "source": [
61 "# uncomment to test on S matrix made with SNAP\n",
62 "S = open_netcdf_beam(input_alos_data)"
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "metadata": {},
68 "source": [
69 "## Apply the classification\n",
70 "\n",
71 "Let's apply the algorithm and write the result to a NetCDF file.\n",
72 "Optionally we can use a progress bar to monitor the progress of the computation.\n",
73 "\n",
74 "**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.\n\n**Memory note:** On large products, running the classifier without h_a_alpha_result can require substantial memory when the result is computed or written, even with tol_pct=None, because the H/A/Alpha decomposition is part of the same Dask pipeline. If memory is limited, compute the H/A/Alpha decomposition first, save it to NetCDF, reopen it with xarray, and pass it as h_a_alpha_result."
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": null,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "# change to the name of your liking\n",
84 "file_out = output_dir / \"psp_wishart_haa.nc\"\n",
85 "\n",
86 "# netcdf writer cannot overwrite\n",
87 "if os.path.isfile(file_out):\n",
88 " os.remove(file_out)\n",
89 "\n",
90 "with ProgressBar():\n",
91 " wishart_h_a_alpha(S, boxcar_size=[7, 7]).to_netcdf(file_out)"
92 ]
93 },
94 {
95 "cell_type": "markdown",
96 "metadata": {},
97 "source": [
98 "## Display outputs"
99 ]
100 },
101 {
102 "cell_type": "markdown",
103 "metadata": {},
104 "source": [
105 "We open the previously saved dataset:"
106 ]
107 },
108 {
109 "cell_type": "code",
110 "execution_count": null,
111 "metadata": {},
112 "outputs": [],
113 "source": [
114 "res = xr.open_dataset(file_out)"
115 ]
116 },
117 {
118 "cell_type": "markdown",
119 "metadata": {},
120 "source": [
121 "Then we may plot the individual outputs:"
122 ]
123 },
124 {
125 "cell_type": "markdown",
126 "metadata": {},
127 "source": [
128 "### 8 Classes based on H/Alpha"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": null,
134 "metadata": {},
135 "outputs": [],
136 "source": [
137 "res.wishart_h_alpha_class.plot.imshow(cmap=\"Dark2\")"
138 ]
139 },
140 {
141 "cell_type": "markdown",
142 "metadata": {},
143 "source": [
144 "### 16 Classes based on H/A/Alpha"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": null,
150 "metadata": {},
151 "outputs": [],
152 "source": [
153 "res.wishart_h_a_alpha_class.plot.imshow(cmap=\"tab20\")"
154 ]
155 }
156 ],
157 "metadata": {
158 "kernelspec": {
159 "display_name": "Python 3",
160 "language": "python",
161 "name": "python3"
162 },
163 "language_info": {
164 "codemirror_mode": {
165 "name": "ipython",
166 "version": 3
167 },
168 "file_extension": ".py",
169 "mimetype": "text/x-python",
170 "name": "python",
171 "nbconvert_exporter": "python",
172 "pygments_lexer": "ipython3",
173 "version": "3.12.13"
174 }
175 },
176 "nbformat": 4,
177 "nbformat_minor": 2
178 }