| 1 |
"""Test of imagery signature management module i.signatures |
| 2 |
|
| 3 |
@author Maris Nartiss |
| 4 |
|
| 5 |
@copyright 2023 by Maris Nartiss and the GRASS Development Team |
| 6 |
|
| 7 |
@license This program is free software under the GNU General Public License (>=v2). |
| 8 |
Read the file COPYING that comes with GRASS |
| 9 |
for details |
| 10 |
""" |
| 11 |
|
| 12 |
import shutil |
| 13 |
import json |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
from grass.gunittest.case import TestCase |
| 17 |
from grass.gunittest.main import test |
| 18 |
from grass.gunittest.gmodules import SimpleModule |
| 19 |
|
| 20 |
from grass.script.core import tempname |
| 21 |
from grass.pygrass import utils |
| 22 |
from grass.pygrass.gis import Mapset |
| 23 |
|
| 24 |
from grass.lib.gis import ( |
| 25 |
G_mapset_path, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
class PrintSignaturesTestCase(TestCase): |
| 30 |
@classmethod |
| 31 |
def setUpClass(cls): |
| 32 |
cls.mpath = utils.decode(G_mapset_path()) |
| 33 |
cls.mapset_name = Mapset().name |
| 34 |
cls.sigdirs = [] |
| 35 |
|
| 36 |
|
| 37 |
Path(f"{cls.mpath}/signatures/sig/").mkdir(exist_ok=True, parents=True) |
| 38 |
Path(f"{cls.mpath}/signatures/sigset/").mkdir(exist_ok=True, parents=True) |
| 39 |
Path(f"{cls.mpath}/signatures/libsvm/").mkdir(exist_ok=True, parents=True) |
| 40 |
|
| 41 |
cls.sig_name1 = tempname(10) |
| 42 |
sig_dir1 = f"{cls.mpath}/signatures/sig/{cls.sig_name1}" |
| 43 |
Path(sig_dir1).mkdir(parents=True) |
| 44 |
cls.sigdirs.append(sig_dir1) |
| 45 |
sigfile_name1 = f"{sig_dir1}/sig" |
| 46 |
open(sigfile_name1, "a").close() |
| 47 |
|
| 48 |
cls.sig_name2 = tempname(10) |
| 49 |
sig_dir2 = f"{cls.mpath}/signatures/sigset/{cls.sig_name2}" |
| 50 |
Path(sig_dir2).mkdir(parents=True) |
| 51 |
cls.sigdirs.append(sig_dir2) |
| 52 |
sigfile_name2 = f"{sig_dir2}/sig" |
| 53 |
open(sigfile_name2, "a").close() |
| 54 |
|
| 55 |
cls.sig_name3 = tempname(10) |
| 56 |
sig_dir3 = f"{cls.mpath}/signatures/libsvm/{cls.sig_name3}" |
| 57 |
Path(sig_dir3).mkdir(parents=True) |
| 58 |
cls.sigdirs.append(sig_dir3) |
| 59 |
sigfile_name3 = f"{sig_dir3}/sig" |
| 60 |
open(sigfile_name3, "a").close() |
| 61 |
|
| 62 |
@classmethod |
| 63 |
def tearDownClass(cls): |
| 64 |
for d in cls.sigdirs: |
| 65 |
shutil.rmtree(d, ignore_errors=True) |
| 66 |
|
| 67 |
def test_print_all_plain(self): |
| 68 |
""" |
| 69 |
If no signature file type is specified, it should print all of them |
| 70 |
""" |
| 71 |
i_sig = SimpleModule("i.signatures", flags="p") |
| 72 |
self.assertModule(i_sig) |
| 73 |
self.assertTrue(i_sig.outputs.stdout) |
| 74 |
self.assertIn(self.sig_name1, i_sig.outputs.stdout) |
| 75 |
self.assertIn(self.sig_name2, i_sig.outputs.stdout) |
| 76 |
self.assertIn(self.sig_name3, i_sig.outputs.stdout) |
| 77 |
|
| 78 |
def test_print_type_plain(self): |
| 79 |
""" |
| 80 |
If a type is specified, only signatures of matching type should be printed |
| 81 |
""" |
| 82 |
|
| 83 |
i_sig = SimpleModule("i.signatures", type="sig", flags="p") |
| 84 |
self.assertModule(i_sig) |
| 85 |
self.assertTrue(i_sig.outputs.stdout) |
| 86 |
self.assertIn(self.sig_name1, i_sig.outputs.stdout) |
| 87 |
self.assertNotIn(self.sig_name2, i_sig.outputs.stdout) |
| 88 |
self.assertNotIn(self.sig_name3, i_sig.outputs.stdout) |
| 89 |
|
| 90 |
i_sig = SimpleModule("i.signatures", type="sigset", flags="p") |
| 91 |
self.assertModule(i_sig) |
| 92 |
self.assertTrue(i_sig.outputs.stdout) |
| 93 |
self.assertNotIn(self.sig_name1, i_sig.outputs.stdout) |
| 94 |
self.assertIn(self.sig_name2, i_sig.outputs.stdout) |
| 95 |
self.assertNotIn(self.sig_name3, i_sig.outputs.stdout) |
| 96 |
|
| 97 |
i_sig = SimpleModule("i.signatures", type="libsvm", flags="p") |
| 98 |
self.assertModule(i_sig) |
| 99 |
self.assertTrue(i_sig.outputs.stdout) |
| 100 |
self.assertNotIn(self.sig_name1, i_sig.outputs.stdout) |
| 101 |
self.assertNotIn(self.sig_name2, i_sig.outputs.stdout) |
| 102 |
self.assertIn(self.sig_name3, i_sig.outputs.stdout) |
| 103 |
|
| 104 |
def test_print_all_json(self): |
| 105 |
""" |
| 106 |
If no signature file type is specified, it should print all of them |
| 107 |
""" |
| 108 |
i_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 109 |
self.assertModule(i_sig) |
| 110 |
self.assertTrue(i_sig.outputs.stdout) |
| 111 |
json_out = json.loads(i_sig.outputs.stdout) |
| 112 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 113 |
self.assertIn(f"{self.sig_name2}@{self.mapset_name}", json_out["sigset"]) |
| 114 |
self.assertIn(f"{self.sig_name3}@{self.mapset_name}", json_out["libsvm"]) |
| 115 |
|
| 116 |
def test_print_type_json(self): |
| 117 |
""" |
| 118 |
If a type is specified, only signatures of matching type should be printed |
| 119 |
""" |
| 120 |
|
| 121 |
i_sig = SimpleModule("i.signatures", type="sig", format="json", flags="p") |
| 122 |
self.assertModule(i_sig) |
| 123 |
self.assertTrue(i_sig.outputs.stdout) |
| 124 |
json_out = json.loads(i_sig.outputs.stdout) |
| 125 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 126 |
self.assertNotIn("sigset", json_out.keys()) |
| 127 |
self.assertNotIn("libsvm", json_out.keys()) |
| 128 |
|
| 129 |
i_sig = SimpleModule("i.signatures", type="sigset", format="json", flags="p") |
| 130 |
self.assertModule(i_sig) |
| 131 |
self.assertTrue(i_sig.outputs.stdout) |
| 132 |
json_out = json.loads(i_sig.outputs.stdout) |
| 133 |
self.assertIn(f"{self.sig_name2}@{self.mapset_name}", json_out["sigset"]) |
| 134 |
self.assertNotIn("sig", json_out.keys()) |
| 135 |
self.assertNotIn("libsvm", json_out.keys()) |
| 136 |
|
| 137 |
i_sig = SimpleModule("i.signatures", type="libsvm", format="json", flags="p") |
| 138 |
self.assertModule(i_sig) |
| 139 |
self.assertTrue(i_sig.outputs.stdout) |
| 140 |
json_out = json.loads(i_sig.outputs.stdout) |
| 141 |
self.assertIn(f"{self.sig_name3}@{self.mapset_name}", json_out["libsvm"]) |
| 142 |
self.assertNotIn("sig", json_out.keys()) |
| 143 |
self.assertNotIn("sigset", json_out.keys()) |
| 144 |
|
| 145 |
|
| 146 |
class ManageSignaturesTestCase(TestCase): |
| 147 |
@classmethod |
| 148 |
def setUpClass(cls): |
| 149 |
cls.mpath = utils.decode(G_mapset_path()) |
| 150 |
cls.mapset_name = Mapset().name |
| 151 |
cls.sigdirs = [] |
| 152 |
|
| 153 |
|
| 154 |
Path(f"{cls.mpath}/signatures/sig/").mkdir(exist_ok=True, parents=True) |
| 155 |
Path(f"{cls.mpath}/signatures/sigset/").mkdir(exist_ok=True, parents=True) |
| 156 |
Path(f"{cls.mpath}/signatures/libsvm/").mkdir(exist_ok=True, parents=True) |
| 157 |
|
| 158 |
cls.sig_name1 = tempname(10) |
| 159 |
sig_dir1 = f"{cls.mpath}/signatures/sig/{cls.sig_name1}" |
| 160 |
Path(sig_dir1).mkdir(parents=True) |
| 161 |
cls.sigdirs.append(sig_dir1) |
| 162 |
sigfile_name1 = f"{sig_dir1}/sig" |
| 163 |
open(sigfile_name1, "a").close() |
| 164 |
|
| 165 |
cls.sig_name2 = tempname(10) |
| 166 |
sig_dir2 = f"{cls.mpath}/signatures/sig/{cls.sig_name2}" |
| 167 |
Path(sig_dir2).mkdir(parents=True) |
| 168 |
cls.sigdirs.append(sig_dir2) |
| 169 |
sigfile_name2 = f"{sig_dir2}/sig" |
| 170 |
open(sigfile_name2, "a").close() |
| 171 |
|
| 172 |
cls.sig_name3 = tempname(10) |
| 173 |
sig_dir3 = f"{cls.mpath}/signatures/sigset/{cls.sig_name3}" |
| 174 |
Path(sig_dir3).mkdir(parents=True) |
| 175 |
cls.sigdirs.append(sig_dir3) |
| 176 |
sigfile_name3 = f"{sig_dir3}/sig" |
| 177 |
open(sigfile_name3, "a").close() |
| 178 |
|
| 179 |
cls.sig_name4 = tempname(10) |
| 180 |
sig_dir4 = f"{cls.mpath}/signatures/sigset/{cls.sig_name4}" |
| 181 |
Path(sig_dir4).mkdir(parents=True) |
| 182 |
cls.sigdirs.append(sig_dir4) |
| 183 |
sigfile_name4 = f"{sig_dir4}/sig" |
| 184 |
open(sigfile_name4, "a").close() |
| 185 |
|
| 186 |
cls.sig_name5 = tempname(10) |
| 187 |
sig_dir5 = f"{cls.mpath}/signatures/libsvm/{cls.sig_name5}" |
| 188 |
Path(sig_dir5).mkdir(parents=True) |
| 189 |
cls.sigdirs.append(sig_dir5) |
| 190 |
sigfile_name5 = f"{sig_dir5}/sig" |
| 191 |
open(sigfile_name5, "a").close() |
| 192 |
|
| 193 |
cls.sig_name6 = tempname(10) |
| 194 |
sig_dir6 = f"{cls.mpath}/signatures/libsvm/{cls.sig_name6}" |
| 195 |
Path(sig_dir6).mkdir(parents=True) |
| 196 |
cls.sigdirs.append(sig_dir6) |
| 197 |
sigfile_name6 = f"{sig_dir6}/sig" |
| 198 |
open(sigfile_name6, "a").close() |
| 199 |
|
| 200 |
@classmethod |
| 201 |
def tearDownClass(cls): |
| 202 |
for d in cls.sigdirs: |
| 203 |
shutil.rmtree(d, ignore_errors=True) |
| 204 |
|
| 205 |
def test_copy(self): |
| 206 |
a_copy = tempname(10) |
| 207 |
|
| 208 |
i_sig = SimpleModule("i.signatures", copy=(self.sig_name1, a_copy)) |
| 209 |
self.assertModuleFail(i_sig) |
| 210 |
|
| 211 |
|
| 212 |
i_sig = SimpleModule( |
| 213 |
"i.signatures", type="sigset", copy=(self.sig_name1, a_copy) |
| 214 |
) |
| 215 |
self.assertModule(i_sig) |
| 216 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 217 |
self.assertModule(l_sig) |
| 218 |
self.assertTrue(l_sig.outputs.stdout) |
| 219 |
json_out = json.loads(l_sig.outputs.stdout) |
| 220 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 221 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sig"]) |
| 222 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sigset"]) |
| 223 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["libsvm"]) |
| 224 |
|
| 225 |
|
| 226 |
i_sig = SimpleModule("i.signatures", type="sig", copy=(self.sig_name1, a_copy)) |
| 227 |
self.assertModule(i_sig) |
| 228 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 229 |
self.assertModule(l_sig) |
| 230 |
self.assertTrue(l_sig.outputs.stdout) |
| 231 |
json_out = json.loads(l_sig.outputs.stdout) |
| 232 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 233 |
self.assertIn(f"{a_copy}@{self.mapset_name}", json_out["sig"]) |
| 234 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sigset"]) |
| 235 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["libsvm"]) |
| 236 |
|
| 237 |
def test_rename(self): |
| 238 |
a_copy = tempname(10) |
| 239 |
|
| 240 |
i_sig = SimpleModule("i.signatures", rename=(self.sig_name2, a_copy)) |
| 241 |
self.assertModuleFail(i_sig) |
| 242 |
|
| 243 |
|
| 244 |
i_sig = SimpleModule( |
| 245 |
"i.signatures", type="sigset", rename=(self.sig_name2, a_copy) |
| 246 |
) |
| 247 |
self.assertModule(i_sig) |
| 248 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 249 |
self.assertModule(l_sig) |
| 250 |
self.assertTrue(l_sig.outputs.stdout) |
| 251 |
json_out = json.loads(l_sig.outputs.stdout) |
| 252 |
self.assertIn(f"{self.sig_name2}@{self.mapset_name}", json_out["sig"]) |
| 253 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sig"]) |
| 254 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sigset"]) |
| 255 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["libsvm"]) |
| 256 |
|
| 257 |
|
| 258 |
i_sig = SimpleModule( |
| 259 |
"i.signatures", type="sig", rename=(self.sig_name2, a_copy) |
| 260 |
) |
| 261 |
self.assertModule(i_sig) |
| 262 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 263 |
self.assertModule(l_sig) |
| 264 |
self.assertTrue(l_sig.outputs.stdout) |
| 265 |
json_out = json.loads(l_sig.outputs.stdout) |
| 266 |
self.assertNotIn(f"{self.sig_name2}@{self.mapset_name}", json_out["sig"]) |
| 267 |
self.assertNotIn(f"{self.sig_name2}@{self.mapset_name}", json_out["sigset"]) |
| 268 |
self.assertNotIn(f"{self.sig_name2}@{self.mapset_name}", json_out["libsvm"]) |
| 269 |
self.assertIn(f"{a_copy}@{self.mapset_name}", json_out["sig"]) |
| 270 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["sigset"]) |
| 271 |
self.assertNotIn(f"{a_copy}@{self.mapset_name}", json_out["libsvm"]) |
| 272 |
|
| 273 |
def test_remove(self): |
| 274 |
|
| 275 |
i_sig = SimpleModule("i.signatures", remove=self.sig_name3) |
| 276 |
self.assertModuleFail(i_sig) |
| 277 |
|
| 278 |
|
| 279 |
i_sig = SimpleModule("i.signatures", type="sig", remove=self.sig_name3) |
| 280 |
self.assertModule(i_sig) |
| 281 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 282 |
self.assertModule(l_sig) |
| 283 |
self.assertTrue(l_sig.outputs.stdout) |
| 284 |
json_out = json.loads(l_sig.outputs.stdout) |
| 285 |
self.assertIn(f"{self.sig_name3}@{self.mapset_name}", json_out["sigset"]) |
| 286 |
self.assertIn(f"{self.sig_name4}@{self.mapset_name}", json_out["sigset"]) |
| 287 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 288 |
self.assertIn(f"{self.sig_name5}@{self.mapset_name}", json_out["libsvm"]) |
| 289 |
self.assertIn(f"{self.sig_name6}@{self.mapset_name}", json_out["libsvm"]) |
| 290 |
|
| 291 |
|
| 292 |
i_sig = SimpleModule("i.signatures", type="sigset", remove=self.sig_name3) |
| 293 |
self.assertModule(i_sig) |
| 294 |
l_sig = SimpleModule("i.signatures", format="json", flags="p") |
| 295 |
self.assertModule(l_sig) |
| 296 |
self.assertTrue(l_sig.outputs.stdout) |
| 297 |
json_out = json.loads(l_sig.outputs.stdout) |
| 298 |
self.assertNotIn(f"{self.sig_name3}@{self.mapset_name}", json_out["sigset"]) |
| 299 |
self.assertIn(f"{self.sig_name4}@{self.mapset_name}", json_out["sigset"]) |
| 300 |
self.assertIn(f"{self.sig_name1}@{self.mapset_name}", json_out["sig"]) |
| 301 |
self.assertIn(f"{self.sig_name5}@{self.mapset_name}", json_out["libsvm"]) |
| 302 |
self.assertIn(f"{self.sig_name6}@{self.mapset_name}", json_out["libsvm"]) |
| 303 |
|
| 304 |
|
| 305 |
if __name__ == "__main__": |
| 306 |
test() |