| 1 | """ |
| 2 | Name: r.smooth.edgepreserve tests |
| 3 | Purpose: Test corectness of outputs |
| 4 | |
| 5 | Author: Maris Nartiss |
| 6 | Copyright: (C) 2025 by Maris Nartiss and the GRASS Development Team |
| 7 | Licence: This program is free software under the GNU General Public |
| 8 | License (>=v2). Read the file COPYING that comes with GRASS |
| 9 | for details. |
| 10 | """ |
| 11 | |
| 12 | import os |
| 13 | from pathlib import Path |
| 14 | |
| 15 | from grass.script.core import tempname |
| 16 | from grass.script.raster import raster_info |
| 17 | from grass.gunittest.case import TestCase |
| 18 | from grass.gunittest.main import test |
| 19 | |
| 20 | |
| 21 | class SmoothingTest(TestCase): |
| 22 | """ |
| 23 | Test correctness of smoothing implementations against hand crafted |
| 24 | reference data |
| 25 | """ |
| 26 | |
| 27 | @classmethod |
| 28 | def setUpClass(cls): |
| 29 | """Import sample maps with known properties""" |
| 30 | |
| 31 | cls.use_temp_region() |
| 32 | cls.runModule("g.region", n=10, s=0, e=10, w=0, res=1) |
| 33 | |
| 34 | cls.data_dir = os.path.join(Path(__file__).parent.absolute(), "data") |
| 35 | cls.rm_rast = [] |
| 36 | |
| 37 | cls.in_map_i = tempname(10) |
| 38 | cls.rm_rast.append(cls.in_map_i) |
| 39 | cls.runModule( |
| 40 | "r.in.ascii", |
| 41 | input=os.path.join(cls.data_dir, "input1.ascii"), |
| 42 | output=cls.in_map_i, |
| 43 | ) |
| 44 | cls.in_map_f = tempname(10) |
| 45 | cls.rm_rast.append(cls.in_map_f) |
| 46 | cls.runModule( |
| 47 | "r.mapcalc", |
| 48 | expression=f"{cls.in_map_f}=float({cls.in_map_i})", |
| 49 | ) |
| 50 | cls.in_map_d = tempname(10) |
| 51 | cls.rm_rast.append(cls.in_map_d) |
| 52 | cls.runModule( |
| 53 | "r.mapcalc", |
| 54 | expression=f"{cls.in_map_d}=double({cls.in_map_i})", |
| 55 | ) |
| 56 | cls.ref_exp_f = tempname(10) |
| 57 | cls.rm_rast.append(cls.ref_exp_f) |
| 58 | cls.runModule( |
| 59 | "r.in.ascii", |
| 60 | input=os.path.join(cls.data_dir, "ref_exp.ascii"), |
| 61 | output=cls.ref_exp_f, |
| 62 | ) |
| 63 | cls.ref_exp_i = tempname(10) |
| 64 | cls.rm_rast.append(cls.ref_exp_i) |
| 65 | cls.runModule( |
| 66 | "r.mapcalc", |
| 67 | expression=f"{cls.ref_exp_i}=round({cls.ref_exp_f})", |
| 68 | ) |
| 69 | cls.ref_quad_f = tempname(10) |
| 70 | cls.rm_rast.append(cls.ref_quad_f) |
| 71 | cls.runModule( |
| 72 | "r.in.ascii", |
| 73 | input=os.path.join(cls.data_dir, "ref_quad.ascii"), |
| 74 | output=cls.ref_quad_f, |
| 75 | ) |
| 76 | cls.ref_tuk_f = tempname(10) |
| 77 | cls.rm_rast.append(cls.ref_tuk_f) |
| 78 | cls.runModule( |
| 79 | "r.in.ascii", |
| 80 | input=os.path.join(cls.data_dir, "ref_tuk.ascii"), |
| 81 | output=cls.ref_tuk_f, |
| 82 | ) |
| 83 | |
| 84 | @classmethod |
| 85 | def tearDownClass(cls): |
| 86 | """Remove temporary data""" |
| 87 | |
| 88 | cls.del_temp_region() |
| 89 | for r in cls.rm_rast: |
| 90 | cls.runModule("g.remove", flags="f", type="raster", name=r) |
| 91 | |
| 92 | def test_exp_i_ram(self): |
| 93 | """ |
| 94 | Test smoothing with Perona & Malik proposed exponential |
| 95 | diffusivity function while keeping data in RAM. |
| 96 | Also tests conversion to int. |
| 97 | """ |
| 98 | |
| 99 | out_map_i = tempname(10) |
| 100 | self.rm_rast.append(out_map_i) |
| 101 | self.assertModule( |
| 102 | "r.smooth.edgepreserve", |
| 103 | input_=self.in_map_i, |
| 104 | output=out_map_i, |
| 105 | memory=100, |
| 106 | steps=5, |
| 107 | threshold=2, |
| 108 | lambda_=0.25, |
| 109 | function="exp", |
| 110 | quiet=True, |
| 111 | ) |
| 112 | self.assertTrue(raster_info(out_map_i)["datatype"] == "CELL") |
| 113 | self.assertRastersEqual(out_map_i, self.ref_exp_i) |
| 114 | |
| 115 | def test_exp_f_ram(self): |
| 116 | """ |
| 117 | Test smoothing with Perona & Malik proposed exponential |
| 118 | diffusivity function while keeping data in RAM. |
| 119 | Also tests conversion to float. |
| 120 | """ |
| 121 | |
| 122 | out_map_f = tempname(10) |
| 123 | self.rm_rast.append(out_map_f) |
| 124 | self.assertModule( |
| 125 | "r.smooth.edgepreserve", |
| 126 | input_=self.in_map_f, |
| 127 | output=out_map_f, |
| 128 | memory=100, |
| 129 | steps=5, |
| 130 | threshold=2, |
| 131 | lambda_=0.25, |
| 132 | function="exp", |
| 133 | quiet=True, |
| 134 | ) |
| 135 | self.assertTrue(raster_info(out_map_f)["datatype"] == "FCELL") |
| 136 | self.assertRastersEqual(out_map_f, self.ref_exp_f, precision=0.1) |
| 137 | |
| 138 | def test_exp_d_ram(self): |
| 139 | """ |
| 140 | Test smoothing with Perona & Malik proposed exponential |
| 141 | diffusivity function while keeping data in RAM. |
| 142 | Also tests conversion to double. |
| 143 | """ |
| 144 | |
| 145 | out_map_d = tempname(10) |
| 146 | self.rm_rast.append(out_map_d) |
| 147 | self.assertModule( |
| 148 | "r.smooth.edgepreserve", |
| 149 | input_=self.in_map_d, |
| 150 | output=out_map_d, |
| 151 | memory=100, |
| 152 | steps=5, |
| 153 | threshold=2, |
| 154 | lambda_=0.25, |
| 155 | function="exp", |
| 156 | quiet=True, |
| 157 | ) |
| 158 | self.assertTrue(raster_info(out_map_d)["datatype"] == "DCELL") |
| 159 | self.assertRastersEqual(out_map_d, self.ref_exp_f, precision=0.1) |
| 160 | |
| 161 | def test_quad_ram(self): |
| 162 | """ |
| 163 | Test smoothing with Perona & Malik proposed quadratic |
| 164 | diffusivity function while keeping data in RAM |
| 165 | """ |
| 166 | |
| 167 | out_map_f = tempname(10) |
| 168 | self.rm_rast.append(out_map_f) |
| 169 | self.assertModule( |
| 170 | "r.smooth.edgepreserve", |
| 171 | input_=self.in_map_f, |
| 172 | output=out_map_f, |
| 173 | memory=100, |
| 174 | steps=5, |
| 175 | threshold=2, |
| 176 | lambda_=0.25, |
| 177 | function="quad", |
| 178 | quiet=True, |
| 179 | ) |
| 180 | self.assertRastersEqual(out_map_f, self.ref_quad_f, precision=0.1) |
| 181 | |
| 182 | def test_tuk_ram(self): |
| 183 | """ |
| 184 | Test smoothing with Black et al. proposed Tukey diffusivity |
| 185 | function while keeping data in RAM |
| 186 | """ |
| 187 | |
| 188 | out_map_f = tempname(10) |
| 189 | self.rm_rast.append(out_map_f) |
| 190 | self.assertModule( |
| 191 | "r.smooth.edgepreserve", |
| 192 | input_=self.in_map_f, |
| 193 | output=out_map_f, |
| 194 | memory=100, |
| 195 | steps=5, |
| 196 | threshold=2, |
| 197 | lambda_=0.25, |
| 198 | function="tuk", |
| 199 | quiet=True, |
| 200 | ) |
| 201 | self.assertRastersEqual(out_map_f, self.ref_tuk_f, precision=0.1) |
| 202 | |
| 203 | def test_exp_disk(self): |
| 204 | """ |
| 205 | Test smoothing with Perona & Malik proposed exponential |
| 206 | diffusivity function while storing data on disk. |
| 207 | """ |
| 208 | |
| 209 | out_map_f = tempname(10) |
| 210 | self.rm_rast.append(out_map_f) |
| 211 | self.assertModule( |
| 212 | "r.smooth.edgepreserve", |
| 213 | input_=self.in_map_f, |
| 214 | output=out_map_f, |
| 215 | memory=0.0022, |
| 216 | steps=5, |
| 217 | threshold=2, |
| 218 | lambda_=0.25, |
| 219 | function="exp", |
| 220 | quiet=True, |
| 221 | ) |
| 222 | self.assertRastersEqual(out_map_f, self.ref_exp_f, precision=0.1) |
| 223 | |
| 224 | |
| 225 | if __name__ == "__main__": |
| 226 | test() |