| 1 |
import dask.array as da |
| 2 |
import pytest |
| 3 |
import numpy as np |
| 4 |
import xarray as xr |
| 5 |
from polsarpro.classification import ( |
| 6 |
wishart_h_a_alpha, |
| 7 |
wishart_supervised, |
| 8 |
_h_alpha_classifier, |
| 9 |
_label_training_clusters, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
# Fast tests with chunk_size=128 (single chunk for 128x128 image) |
| 14 |
@pytest.mark.parametrize( |
| 15 |
"synthetic_poldata", |
| 16 |
[ |
| 17 |
{"poltypes": "S", "size": 64, "chunk_size": 64}, |
| 18 |
{"poltypes": "C3", "size": 64, "chunk_size": 64}, |
| 19 |
{"poltypes": "C4", "size": 64, "chunk_size": 64}, |
| 20 |
{"poltypes": "T3", "size": 64, "chunk_size": 64}, |
| 21 |
{"poltypes": "T4", "size": 64, "chunk_size": 64}, |
| 22 |
], |
| 23 |
indirect=True, |
| 24 |
) |
| 25 |
def test_wishart_h_a_alpha(synthetic_poldata): |
| 26 |
input_data = synthetic_poldata |
| 27 |
for poltype, ds in input_data.items(): |
| 28 |
result = wishart_h_a_alpha(ds).compute() |
| 29 |
|
| 30 |
# Check output type |
| 31 |
assert isinstance( |
| 32 |
result, type(ds) |
| 33 |
), f"Output should be {type(ds)}, got {type(result)}" |
| 34 |
|
| 35 |
# Check output shape matches input spatial dimensions |
| 36 |
assert ( |
| 37 |
result["wishart_h_alpha_class"].shape == ds.m11.shape if "m11" in ds else ds.hh.shape |
| 38 |
), f"Output shape {result['wishart_h_alpha_class'].shape} should match input spatial shape" |
| 39 |
|
| 40 |
# Access computed data (already in memory after .compute() above) |
| 41 |
class_data = result["wishart_h_alpha_class"] |
| 42 |
|
| 43 |
# Check class values are integers in range [1, 9] |
| 44 |
assert class_data.dtype == np.uint8, f"Class values should be uint8, got {class_data.dtype}" |
| 45 |
assert np.issubdtype( |
| 46 |
class_data.dtype, np.integer |
| 47 |
), f"Class values should be integers, got {class_data.dtype}" |
| 48 |
assert ( |
| 49 |
class_data.min() >= 1 |
| 50 |
), f"Min class value should be >= 1, got {class_data.min()}" |
| 51 |
assert ( |
| 52 |
class_data.max() <= 8 |
| 53 |
), f"Max class value should be <= 8, got {class_data.max()}" |
| 54 |
|
| 55 |
class_data = result["wishart_h_a_alpha_class"] |
| 56 |
|
| 57 |
# Check class values are integers in range [1, 9] |
| 58 |
assert class_data.dtype == np.uint8, f"Class values should be uint8, got {class_data.dtype}" |
| 59 |
assert np.issubdtype( |
| 60 |
class_data.dtype, np.integer |
| 61 |
), f"Class values should be integers, got {class_data.dtype}" |
| 62 |
assert ( |
| 63 |
class_data.min() >= 1 |
| 64 |
), f"Min class value should be >= 1, got {class_data.min()}" |
| 65 |
assert ( |
| 66 |
class_data.max() <= 16 |
| 67 |
), f"Max class value should be <= 8, got {class_data.max()}" |
| 68 |
|
| 69 |
|
| 70 |
# Check attributes |
| 71 |
assert ( |
| 72 |
result.attrs.get("poltype") == "wishart_h_a_alpha" |
| 73 |
), "Output poltype should be 'wishart_h_a_alpha'" |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize( |
| 77 |
"synthetic_poldata", |
| 78 |
[ |
| 79 |
{"poltypes": "S", "chunk_size": 128}, |
| 80 |
{"poltypes": "C3", "chunk_size": 128}, |
| 81 |
], |
| 82 |
indirect=True, |
| 83 |
) |
| 84 |
def test_wishart_h_a_alpha_with_ha_result(synthetic_poldata): |
| 85 |
"""Test wishart_h_a_alpha with pre-computed h_a_alpha_result.""" |
| 86 |
from polsarpro.decompositions import h_a_alpha |
| 87 |
|
| 88 |
input_data = synthetic_poldata |
| 89 |
for poltype, ds in input_data.items(): |
| 90 |
# Compute h_a_alpha decomposition first |
| 91 |
ha_result = h_a_alpha( |
| 92 |
ds, boxcar_size=[5, 5], flags=("entropy", "anisotropy", "alpha") |
| 93 |
) |
| 94 |
|
| 95 |
# Use pre-computed result |
| 96 |
result = wishart_h_a_alpha(ds, h_a_alpha_result=ha_result).compute() |
| 97 |
|
| 98 |
# Check output type |
| 99 |
assert isinstance( |
| 100 |
result, type(ds) |
| 101 |
), f"Output should be {type(ds)}, got {type(result)}" |
| 102 |
|
| 103 |
# Check output shape matches input spatial dimensions |
| 104 |
assert ( |
| 105 |
result["wishart_h_alpha_class"].shape == ds.m11.shape if "m11" in ds else ds.hh.shape |
| 106 |
), f"Output shape {result['wishart_h_alpha_class'].shape} should match input spatial shape" |
| 107 |
|
| 108 |
# Access computed data (already in memory after .compute() above) |
| 109 |
class_data = result["wishart_h_alpha_class"] |
| 110 |
|
| 111 |
# Check class values are integers in range [1, 9] |
| 112 |
assert class_data.dtype == np.uint8, f"Class values should be uint8, got {class_data.dtype}" |
| 113 |
assert np.issubdtype( |
| 114 |
class_data.dtype, np.integer |
| 115 |
), f"Class values should be integers, got {class_data.dtype}" |
| 116 |
assert ( |
| 117 |
class_data.min() >= 1 |
| 118 |
), f"Min class value should be >= 1, got {class_data.min()}" |
| 119 |
assert ( |
| 120 |
class_data.max() <= 9 |
| 121 |
), f"Max class value should be <= 9, got {class_data.max()}" |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("synthetic_poldata", ["h_a_alpha"], indirect=True) |
| 126 |
def test_h_alpha_classifier(synthetic_poldata): |
| 127 |
ds = synthetic_poldata["h_a_alpha"] |
| 128 |
res = _h_alpha_classifier(ds).compute() |
| 129 |
|
| 130 |
assert res.dtype == np.uint8 |
| 131 |
assert res.shape == ds.entropy.shape |
| 132 |
assert res.min() >= 1 |
| 133 |
assert res.max() <= 9 |
| 134 |
|
| 135 |
|
| 136 |
def test_label_training_clusters(): |
| 137 |
# ensure that connected regions with |
| 138 |
# different classes are in different clusters |
| 139 |
training_labels = np.zeros((5, 5), dtype=np.uint8) |
| 140 |
training_labels[0, 0] = 1 |
| 141 |
training_labels[0, 4] = 1 |
| 142 |
training_labels[4, 4] = 2 |
| 143 |
|
| 144 |
lab, cluster_classes = _label_training_clusters(training_labels) |
| 145 |
|
| 146 |
assert lab.max() == 3 |
| 147 |
assert cluster_classes.tolist() == [0, 1, 1, 2] |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.parametrize( |
| 151 |
"synthetic_poldata", |
| 152 |
[ |
| 153 |
{ |
| 154 |
"poltypes": ["S", "C3", "C4", "T3", "T4"], |
| 155 |
"size": 64, |
| 156 |
"chunk_size": 16, |
| 157 |
} |
| 158 |
], |
| 159 |
indirect=True, |
| 160 |
) |
| 161 |
def test_wishart_supervised(synthetic_poldata): |
| 162 |
fake_class_map = np.zeros((64, 64), dtype=np.uint8) |
| 163 |
fake_class_map[1, 1] = 1 |
| 164 |
fake_class_map[1, 30] = 1 |
| 165 |
fake_class_map[30, 1] = 2 |
| 166 |
fake_class_map[30, 30] = 2 |
| 167 |
|
| 168 |
for poltype, ds in synthetic_poldata.items(): |
| 169 |
training_labels = xr.DataArray( |
| 170 |
fake_class_map, |
| 171 |
dims=("y", "x"), |
| 172 |
coords={"y": ds.coords["y"], "x": ds.coords["x"]}, |
| 173 |
) |
| 174 |
result = wishart_supervised(ds, training_labels, boxcar_size=[1, 1]).compute() |
| 175 |
class_map = result["wishart_supervised_class"].values |
| 176 |
|
| 177 |
assert class_map.shape == (ds.sizes["y"], ds.sizes["x"]) |
| 178 |
assert class_map.dtype == np.uint8 |
| 179 |
assert np.issubdtype(class_map.dtype, np.integer) |
| 180 |
assert set(np.unique(class_map)).issubset({1, 2}), ( |
| 181 |
f"Unexpected class labels for poltype {poltype}: " |
| 182 |
f"{set(np.unique(class_map))}" |
| 183 |
) |
| 184 |
assert result.attrs.get("poltype") == "wishart_supervised" |
| 185 |
|
| 186 |
|
| 187 |
# Test with default chunk_size=16 to verify small chunks still work |
| 188 |
@pytest.mark.parametrize( |
| 189 |
"synthetic_poldata", [{"poltype": "T3", "chunk_size": 128}], indirect=True |
| 190 |
) |
| 191 |
def test_wishart_h_a_alpha_max_iter(synthetic_poldata): |
| 192 |
"""Test wishart_h_a_alpha with custom max_iter parameter. |
| 193 |
|
| 194 |
Uses default chunk_size=16 to verify the algorithm works with |
| 195 |
smaller chunks (slower but tests chunked computation). |
| 196 |
""" |
| 197 |
input_data = synthetic_poldata |
| 198 |
ds = input_data["T3"] |
| 199 |
|
| 200 |
# Test with max_iter=1 |
| 201 |
result_1_iter = wishart_h_a_alpha(ds, max_iter=1) |
| 202 |
assert isinstance(result_1_iter, type(ds)) |
| 203 |
|
| 204 |
# Test with max_iter=5 |
| 205 |
result_5_iter = wishart_h_a_alpha(ds, max_iter=5) |
| 206 |
assert isinstance(result_5_iter, type(ds)) |
| 207 |
|
| 208 |
|
| 209 |
@pytest.mark.parametrize( |
| 210 |
"synthetic_poldata", |
| 211 |
[{"poltypes": "T3", "chunk_size": 128}], |
| 212 |
indirect=True, |
| 213 |
) |
| 214 |
def test_wishart_h_a_alpha_tol_pct(synthetic_poldata): |
| 215 |
"""Test wishart_h_a_alpha with custom tol_pct parameter.""" |
| 216 |
input_data = synthetic_poldata |
| 217 |
ds = input_data["T3"] |
| 218 |
|
| 219 |
# Test with tol_pct=9.0 (more strict convergence) |
| 220 |
result_strict = wishart_h_a_alpha(ds, tol_pct=9.0) |
| 221 |
assert isinstance(result_strict, type(ds)) |
| 222 |
|
| 223 |
# Test with tol_pct=11.0 (less strict convergence) |
| 224 |
result_relaxed = wishart_h_a_alpha(ds, tol_pct=11.0) |
| 225 |
assert isinstance(result_relaxed, type(ds)) |
| 226 |
|
| 227 |
|
| 228 |
@pytest.mark.parametrize( |
| 229 |
"synthetic_poldata", |
| 230 |
[{"poltypes": "T3", "chunk_size": 128}], |
| 231 |
indirect=True, |
| 232 |
) |
| 233 |
def test_wishart_h_a_alpha_combined_params(synthetic_poldata): |
| 234 |
"""Test wishart_h_a_alpha with both max_iter and tol_pct.""" |
| 235 |
input_data = synthetic_poldata |
| 236 |
ds = input_data["T3"] |
| 237 |
|
| 238 |
# Test with custom values for both parameters |
| 239 |
result = wishart_h_a_alpha(ds, max_iter=5, tol_pct=15.0) |
| 240 |
assert isinstance(result, type(ds)) |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.parametrize( |
| 244 |
"synthetic_poldata", |
| 245 |
[{"poltypes": "T3", "chunk_size": 128}], |
| 246 |
indirect=True, |
| 247 |
) |
| 248 |
def test_wishart_h_a_alpha_invalid_params(synthetic_poldata): |
| 249 |
"""Test wishart_h_a_alpha raises errors for invalid parameters.""" |
| 250 |
input_data = synthetic_poldata |
| 251 |
ds = input_data["T3"] |
| 252 |
|
| 253 |
# Test invalid max_iter |
| 254 |
with pytest.raises(TypeError, match="max_iter must be an integer"): |
| 255 |
wishart_h_a_alpha(ds, max_iter=3.5) |
| 256 |
|
| 257 |
with pytest.raises(TypeError, match="max_iter must be an integer"): |
| 258 |
wishart_h_a_alpha(ds, max_iter="10") |
| 259 |
|
| 260 |
with pytest.raises(ValueError, match="max_iter must be a positive integer"): |
| 261 |
wishart_h_a_alpha(ds, max_iter=0) |
| 262 |
|
| 263 |
with pytest.raises(ValueError, match="max_iter must be a positive integer"): |
| 264 |
wishart_h_a_alpha(ds, max_iter=-1) |
| 265 |
|
| 266 |
# Test invalid tol_pct |
| 267 |
with pytest.raises(TypeError, match="tol_pct must be a number"): |
| 268 |
wishart_h_a_alpha(ds, tol_pct="10") |
| 269 |
|
| 270 |
with pytest.raises(ValueError, match="tol_pct must be in the range"): |
| 271 |
wishart_h_a_alpha(ds, tol_pct=-1.0) |
| 272 |
|
| 273 |
with pytest.raises(ValueError, match="tol_pct must be in the range"): |
| 274 |
wishart_h_a_alpha(ds, tol_pct=101.0) |