| 1 |
import pytest |
| 2 |
import numpy as np |
| 3 |
from polsarpro.auxil import POLTYPES |
| 4 |
from polsarpro.util import vec_to_mat |
| 5 |
from polsarpro.util import S_to_C3, S_to_C4, S_to_C2 |
| 6 |
from polsarpro.util import S_to_T3, S_to_T4 |
| 7 |
from polsarpro.util import T3_to_C3, T4_to_C4, T4_to_T3 |
| 8 |
from polsarpro.util import C3_to_T3, C4_to_T4 |
| 9 |
from polsarpro.util import boxcar |
| 10 |
from polsarpro.util import multilook |
| 11 |
from polsarpro.util import pauli_rgb |
| 12 |
|
| 13 |
|
| 14 |
def _assert_polmatrix(res, poltype, shape): |
| 15 |
expected_vars = POLTYPES[poltype]["vars"] |
| 16 |
|
| 17 |
assert res.poltype == poltype |
| 18 |
assert set(res.data_vars) == set(expected_vars) |
| 19 |
assert all(res[var].shape == shape for var in expected_vars) |
| 20 |
assert all(res[var].dtype == spec["dtype"] for var, spec in expected_vars.items()) |
| 21 |
|
| 22 |
|
| 23 |
def test_vec_to_mat(): |
| 24 |
|
| 25 |
N = 128 |
| 26 |
D = 3 |
| 27 |
v = np.random.rand(N, N, D) + 1j * np.random.rand(N, N, D) |
| 28 |
M = vec_to_mat(v) |
| 29 |
assert M.shape == (N, N, D, D) |
| 30 |
# M has to be Hermitian |
| 31 |
assert np.allclose(M.transpose((0, 1, 3, 2)), M.conj()) |
| 32 |
assert np.allclose(M.diagonal(axis1=2, axis2=3).imag, 0) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("synthetic_poldata", ["S"], indirect=True) |
| 36 |
@pytest.mark.parametrize( |
| 37 |
"converter, poltype", |
| 38 |
[ |
| 39 |
(S_to_C2, "C2"), |
| 40 |
(S_to_C3, "C3"), |
| 41 |
(S_to_C4, "C4"), |
| 42 |
(S_to_T3, "T3"), |
| 43 |
(S_to_T4, "T4"), |
| 44 |
], |
| 45 |
) |
| 46 |
def test_S_conversions(synthetic_poldata, converter, poltype): |
| 47 |
input_data = synthetic_poldata |
| 48 |
|
| 49 |
for _, ds in input_data.items(): |
| 50 |
res = converter(ds) |
| 51 |
_assert_polmatrix(res, poltype, ds.hh.shape) |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize( |
| 55 |
"synthetic_poldata, input_poltype, converter, poltype", |
| 56 |
[ |
| 57 |
("T3", "T3", T3_to_C3, "C3"), |
| 58 |
({"poltypes": ["T3", "T4"]}, "T4", T4_to_C4, "C4"), |
| 59 |
({"poltypes": ["T3", "T4"]}, "T4", T4_to_T3, "T3"), |
| 60 |
("C3", "C3", C3_to_T3, "T3"), |
| 61 |
({"poltypes": ["C3", "C4"]}, "C4", C4_to_T4, "T4"), |
| 62 |
], |
| 63 |
indirect=["synthetic_poldata"], |
| 64 |
) |
| 65 |
def test_matrix_conversions(synthetic_poldata, input_poltype, converter, poltype): |
| 66 |
input_data = synthetic_poldata |
| 67 |
ds = input_data[input_poltype] |
| 68 |
res = converter(ds) |
| 69 |
|
| 70 |
_assert_polmatrix(res, poltype, ds.m11.shape) |
| 71 |
if converter is T4_to_T3: |
| 72 |
assert all( |
| 73 |
np.allclose(res[var].compute().values, ds[var].compute().values) |
| 74 |
for var in res.data_vars |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize("synthetic_poldata", ["C3"], indirect=True) |
| 79 |
def test_boxcar(synthetic_poldata): |
| 80 |
input_data = synthetic_poldata |
| 81 |
|
| 82 |
for _, ds in input_data.items(): |
| 83 |
res = boxcar(img=ds, dim_az=5, dim_rg=3) |
| 84 |
_assert_polmatrix(res, ds.poltype, ds.m11.shape) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.parametrize( |
| 88 |
"synthetic_poldata, input_poltype", |
| 89 |
[ |
| 90 |
({"poltypes": ["C2", "C3"]}, "C2"), |
| 91 |
("C3", "C3"), |
| 92 |
({"poltypes": ["C3", "C4"]}, "C4"), |
| 93 |
("T3", "T3"), |
| 94 |
({"poltypes": ["T3", "T4"]}, "T4"), |
| 95 |
], |
| 96 |
indirect=["synthetic_poldata"], |
| 97 |
) |
| 98 |
def test_multilook(synthetic_poldata, input_poltype): |
| 99 |
ds = synthetic_poldata[input_poltype] |
| 100 |
dim_az = 5 |
| 101 |
dim_rg = 2 |
| 102 |
|
| 103 |
input_data = ds.chunk(x=64, y=64) |
| 104 |
res = multilook(input_data=input_data, dim_az=dim_az, dim_rg=dim_rg) |
| 105 |
for var in input_data.data_vars: |
| 106 |
shp = ds[var].shape |
| 107 |
naz_out = shp[0] // dim_az |
| 108 |
nrg_out = shp[1] // dim_rg |
| 109 |
assert res[var].shape == (naz_out, nrg_out) |
| 110 |
assert res[var].dtype == input_data[var].dtype |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.parametrize("synthetic_poldata", ["S", "C3", "T3"], indirect=True) |
| 114 |
def test_pauli_rgb(synthetic_poldata): |
| 115 |
input_data = synthetic_poldata |
| 116 |
for _, ds in input_data.items(): |
| 117 |
input_data = ds.chunk(x=64, y=64) |
| 118 |
res = pauli_rgb(input_data=input_data) |
| 119 |
var = "hh" if "hh" in ds.data_vars else "m11" |
| 120 |
shp = ds[var].shape |
| 121 |
assert res.shape == (3,) + shp |
| 122 |
assert res.dtype == "float32" |