Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Van Zyl 1992 decomposition evaluation\n",
8 "\n",
9 "In this notebook we compare results of the new implementation of the van Zyl 1992 decomposition with the C legacy version of PolSARpro. "
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": null,
15 "metadata": {},
16 "outputs": [],
17 "source": [
18 "%load_ext autoreload\n",
19 "%autoreload 2\n",
20 "\n",
21 "import os\n",
22 "\n",
23 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
24 "import numpy as np\n",
25 "from pathlib import Path\n",
26 "import xarray as xr\n",
27 "from polsarpro.io import open_netcdf_beam\n",
28 "from polsarpro.dev.io import read_psp_bin\n",
29 "from polsarpro.decompositions import vanzyl\n",
30 "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
31 "import shutil\n",
32 "\n",
33 "# optional import for progress bar\n",
34 "from dask.diagnostics import ProgressBar\n",
35 "\n",
36 "# change to your local C-PolSARpro install dir\n",
37 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
38 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
39 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\n",
40 "\n",
41 "# change to your data paths\n",
42 "# original dataset\n",
43 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
44 "\n",
45 "# S2 inputs generated with C-PSP\n",
46 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
47 "\n",
48 "# output files from C\n",
49 "output_test_dir = Path(\"/data/psp/res/vanzyl_c/\")\n",
50 "if not os.path.isdir(output_test_dir):\n",
51 " os.mkdir(output_test_dir)"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {},
57 "source": [
58 "## Run the C-version on some test data"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": null,
64 "metadata": {},
65 "outputs": [],
66 "source": [
67 "input_str = f\"\"\"id: {input_test_dir}\n",
68 "od: {output_test_dir}\n",
69 "iodf: S2\n",
70 "nwr: 7\n",
71 "nwc: 7\n",
72 "ofr: 0\n",
73 "ofc: 0\n",
74 "fnr: 18432\n",
75 "fnc: 1248\n",
76 "errf: /tmp\"\"\"\n",
77 "\n",
78 "if not os.path.exists(output_test_dir):\n",
79 " os.makedirs(output_test_dir)\n",
80 "\n",
81 "\n",
82 "result = parse_psp_parameter_string(input_str)\n",
83 "os.system(f\"vanzyl92_3components_decomposition.exe {result}\")\n",
84 "\n",
85 "# CPSP does not create another config file in output dir\n",
86 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
87 ]
88 },
89 {
90 "cell_type": "markdown",
91 "metadata": {},
92 "source": [
93 "## Load ALOS data and C outputs"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": null,
99 "metadata": {},
100 "outputs": [],
101 "source": [
102 "# uncomment to test on S matrix made with SNAP\n",
103 "S = open_netcdf_beam(input_alos_data)"
104 ]
105 },
106 {
107 "cell_type": "markdown",
108 "metadata": {},
109 "source": [
110 "## Apply the decomposition"
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": null,
116 "metadata": {},
117 "outputs": [],
118 "source": [
119 "file_out = \"/data/psp/res/test_vanzyl.nc\"\n",
120 "# netcdf writer cannot overwrite\n",
121 "if os.path.isfile(file_out):\n",
122 " os.remove(file_out)\n",
123 "\n",
124 "\n",
125 "with ProgressBar():\n",
126 " vanzyl(S, boxcar_size=[7, 7]).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_vanzyl.nc\")\n",
145 "# open C-PolSARPro outputs\n",
146 "out_c = {}\n",
147 "out_names = ((\"odd\", \"VanZyl3_Odd\"), (\"double\", \"VanZyl3_Dbl\"), (\"volume\", \"VanZyl3_Vol\"))\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)\n",
151 "\n",
152 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
153 "df"
154 ]
155 },
156 {
157 "cell_type": "code",
158 "execution_count": null,
159 "metadata": {},
160 "outputs": [],
161 "source": [
162 "visualize_errors(out_py=out_py, out_c=out_c)"
163 ]
164 }
165 ],
166 "metadata": {
167 "kernelspec": {
168 "display_name": "psp",
169 "language": "python",
170 "name": "python3"
171 },
172 "language_info": {
173 "codemirror_mode": {
174 "name": "ipython",
175 "version": 3
176 },
177 "file_extension": ".py",
178 "mimetype": "text/x-python",
179 "name": "python",
180 "nbconvert_exporter": "python",
181 "pygments_lexer": "ipython3",
182 "version": "3.12.12"
183 }
184 },
185 "nbformat": 4,
186 "nbformat_minor": 2
187 }