| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# H A $\\alpha$ decomposition evaluation\n", |
| 8 |
"\n", |
| 9 |
"In this notebook we compare results of the new implementation of the\n", |
| 10 |
"H A $\\alpha$ decomposition with the C legacy version of PolSARpro. \n", |
| 11 |
"It assumes the user has a working installation of PolSARpro and has followed the \n", |
| 12 |
"instructions in the README.md file." |
| 13 |
] |
| 14 |
}, |
| 15 |
{ |
| 16 |
"cell_type": "code", |
| 17 |
"execution_count": null, |
| 18 |
"metadata": {}, |
| 19 |
"outputs": [], |
| 20 |
"source": [ |
| 21 |
"%load_ext autoreload\n", |
| 22 |
"%autoreload 2\n", |
| 23 |
"\n", |
| 24 |
"import os\n", |
| 25 |
"# avoid thread conflicts between numpy and dask\n", |
| 26 |
"os.environ[\"OMP_NUM_THREADS\"] = \"1\"\n", |
| 27 |
"os.environ[\"OPENBLAS_NUM_THREADS\"] = \"1\"\n", |
| 28 |
"os.environ[\"MKL_NUM_THREADS\"] = \"1\"\n", |
| 29 |
"os.environ[\"VECLIB_MAXIMUM_THREADS\"] = \"1\"\n", |
| 30 |
"os.environ[\"NUMEXPR_NUM_THREADS\"] = \"1\"\n", |
| 31 |
"\n", |
| 32 |
"from polsarpro.dev.devtools import parse_psp_parameter_string\n", |
| 33 |
"import numpy as np\n", |
| 34 |
"from pathlib import Path\n", |
| 35 |
"import xarray as xr\n", |
| 36 |
"from polsarpro.io import open_netcdf_beam\n", |
| 37 |
"from polsarpro.dev.io import read_psp_bin\n", |
| 38 |
"from polsarpro.decompositions import h_a_alpha\n", |
| 39 |
"from polsarpro.util import plot_h_alpha_plane\n", |
| 40 |
"from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n", |
| 41 |
"import shutil\n", |
| 42 |
"\n", |
| 43 |
"# optional import for progress bar\n", |
| 44 |
"from dask.diagnostics import ProgressBar\n", |
| 45 |
"\n", |
| 46 |
"# change to your local C-PolSARpro install dir\n", |
| 47 |
"c_psp_dir = \"/home/c_psp/Soft/bin/\"\n", |
| 48 |
"os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n", |
| 49 |
"os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\n", |
| 50 |
"\n", |
| 51 |
"# change to your data paths\n", |
| 52 |
"# original dataset\n", |
| 53 |
"input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n", |
| 54 |
"\n", |
| 55 |
"# input files from C\n", |
| 56 |
"input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n", |
| 57 |
"\n", |
| 58 |
"# output files from C\n", |
| 59 |
"output_test_dir = Path(\"/data/psp/res/h_a_alpha_c\")" |
| 60 |
] |
| 61 |
}, |
| 62 |
{ |
| 63 |
"cell_type": "markdown", |
| 64 |
"metadata": {}, |
| 65 |
"source": [ |
| 66 |
"## Run the C-version on some test data" |
| 67 |
] |
| 68 |
}, |
| 69 |
{ |
| 70 |
"cell_type": "code", |
| 71 |
"execution_count": null, |
| 72 |
"metadata": {}, |
| 73 |
"outputs": [], |
| 74 |
"source": [ |
| 75 |
"# fl1: alpha, beta, delta, lambda\n", |
| 76 |
"# fl2: lambda\n", |
| 77 |
"# fl3: alpha\n", |
| 78 |
"# fl4: entropy \n", |
| 79 |
"# fl5: anisotropy\n", |
| 80 |
"# fl6-9 combinations\n", |
| 81 |
"\n", |
| 82 |
"input_str= f\"\"\"id: {input_test_dir} \n", |
| 83 |
"od: {output_test_dir}\n", |
| 84 |
"iodf: S2T3\n", |
| 85 |
"nwr: 7\n", |
| 86 |
"nwc: 7\n", |
| 87 |
"ofr: 0\n", |
| 88 |
"ofc: 0\n", |
| 89 |
"fnr: 18432\n", |
| 90 |
"fnc: 1248\n", |
| 91 |
"fl1: 1\n", |
| 92 |
"fl2: 1\n", |
| 93 |
"fl3: 1\n", |
| 94 |
"fl4: 1\n", |
| 95 |
"fl5: 1\n", |
| 96 |
"fl6: 0\n", |
| 97 |
"fl7: 0\n", |
| 98 |
"fl8: 0\n", |
| 99 |
"fl9: 0\n", |
| 100 |
"errf: /tmp/MemoryAllocError.txt\n", |
| 101 |
"mask: {input_test_dir}/mask_valid_pixels.bin\n", |
| 102 |
"\"\"\"\n", |
| 103 |
"result = parse_psp_parameter_string(input_str)\n", |
| 104 |
"os.system(f\"h_a_alpha_decomposition.exe {result}\")\n", |
| 105 |
"\n", |
| 106 |
"# CPSP does not create another config file in output dir\n", |
| 107 |
"shutil.copy(input_test_dir / \"config.txt\", output_test_dir)" |
| 108 |
] |
| 109 |
}, |
| 110 |
{ |
| 111 |
"cell_type": "markdown", |
| 112 |
"metadata": {}, |
| 113 |
"source": [ |
| 114 |
"## Load ALOS data and C outputs" |
| 115 |
] |
| 116 |
}, |
| 117 |
{ |
| 118 |
"cell_type": "code", |
| 119 |
"execution_count": null, |
| 120 |
"metadata": {}, |
| 121 |
"outputs": [], |
| 122 |
"source": [ |
| 123 |
"# uncomment to test on S matrix made with SNAP\n", |
| 124 |
"S = open_netcdf_beam(input_alos_data)\n", |
| 125 |
"\n", |
| 126 |
"out_names = (\"entropy\", \"anisotropy\", \"alpha\", \"beta\", \"delta\", \"gamma\", \"lambda\")" |
| 127 |
] |
| 128 |
}, |
| 129 |
{ |
| 130 |
"cell_type": "markdown", |
| 131 |
"metadata": {}, |
| 132 |
"source": [ |
| 133 |
"## Apply the decomposition" |
| 134 |
] |
| 135 |
}, |
| 136 |
{ |
| 137 |
"cell_type": "code", |
| 138 |
"execution_count": null, |
| 139 |
"metadata": {}, |
| 140 |
"outputs": [], |
| 141 |
"source": [ |
| 142 |
"file_out = \"/data/psp/res/test_h_a_alpha.nc\"\n", |
| 143 |
"# netcdf writer cannot overwrite\n", |
| 144 |
"if os.path.isfile(file_out):\n", |
| 145 |
" os.remove(file_out)\n", |
| 146 |
"\n", |
| 147 |
"with ProgressBar():\n", |
| 148 |
"\n", |
| 149 |
" h_a_alpha(S, boxcar_size=[7, 7], flags=out_names).to_netcdf(file_out)" |
| 150 |
] |
| 151 |
}, |
| 152 |
{ |
| 153 |
"cell_type": "markdown", |
| 154 |
"metadata": {}, |
| 155 |
"source": [ |
| 156 |
"# Numerical evaluation" |
| 157 |
] |
| 158 |
}, |
| 159 |
{ |
| 160 |
"cell_type": "code", |
| 161 |
"execution_count": null, |
| 162 |
"metadata": {}, |
| 163 |
"outputs": [], |
| 164 |
"source": [ |
| 165 |
"# open python output (comment if using compute)\n", |
| 166 |
"out_py = xr.open_dataset(\"/data/psp/res/test_h_a_alpha.nc\")\n", |
| 167 |
"# open C-PolSARPro outputs\n", |
| 168 |
"out_c = {}\n", |
| 169 |
"for name in out_names:\n", |
| 170 |
" file_name = output_test_dir / f\"{name}.bin\"\n", |
| 171 |
" out_c[name] = read_psp_bin(file_name)\n", |
| 172 |
"\n", |
| 173 |
"df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n", |
| 174 |
"df" |
| 175 |
] |
| 176 |
}, |
| 177 |
{ |
| 178 |
"cell_type": "code", |
| 179 |
"execution_count": null, |
| 180 |
"metadata": {}, |
| 181 |
"outputs": [], |
| 182 |
"source": [ |
| 183 |
"visualize_errors(out_py=out_py, out_c=out_c, clip=False)" |
| 184 |
] |
| 185 |
}, |
| 186 |
{ |
| 187 |
"cell_type": "code", |
| 188 |
"execution_count": null, |
| 189 |
"metadata": {}, |
| 190 |
"outputs": [], |
| 191 |
"source": [ |
| 192 |
"# uncomment to test H/Alpha plane histogram\n", |
| 193 |
"ax, fig = plot_h_alpha_plane(out_py, bins=500, min_pts=2)\n", |
| 194 |
"# fig.savefig(output_test_dir / \"h_a_alpha_plane.png\")" |
| 195 |
] |
| 196 |
} |
| 197 |
], |
| 198 |
"metadata": { |
| 199 |
"kernelspec": { |
| 200 |
"display_name": "psp", |
| 201 |
"language": "python", |
| 202 |
"name": "python3" |
| 203 |
}, |
| 204 |
"language_info": { |
| 205 |
"codemirror_mode": { |
| 206 |
"name": "ipython", |
| 207 |
"version": 3 |
| 208 |
}, |
| 209 |
"file_extension": ".py", |
| 210 |
"mimetype": "text/x-python", |
| 211 |
"name": "python", |
| 212 |
"nbconvert_exporter": "python", |
| 213 |
"pygments_lexer": "ipython3", |
| 214 |
"version": "3.12.12" |
| 215 |
} |
| 216 |
}, |
| 217 |
"nbformat": 4, |
| 218 |
"nbformat_minor": 2 |
| 219 |
} |