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