{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Wishart H / A / Alpha classification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows how to apply the Wishart unsupervised classification to a PolSARpro NetCDF dataset." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import packages and set directories" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os \n", "from pathlib import Path\n", "import xarray as xr\n", "from polsarpro.io import open_netcdf_beam\n", "from polsarpro.classification import wishart_h_a_alpha\n", "\n", "# optional import for progress bar\n", "from dask.diagnostics import ProgressBar\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", "output_dir = Path(\"/data/psp/res\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load data\n", "\n", "We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n", "To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook." ] }, { "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 classification\n", "\n", "Let's apply the algorithm and write the result to a NetCDF file.\n", "Optionally we can use a progress bar to monitor the progress of the computation.\n", "\n", "**Note:** Unlike the C implementation, this version does not require precomputed H/A/Alpha parameters. However, for backward compatibility, you may provide existing parameters via the h_a_alpha_result argument. In that case, the H/A/Alpha parameters may have been computed using a different boxcar window size than the one used in this function.\n\n**Memory note:** On large products, running the classifier without h_a_alpha_result can require substantial memory when the result is computed or written, even with tol_pct=None, because the H/A/Alpha decomposition is part of the same Dask pipeline. If memory is limited, compute the H/A/Alpha decomposition first, save it to NetCDF, reopen it with xarray, and pass it as h_a_alpha_result." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# change to the name of your liking\n", "file_out = output_dir / \"psp_wishart_haa.nc\"\n", "\n", "# netcdf writer cannot overwrite\n", "if os.path.isfile(file_out):\n", " os.remove(file_out)\n", "\n", "with ProgressBar():\n", " wishart_h_a_alpha(S, boxcar_size=[7, 7]).to_netcdf(file_out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display outputs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We open the previously saved dataset:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res = xr.open_dataset(file_out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we may plot the individual outputs:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8 Classes based on H/Alpha" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res.wishart_h_alpha_class.plot.imshow(cmap=\"Dark2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 16 Classes based on H/A/Alpha" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res.wishart_h_a_alpha_class.plot.imshow(cmap=\"tab20\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }