Debian Codegraph @

notebooks/dev/test-h-a-alpha-4x4-real-data.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# H A $\\alpha$ decomposition evaluation\n",
8 "\n",
9 "In this notebook we compare results of the new implementation of the\n",
10 "H A $\\alpha$ decomposition with the C legacy version of PolSARpro. \n",
11 "It assumes the user has a working installation of PolSARpro and has followed the \n",
12 "instructions in the README.md file."
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 2,
18 "metadata": {},
19 "outputs": [
20 {
21 "name": "stdout",
22 "output_type": "stream",
23 "text": [
24 "The autoreload extension is already loaded. To reload it, use:\n",
25 " %reload_ext autoreload\n"
26 ]
27 }
28 ],
29 "source": [
30 "%load_ext autoreload\n",
31 "%autoreload 2\n",
32 "\n",
33 "import os\n",
34 "# avoid thread conflicts between numpy and dask\n",
35 "os.environ[\"OMP_NUM_THREADS\"] = \"1\"\n",
36 "os.environ[\"OPENBLAS_NUM_THREADS\"] = \"1\"\n",
37 "os.environ[\"MKL_NUM_THREADS\"] = \"1\"\n",
38 "os.environ[\"VECLIB_MAXIMUM_THREADS\"] = \"1\"\n",
39 "os.environ[\"NUMEXPR_NUM_THREADS\"] = \"1\"\n",
40 "\n",
41 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
42 "from pathlib import Path\n",
43 "import xarray as xr\n",
44 "from polsarpro.io import open_netcdf_beam\n",
45 "from polsarpro.dev.io import read_psp_bin\n",
46 "from polsarpro.util import S_to_T4\n",
47 "from polsarpro.decompositions import h_a_alpha\n",
48 "from polsarpro.dev.metrics import summarize_metrics\n",
49 "import shutil\n",
50 "\n",
51 "# optional import for progress bar\n",
52 "from dask.diagnostics import ProgressBar\n",
53 "\n",
54 "# change to your local C-PolSARpro install dir\n",
55 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
56 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
57 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\n",
58 "\n",
59 "# change to your data paths\n",
60 "# original dataset\n",
61 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
62 "\n",
63 "# input files from C\n",
64 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1\")\n",
65 "\n",
66 "# output files from C\n",
67 "output_test_dir = Path(\"/data/psp/res/h_a_alpha_4x4_c\")"
68 ]
69 },
70 {
71 "cell_type": "markdown",
72 "metadata": {},
73 "source": [
74 "## Run the C-version on some test data"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": null,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "# fl1: alpha, beta, delta, lambda\n",
84 "# fl2: lambda\n",
85 "# fl3: alpha\n",
86 "# fl4: entropy \n",
87 "# fl5: anisotropy\n",
88 "# fl6-9 combinations\n",
89 "\n",
90 "input_str= f\"\"\"id: {input_test_dir} \n",
91 "od: {output_test_dir}\n",
92 "iodf: S2T4\n",
93 "nwr: 7\n",
94 "nwc: 7\n",
95 "ofr: 0\n",
96 "ofc: 0\n",
97 "fnr: 18432\n",
98 "fnc: 1248\n",
99 "fl1: 1\n",
100 "fl2: 1\n",
101 "fl3: 1\n",
102 "fl4: 1\n",
103 "fl5: 1\n",
104 "fl6: 0\n",
105 "fl7: 0\n",
106 "fl8: 0\n",
107 "fl9: 0\n",
108 "errf: /tmp/MemoryAllocError.txt\n",
109 "mask: {input_test_dir}/mask_valid_pixels.bin\n",
110 "\"\"\"\n",
111 "result = parse_psp_parameter_string(input_str)\n",
112 "os.system(f\"h_a_alpha_decomposition.exe {result}\")\n",
113 "\n",
114 "# CPSP does not create another config file in output dir\n",
115 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
116 ]
117 },
118 {
119 "cell_type": "markdown",
120 "metadata": {},
121 "source": [
122 "## Load ALOS data and C outputs"
123 ]
124 },
125 {
126 "cell_type": "code",
127 "execution_count": null,
128 "metadata": {},
129 "outputs": [],
130 "source": [
131 "# uncomment to test on S matrix made with SNAP\n",
132 "S = open_netcdf_beam(input_alos_data)\n",
133 "\n",
134 "out_names = (\"entropy\", \"anisotropy\", \"alpha\", \"beta\", \"delta\", \"gamma\", \"lambda\", \"nhu\", \"epsilon\")"
135 ]
136 },
137 {
138 "cell_type": "markdown",
139 "metadata": {},
140 "source": [
141 "## Apply the decomposition"
142 ]
143 },
144 {
145 "cell_type": "code",
146 "execution_count": null,
147 "metadata": {},
148 "outputs": [],
149 "source": [
150 "file_out = \"/data/psp/res/test_h_a_alpha_4x4.nc\"\n",
151 "# netcdf writer cannot overwrite\n",
152 "if os.path.isfile(file_out):\n",
153 " os.remove(file_out)\n",
154 "\n",
155 "with ProgressBar():\n",
156 " # uncomment to compute and store result in a file\n",
157 " T4 = S_to_T4(S)\n",
158 " h_a_alpha(T4, boxcar_size=[7, 7], flags=out_names).to_netcdf(file_out)"
159 ]
160 },
161 {
162 "cell_type": "markdown",
163 "metadata": {},
164 "source": [
165 "# Numerical evaluation"
166 ]
167 },
168 {
169 "cell_type": "code",
170 "execution_count": null,
171 "metadata": {},
172 "outputs": [],
173 "source": [
174 "# open python output (comment if using compute)\n",
175 "out_py = xr.open_dataset(\"/data/psp/res/test_h_a_alpha_4x4.nc\")\n",
176 "\n",
177 "# open C-PolSARPro outputs\n",
178 "out_c = {}\n",
179 "for name in out_names:\n",
180 " file_name = output_test_dir / f\"{name}.bin\"\n",
181 " out_c[name] = read_psp_bin(file_name)\n",
182 "\n",
183 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
184 "df"
185 ]
186 }
187 ],
188 "metadata": {
189 "kernelspec": {
190 "display_name": "psp",
191 "language": "python",
192 "name": "python3"
193 },
194 "language_info": {
195 "codemirror_mode": {
196 "name": "ipython",
197 "version": 3
198 },
199 "file_extension": ".py",
200 "mimetype": "text/x-python",
201 "name": "python",
202 "nbconvert_exporter": "python",
203 "pygments_lexer": "ipython3",
204 "version": "3.12.12"
205 }
206 },
207 "nbformat": 4,
208 "nbformat_minor": 2
209 }