Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Freeman decomposition evaluation\n",
8 "\n",
9 "In this notebook we compare results of the new implementation of the Freeman 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 freeman\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/freeman_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 "result = parse_psp_parameter_string(input_str)\n",
79 "os.system(f\"freeman_decomposition.exe {result}\")\n",
80 "\n",
81 "# CPSP does not create another config file in output dir\n",
82 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
83 ]
84 },
85 {
86 "cell_type": "markdown",
87 "metadata": {},
88 "source": [
89 "## Load ALOS data and C outputs"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": null,
95 "metadata": {},
96 "outputs": [],
97 "source": [
98 "# uncomment to test on S matrix made with SNAP\n",
99 "S = open_netcdf_beam(input_alos_data)"
100 ]
101 },
102 {
103 "cell_type": "markdown",
104 "metadata": {},
105 "source": [
106 "## Apply the decomposition"
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": null,
112 "metadata": {},
113 "outputs": [],
114 "source": [
115 "file_out = \"/data/psp/res/test_freeman.nc\"\n",
116 "# netcdf writer cannot overwrite\n",
117 "if os.path.isfile(file_out):\n",
118 " os.remove(file_out)\n",
119 "\n",
120 "with ProgressBar():\n",
121 " freeman(S, boxcar_size=[7, 7]).to_netcdf(file_out)"
122 ]
123 },
124 {
125 "cell_type": "markdown",
126 "metadata": {},
127 "source": [
128 "# Numerical evaluation"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": null,
134 "metadata": {},
135 "outputs": [],
136 "source": [
137 "import matplotlib.pyplot as plt\n",
138 "# open python output (comment if using compute)\n",
139 "out_py = xr.open_dataset(\"/data/psp/res/test_freeman.nc\")\n",
140 "# open C-PolSARPro outputs\n",
141 "out_c = {}\n",
142 "out_names = ((\"odd\", \"Freeman_Odd\"), (\"double\", \"Freeman_Dbl\"), (\"volume\", \"Freeman_Vol\"))\n",
143 "for name in out_names:\n",
144 " file_name = output_test_dir / f\"{name[1]}.bin\"\n",
145 " out_c[name[0]] = read_psp_bin(file_name)\n",
146 "\n",
147 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
148 "df"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "visualize_errors(out_py=out_py, out_c=out_c)"
158 ]
159 }
160 ],
161 "metadata": {
162 "kernelspec": {
163 "display_name": "psp",
164 "language": "python",
165 "name": "python3"
166 },
167 "language_info": {
168 "codemirror_mode": {
169 "name": "ipython",
170 "version": 3
171 },
172 "file_extension": ".py",
173 "mimetype": "text/x-python",
174 "name": "python",
175 "nbconvert_exporter": "python",
176 "pygments_lexer": "ipython3",
177 "version": "3.12.12"
178 }
179 },
180 "nbformat": 4,
181 "nbformat_minor": 2
182 }