| 1 |
from pathlib import Path |
| 2 |
from grass.gunittest.case import TestCase |
| 3 |
from grass.gunittest.main import test |
| 4 |
from grass.script.raster import raster_info |
| 5 |
from grass.script import tempfile |
| 6 |
|
| 7 |
|
| 8 |
class TestNeighbors(TestCase): |
| 9 |
""" |
| 10 |
|
| 11 |
Used dataset: nc_spm_08_grass7 |
| 12 |
|
| 13 |
Test cases: |
| 14 |
test_standard_options: Test output with standard options |
| 15 |
(size=3, quadratic moving window, standard statistics). |
| 16 |
test_standard_options_quantile: Test output with standard options |
| 17 |
(size=3, quadratic moving window, quantile statistics). |
| 18 |
test_standard_options_circular: Test output with circular neighborhood |
| 19 |
(size=7, standard statistics). |
| 20 |
test_standard_options_selection: Test output with selection raster |
| 21 |
(standard options otherwise). |
| 22 |
test_weighting_function: Test results with gaussian weighting |
| 23 |
(standard options otherwise). |
| 24 |
test_weighting_file: Test results with file for weighting |
| 25 |
(standard options otherwise). |
| 26 |
""" |
| 27 |
|
| 28 |
test_options = { |
| 29 |
"test_standard_options": { |
| 30 |
"minimum": { |
| 31 |
"n": 2025000, |
| 32 |
"null_cells": 0, |
| 33 |
"cells": 2025000, |
| 34 |
"min": 55.5787925720215, |
| 35 |
"max": 156.201751708984, |
| 36 |
"range": 100.622959136963, |
| 37 |
"mean": 109.482040656279, |
| 38 |
"mean_of_abs": 109.482040656279, |
| 39 |
"stddev": 20.3075456214408, |
| 40 |
"variance": 412.3964091669, |
| 41 |
"coeff_var": 18.5487459858342, |
| 42 |
"sum": 221701132.328964, |
| 43 |
}, |
| 44 |
"perc90": { |
| 45 |
"n": 2025000, |
| 46 |
"null_cells": 0, |
| 47 |
"cells": 2025000, |
| 48 |
"min": 56.0450115203857, |
| 49 |
"max": 156.317572021484, |
| 50 |
"range": 100.272560501099, |
| 51 |
"mean": 111.060386339868, |
| 52 |
"mean_of_abs": 111.060386339868, |
| 53 |
"stddev": 20.301968270214, |
| 54 |
"variance": 412.169915644776, |
| 55 |
"coeff_var": 18.2801167358501, |
| 56 |
"sum": 224897282.338233, |
| 57 |
}, |
| 58 |
"average": { |
| 59 |
"n": 2025000, |
| 60 |
"null_cells": 0, |
| 61 |
"cells": 2025000, |
| 62 |
"min": 56.0054066975911, |
| 63 |
"max": 156.26158481174, |
| 64 |
"range": 100.256178114149, |
| 65 |
"mean": 110.375489343136, |
| 66 |
"mean_of_abs": 110.375489343136, |
| 67 |
"stddev": 20.302357806975, |
| 68 |
"variance": 412.18573252244, |
| 69 |
"coeff_var": 18.3939006094541, |
| 70 |
"sum": 223510365.919864, |
| 71 |
}, |
| 72 |
"maximum": { |
| 73 |
"n": 2025000, |
| 74 |
"null_cells": 0, |
| 75 |
"cells": 2025000, |
| 76 |
"min": 56.0526123046875, |
| 77 |
"max": 156.329864501953, |
| 78 |
"range": 100.277252197266, |
| 79 |
"mean": 111.262932473037, |
| 80 |
"mean_of_abs": 111.262932473037, |
| 81 |
"stddev": 20.2987699017146, |
| 82 |
"variance": 412.040059522756, |
| 83 |
"coeff_var": 18.2439644997077, |
| 84 |
"sum": 225307438.2579, |
| 85 |
}, |
| 86 |
"stddev": { |
| 87 |
"n": 2025000, |
| 88 |
"null_cells": 0, |
| 89 |
"cells": 2025000, |
| 90 |
"min": 0, |
| 91 |
"max": 6.39749880494391, |
| 92 |
"range": 6.39749880494391, |
| 93 |
"mean": 0.576904240112151, |
| 94 |
"mean_of_abs": 0.576904240112151, |
| 95 |
"stddev": 0.440364969041587, |
| 96 |
"variance": 0.193921305958998, |
| 97 |
"coeff_var": 76.3324202567793, |
| 98 |
"sum": 1168231.08622711, |
| 99 |
}, |
| 100 |
"sum": { |
| 101 |
"n": 2025000, |
| 102 |
"null_cells": 0, |
| 103 |
"cells": 2025000, |
| 104 |
"min": 233.24821472168, |
| 105 |
"max": 1406.35424804688, |
| 106 |
"range": 1173.1060333252, |
| 107 |
"mean": 992.482567853469, |
| 108 |
"mean_of_abs": 992.482567853469, |
| 109 |
"stddev": 183.486667152836, |
| 110 |
"variance": 33667.3570228558, |
| 111 |
"coeff_var": 18.4876463422103, |
| 112 |
"sum": 2009777199.90327, |
| 113 |
}, |
| 114 |
}, |
| 115 |
"test_standard_options_quantile": { |
| 116 |
0.01: { |
| 117 |
"n": 2025000, |
| 118 |
"null_cells": 0, |
| 119 |
"cells": 2025000, |
| 120 |
"min": 55.5847009658813, |
| 121 |
"max": 156.203791503906, |
| 122 |
"range": 100.619090538025, |
| 123 |
"mean": 109.503018547944, |
| 124 |
"mean_of_abs": 109.503018547944, |
| 125 |
"stddev": 20.3076172385226, |
| 126 |
"variance": 412.398941046239, |
| 127 |
"coeff_var": 18.5452494634586, |
| 128 |
"sum": 221743612.559588, |
| 129 |
}, |
| 130 |
0.03: { |
| 131 |
"n": 2025000, |
| 132 |
"null_cells": 0, |
| 133 |
"cells": 2025000, |
| 134 |
"min": 55.5965177536011, |
| 135 |
"max": 156.20787109375, |
| 136 |
"range": 100.611353340149, |
| 137 |
"mean": 109.544974331288, |
| 138 |
"mean_of_abs": 109.544974331288, |
| 139 |
"stddev": 20.3078125947647, |
| 140 |
"variance": 412.407252384084, |
| 141 |
"coeff_var": 18.5383334276472, |
| 142 |
"sum": 221828573.020859, |
| 143 |
}, |
| 144 |
0.95: { |
| 145 |
"n": 2025000, |
| 146 |
"null_cells": 0, |
| 147 |
"cells": 2025000, |
| 148 |
"min": 56.0488119125366, |
| 149 |
"max": 156.323718261719, |
| 150 |
"range": 100.274906349182, |
| 151 |
"mean": 111.161659406447, |
| 152 |
"mean_of_abs": 111.161659406447, |
| 153 |
"stddev": 20.3000320258108, |
| 154 |
"variance": 412.091300248942, |
| 155 |
"coeff_var": 18.2617209334619, |
| 156 |
"sum": 225102360.298065, |
| 157 |
}, |
| 158 |
}, |
| 159 |
"test_standard_options_circular": { |
| 160 |
"minimum": { |
| 161 |
"n": 2025000, |
| 162 |
"null_cells": 0, |
| 163 |
"cells": 2025000, |
| 164 |
"min": 55.5787925720215, |
| 165 |
"max": 155.872573852539, |
| 166 |
"range": 100.293781280518, |
| 167 |
"mean": 108.457334170361, |
| 168 |
"mean_of_abs": 108.457334170361, |
| 169 |
"stddev": 20.2561980947474, |
| 170 |
"variance": 410.313561253647, |
| 171 |
"coeff_var": 18.67665128384, |
| 172 |
"sum": 219626101.694981, |
| 173 |
}, |
| 174 |
"perc90": { |
| 175 |
"n": 2025000, |
| 176 |
"null_cells": 0, |
| 177 |
"cells": 2025000, |
| 178 |
"min": 56.2710201263428, |
| 179 |
"max": 156.299404907227, |
| 180 |
"range": 100.028384780884, |
| 181 |
"mean": 111.688409057181, |
| 182 |
"mean_of_abs": 111.688409057181, |
| 183 |
"stddev": 20.286687968792, |
| 184 |
"variance": 411.54970874313, |
| 185 |
"coeff_var": 18.1636466487815, |
| 186 |
"sum": 226169028.340791, |
| 187 |
}, |
| 188 |
"average": { |
| 189 |
"n": 2025000, |
| 190 |
"null_cells": 0, |
| 191 |
"cells": 2025000, |
| 192 |
"min": 56.1745609707303, |
| 193 |
"max": 156.191996607287, |
| 194 |
"range": 100.017435636557, |
| 195 |
"mean": 110.375576827845, |
| 196 |
"mean_of_abs": 110.375576827845, |
| 197 |
"stddev": 20.2777760438563, |
| 198 |
"variance": 411.188201284794, |
| 199 |
"coeff_var": 18.371615013604, |
| 200 |
"sum": 223510543.076386, |
| 201 |
}, |
| 202 |
"maximum": { |
| 203 |
"n": 2025000, |
| 204 |
"null_cells": 0, |
| 205 |
"cells": 2025000, |
| 206 |
"min": 56.6209869384766, |
| 207 |
"max": 156.329864501953, |
| 208 |
"range": 99.7088775634766, |
| 209 |
"mean": 112.257478616111, |
| 210 |
"mean_of_abs": 112.257478616111, |
| 211 |
"stddev": 20.268387328814, |
| 212 |
"variance": 410.807524910826, |
| 213 |
"coeff_var": 18.0552668549827, |
| 214 |
"sum": 227321394.197624, |
| 215 |
}, |
| 216 |
"stddev": { |
| 217 |
"n": 2025000, |
| 218 |
"null_cells": 0, |
| 219 |
"cells": 2025000, |
| 220 |
"min": 0, |
| 221 |
"max": 9.34457118441325, |
| 222 |
"range": 9.34457118441325, |
| 223 |
"mean": 1.02915670094965, |
| 224 |
"mean_of_abs": 1.02915670094965, |
| 225 |
"stddev": 0.682319147525201, |
| 226 |
"variance": 0.465559419079517, |
| 227 |
"coeff_var": 66.2988587545119, |
| 228 |
"sum": 2084042.31942303, |
| 229 |
}, |
| 230 |
}, |
| 231 |
"test_standard_options_selection": { |
| 232 |
"minimum": { |
| 233 |
"n": 2025000, |
| 234 |
"null_cells": 0, |
| 235 |
"cells": 2025000, |
| 236 |
"min": 55.5787925720215, |
| 237 |
"max": 156.329864501953, |
| 238 |
"range": 100.751071929932, |
| 239 |
"mean": 110.375440275606, |
| 240 |
"mean_of_abs": 110.375440275606, |
| 241 |
"stddev": 20.3153233205981, |
| 242 |
"variance": 412.712361620436, |
| 243 |
"coeff_var": 18.4056555243368, |
| 244 |
"sum": 223510266.558102, |
| 245 |
}, |
| 246 |
"perc90": { |
| 247 |
"n": 2025000, |
| 248 |
"null_cells": 0, |
| 249 |
"cells": 2025000, |
| 250 |
"min": 55.5787925720215, |
| 251 |
"max": 156.329864501953, |
| 252 |
"range": 100.751071929932, |
| 253 |
"mean": 110.375440275606, |
| 254 |
"mean_of_abs": 110.375440275606, |
| 255 |
"stddev": 20.3153233205981, |
| 256 |
"variance": 412.712361620436, |
| 257 |
"coeff_var": 18.4056555243368, |
| 258 |
"sum": 223510266.558102, |
| 259 |
}, |
| 260 |
"average": { |
| 261 |
"n": 2025000, |
| 262 |
"null_cells": 0, |
| 263 |
"cells": 2025000, |
| 264 |
"min": 55.5787925720215, |
| 265 |
"max": 156.329864501953, |
| 266 |
"range": 100.751071929932, |
| 267 |
"mean": 110.375440275606, |
| 268 |
"mean_of_abs": 110.375440275606, |
| 269 |
"stddev": 20.3153233205981, |
| 270 |
"variance": 412.712361620436, |
| 271 |
"coeff_var": 18.4056555243368, |
| 272 |
"sum": 223510266.558102, |
| 273 |
}, |
| 274 |
"maximum": { |
| 275 |
"n": 2025000, |
| 276 |
"null_cells": 0, |
| 277 |
"cells": 2025000, |
| 278 |
"min": 55.5787925720215, |
| 279 |
"max": 156.329864501953, |
| 280 |
"range": 100.751071929932, |
| 281 |
"mean": 110.375440275606, |
| 282 |
"mean_of_abs": 110.375440275606, |
| 283 |
"stddev": 20.3153233205981, |
| 284 |
"variance": 412.712361620436, |
| 285 |
"coeff_var": 18.4056555243368, |
| 286 |
"sum": 223510266.558102, |
| 287 |
}, |
| 288 |
"stddev": { |
| 289 |
"n": 2025000, |
| 290 |
"null_cells": 0, |
| 291 |
"cells": 2025000, |
| 292 |
"min": 55.5787925720215, |
| 293 |
"max": 156.329864501953, |
| 294 |
"range": 100.751071929932, |
| 295 |
"mean": 110.375440275606, |
| 296 |
"mean_of_abs": 110.375440275606, |
| 297 |
"stddev": 20.3153233205981, |
| 298 |
"variance": 412.712361620436, |
| 299 |
"coeff_var": 18.4056555243368, |
| 300 |
"sum": 223510266.558102, |
| 301 |
}, |
| 302 |
}, |
| 303 |
"test_weighting_function": { |
| 304 |
"perc90": { |
| 305 |
"n": 2025000, |
| 306 |
"null_cells": 0, |
| 307 |
"cells": 2025000, |
| 308 |
"min": 56.4677543640137, |
| 309 |
"max": 156.30549621582, |
| 310 |
"range": 99.8377418518066, |
| 311 |
"mean": 111.789766253374, |
| 312 |
"mean_of_abs": 111.789766253374, |
| 313 |
"stddev": 20.2825397924116, |
| 314 |
"variance": 411.381420430759, |
| 315 |
"coeff_var": 18.1434673961486, |
| 316 |
"sum": 226374276.663082, |
| 317 |
}, |
| 318 |
"average": { |
| 319 |
"n": 2025000, |
| 320 |
"null_cells": 0, |
| 321 |
"cells": 2025000, |
| 322 |
"min": 56.235548348967, |
| 323 |
"max": 156.164602692463, |
| 324 |
"range": 99.9290543434962, |
| 325 |
"mean": 110.375593417834, |
| 326 |
"mean_of_abs": 110.375593417834, |
| 327 |
"stddev": 20.2736626611521, |
| 328 |
"variance": 411.021397698193, |
| 329 |
"coeff_var": 18.3678855382501, |
| 330 |
"sum": 223510576.671115, |
| 331 |
}, |
| 332 |
"stddev": { |
| 333 |
"n": 2025000, |
| 334 |
"null_cells": 0, |
| 335 |
"cells": 2025000, |
| 336 |
"min": 0, |
| 337 |
"max": 9.43832203774046, |
| 338 |
"range": 9.43832203774046, |
| 339 |
"mean": 1.09653550798646, |
| 340 |
"mean_of_abs": 1.09653550798646, |
| 341 |
"stddev": 0.699381561482314, |
| 342 |
"variance": 0.48913456854144, |
| 343 |
"coeff_var": 63.7810227200549, |
| 344 |
"sum": 2220484.40367258, |
| 345 |
}, |
| 346 |
"sum": { |
| 347 |
"n": 2025000, |
| 348 |
"null_cells": 0, |
| 349 |
"cells": 2025000, |
| 350 |
"min": 18.3855979069574, |
| 351 |
"max": 133.048299539197, |
| 352 |
"range": 114.66270163224, |
| 353 |
"mean": 93.868177703987, |
| 354 |
"mean_of_abs": 93.868177703987, |
| 355 |
"stddev": 17.4006236823834, |
| 356 |
"variance": 302.781704535922, |
| 357 |
"coeff_var": 18.5372978447033, |
| 358 |
"sum": 190083059.850574, |
| 359 |
}, |
| 360 |
}, |
| 361 |
"test_weighting_file": { |
| 362 |
"perc90": { |
| 363 |
"n": 2025000, |
| 364 |
"null_cells": 0, |
| 365 |
"cells": 2025000, |
| 366 |
"min": 56.2366943359375, |
| 367 |
"max": 156.314498901367, |
| 368 |
"range": 100.07780456543, |
| 369 |
"mean": 111.666863002851, |
| 370 |
"mean_of_abs": 111.666863002851, |
| 371 |
"stddev": 20.2890537023546, |
| 372 |
"variance": 411.64570013703, |
| 373 |
"coeff_var": 18.1692698771673, |
| 374 |
"sum": 226125397.580772, |
| 375 |
}, |
| 376 |
"average": { |
| 377 |
"n": 2025000, |
| 378 |
"null_cells": 0, |
| 379 |
"cells": 2025000, |
| 380 |
"min": 56.0924235752651, |
| 381 |
"max": 156.219111124674, |
| 382 |
"range": 100.126687549409, |
| 383 |
"mean": 110.375616311748, |
| 384 |
"mean_of_abs": 110.375616311748, |
| 385 |
"stddev": 20.2776463025833, |
| 386 |
"variance": 411.182939572671, |
| 387 |
"coeff_var": 18.3714908964228, |
| 388 |
"sum": 223510623.03129, |
| 389 |
}, |
| 390 |
"stddev": { |
| 391 |
"n": 2025000, |
| 392 |
"null_cells": 0, |
| 393 |
"cells": 2025000, |
| 394 |
"min": 0, |
| 395 |
"max": 10.0342267239471, |
| 396 |
"range": 10.0342267239471, |
| 397 |
"mean": 1.01708477919276, |
| 398 |
"mean_of_abs": 1.01708477919276, |
| 399 |
"stddev": 0.70410774305486, |
| 400 |
"variance": 0.495767713829808, |
| 401 |
"coeff_var": 69.2280287208402, |
| 402 |
"sum": 2059596.67786534, |
| 403 |
}, |
| 404 |
"sum": { |
| 405 |
"n": 2025000, |
| 406 |
"null_cells": 0, |
| 407 |
"cells": 2025000, |
| 408 |
"min": 233.716758728027, |
| 409 |
"max": 1874.62933349609, |
| 410 |
"range": 1640.91257476807, |
| 411 |
"mean": 1322.11428605925, |
| 412 |
"mean_of_abs": 1322.11428605925, |
| 413 |
"stddev": 245.497028073259, |
| 414 |
"variance": 60268.7907928026, |
| 415 |
"coeff_var": 18.568517915724, |
| 416 |
"sum": 2677281429.26999, |
| 417 |
}, |
| 418 |
}, |
| 419 |
} |
| 420 |
|
| 421 |
# TODO: replace by unified handing of maps |
| 422 |
to_remove = [] |
| 423 |
|
| 424 |
def check_univar(self, test_case): |
| 425 |
"""Checks multiple output against unviariate reference from dict""" |
| 426 |
for method in self.test_options[test_case]: |
| 427 |
self.assertRasterFitsUnivar( |
| 428 |
raster="{}_raster_{}".format(test_case, method), |
| 429 |
reference=self.test_options[test_case][method], |
| 430 |
precision=1e-5, |
| 431 |
) |
| 432 |
self.assertRasterFitsUnivar( |
| 433 |
raster="{}_threaded_raster_{}".format(test_case, method), |
| 434 |
reference=self.test_options[test_case][method], |
| 435 |
precision=1e-5, |
| 436 |
) |
| 437 |
|
| 438 |
@classmethod |
| 439 |
def setUpClass(cls): |
| 440 |
cls.use_temp_region() |
| 441 |
cls.runModule("g.region", raster="elevation") |
| 442 |
|
| 443 |
@classmethod |
| 444 |
def tearDownClass(cls): |
| 445 |
cls.del_temp_region() |
| 446 |
if cls.to_remove: |
| 447 |
cls.runModule( |
| 448 |
"g.remove", flags="f", type="raster", name=",".join(cls.to_remove) |
| 449 |
) |
| 450 |
|
| 451 |
def test_standard_options(self): |
| 452 |
"""Test output with standard options (size=3, quadratic moving window, |
| 453 |
standard statistics). |
| 454 |
""" |
| 455 |
test_case = "test_standard_options" |
| 456 |
outputs = [ |
| 457 |
"{}_raster_{}".format(test_case, method) |
| 458 |
for method in self.test_options[test_case] |
| 459 |
] |
| 460 |
outputs_threaded = [ |
| 461 |
"{}_threaded_raster_{}".format(test_case, method) |
| 462 |
for method in self.test_options[test_case] |
| 463 |
] |
| 464 |
self.to_remove.extend(outputs) |
| 465 |
self.to_remove.extend(outputs_threaded) |
| 466 |
self.assertModule( |
| 467 |
"r.neighbors", |
| 468 |
input="elevation", |
| 469 |
output=",".join(outputs), |
| 470 |
method=list(self.test_options[test_case].keys()), |
| 471 |
) |
| 472 |
self.assertModule( |
| 473 |
"r.neighbors", |
| 474 |
input="elevation", |
| 475 |
output=",".join(outputs_threaded), |
| 476 |
method=list(self.test_options[test_case].keys()), |
| 477 |
nprocs=4, |
| 478 |
) |
| 479 |
self.check_univar(test_case) |
| 480 |
|
| 481 |
def test_standard_options_quantile(self): |
| 482 |
"""Test output with standard options (size=3, quadratic moving window, |
| 483 |
quantile statistics). |
| 484 |
""" |
| 485 |
test_case = "test_standard_options_quantile" |
| 486 |
outputs = [ |
| 487 |
"{}_raster_{}".format(test_case, method) |
| 488 |
for method in self.test_options[test_case] |
| 489 |
] |
| 490 |
outputs_threaded = [ |
| 491 |
"{}_threaded_raster_{}".format(test_case, method) |
| 492 |
for method in self.test_options[test_case] |
| 493 |
] |
| 494 |
self.to_remove.extend(outputs) |
| 495 |
self.to_remove.extend(outputs_threaded) |
| 496 |
self.assertModule( |
| 497 |
"r.neighbors", |
| 498 |
input="elevation", |
| 499 |
output=",".join(outputs), |
| 500 |
method=["quantile"] * len(self.test_options[test_case]), |
| 501 |
quantile=list(self.test_options[test_case].keys()), |
| 502 |
) |
| 503 |
self.assertModule( |
| 504 |
"r.neighbors", |
| 505 |
input="elevation", |
| 506 |
output=",".join(outputs_threaded), |
| 507 |
method=["quantile"] * len(self.test_options[test_case]), |
| 508 |
quantile=list(self.test_options[test_case].keys()), |
| 509 |
nprocs=4, |
| 510 |
) |
| 511 |
self.check_univar(test_case) |
| 512 |
|
| 513 |
def test_standard_options_circular(self): |
| 514 |
"""Test output with circular neighborhood (size=7, |
| 515 |
standard statistics).""" |
| 516 |
test_case = "test_standard_options_circular" |
| 517 |
outputs = [ |
| 518 |
"{}_raster_{}".format(test_case, method) |
| 519 |
for method in self.test_options[test_case] |
| 520 |
] |
| 521 |
outputs_threaded = [ |
| 522 |
"{}_threaded_raster_{}".format(test_case, method) |
| 523 |
for method in self.test_options[test_case] |
| 524 |
] |
| 525 |
self.to_remove.extend(outputs) |
| 526 |
self.to_remove.extend(outputs_threaded) |
| 527 |
self.assertModule( |
| 528 |
"r.neighbors", |
| 529 |
flags="c", |
| 530 |
input="elevation", |
| 531 |
size=7, |
| 532 |
output=",".join(outputs), |
| 533 |
method=list(self.test_options[test_case].keys()), |
| 534 |
) |
| 535 |
self.assertModule( |
| 536 |
"r.neighbors", |
| 537 |
flags="c", |
| 538 |
input="elevation", |
| 539 |
size=7, |
| 540 |
output=",".join(outputs_threaded), |
| 541 |
method=list(self.test_options[test_case].keys()), |
| 542 |
nprocs=4, |
| 543 |
) |
| 544 |
self.check_univar(test_case) |
| 545 |
|
| 546 |
def test_standard_options_selection(self): |
| 547 |
"""Test output with selection raster (standard options otherwise).""" |
| 548 |
test_case = "test_standard_options_selection" |
| 549 |
outputs = [ |
| 550 |
"{}_raster_{}".format(test_case, method) |
| 551 |
for method in self.test_options[test_case] |
| 552 |
] |
| 553 |
outputs_threaded = [ |
| 554 |
"{}_threaded_raster_{}".format(test_case, method) |
| 555 |
for method in self.test_options[test_case] |
| 556 |
] |
| 557 |
self.to_remove.extend(outputs) |
| 558 |
self.to_remove.extend(outputs_threaded) |
| 559 |
selection = "test_neighbors_selection" |
| 560 |
self.to_remove.append(selection) |
| 561 |
self.runModule( |
| 562 |
"r.mapcalc", |
| 563 |
expression="{}=if(y()==15,1,null())".format(selection), |
| 564 |
) |
| 565 |
self.assertModule( |
| 566 |
"r.neighbors", |
| 567 |
input="elevation", |
| 568 |
output=",".join(outputs), |
| 569 |
selection=selection, |
| 570 |
method=list(self.test_options[test_case].keys()), |
| 571 |
) |
| 572 |
self.assertModule( |
| 573 |
"r.neighbors", |
| 574 |
input="elevation", |
| 575 |
output=",".join(outputs_threaded), |
| 576 |
selection=selection, |
| 577 |
method=list(self.test_options[test_case].keys()), |
| 578 |
nprocs=4, |
| 579 |
) |
| 580 |
self.check_univar(test_case) |
| 581 |
|
| 582 |
def test_weighting_function(self): |
| 583 |
"""Test results with gaussian weighting (standard options otherwise).""" |
| 584 |
test_case = "test_weighting_function" |
| 585 |
outputs = [ |
| 586 |
"{}_raster_{}".format(test_case, method) |
| 587 |
for method in self.test_options[test_case] |
| 588 |
] |
| 589 |
outputs_threaded = [ |
| 590 |
"{}_threaded_raster_{}".format(test_case, method) |
| 591 |
for method in self.test_options[test_case] |
| 592 |
] |
| 593 |
self.to_remove.extend(outputs) |
| 594 |
self.to_remove.extend(outputs_threaded) |
| 595 |
self.assertModule( |
| 596 |
"r.neighbors", |
| 597 |
input="elevation", |
| 598 |
size=7, |
| 599 |
weighting_function="gaussian", |
| 600 |
weighting_factor=2.0, |
| 601 |
output=",".join(outputs), |
| 602 |
method=list(self.test_options[test_case].keys()), |
| 603 |
) |
| 604 |
self.assertModule( |
| 605 |
"r.neighbors", |
| 606 |
input="elevation", |
| 607 |
size=7, |
| 608 |
weighting_function="gaussian", |
| 609 |
weighting_factor=2.0, |
| 610 |
output=",".join(outputs_threaded), |
| 611 |
method=list(self.test_options[test_case].keys()), |
| 612 |
nprocs=4, |
| 613 |
) |
| 614 |
self.check_univar(test_case) |
| 615 |
|
| 616 |
def test_weighting_file(self): |
| 617 |
"""Test results with file for weighting (standard options otherwise).""" |
| 618 |
|
| 619 |
test_case = "test_weighting_file" |
| 620 |
outputs = [ |
| 621 |
"{}_raster_{}".format(test_case, method) |
| 622 |
for method in self.test_options[test_case] |
| 623 |
] |
| 624 |
outputs_threaded = [ |
| 625 |
"{}_threaded_raster_{}".format(test_case, method) |
| 626 |
for method in self.test_options[test_case] |
| 627 |
] |
| 628 |
self.to_remove.extend(outputs) |
| 629 |
self.to_remove.extend(outputs_threaded) |
| 630 |
|
| 631 |
weights = tempfile() |
| 632 |
Path(weights).write_text( |
| 633 |
"0 1 1 1 0\n1 0 0 0 1\n1 0 0 0 1\n1 0 0 0 1\n0 1 1 1 0" |
| 634 |
) |
| 635 |
|
| 636 |
self.assertModule( |
| 637 |
"r.neighbors", |
| 638 |
input="elevation", |
| 639 |
size=5, |
| 640 |
weighting_function="file", |
| 641 |
output=",".join(outputs), |
| 642 |
method=list(self.test_options[test_case].keys()), |
| 643 |
weight=weights, |
| 644 |
) |
| 645 |
self.assertModule( |
| 646 |
"r.neighbors", |
| 647 |
input="elevation", |
| 648 |
size=5, |
| 649 |
weighting_function="file", |
| 650 |
output=",".join(outputs_threaded), |
| 651 |
method=list(self.test_options[test_case].keys()), |
| 652 |
weight=weights, |
| 653 |
nprocs=4, |
| 654 |
) |
| 655 |
self.check_univar(test_case) |
| 656 |
|
| 657 |
# Module fails with mutual exclusive options |
| 658 |
self.assertModuleFail( |
| 659 |
"r.neighbors", |
| 660 |
input="elevation", |
| 661 |
flags="c", |
| 662 |
size=5, |
| 663 |
weighting_function="file", |
| 664 |
output="{}_fails".format(test_case), |
| 665 |
method="sum", |
| 666 |
weight=weights, |
| 667 |
) |
| 668 |
self.assertModuleFail( |
| 669 |
"r.neighbors", |
| 670 |
input="elevation", |
| 671 |
flags="c", |
| 672 |
size=5, |
| 673 |
weighting_function="file", |
| 674 |
output="{}_fails".format(test_case), |
| 675 |
method="sum", |
| 676 |
weight=weights, |
| 677 |
nprocs=4, |
| 678 |
) |
| 679 |
|
| 680 |
def test_standard_options_datatype(self): |
| 681 |
"""Test if result is of integer or float data type depending on |
| 682 |
input datatype and method""" |
| 683 |
# landclass96 (values 1-7) |
| 684 |
|
| 685 |
test_case = "test_standard_options_datatype" |
| 686 |
|
| 687 |
methods = ["average", "variance", "diversity", "range", "mode"] |
| 688 |
outputs = ["{}_{}".format(test_case, method) for method in methods] |
| 689 |
outputs_threaded = [ |
| 690 |
"{}_threaded_{}".format(test_case, method) for method in methods |
| 691 |
] |
| 692 |
self.to_remove.extend(outputs) |
| 693 |
self.to_remove.extend(outputs_threaded) |
| 694 |
self.assertModule( |
| 695 |
"r.neighbors", |
| 696 |
input="landclass96", |
| 697 |
output=",".join(outputs), |
| 698 |
method=methods, |
| 699 |
) |
| 700 |
self.assertModule( |
| 701 |
"r.neighbors", |
| 702 |
input="landclass96", |
| 703 |
output=",".join(outputs_threaded), |
| 704 |
method=methods, |
| 705 |
nprocs=4, |
| 706 |
) |
| 707 |
from itertools import chain |
| 708 |
|
| 709 |
for rmap in chain(outputs, outputs_threaded): |
| 710 |
rinfo = raster_info(rmap) |
| 711 |
self.assertTrue( |
| 712 |
rinfo["datatype"] == "CELL" |
| 713 |
if rinfo["datatype"] in {"diversity", "range", "mode"} |
| 714 |
else "DCELL" |
| 715 |
) |
| 716 |
|
| 717 |
|
| 718 |
if __name__ == "__main__": |
| 719 |
test() |