{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# H A $\\alpha$ decomposition evaluation\n", "\n", "In this notebook we compare results of the new implementation of the\n", "H A $\\alpha$ decomposition with the C legacy version of PolSARpro. \n", "It assumes the user has a working installation of PolSARpro and has followed the \n", "instructions in the README.md file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import os\n", "# avoid thread conflicts between numpy and dask\n", "os.environ[\"OMP_NUM_THREADS\"] = \"1\"\n", "os.environ[\"OPENBLAS_NUM_THREADS\"] = \"1\"\n", "os.environ[\"MKL_NUM_THREADS\"] = \"1\"\n", "os.environ[\"VECLIB_MAXIMUM_THREADS\"] = \"1\"\n", "os.environ[\"NUMEXPR_NUM_THREADS\"] = \"1\"\n", "\n", "from polsarpro.dev.devtools import parse_psp_parameter_string\n", "import numpy as np\n", "from pathlib import Path\n", "import xarray as xr\n", "from polsarpro.io import open_netcdf_beam\n", "from polsarpro.dev.io import read_psp_bin\n", "from polsarpro.decompositions import h_a_alpha\n", "from polsarpro.util import plot_h_alpha_plane\n", "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n", "import shutil\n", "\n", "# optional import for progress bar\n", "from dask.diagnostics import ProgressBar\n", "\n", "# change to your local C-PolSARpro install dir\n", "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n", "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n", "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\n", "\n", "# change to your data paths\n", "# original dataset\n", "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n", "\n", "# input files from C\n", "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n", "\n", "# output files from C\n", "output_test_dir = Path(\"/data/psp/res/h_a_alpha_c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run the C-version on some test data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fl1: alpha, beta, delta, lambda\n", "# fl2: lambda\n", "# fl3: alpha\n", "# fl4: entropy \n", "# fl5: anisotropy\n", "# fl6-9 combinations\n", "\n", "input_str= f\"\"\"id: {input_test_dir} \n", "od: {output_test_dir}\n", "iodf: S2T3\n", "nwr: 7\n", "nwc: 7\n", "ofr: 0\n", "ofc: 0\n", "fnr: 18432\n", "fnc: 1248\n", "fl1: 1\n", "fl2: 1\n", "fl3: 1\n", "fl4: 1\n", "fl5: 1\n", "fl6: 0\n", "fl7: 0\n", "fl8: 0\n", "fl9: 0\n", "errf: /tmp/MemoryAllocError.txt\n", "mask: {input_test_dir}/mask_valid_pixels.bin\n", "\"\"\"\n", "result = parse_psp_parameter_string(input_str)\n", "os.system(f\"h_a_alpha_decomposition.exe {result}\")\n", "\n", "# CPSP does not create another config file in output dir\n", "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load ALOS data and C outputs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# uncomment to test on S matrix made with SNAP\n", "S = open_netcdf_beam(input_alos_data)\n", "\n", "out_names = (\"entropy\", \"anisotropy\", \"alpha\", \"beta\", \"delta\", \"gamma\", \"lambda\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Apply the decomposition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file_out = \"/data/psp/res/test_h_a_alpha.nc\"\n", "# netcdf writer cannot overwrite\n", "if os.path.isfile(file_out):\n", " os.remove(file_out)\n", "\n", "with ProgressBar():\n", "\n", " h_a_alpha(S, boxcar_size=[7, 7], flags=out_names).to_netcdf(file_out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numerical evaluation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# open python output (comment if using compute)\n", "out_py = xr.open_dataset(\"/data/psp/res/test_h_a_alpha.nc\")\n", "# open C-PolSARPro outputs\n", "out_c = {}\n", "for name in out_names:\n", " file_name = output_test_dir / f\"{name}.bin\"\n", " out_c[name] = read_psp_bin(file_name)\n", "\n", "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "visualize_errors(out_py=out_py, out_c=out_c, clip=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# uncomment to test H/Alpha plane histogram\n", "ax, fig = plot_h_alpha_plane(out_py, bins=500, min_pts=2)\n", "# fig.savefig(output_test_dir / \"h_a_alpha_plane.png\")" ] } ], "metadata": { "kernelspec": { "display_name": "psp", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" } }, "nbformat": 4, "nbformat_minor": 2 }