source: grass/trunk/raster/r.mapcalc/testsuite/test_r_mapcalc.py

Last change on this file was 71360, checked in by huhabla, 7 years ago

raster modules: Implemented computational region settings based on disjoint union and intersection computation from all input raster maps in an expression

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-python
File size: 12.6 KB
Line 
1from grass.gunittest.case import TestCase
2from grass.gunittest.main import test
3from grass.gunittest.gmodules import SimpleModule
4
5cell_seed_500 = """\
6north: 20
7south: 10
8east: 25
9west: 15
10rows: 10
11cols: 10
12121 12 183 55 37 96 138 117 182 40
13157 70 115 1 149 125 42 193 108 24
1483 66 82 84 186 182 179 122 67 113
15151 93 144 173 128 196 61 125 64 193
16180 175 14 41 44 27 165 27 90 60
1797 57 12 104 98 13 87 24 83 107
18174 133 146 114 115 60 78 154 49 130
1955 138 144 25 32 58 47 137 139 32
20143 193 155 190 131 124 87 81 160 154
2156 45 48 66 9 182 69 12 154 19
22"""
23
24dcell_seed_600 = """\
25north: 20
26south: 10
27east: 25
28west: 15
29rows: 10
30cols: 10
31130.790433856418332 101.3319248101491041 33.5781271447787759 37.4064724824657944 98.2794723130458152 73.9118866262841863 185.9530433718733775 74.5210037729812882 166.1178416001017695 90.9915650902159996
32109.2478664232956334 25.6499350759712215 150.9024447059825036 125.7544119036241312 66.7235333366722614 167.9375729129454271 123.1009291055983965 12.0922254083554606 59.389026967287819 113.2843489528100349
3340.0044184023145277 135.8273774212801186 71.6737798852435049 191.6223505280372876 4.1546013811569615 143.3082794522489962 177.043829294835632 115.0300571162354402 141.8985452774071234 127.8949967123638061
3493.2842559637482793 9.7471880052423856 118.1216452002055632 158.1474162140586088 67.2957262519499437 3.6524546350146849 147.0965842667525862 37.060628529871579 47.3408278816968959 66.2219633495724054
35175.5638637866295539 67.1399023507611901 162.2058392782793703 198.1586789345953719 36.474049475167746 49.2589028048889617 112.1169663235969836 22.0227597984432535 95.9169228571662131 86.7470895014531322
3693.5401613888204935 193.7821104138942587 193.8286564351004699 3.2623643889134684 94.6955247357847725 25.7099391122614307 155.592251526775442 25.3392337002970294 48.3979699868663005 99.6836079482556272
37104.16296861365457 190.7865884377180805 6.2841805474238619 49.3731395705159528 100.1903962703459285 116.927654961282343 19.8626348109264264 40.9693022766258466 81.6500759554420057 169.2220572316770131
38118.8112518721558217 55.8955021401724039 112.9150308215961331 62.6399760484719081 85.400498505854145 191.0144187084912062 124.2128169358724534 167.9341741649760706 170.6149695243870781 158.3034517206661462
39130.0453795775294736 64.1996403829061535 62.9317494959142465 175.1909990236256931 122.9624852869890361 79.9546265736285733 9.6594013716963367 114.0611338072915544 11.9371167643030809 186.9121199748369122
403.2891990250261536 30.9245408751958379 46.4021422454598991 104.2378950097200203 47.424093232347019 73.4801303522840499 22.4778583078695213 132.870185207462697 48.1666164169167388 100.5504714442693057
41"""
42
43fcell_seed_700 = """\
44north: 20
45south: 10
46east: 25
47west: 15
48rows: 10
49cols: 10
50146.756378 192.682159 2.644822 147.270462 62.178818 192.668198 94.320778 107.710426 98.319664 114.444504
5112.995321 18.026272 151.590958 5.249451 197.266708 103.663635 115.424088 28.01062 78.555168 62.912098
52164.053619 154.652039 98.536011 44.601639 85.322289 168.383957 44.93845 128.62262 89.910591 107.242188
53111.182487 63.080284 177.791473 47.439354 42.451859 72.396568 170.597778 170.622742 141.88858 105.126854
54120.76828 148.581085 42.124866 56.432236 164.652176 98.094009 60.741329 66.286987 187.847427 160.120056
5550.530689 179.090652 138.114014 138.629211 193.147903 172.861481 133.72728 108.720459 103.508438 28.81559
5639.653179 101.948265 35.744762 25.570076 78.767021 154.600616 144.907684 82.370148 116.378654 18.218494
5735.587288 66.534409 65.744408 186.476959 137.081116 151.379272 48.261463 8.323328 130.432739 53.346546
58152.67189 15.512391 146.049072 185.276245 34.417141 127.522453 124.54998 52.08218 167.141342 87.771118
5969.0522 43.57811 63.15279 68.677063 74.202805 97.429077 167.123199 19.892767 120.593437 190.960815
60"""
61
62
63class TestRandFunction(TestCase):
64
65 # TODO: replace by unified handing of maps
66 to_remove = []
67
68 @classmethod
69 def setUpClass(cls):
70 cls.use_temp_region()
71 cls.runModule('g.region', n=20, s=10, e=25, w=15, res=1)
72
73 @classmethod
74 def tearDownClass(cls):
75 cls.del_temp_region()
76 if cls.to_remove:
77 cls.runModule('g.remove', flags='f', type='raster',
78 name=','.join(cls.to_remove))
79
80 def rinfo_contains_number(self, raster, number):
81 """Test that r.info standard output for raster contains a given number
82
83 To be used in test methods for testing presence of a given number.
84 """
85 rinfo = SimpleModule('r.info', map=raster)
86 self.runModule(rinfo)
87 self.assertIn(str(number), rinfo.outputs.stdout)
88
89 def test_seed_not_required(self):
90 """Test that seed is not required when rand() is not used"""
91 self.assertModule('r.mapcalc', expression='nonrand_cell = 200')
92 self.to_remove.append('nonrand_cell')
93
94 def test_seed_required(self):
95 """Test that seed is required when rand() is used
96
97 This test can, and probably should, generate an error message.
98 """
99 self.assertModuleFail('r.mapcalc', expression='rand_x = rand(1, 200)')
100 # TODO: assert map not exists but it would be handy here
101 # TODO: test that error message was generated
102
103 def test_seed_cell(self):
104 """Test given seed with CELL against reference map"""
105 seed = 500
106 self.runModule('r.in.ascii', input='-', stdin=cell_seed_500,
107 output='rand_cell_ref')
108 self.to_remove.append('rand_cell_ref')
109 self.assertModule('r.mapcalc', seed=seed,
110 expression='rand_cell = rand(1, 200)')
111 self.to_remove.append('rand_cell')
112 # this assert is using r.mapcalc but we are testing different
113 # functionality than used by assert
114 self.assertRastersNoDifference(actual='rand_cell',
115 reference='rand_cell_ref',
116 precision=0)
117 self.rinfo_contains_number('rand_cell', seed)
118
119 def test_seed_dcell(self):
120 """Test given seed with DCELL against reference map"""
121 seed = 600
122 self.runModule('r.in.ascii', input='-', stdin=dcell_seed_600,
123 output='rand_dcell_ref')
124 self.to_remove.append('rand_dcell_ref')
125 self.assertModule('r.mapcalc', seed=seed,
126 expression='rand_dcell = rand(1.0, 200.0)')
127 self.to_remove.append('rand_dcell')
128 # this assert is using r.mapcalc but we are testing different
129 # functionality than used by assert
130 self.assertRastersNoDifference(actual='rand_dcell',
131 reference='rand_dcell_ref',
132 precision=0.00000000000001)
133 self.rinfo_contains_number('rand_dcell', seed)
134
135 def test_seed_fcell(self):
136 """Test given seed with FCELL against reference map"""
137 seed = 700
138 self.runModule('r.in.ascii', input='-', stdin=fcell_seed_700,
139 output='rand_fcell_ref')
140 self.to_remove.append('rand_fcell_ref')
141 self.assertModule('r.mapcalc', seed=seed,
142 expression='rand_fcell = rand(float(1), 200)')
143 self.to_remove.append('rand_fcell')
144 # this assert is using r.mapcalc but we are testing different
145 # functionality than used by assert
146 self.assertRastersNoDifference(actual='rand_fcell',
147 reference='rand_fcell_ref',
148 precision=0.000001)
149 self.rinfo_contains_number('rand_fcell', seed)
150
151 def test_auto_seed(self):
152 """Test that two runs with -s does not give same maps"""
153 self.assertModule('r.mapcalc', flags='s',
154 expression='rand_auto_1 = rand(1., 2)')
155 self.to_remove.append('rand_auto_1')
156 self.assertModule('r.mapcalc', flags='s',
157 expression='rand_auto_2 = rand(1., 2)')
158 self.to_remove.append('rand_auto_2')
159 self.assertRastersDifference(
160 'rand_auto_1', 'rand_auto_2',
161 statistics=dict(min=-1, max=1, mean=0),
162 precision=0.5) # low precision, we have few cells
163
164
165# TODO: add more expressions
166# TODO: add tests with prepared data
167
168class TestBasicOperations(TestCase):
169
170 # TODO: replace by unified handing of maps
171 to_remove = []
172
173 @classmethod
174 def setUpClass(cls):
175 cls.use_temp_region()
176 cls.runModule('g.region', n=20, s=10, e=25, w=15, res=1)
177
178 @classmethod
179 def tearDownClass(cls):
180 cls.del_temp_region()
181 if cls.to_remove:
182 cls.runModule('g.remove', flags='f', type='raster',
183 name=','.join(cls.to_remove), verbose=True)
184
185 def test_difference_of_the_same_map_double(self):
186 """Test zero difference of map with itself"""
187 self.runModule('r.mapcalc', flags='s',
188 expression='a = rand(1.0, 200)')
189 self.to_remove.append('a')
190 self.assertModule('r.mapcalc',
191 expression='diff_a_a = a - a')
192 self.to_remove.append('diff_a_a')
193 self.assertRasterMinMax('diff_a_a', refmin=0, refmax=0)
194
195 def test_difference_of_the_same_map_float(self):
196 """Test zero difference of map with itself"""
197 self.runModule('r.mapcalc', flags='s',
198 expression='af = rand(float(1), 200)')
199 self.to_remove.append('af')
200 self.assertModule('r.mapcalc',
201 expression='diff_af_af = af - af')
202 self.to_remove.append('diff_af_af')
203 self.assertRasterMinMax('diff_af_af', refmin=0, refmax=0)
204
205 def test_difference_of_the_same_map_int(self):
206 """Test zero difference of map with itself"""
207 self.runModule('r.mapcalc', flags='s',
208 expression='ai = rand(1, 200)')
209 self.to_remove.append('ai')
210 self.assertModule('r.mapcalc',
211 expression='diff_ai_ai = ai - ai')
212 self.to_remove.append('diff_ai_ai')
213 self.assertRasterMinMax('diff_ai_ai', refmin=0, refmax=0)
214
215 def test_difference_of_the_same_expression(self):
216 """Test zero difference of two same expressions"""
217 self.assertModule('r.mapcalc',
218 expression='diff_e_e = 3 * x() * y() - 3 * x() * y()')
219 self.to_remove.append('diff_e_e')
220 self.assertRasterMinMax('diff_e_e', refmin=0, refmax=0)
221
222 def test_nrows_ncols_sum(self):
223 """Test if sum of nrows and ncols matches one
224 expected from current region settigs"""
225 self.assertModule('r.mapcalc',
226 expression='nrows_ncols_sum = nrows() + ncols()')
227 self.to_remove.append('nrows_ncols_sum')
228 self.assertRasterMinMax('nrows_ncols_sum', refmin=20, refmax=20)
229
230
231class TestRegionOperations(TestCase):
232
233 # TODO: replace by unified handing of maps
234 to_remove = []
235
236 @classmethod
237 def setUpClass(cls):
238 cls.use_temp_region()
239 cls.runModule('g.region', n=30, s=15, e=30, w=15, res=5)
240 cls.runModule('r.mapcalc', expression="test_region_1 = 1", seed=1)
241 cls.runModule('g.region', n=25, s=10, e=25, w=10, res=5)
242 cls.runModule('r.mapcalc', expression="test_region_2 = 2", seed=1)
243 cls.runModule('g.region', n=20, s=5, e=20, w=5, res=1)
244 cls.runModule('r.mapcalc', expression="test_region_3 = 3", seed=1)
245
246 cls.to_remove.append("test_region_1")
247 cls.to_remove.append("test_region_2")
248 cls.to_remove.append("test_region_3")
249
250 @classmethod
251 def tearDownClass(cls):
252 cls.del_temp_region()
253 if cls.to_remove:
254 cls.runModule('g.remove', flags='f', type='raster',
255 name=','.join(cls.to_remove), verbose=True)
256
257 def test_union(self):
258 """Test the union region option"""
259 self.assertModule('r.mapcalc', region="union", seed=1,
260 expression='test_region_4 = test_region_1 + test_region_2 + test_region_3')
261 self.to_remove.append('test_region_4')
262
263 self.assertModuleKeyValue('r.info', map='test_region_4', flags='gr',
264 reference=dict(min=6, max=6, cells=625, north=30,
265 south=5, west=5, east=30, nsres=1, ewres=1),
266 precision=0.01, sep='=')
267
268 def test_intersect(self):
269 """Test the intersect region option"""
270 self.assertModule('r.mapcalc', region="intersect", seed=1,
271 expression='test_region_5 = test_region_1 + test_region_2 + test_region_3')
272 self.to_remove.append('test_region_5')
273
274 self.assertModuleKeyValue('r.info', map='test_region_5', flags='gr',
275 reference=dict(min=6, max=6, cells=25, north=20,
276 south=15, west=15, east=20, nsres=1, ewres=1),
277 precision=0.01, sep='=')
278
279
280if __name__ == '__main__':
281 test()
Note: See TracBrowser for help on using the repository browser.