{ "cells": [ { "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 tsvm\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/tsvm_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, phi,tau, psi \n", "# fl2: alphas\n", "# fl3: phis\n", "# fl4: taus \n", "# fl5: psis\n", "\n", "if not os.path.exists(output_test_dir):\n", " os.makedirs(output_test_dir)\n", "\n", "input_str= f\"\"\"id: {input_test_dir} \n", "od: {output_test_dir}\n", "iodf: S2\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", "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\"tsvm_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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Apply the decomposition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file_out = \"/data/psp/res/test_tsvm.nc\"\n", "# netcdf writer cannot overwrite\n", "if os.path.isfile(file_out):\n", " os.remove(file_out)\n", "\n", "flags = (\"alpha_phi_tau_psi\", \"alpha\", \"phi\", \"tau\", \"psi\")\n", "\n", "with ProgressBar():\n", " # uncomment to compute and store result in a file\n", " tsvm(S, boxcar_size=[7, 7], flags=flags).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_tsvm.nc\")\n", "# open C-PolSARPro outputs\n", "out_c = {}\n", "out_names = []\n", "if \"alpha_phi_tau_psi\" in flags:\n", " out_names.extend([\"alpha_s\", \"phi_s\", \"tau_m\", \"psi\"])\n", "if \"alpha\" in flags:\n", " out_names.extend([\"alpha_s1\", \"alpha_s2\", \"alpha_s3\"])\n", "if \"phi\" in flags:\n", " out_names.extend([\"phi_s1\", \"phi_s2\", \"phi_s3\"])\n", "if \"tau\" in flags:\n", " out_names.extend([\"tau_m1\", \"tau_m2\", \"tau_m3\"])\n", "if \"psi\" in flags:\n", " out_names.extend([\"psi1\", \"psi2\", \"psi3\"])\n", "\n", "for name in out_names:\n", " file_name = output_test_dir / f\"TSVM_{name}.bin\"\n", " out_c[name] = read_psp_bin(file_name)\n", "\n", "df = summarize_metrics(out_py, out_c, short_titles=False)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "visualize_errors(out_py=out_py, out_c=out_c, clip=False)" ] } ], "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 }