| 1 |
import os |
| 2 |
import re |
| 3 |
import math |
| 4 |
from grass.gunittest.case import TestCase |
| 5 |
from grass.gunittest.main import test |
| 6 |
from pathlib import Path |
| 7 |
from grass.script import find_file |
| 8 |
from itertools import combinations |
| 9 |
|
| 10 |
|
| 11 |
class TestIClusterWithSyntheticData(TestCase): |
| 12 |
group_name = "test_cluster_group" |
| 13 |
subgroup_name = "test_cluster_subgroup" |
| 14 |
input_maps = ["synth_map1", "synth_map2", "synth_map3"] |
| 15 |
signature_file = "sig" |
| 16 |
num_classes = 3 |
| 17 |
|
| 18 |
@classmethod |
| 19 |
def setUpClass(cls): |
| 20 |
"""Set up test environment""" |
| 21 |
cls.use_temp_region() |
| 22 |
cls.runModule("g.region", n=50, s=0, e=50, w=0, rows=100, cols=100) |
| 23 |
cls.runModule( |
| 24 |
"r.mapcalc", |
| 25 |
expression=f"{cls.input_maps[0]} = if(row() < 25, 10, 5)", |
| 26 |
overwrite=True, |
| 27 |
) |
| 28 |
cls.runModule( |
| 29 |
"r.mapcalc", |
| 30 |
expression=f"{cls.input_maps[1]} = if(col() < 25, 20, 10)", |
| 31 |
overwrite=True, |
| 32 |
) |
| 33 |
cls.runModule( |
| 34 |
"r.mapcalc", |
| 35 |
expression=f"{cls.input_maps[2]} = row() + col()", |
| 36 |
overwrite=True, |
| 37 |
) |
| 38 |
cls.runModule( |
| 39 |
"i.group", |
| 40 |
group=cls.group_name, |
| 41 |
subgroup=cls.subgroup_name, |
| 42 |
input=",".join(cls.input_maps), |
| 43 |
) |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def tearDownClass(cls): |
| 47 |
"""Cleanup test environment""" |
| 48 |
cls.runModule("g.remove", flags="f", type="group", name=cls.group_name) |
| 49 |
cls.runModule("g.remove", flags="f", type="raster", name=cls.input_maps) |
| 50 |
|
| 51 |
def setUp(self): |
| 52 |
"""Ensure clean signature file state before each test""" |
| 53 |
if self._signature_exists(): |
| 54 |
self.runModule( |
| 55 |
"g.remove", flags="f", type="raster", name=self.signature_file |
| 56 |
) |
| 57 |
|
| 58 |
def _signature_exists(self): |
| 59 |
"""Check if the signature file exists in the nested directory""" |
| 60 |
parent_dir_info = find_file("sig", element="signatures") |
| 61 |
sig_file_path = os.path.join( |
| 62 |
parent_dir_info["file"], |
| 63 |
"sig", |
| 64 |
"sig", |
| 65 |
) |
| 66 |
return Path(sig_file_path).is_file() |
| 67 |
|
| 68 |
def test_cluster_creation(self): |
| 69 |
"""Test the creation of a signature file and validate the number of classes.""" |
| 70 |
self.assertModule( |
| 71 |
"i.cluster", |
| 72 |
group=self.group_name, |
| 73 |
subgroup=self.subgroup_name, |
| 74 |
signaturefile=self.signature_file, |
| 75 |
classes=self.num_classes, |
| 76 |
overwrite=True, |
| 77 |
) |
| 78 |
self.assertTrue( |
| 79 |
self._signature_exists(), |
| 80 |
f"Signature file '{self.signature_file}' was not created", |
| 81 |
) |
| 82 |
parent_dir_info = find_file("sig", element="signatures") |
| 83 |
sig_path = os.path.join( |
| 84 |
parent_dir_info["file"], |
| 85 |
"sig", |
| 86 |
"sig", |
| 87 |
) |
| 88 |
content = Path(sig_path).read_text() |
| 89 |
class_count = content.count("#Class ") |
| 90 |
self.assertEqual( |
| 91 |
class_count, |
| 92 |
self.num_classes, |
| 93 |
f"Expected {self.num_classes} classes, but found {class_count}", |
| 94 |
) |
| 95 |
|
| 96 |
def test_sample_interval(self): |
| 97 |
"""Test that sample interval affects the number of cells sampled""" |
| 98 |
sample_interval = (2, 2) |
| 99 |
report_file = "sample_report.txt" |
| 100 |
self.addCleanup(os.remove, report_file) if Path(report_file).exists() else None |
| 101 |
|
| 102 |
self.assertModule( |
| 103 |
"i.cluster", |
| 104 |
group=self.group_name, |
| 105 |
subgroup=self.subgroup_name, |
| 106 |
signaturefile=self.signature_file, |
| 107 |
classes=self.num_classes, |
| 108 |
reportfile=report_file, |
| 109 |
sample=sample_interval, |
| 110 |
overwrite=True, |
| 111 |
) |
| 112 |
self.assertTrue(Path(report_file).is_file()) |
| 113 |
content = Path(report_file).read_text() |
| 114 |
expected_cells = (100 // sample_interval[0]) * (100 // sample_interval[1]) |
| 115 |
match = re.search(r"Sample size:\s+(\d+)", content) |
| 116 |
self.assertIsNotNone(match, "Total cells line not found in report") |
| 117 |
actual_cells = int(match.group(1)) |
| 118 |
self.assertEqual(actual_cells, expected_cells) |
| 119 |
|
| 120 |
def test_convergence_parameters(self): |
| 121 |
"""Test that higher convergence target results in more iterations and higher convergence""" |
| 122 |
low_percent = 90.0 |
| 123 |
high_percent = 99.5 |
| 124 |
low_report = "low_percent_report.txt" |
| 125 |
high_report = "high_percent_report.txt" |
| 126 |
self.addCleanup(os.remove, low_report) if Path(low_report).exists() else None |
| 127 |
self.addCleanup(os.remove, high_report) if Path(high_report).exists() else None |
| 128 |
self.assertModule( |
| 129 |
"i.cluster", |
| 130 |
group=self.group_name, |
| 131 |
subgroup=self.subgroup_name, |
| 132 |
signaturefile=self.signature_file + "_low", |
| 133 |
classes=self.num_classes, |
| 134 |
reportfile=low_report, |
| 135 |
convergence=low_percent, |
| 136 |
overwrite=True, |
| 137 |
) |
| 138 |
self.assertModule( |
| 139 |
"i.cluster", |
| 140 |
group=self.group_name, |
| 141 |
subgroup=self.subgroup_name, |
| 142 |
signaturefile=self.signature_file + "_high", |
| 143 |
classes=self.num_classes, |
| 144 |
reportfile=high_report, |
| 145 |
convergence=high_percent, |
| 146 |
overwrite=True, |
| 147 |
) |
| 148 |
low_content = Path(low_report).read_text() |
| 149 |
high_content = Path(high_report).read_text() |
| 150 |
low_iterations = len( |
| 151 |
re.findall(r"######## iteration \d+ ###########", low_content) |
| 152 |
) |
| 153 |
high_iterations = len( |
| 154 |
re.findall(r"######## iteration \d+ ###########", high_content) |
| 155 |
) |
| 156 |
self.assertGreater( |
| 157 |
high_iterations, |
| 158 |
low_iterations, |
| 159 |
f"Higher convergence should require more iterations. Found {high_iterations} for high vs {low_iterations} for low.", |
| 160 |
) |
| 161 |
low_final_conv = float( |
| 162 |
re.search(r"(\d+) classes \(convergence=(\d+\.\d+)%\)", low_content).group( |
| 163 |
2 |
| 164 |
) |
| 165 |
) |
| 166 |
high_final_conv = float( |
| 167 |
re.search(r"(\d+) classes \(convergence=(\d+\.\d+)%\)", high_content).group( |
| 168 |
2 |
| 169 |
) |
| 170 |
) |
| 171 |
self.assertGreater( |
| 172 |
high_final_conv, |
| 173 |
low_final_conv, |
| 174 |
f"Higher target should yield higher convergence. Found {high_final_conv}% for high vs {low_final_conv}% for low.", |
| 175 |
) |
| 176 |
|
| 177 |
def parse_signature_means(self, sig_path): |
| 178 |
"""Parse a signature file and return a list of cluster means.""" |
| 179 |
means = [] |
| 180 |
with open(sig_path) as f: |
| 181 |
current_class = None |
| 182 |
for line in f: |
| 183 |
line = line.strip() |
| 184 |
if line.startswith("#Class "): |
| 185 |
current_class = int(line.split()[1]) |
| 186 |
means.append([]) |
| 187 |
elif line == "# Mean": |
| 188 |
# The next line contains the mean values |
| 189 |
mean_line = next(f).strip() |
| 190 |
mean_values = list(map(float, mean_line.split())) |
| 191 |
if current_class is not None: |
| 192 |
# Ensure we're appending to the correct class |
| 193 |
means[current_class - 1] = mean_values |
| 194 |
current_class = None # Reset to avoid reading other means |
| 195 |
return means |
| 196 |
|
| 197 |
def minimum_pairwise_distance(self, means): |
| 198 |
"""Calculate the minimum pairwise Euclidean distance between cluster means.""" |
| 199 |
min_dist = float("inf") |
| 200 |
for a, b in combinations(means, 2): |
| 201 |
dist = math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b, strict=False))) |
| 202 |
min_dist = min(min_dist, dist) |
| 203 |
return min_dist |
| 204 |
|
| 205 |
def test_separation_parameter1(self): |
| 206 |
"""Test the effect of the separation parameter on cluster separation quality.""" |
| 207 |
report_file = "separation_report.txt" |
| 208 |
self.addCleanup(os.remove, report_file) if Path(report_file).exists() else None |
| 209 |
sig_file_default = self.signature_file + "_sep0" |
| 210 |
self.assertModule( |
| 211 |
"i.cluster", |
| 212 |
group=self.group_name, |
| 213 |
subgroup=self.subgroup_name, |
| 214 |
signaturefile=sig_file_default, |
| 215 |
separation=0.0, |
| 216 |
classes=self.num_classes, |
| 217 |
reportfile=report_file, |
| 218 |
overwrite=True, |
| 219 |
) |
| 220 |
self.addCleanup( |
| 221 |
self.runModule, "g.remove", flags="f", type="raster", name=sig_file_default |
| 222 |
) |
| 223 |
sig_file_high = self.signature_file + "_sep2" |
| 224 |
self.assertModule( |
| 225 |
"i.cluster", |
| 226 |
group=self.group_name, |
| 227 |
subgroup=self.subgroup_name, |
| 228 |
signaturefile=sig_file_high, |
| 229 |
separation=2.0, |
| 230 |
classes=self.num_classes, |
| 231 |
reportfile=report_file, |
| 232 |
overwrite=True, |
| 233 |
) |
| 234 |
self.addCleanup( |
| 235 |
self.runModule, "g.remove", flags="f", type="raster", name=sig_file_high |
| 236 |
) |
| 237 |
parent_dir_info = find_file("sig", element="signatures") |
| 238 |
default_sig_path = os.path.join( |
| 239 |
parent_dir_info["file"], sig_file_default, "sig" |
| 240 |
) |
| 241 |
high_sig_path = os.path.join(parent_dir_info["file"], sig_file_high, "sig") |
| 242 |
default_means = self.parse_signature_means(default_sig_path) |
| 243 |
high_means = self.parse_signature_means(high_sig_path) |
| 244 |
default_min_dist = self.minimum_pairwise_distance(default_means) |
| 245 |
high_min_dist = self.minimum_pairwise_distance(high_means) |
| 246 |
self.assertGreater( |
| 247 |
high_min_dist, |
| 248 |
default_min_dist, |
| 249 |
f"Minimum cluster distance should be higher with separation=2.0 (got {high_min_dist}) than with separation=0.0 (got {default_min_dist})", |
| 250 |
) |
| 251 |
|
| 252 |
def parse_signature_npoints(self, sig_path): |
| 253 |
"""Parse a signature file and return a list of pixel counts per class.""" |
| 254 |
npoints = [] |
| 255 |
with open(sig_path) as f: |
| 256 |
for line in f: |
| 257 |
line = line.strip() |
| 258 |
if line.startswith("#Class "): |
| 259 |
npoints_line = next(f).strip() |
| 260 |
npoints.append(int(npoints_line)) |
| 261 |
return npoints |
| 262 |
|
| 263 |
def test_min_size_parameter(self): |
| 264 |
"""Test that classes have at least min_size pixels.""" |
| 265 |
min_size = 2000 |
| 266 |
sig_file = self.signature_file + "_min_size" |
| 267 |
self.assertModule( |
| 268 |
"i.cluster", |
| 269 |
group=self.group_name, |
| 270 |
subgroup=self.subgroup_name, |
| 271 |
signaturefile=sig_file, |
| 272 |
classes=self.num_classes, |
| 273 |
min_size=min_size, |
| 274 |
overwrite=True, |
| 275 |
) |
| 276 |
self.addCleanup( |
| 277 |
self.runModule, "g.remove", flags="f", type="raster", name=sig_file |
| 278 |
) |
| 279 |
parent_dir_info = find_file("sig", element="signatures") |
| 280 |
sig_path = os.path.join(parent_dir_info["file"], sig_file, "sig") |
| 281 |
npoints = self.parse_signature_npoints(sig_path) |
| 282 |
|
| 283 |
for count in npoints: |
| 284 |
self.assertGreaterEqual( |
| 285 |
count, |
| 286 |
min_size, |
| 287 |
f"Class has {count} pixels, less than min_size={min_size}", |
| 288 |
) |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == "__main__": |
| 292 |
test() |