Debian Codegraph @

notebooks/dev/tmp-dev-wishart.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "id": "49a6d466",
7 "metadata": {},
8 "outputs": [],
9 "source": [
10 "%load_ext autoreload\n",
11 "%autoreload 2\n",
12 "\n",
13 "import numpy as np\n",
14 "import xarray as xr\n",
15 "import dask.array as da\n",
16 "from polsarpro.util import boxcar, C3_to_T3, S_to_C3, S_to_T3, C4_to_T4, T3_to_C3\n",
17 "from polsarpro.io import open_netcdf_beam\n",
18 "from polsarpro.classification import wishart_h_a_alpha\n",
19 "# from polsarpro.classification import _reconstruct_matrix_from_ds\n",
20 "from pathlib import Path\n",
21 "\n",
22 "# optional import for progress bar\n",
23 "from dask.diagnostics import ProgressBar\n",
24 "\n",
25 "\n",
26 "# change to your data paths\n",
27 "# original dataset\n",
28 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
29 "\n",
30 "# S2 inputs generated with C-PSP\n",
31 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
32 "\n",
33 "# # output files from C\n",
34 "# output_test_dir = Path(\"/data/psp/res/vanzyl_c/\")\n",
35 "# if not os.path.isdir(output_test_dir):\n",
36 "# os.mkdir(output_test_dir)"
37 ]
38 },
39 {
40 "cell_type": "code",
41 "execution_count": null,
42 "id": "8c1d8707",
43 "metadata": {},
44 "outputs": [],
45 "source": [
46 "# uncomment to test on S matrix made with SNAP\n",
47 "S = open_netcdf_beam(input_alos_data)\n",
48 "label = wishart_h_a_alpha(S)\n",
49 "# C3 = boxcar(S_to_C3(S), 5, 5)"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": null,
55 "id": "ff9da699",
56 "metadata": {},
57 "outputs": [],
58 "source": [
59 "with ProgressBar():\n",
60 " label = label.compute()"
61 ]
62 },
63 {
64 "cell_type": "code",
65 "execution_count": null,
66 "id": "955d437f",
67 "metadata": {},
68 "outputs": [],
69 "source": [
70 "import matplotlib.pyplot as plt\n",
71 "plt.figure(figsize=(10, 10))\n",
72 "plt.imshow(label.label, interpolation=\"none\")\n",
73 "plt.colorbar(fraction=0.046, pad=0.04)"
74 ]
75 },
76 {
77 "cell_type": "code",
78 "execution_count": null,
79 "id": "d5a1499e",
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "shp = C3.m11.shape\n",
84 "# to be replaced by h_a_alpha initial classes\n",
85 "class_map = da.random.randint(1, 9, size=shp)#.chunks(C3.chunks)"
86 ]
87 },
88 {
89 "cell_type": "code",
90 "execution_count": null,
91 "id": "332e81c1",
92 "metadata": {},
93 "outputs": [],
94 "source": [
95 "def _trace_product_3_wish(M_inv, cov_ds):\n",
96 "\n",
97 " m11 = cov_ds.m11.data\n",
98 " m12 = cov_ds.m12.data\n",
99 " m22 = cov_ds.m22.data\n",
100 " m13 = cov_ds.m13.data\n",
101 " m23 = cov_ds.m23.data\n",
102 " m33 = cov_ds.m33.data\n",
103 " \n",
104 " trace = (M_inv[0, 0] * m11 +\n",
105 " M_inv[0, 1] * da.conj(m12) +\n",
106 " M_inv[0, 2] * da.conj(m13) +\n",
107 " M_inv[1, 0] * m12 +\n",
108 " M_inv[1, 1] * m22 +\n",
109 " M_inv[1, 2] * da.conj(m23) +\n",
110 " M_inv[2, 0] * m13 +\n",
111 " M_inv[2, 1] * m23 +\n",
112 " M_inv[2, 2] * m33)\n",
113 " \n",
114 " return trace"
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": null,
120 "id": "9fbe8ca5",
121 "metadata": {},
122 "outputs": [],
123 "source": [
124 "nclass = 8\n",
125 "centers = []\n",
126 "inv_centers = []\n",
127 "dist_min = da.full_like(C3.m11, fill_value=da.inf)\n",
128 "label_min = class_map.copy()\n",
129 "for i in range(1, nclass + 1):\n",
130 " # class center\n",
131 " mask = class_map == i\n",
132 " npts = mask.sum()\n",
133 " # center = xr.where(class_map == i, C3.chunk(dict(x=1024,y=1024)), 0).sum() / npts\n",
134 " center = xr.where(class_map == i, C3, 0).sum() / npts\n",
135 " # TODO: hardcode chunk shape (C3 T3 -> 3x3, C4 T4 -> 4x4)\n",
136 " M = _reconstruct_matrix_from_ds(center).rechunk(\"auto\") + 1e-12*da.eye(3)\n",
137 "\n",
138 " # compute distance components\n",
139 " meta = (np.array([], dtype=\"complex64\").reshape((0, 0)),)\n",
140 " M_inv = da.apply_gufunc(np.linalg.inv, \"(i,j)->(i,j)\", M, meta=meta)\n",
141 " M_det = da.apply_gufunc(np.linalg.det, \"(i,j)->()\", M, meta=meta)\n",
142 "\n",
143 " # update classification \n",
144 " dist = da.log(M_det.real) + _trace_product_3_wish(M_inv, C3)\n",
145 " is_smaller = dist < dist_min\n",
146 " label_min = da.where(is_smaller, i, label_min)\n",
147 " dist_min = da.where(is_smaller, dist, dist_min)\n",
148 "\n"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "id": "ddfd89cd",
155 "metadata": {},
156 "outputs": [],
157 "source": [
158 "with ProgressBar():\n",
159 " # center = center.compute()\n",
160 " # centers = da.compute(centers)\n",
161 " label_map = label_min.compute()"
162 ]
163 },
164 {
165 "cell_type": "code",
166 "execution_count": null,
167 "id": "fb35c141",
168 "metadata": {},
169 "outputs": [],
170 "source": [
171 "import matplotlib.pyplot as plt\n",
172 "plt.figure(figsize=(10, 10))\n",
173 "plt.imshow(label_map[::8], interpolation=\"none\")\n",
174 "plt.colorbar(fraction=0.046, pad=0.04)"
175 ]
176 },
177 {
178 "cell_type": "code",
179 "execution_count": null,
180 "id": "2d67dfa2",
181 "metadata": {},
182 "outputs": [],
183 "source": []
184 }
185 ],
186 "metadata": {
187 "kernelspec": {
188 "display_name": "psp",
189 "language": "python",
190 "name": "python3"
191 },
192 "language_info": {
193 "codemirror_mode": {
194 "name": "ipython",
195 "version": 3
196 },
197 "file_extension": ".py",
198 "mimetype": "text/x-python",
199 "name": "python",
200 "nbconvert_exporter": "python",
201 "pygments_lexer": "ipython3",
202 "version": "3.12.13"
203 }
204 },
205 "nbformat": 4,
206 "nbformat_minor": 5
207 }