Debian Codegraph @

notebooks/dev/test-refined-lee-real-data.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Lee refined speckle filter evaluation"
8 ]
9 },
10 {
11 "cell_type": "code",
12 "execution_count": null,
13 "metadata": {},
14 "outputs": [],
15 "source": [
16 "%load_ext autoreload\n",
17 "%autoreload 2\n",
18 "\n",
19 "import os\n",
20 "\n",
21 "\n",
22 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
23 "from pathlib import Path\n",
24 "from polsarpro.io import open_netcdf_beam, polmat_to_netcdf\n",
25 "from polsarpro.dev.io import read_C3_psp, read_T3_psp\n",
26 "from polsarpro.speckle_filters import refined_lee\n",
27 "from polsarpro.util import S_to_C3, S_to_T3\n",
28 "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
29 "import shutil\n",
30 "\n",
31 "import matplotlib.pyplot as plt\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}/speckle_filter/\"\n",
39 "\n",
40 "# change to your data paths\n",
41 "# original dataset\n",
42 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
43 "\n",
44 "# S2 inputs generated with C-PSP\n",
45 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1\")\n",
46 "\n",
47 "# output files from C\n",
48 "output_test_dir = Path(\"/data/psp/res/ref_lee_c\")\n",
49 "if not output_test_dir.exists():\n",
50 " output_test_dir.mkdir(parents=True)\n",
51 "\n",
52 "output_type = \"T3\"\n",
53 "if output_type not in [\"T3\", \"C3\"]:\n",
54 " raise ValueError(\"output_type must be T3 or C3\")"
55 ]
56 },
57 {
58 "cell_type": "markdown",
59 "metadata": {},
60 "source": [
61 "## Run the C-version on some test data"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": null,
67 "metadata": {},
68 "outputs": [],
69 "source": [
70 "input_str= f\"\"\"id: {input_test_dir} \n",
71 "od: {output_test_dir}\n",
72 "iodf: S2{output_type}\n",
73 "nw: 11\n",
74 "nlk: 1\n",
75 "ofr: 0\n",
76 "ofc: 0\n",
77 "fnr: 18432\n",
78 "fnc: 1248\n",
79 "errf: /tmp/MemoryAllocError.txt\n",
80 "mask: {input_test_dir}/mask_valid_pixels.bin\n",
81 "\"\"\"\n",
82 "result = parse_psp_parameter_string(input_str)\n",
83 "os.system(f\"lee_refined_filter.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 "if os.path.exists(output_test_dir / f\"refined_lee_{output_type}_py.nc\"):\n",
120 " os.remove(output_test_dir / f\"refined_lee_{output_type}_py.nc\")\n",
121 "\n",
122 "with ProgressBar():\n",
123 " if output_type == \"T3\":\n",
124 " out_py = refined_lee(S_to_T3(S), window_size=11, num_looks=1)\n",
125 " else:\n",
126 " out_py = refined_lee(S_to_C3(S), window_size=11, num_looks=1)\n",
127 " # use custom function that writes complex data and preserves chunks\n",
128 " polmat_to_netcdf(out_py, output_test_dir / f\"refined_lee_{output_type}_py.nc\")\n",
129 " "
130 ]
131 },
132 {
133 "cell_type": "markdown",
134 "metadata": {},
135 "source": [
136 "# Numerical evaluation"
137 ]
138 },
139 {
140 "cell_type": "code",
141 "execution_count": null,
142 "metadata": {},
143 "outputs": [],
144 "source": [
145 "if output_type == \"T3\":\n",
146 " out_c = read_T3_psp(output_test_dir).compute()\n",
147 "else:\n",
148 " out_c = read_C3_psp(output_test_dir).compute()\n",
149 "out_py = open_netcdf_beam(output_test_dir / f\"refined_lee_{output_type}_py.nc\").compute()"
150 ]
151 },
152 {
153 "cell_type": "code",
154 "execution_count": null,
155 "metadata": {},
156 "outputs": [],
157 "source": [
158 "df = summarize_metrics(out_c, out_py)\n",
159 "df"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "visualize_errors(out_py=out_py, out_c=out_c)"
169 ]
170 }
171 ],
172 "metadata": {
173 "kernelspec": {
174 "display_name": "psp",
175 "language": "python",
176 "name": "python3"
177 },
178 "language_info": {
179 "codemirror_mode": {
180 "name": "ipython",
181 "version": 3
182 },
183 "file_extension": ".py",
184 "mimetype": "text/x-python",
185 "name": "python",
186 "nbconvert_exporter": "python",
187 "pygments_lexer": "ipython3",
188 "version": "3.12.12"
189 }
190 },
191 "nbformat": 4,
192 "nbformat_minor": 2
193 }