Debian Codegraph @

notebooks/dev/test-cameron-real-data.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Cameron decomposition evaluation\n",
8 "\n",
9 "In this notebook we compare results of the new implementation of the Cameron decomposition with the C legacy version of PolSARpro. \n",
10 "\n",
11 "## Notes about numerical differences between implementations:\n",
12 "- Cameron is a classification process, meaning it operates with thresholds on the components (max component is retained). \n",
13 "- Errors might be larger than with other decompositions due to \n",
14 " - the n-ary type of output data (differences won't work, we should apply class metrics)\n",
15 " - the fact that it operates on S matrices. Single look data is very noisy and small numerical differences are amplified, especially when dividing with small numbers.\n",
16 "- Not recommended for geocoded data (resampling S matrices leads to a loss of polatrimetric information.)"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": null,
22 "metadata": {},
23 "outputs": [],
24 "source": [
25 "%load_ext autoreload\n",
26 "%autoreload 2\n",
27 "\n",
28 "import os\n",
29 "\n",
30 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
31 "import numpy as np\n",
32 "from pathlib import Path\n",
33 "import xarray as xr\n",
34 "from polsarpro.io import open_netcdf_beam\n",
35 "from polsarpro.dev.io import read_psp_bin\n",
36 "from polsarpro.decompositions import cameron\n",
37 "import shutil\n",
38 "\n",
39 "# optional import for progress bar\n",
40 "from dask.diagnostics import ProgressBar\n",
41 "\n",
42 "# change to your local C-PolSARpro install dir\n",
43 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
44 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
45 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\n",
46 "\n",
47 "# change to your data paths\n",
48 "# original dataset\n",
49 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
50 "\n",
51 "# S2 inputs generated with C-PSP\n",
52 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
53 "\n",
54 "# output files from C\n",
55 "output_test_dir = Path(\"/data/psp/res/freeman_c/\")\n",
56 "if not os.path.isdir(output_test_dir):\n",
57 " os.mkdir(output_test_dir)"
58 ]
59 },
60 {
61 "cell_type": "markdown",
62 "metadata": {},
63 "source": [
64 "## Run the C-version on some test data"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": null,
70 "metadata": {},
71 "outputs": [],
72 "source": [
73 "input_str = f\"\"\"id: {input_test_dir}\n",
74 "od: {output_test_dir}\n",
75 "iodf: S2\n",
76 "ofr: 0\n",
77 "ofc: 0\n",
78 "fnr: 18432\n",
79 "fnc: 1248\n",
80 "col: /home/c_psp/ColorMap/Cameron_ColorMap.pal\n",
81 "errf: /tmp\"\"\"\n",
82 "\n",
83 "result = parse_psp_parameter_string(input_str)\n",
84 "os.system(f\"cameron_decomposition.exe {result}\")\n",
85 "\n",
86 "# CPSP does not create another config file in output dir\n",
87 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
88 ]
89 },
90 {
91 "cell_type": "markdown",
92 "metadata": {},
93 "source": [
94 "## Load ALOS data and C outputs"
95 ]
96 },
97 {
98 "cell_type": "code",
99 "execution_count": null,
100 "metadata": {},
101 "outputs": [],
102 "source": [
103 "# uncomment to test on S matrix made with SNAP\n",
104 "S = open_netcdf_beam(input_alos_data)"
105 ]
106 },
107 {
108 "cell_type": "markdown",
109 "metadata": {},
110 "source": [
111 "## Apply the decomposition"
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": null,
117 "metadata": {},
118 "outputs": [],
119 "source": [
120 "file_out = \"/data/psp/res/test_cameron.nc\"\n",
121 "# netcdf writer cannot overwrite\n",
122 "if os.path.isfile(file_out):\n",
123 " os.remove(file_out)\n",
124 "\n",
125 "with ProgressBar():\n",
126 " cameron(S).to_netcdf(file_out)"
127 ]
128 },
129 {
130 "cell_type": "markdown",
131 "metadata": {},
132 "source": [
133 "# Numerical evaluation"
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": null,
139 "metadata": {},
140 "outputs": [],
141 "source": [
142 "import matplotlib.pyplot as plt\n",
143 "# open python output (comment if using compute)\n",
144 "out_py = xr.open_dataset(\"/data/psp/res/test_cameron.nc\")\n",
145 "# open C-PolSARPro outputs\n",
146 "out_c = {}\n",
147 "out_names = ((\"cameron\", \"Cameron\"),)\n",
148 "for name in out_names:\n",
149 " file_name = output_test_dir / f\"{name[1]}.bin\"\n",
150 " out_c[name[0]] = read_psp_bin(file_name)#.astype(np.int32)"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": null,
156 "metadata": {},
157 "outputs": [],
158 "source": [
159 "arr_c = out_c[\"cameron\"]\n",
160 "arr_py = out_py[\"cameron\"]\n",
161 "\n",
162 "import matplotlib.pyplot as plt\n",
163 "plt.figure(figsize=(15, 10))\n",
164 "plt.subplot(131)\n",
165 "plt.imshow((arr_c - arr_py)[8000:12000,200:1100][::4], interpolation=\"none\", cmap=\"Dark2\")\n",
166 "plt.title(\"diff\")\n",
167 "plt.subplot(132)\n",
168 "plt.imshow(arr_c[8000:12000,200:1100][::4], interpolation=\"none\", cmap=\"Dark2\")\n",
169 "plt.colorbar(fraction=0.046, pad=0.04)\n",
170 "plt.title(\"c\")\n",
171 "plt.subplot(133)\n",
172 "plt.imshow(arr_py[8000:12000,200:1100][::4], interpolation=\"none\", cmap=\"Dark2\")\n",
173 "plt.colorbar(fraction=0.046, pad=0.04)\n",
174 "plt.title(\"py\")"
175 ]
176 }
177 ],
178 "metadata": {
179 "kernelspec": {
180 "display_name": "psp",
181 "language": "python",
182 "name": "python3"
183 },
184 "language_info": {
185 "codemirror_mode": {
186 "name": "ipython",
187 "version": 3
188 },
189 "file_extension": ".py",
190 "mimetype": "text/x-python",
191 "name": "python",
192 "nbconvert_exporter": "python",
193 "pygments_lexer": "ipython3",
194 "version": "3.12.12"
195 }
196 },
197 "nbformat": 4,
198 "nbformat_minor": 2
199 }