Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Polarimetric Whitening Filter (PWF)\n",
8 "\n",
9 "In this notebook we compare results of the new implementation of the PWF 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.util import S_to_C3\n",
30 "from polsarpro.speckle_filters import pwf\n",
31 "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
32 "import shutil\n",
33 "\n",
34 "# optional import for progress bar\n",
35 "from dask.diagnostics import ProgressBar\n",
36 "\n",
37 "# change to your local C-PolSARpro install dir\n",
38 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
39 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
40 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/speckle_filter/\"\n",
41 "\n",
42 "# change to your data paths\n",
43 "# original dataset\n",
44 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
45 "\n",
46 "# input files from C (use S matrix)\n",
47 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
48 "\n",
49 "# output files from C\n",
50 "output_test_dir = Path(\"/data/psp/res/pwf_c/\")\n",
51 "if not os.path.isdir(output_test_dir):\n",
52 " os.mkdir(output_test_dir)"
53 ]
54 },
55 {
56 "cell_type": "markdown",
57 "metadata": {},
58 "source": [
59 "## Run the C-version on some test data"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": null,
65 "metadata": {},
66 "outputs": [],
67 "source": [
68 "input_str = f\"\"\"id: {input_test_dir}\n",
69 "od: {output_test_dir}\n",
70 "iodf: S2\n",
71 "nwr: 9\n",
72 "nwc: 9\n",
73 "ofr: 0\n",
74 "ofc: 0\n",
75 "fnr: 18432\n",
76 "fnc: 1248\n",
77 "errf: /tmp\"\"\"\n",
78 "\n",
79 "result = parse_psp_parameter_string(input_str)\n",
80 "os.system(f\"PWF_filter.exe {result}\")\n",
81 "\n",
82 "# CPSP does not create another config file in output dir\n",
83 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
84 ]
85 },
86 {
87 "cell_type": "markdown",
88 "metadata": {},
89 "source": [
90 "## Load ALOS data and C outputs"
91 ]
92 },
93 {
94 "cell_type": "code",
95 "execution_count": null,
96 "metadata": {},
97 "outputs": [],
98 "source": [
99 "# uncomment to test on S matrix made with SNAP\n",
100 "S = open_netcdf_beam(input_alos_data)"
101 ]
102 },
103 {
104 "cell_type": "markdown",
105 "metadata": {},
106 "source": [
107 "## Apply the decomposition"
108 ]
109 },
110 {
111 "cell_type": "code",
112 "execution_count": null,
113 "metadata": {},
114 "outputs": [],
115 "source": [
116 "file_out = \"/data/psp/res/test_pwf.nc\"\n",
117 "# netcdf writer cannot overwrite\n",
118 "if os.path.isfile(file_out):\n",
119 " os.remove(file_out)\n",
120 "\n",
121 "with ProgressBar():\n",
122 " res = pwf(S_to_C3(S), train_window_size=[9, 9], test_window_size=[1, 1]).to_netcdf(file_out)"
123 ]
124 },
125 {
126 "cell_type": "markdown",
127 "metadata": {},
128 "source": [
129 "# Numerical evaluation"
130 ]
131 },
132 {
133 "cell_type": "code",
134 "execution_count": null,
135 "metadata": {},
136 "outputs": [],
137 "source": [
138 "import matplotlib.pyplot as plt\n",
139 "# open python output (comment if using compute)\n",
140 "out_py = xr.open_dataset(\"/data/psp/res/test_pwf.nc\")\n",
141 "# open C-PolSARPro outputs\n",
142 "out_c = {}\n",
143 "out_names = [(\"pwf\", \"PWF\")]\n",
144 "for name in out_names:\n",
145 " file_name = output_test_dir / f\"{name[1]}.bin\"\n",
146 " print(f\"Loading {file_name}\")\n",
147 " out_c[name[0]] = read_psp_bin(file_name)\n",
148 "\n",
149 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
150 "df"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": null,
156 "metadata": {},
157 "outputs": [],
158 "source": [
159 "visualize_errors(out_py=out_py, out_c=out_c)"
160 ]
161 }
162 ],
163 "metadata": {
164 "kernelspec": {
165 "display_name": "psp",
166 "language": "python",
167 "name": "python3"
168 },
169 "language_info": {
170 "codemirror_mode": {
171 "name": "ipython",
172 "version": 3
173 },
174 "file_extension": ".py",
175 "mimetype": "text/x-python",
176 "name": "python",
177 "nbconvert_exporter": "python",
178 "pygments_lexer": "ipython3",
179 "version": "3.12.12"
180 }
181 },
182 "nbformat": 4,
183 "nbformat_minor": 2
184 }