| 1 |
import grass.script as gs |
| 2 |
from grass.gunittest.case import TestCase |
| 3 |
from grass.gunittest.main import test |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
|
| 7 |
class TestIGensigset(TestCase): |
| 8 |
"""Regression testsuite for i.gensigset module.""" |
| 9 |
|
| 10 |
group_name = "test_gensigset_group" |
| 11 |
subgroup_name = "test_gensigset_subgroup" |
| 12 |
input_maps = ["gensig_band1", "gensig_band2", "gensig_band3"] |
| 13 |
training_map = "gensig_training" |
| 14 |
signature_file = "gensig_signatures" |
| 15 |
temp_rasters = [] |
| 16 |
temp_signatures = [] |
| 17 |
|
| 18 |
@classmethod |
| 19 |
def setUpClass(cls): |
| 20 |
"""Set up the test environment and create input raster maps.""" |
| 21 |
cls.use_temp_region() |
| 22 |
cls.runModule("g.region", n=10, s=0, e=10, w=0, rows=200, cols=200) |
| 23 |
|
| 24 |
cls.runModule( |
| 25 |
"r.mapcalc", |
| 26 |
expression=f"{cls.input_maps[0]} = 50 + 30 * sin(row() / 20.0) + 20 * cos(col() / 15.0)", |
| 27 |
overwrite=True, |
| 28 |
) |
| 29 |
cls.runModule( |
| 30 |
"r.mapcalc", |
| 31 |
expression=f"{cls.input_maps[1]} = 40 + 25 * exp(-(((row() - 100)^2 + (col() - 100)^2) / 2000))", |
| 32 |
overwrite=True, |
| 33 |
) |
| 34 |
cls.runModule( |
| 35 |
"r.mapcalc", |
| 36 |
expression=f"{cls.input_maps[2]} = 30 + (row() + col()) / 10.0", |
| 37 |
overwrite=True, |
| 38 |
) |
| 39 |
cls.temp_rasters.extend(cls.input_maps) |
| 40 |
|
| 41 |
cls.runModule( |
| 42 |
"r.mapcalc", |
| 43 |
expression=( |
| 44 |
f"{cls.training_map} = " |
| 45 |
"if(row() < 60 && col() < 60, 1, " |
| 46 |
"if(row() > 60 && row() < 140 && col() > 60 && col() < 140, 2, " |
| 47 |
"if(row() > 140 && col() > 140, 3, 0)))" |
| 48 |
), |
| 49 |
overwrite=True, |
| 50 |
) |
| 51 |
cls.temp_rasters.append(cls.training_map) |
| 52 |
|
| 53 |
cls.runModule( |
| 54 |
"i.group", |
| 55 |
group=cls.group_name, |
| 56 |
subgroup=cls.subgroup_name, |
| 57 |
input=",".join(cls.input_maps), |
| 58 |
) |
| 59 |
|
| 60 |
@classmethod |
| 61 |
def tearDownClass(cls): |
| 62 |
"""Clean up the test environment by removing created maps and signatures.""" |
| 63 |
cls.runModule("g.remove", flags="f", type="group", name=cls.group_name) |
| 64 |
cls.runModule("g.remove", flags="f", type="raster", name=cls.temp_rasters) |
| 65 |
cls.runModule("i.signatures", remove=cls.temp_signatures, type="sigset") |
| 66 |
cls.del_temp_region() |
| 67 |
|
| 68 |
def _generate_signatures(self, sig_name, **kwargs): |
| 69 |
"""Helper method to generate signatures using i.gensigset.""" |
| 70 |
self.assertModule( |
| 71 |
"i.gensigset", |
| 72 |
trainingmap=self.training_map, |
| 73 |
group=self.group_name, |
| 74 |
subgroup=self.subgroup_name, |
| 75 |
signaturefile=sig_name, |
| 76 |
overwrite=True, |
| 77 |
**kwargs, |
| 78 |
) |
| 79 |
self.temp_signatures.append(sig_name) |
| 80 |
|
| 81 |
def _get_sigfile_path(self, sigfile_name): |
| 82 |
"""Helper method to get the path of the generated signature file.""" |
| 83 |
env = gs.gisenv() |
| 84 |
return ( |
| 85 |
Path(env["GISDBASE"]) |
| 86 |
/ env["LOCATION_NAME"] |
| 87 |
/ env["MAPSET"] |
| 88 |
/ "signatures" |
| 89 |
/ "sigset" |
| 90 |
/ sigfile_name |
| 91 |
/ "sig" |
| 92 |
) |
| 93 |
|
| 94 |
def _parse_sigfile(self, path): |
| 95 |
"""Helper method to parse the signature file and extract statistics.""" |
| 96 |
class_count = 0 |
| 97 |
subclass_count = 0 |
| 98 |
means = [] |
| 99 |
|
| 100 |
with open(path) as f: |
| 101 |
for line in f: |
| 102 |
if line.strip().startswith("class:"): |
| 103 |
class_count += 1 |
| 104 |
elif line.strip().startswith("subclass:"): |
| 105 |
subclass_count += 1 |
| 106 |
elif line.strip().startswith("means:"): |
| 107 |
mean_vals = list(map(float, line.strip().split(":")[1].split())) |
| 108 |
means.append(mean_vals) |
| 109 |
|
| 110 |
return { |
| 111 |
"classes": class_count, |
| 112 |
"total_subsigs": subclass_count, |
| 113 |
"mean_vectors": means, |
| 114 |
} |
| 115 |
|
| 116 |
def test_basic_signature_generation(self): |
| 117 |
"""Validate that the generated signature file contains expected classes and subsignatures.""" |
| 118 |
sig_file = f"{self.signature_file}_basic" |
| 119 |
self._generate_signatures(sig_file) |
| 120 |
sig_path = self._get_sigfile_path(sig_file) |
| 121 |
|
| 122 |
self.assertTrue( |
| 123 |
sig_path.exists(), f"Expected signature file does not exist: {sig_path}" |
| 124 |
) |
| 125 |
stats = self._parse_sigfile(sig_path) |
| 126 |
|
| 127 |
self.assertEqual( |
| 128 |
stats["classes"], 3, "Expected 3 classes based on training map" |
| 129 |
) |
| 130 |
self.assertGreaterEqual( |
| 131 |
stats["total_subsigs"], 3, "Expected at least 3 subsignatures" |
| 132 |
) |
| 133 |
for vec in stats["mean_vectors"]: |
| 134 |
self.assertEqual(len(vec), 3, "Each mean vector should have 3 dimensions.") |
| 135 |
for val in vec: |
| 136 |
self.assertGreaterEqual(val, 0.0) |
| 137 |
self.assertLessEqual(val, 300.0) |
| 138 |
|
| 139 |
def test_maxsig_parameter(self): |
| 140 |
"""Verify that increasing maxsig yields same or more classes and subsignatures.""" |
| 141 |
sig_file_max3 = f"{self.signature_file}_max3" |
| 142 |
sig_file_max10 = f"{self.signature_file}_max10" |
| 143 |
|
| 144 |
self._generate_signatures(sig_file_max3, maxsig=3) |
| 145 |
self._generate_signatures(sig_file_max10, maxsig=10) |
| 146 |
|
| 147 |
path3 = self._get_sigfile_path(sig_file_max3) |
| 148 |
path10 = self._get_sigfile_path(sig_file_max10) |
| 149 |
|
| 150 |
stats3 = self._parse_sigfile(path3) |
| 151 |
stats10 = self._parse_sigfile(path10) |
| 152 |
|
| 153 |
self.assertGreaterEqual( |
| 154 |
stats10["classes"], |
| 155 |
stats3["classes"], |
| 156 |
"Expected maxsig=10 to result in equal or more classes than maxsig=3", |
| 157 |
) |
| 158 |
|
| 159 |
self.assertGreaterEqual( |
| 160 |
stats10["total_subsigs"], |
| 161 |
stats3["total_subsigs"], |
| 162 |
"Expected maxsig=10 to result in equal or more subsignatures than maxsig=3", |
| 163 |
) |
| 164 |
|
| 165 |
def test_minimal_maxsig(self): |
| 166 |
"""Test that setting maxsig=1 produces minimal valid signatures.""" |
| 167 |
sig_file = f"{self.signature_file}_min" |
| 168 |
self._generate_signatures(sig_file, maxsig=1) |
| 169 |
path = self._get_sigfile_path(sig_file) |
| 170 |
stats = self._parse_sigfile(path) |
| 171 |
|
| 172 |
self.assertLessEqual( |
| 173 |
stats["classes"], |
| 174 |
4, |
| 175 |
"Should not have more than 4 classes due to map structure", |
| 176 |
) |
| 177 |
self.assertGreaterEqual( |
| 178 |
stats["total_subsigs"], |
| 179 |
3, |
| 180 |
"Should produce at least one subsignature per valid class", |
| 181 |
) |
| 182 |
self.assertLessEqual( |
| 183 |
stats["total_subsigs"], |
| 184 |
4, |
| 185 |
"Unexpected number of subsignatures with maxsig=1", |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == "__main__": |
| 190 |
test() |