Debian Codegraph @

imagery/i.albedo/testsuite/test_i_albedo.py

raw
1 import grass.script as gs
2 from grass.gunittest.case import TestCase
3 from grass.gunittest.main import test
4
5
6 class TestIAlbedo(TestCase):
7 """Regression tests for the i.albedo GRASS module."""
8
9 output_raster = "albedo_output"
10
11 def setUp(self):
12 """Initialize temporary region with 10x10 grid"""
13 self.use_temp_region()
14 self.runModule("g.region", n=10, s=0, e=10, w=0, rows=10, cols=10)
15
16 def tearDown(self):
17 """Remove temporary maps and restore original region."""
18 self.runModule("g.remove", type="raster", flags="f", pattern="test_*")
19 self.runModule("g.remove", type="raster", flags="f", name=self.output_raster)
20 self.del_temp_region()
21
22 def _create_input_rasters(self, sensor, values=None):
23 """Generate test rasters for sensor bands."""
24 bands = {
25 "noaa": ["test_red", "test_nir"],
26 "modis": [f"test_b{i}" for i in range(1, 8)],
27 "landsat": [f"test_b{i}" for i in [1, 2, 3, 4, 5, 7]],
28 "aster": [f"test_b{i}" for i in [1, 3, 5, 6, 8, 9]],
29 }.get(sensor)
30 values = values or [0.1 * (i + 1) for i in range(len(bands))]
31 for band, value in zip(bands, values, strict=True):
32 self.runModule("r.mapcalc", expression=f"{band} = {value}", overwrite=True)
33 return bands
34
35 def _test_sensor_albedo(self, sensor, flag, values, reference_stats):
36 """Core test runner for sensor-specific albedo calculations.
37 Validates output against precomputed statistical references."""
38 input_rasters = self._create_input_rasters(sensor, values)
39 self.assertModule(
40 "i.albedo",
41 input=",".join(input_rasters),
42 output=self.output_raster,
43 flags=flag,
44 overwrite=True,
45 )
46 self.assertRasterFitsUnivar(
47 raster=self.output_raster, reference=reference_stats, precision=1e-6
48 )
49
50 def test_modis_albedo(self):
51 self._test_sensor_albedo(
52 "modis",
53 "m",
54 [0.1 * i for i in range(1, 8)],
55 {"mean": 0.38995, "sum": 38.995},
56 )
57
58 def test_landsat_albedo(self):
59 self._test_sensor_albedo(
60 "landsat",
61 "l",
62 [0.1 * i for i in [1, 2, 3, 4, 5, 7]],
63 {"mean": 0.2406, "sum": 24.06},
64 )
65
66 def test_noaa_albedo(self):
67 self._test_sensor_albedo(
68 "noaa", "n", [0.5, 0.8], {"mean": 0.311, "sum": 31.0999989}
69 )
70
71 def test_aster_albedo(self):
72 self._test_sensor_albedo(
73 "aster",
74 "a",
75 [0.1 * i for i in [1, 3, 5, 6, 8, 9]],
76 {"mean": 0.2297, "sum": 22.9699999},
77 )
78
79 def test_albedo_output_range(self):
80 """Ensure computed albedo values remain within valid 0-1 range."""
81 input_rasters = self._create_input_rasters("landsat")
82 self.assertModule(
83 "i.albedo",
84 input=",".join(input_rasters),
85 output=self.output_raster,
86 flags="l",
87 overwrite=True,
88 )
89 stats = gs.parse_command("r.info", map=self.output_raster, format="json")
90 min_val = stats["min"]
91 max_val = stats["max"]
92 self.assertGreaterEqual(min_val, 0, "Albedo values < 0 detected")
93 self.assertLessEqual(max_val, 1, "Albedo values > 1 detected")
94
95 def test_landsat_aggressive_mode_comparison(self):
96 """Compare standard vs aggressive processing modes for dynamic range reduction and mean value differences."""
97 bands = [f"test_b{i}" for i in [1, 2, 3, 4, 5, 7]]
98 for idx, band in enumerate(bands):
99 self.runModule(
100 "r.mapcalc",
101 expression=f"{band}=0.1*({idx + 1})+rand(0,0.15)",
102 seed=42,
103 overwrite=True,
104 )
105 for flag, out in [("l", "albedo_standard"), ("lc", "albedo_aggressive")]:
106 self.assertModule(
107 "i.albedo",
108 input=",".join(bands),
109 output=out,
110 flags=flag,
111 overwrite=True,
112 )
113
114 std_stats = gs.parse_command("r.univar", map="albedo_standard", format="json")
115 agg_stats = gs.parse_command("r.univar", map="albedo_aggressive", format="json")
116 self.assertLess(
117 agg_stats["max"] - agg_stats["min"],
118 std_stats["max"] - std_stats["min"],
119 "Aggressive mode should reduce dynamic range",
120 )
121 self.assertNotAlmostEqual(
122 std_stats["mean"],
123 agg_stats["mean"],
124 places=3,
125 msg="Aggressive mode should shift the mean",
126 )
127
128 def test_linearity(self):
129 """Validate linear reflectance scaling by comparing that doubled input results to doubled output expectations."""
130
131 def run_test(values, out_name):
132 bands = self._create_input_rasters("landsat", values)
133 self.assertModule(
134 "i.albedo",
135 input=",".join(bands),
136 output=out_name,
137 flags="l",
138 overwrite=True,
139 )
140
141 run_test([0.1] * 6, "albedo_low")
142 run_test([0.2] * 6, "albedo_high")
143 self.runModule(
144 "r.mapcalc", expression="diff=albedo_high-2*albedo_low", overwrite=True
145 )
146 diff_stats = gs.parse_command("r.univar", map="diff", format="json")
147 max_diff = diff_stats["max"]
148 min_diff = diff_stats["min"]
149 self.assertAlmostEqual(
150 max_diff,
151 0.0,
152 places=6,
153 msg="Albedo does not scale linearly with input (max difference).",
154 )
155 self.assertAlmostEqual(
156 min_diff,
157 0.0,
158 places=6,
159 msg="Albedo does not scale linearly with input (min difference).",
160 )
161
162
163 if __name__ == "__main__":
164 test()