| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
MODULE: Test of r.surf.random |
| 5 |
|
| 6 |
AUTHOR(S): Vaclav Petras <wenzeslaus gmail com> |
| 7 |
|
| 8 |
PURPOSE: Test of min and max parameters |
| 9 |
|
| 10 |
COPYRIGHT: (C) 2020 by Vaclav Petras and the GRASS Development Team |
| 11 |
|
| 12 |
This program is free software under the GNU General Public |
| 13 |
License (>=v2). Read the file COPYING that comes with GRASS |
| 14 |
for details. |
| 15 |
""" |
| 16 |
|
| 17 |
import grass.script as gs |
| 18 |
|
| 19 |
from grass.gunittest.case import TestCase |
| 20 |
from grass.gunittest.main import test |
| 21 |
|
| 22 |
|
| 23 |
def get_raster_min_max(raster_map): |
| 24 |
"""Get minimum and maximum value from raster metadata as a tuple""" |
| 25 |
info = gs.raster_info(raster_map) |
| 26 |
return info["min"], info["max"] |
| 27 |
|
| 28 |
|
| 29 |
class MinMaxTestCase(TestCase): |
| 30 |
"""Test min and max of r.surf.random module""" |
| 31 |
|
| 32 |
# Raster map name be used as output |
| 33 |
output = "random_result" |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def setUpClass(cls): |
| 37 |
"""Ensures expected computational region""" |
| 38 |
# modifying region just for this script |
| 39 |
cls.use_temp_region() |
| 40 |
cls.runModule("g.region", rows=10, cols=10) |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def tearDownClass(cls): |
| 44 |
"""Remove the temporary region""" |
| 45 |
cls.del_temp_region() |
| 46 |
|
| 47 |
def tearDown(self): |
| 48 |
"""Remove the output created from the module""" |
| 49 |
self.runModule("g.remove", flags="f", type="raster", name=[self.output]) |
| 50 |
|
| 51 |
def test_min_max_double(self): |
| 52 |
"""Check to see if double output has the expected range""" |
| 53 |
min_value = -3.3 |
| 54 |
max_value = 5.8 |
| 55 |
precision = 0.00001 |
| 56 |
self.assertModule( |
| 57 |
"r.surf.random", min=min_value, max=max_value, output=self.output, seed=42 |
| 58 |
) |
| 59 |
self.assertRasterExists(self.output, msg="Output was not created") |
| 60 |
self.assertRasterMinMax( |
| 61 |
map=self.output, |
| 62 |
refmin=min_value, |
| 63 |
refmax=max_value, |
| 64 |
msg="Output exceeds the min and max values from parameters", |
| 65 |
) |
| 66 |
self.assertRasterFitsInfo( |
| 67 |
raster=self.output, |
| 68 |
reference={"min": -3.20423, "max": 5.68621}, |
| 69 |
precision=precision, |
| 70 |
msg="Output min and max too far from parameters", |
| 71 |
) |
| 72 |
|
| 73 |
def test_min_max_int(self): |
| 74 |
"""Check to see if integer output has the expected range""" |
| 75 |
# GRASS_RANDOM_SEED=42 r.surf.random output=test min=-2 max=13 -i |
| 76 |
# in 7.6.2 causes all no 2 bin and extra 14 bin (also doubles 0). |
| 77 |
min_value = -2 |
| 78 |
max_value = 13 |
| 79 |
precision = 0 |
| 80 |
self.assertModule( |
| 81 |
"r.surf.random", |
| 82 |
min=min_value, |
| 83 |
max=max_value, |
| 84 |
output=self.output, |
| 85 |
seed=42, |
| 86 |
flags="i", |
| 87 |
) |
| 88 |
self.assertRasterExists(self.output, msg="Output was not created") |
| 89 |
self.assertRasterMinMax( |
| 90 |
map=self.output, |
| 91 |
refmin=min_value, |
| 92 |
refmax=max_value, |
| 93 |
msg="Output exceeds the min and max values from parameters", |
| 94 |
) |
| 95 |
self.assertRasterFitsInfo( |
| 96 |
raster=self.output, |
| 97 |
reference={"min": min_value, "max": max_value}, |
| 98 |
precision=precision, |
| 99 |
msg="Output min and max too far from parameters", |
| 100 |
) |
| 101 |
|
| 102 |
def test_double_params_with_int(self): |
| 103 |
"""Check if doubles instead of ints are refused""" |
| 104 |
min_value = -3.3 |
| 105 |
max_value = 5.8 |
| 106 |
self.assertModuleFail( |
| 107 |
"r.surf.random", |
| 108 |
min=min_value, |
| 109 |
max=max_value, |
| 110 |
output=self.output, |
| 111 |
seed=42, |
| 112 |
flags="i", |
| 113 |
) |
| 114 |
|
| 115 |
def test_min_greater_than_max(self): |
| 116 |
"""Check if minimum greater than maximum is refused""" |
| 117 |
min_value = 10 |
| 118 |
max_value = 5.8 |
| 119 |
self.assertModuleFail( |
| 120 |
"r.surf.random", min=min_value, max=max_value, output=self.output, seed=42 |
| 121 |
) |
| 122 |
|
| 123 |
def test_auto_seed(self): |
| 124 |
"""Check if random seed is generated without seed""" |
| 125 |
min_value = -3.3 |
| 126 |
max_value = 5.8 |
| 127 |
self.assertModule( |
| 128 |
"r.surf.random", min=min_value, max=max_value, output=self.output |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__": |
| 133 |
test() |