Debian Codegraph @

raster/r.univar/testsuite/test_r_univar.py

raw symbols SCIP
1 """Test of r.univar
2
3 @author Soeren Gebbert
4 """
5
6 import json
7 from itertools import zip_longest
8
9 from grass.gunittest.case import TestCase
10
11 from grass.gunittest.gmodules import SimpleModule
12
13
14 class TestRasterUnivar(TestCase):
15 @classmethod
16 def setUpClass(cls):
17 """Use temporary region settings"""
18 cls.use_temp_region()
19
20 @classmethod
21 def tearDownClass(cls):
22 """!Remove the temporary region"""
23 cls.del_temp_region()
24
25 def tearDown(self):
26 self.runModule(
27 "g.remove",
28 flags="f",
29 type="raster",
30 name="map_a,map_b,map_negative,zone_map,zone_map_with_gap",
31 )
32
33 def setUp(self):
34 """Create input data"""
35 self.runModule("g.region", res=1, n=90, s=0, w=0, e=90)
36 self.runModule(
37 "r.mapcalc", expression="map_a = 100 + row() + col()", overwrite=True
38 )
39 self.runModule(
40 "r.mapcalc", expression="map_b = 200 + row() + col()", overwrite=True
41 )
42 self.runModule(
43 "r.mapcalc", expression="zone_map = if(row() < 20, 1, 2)", overwrite=True
44 )
45 self.runModule(
46 "r.mapcalc",
47 expression="zone_map_with_gap = if(row()> 20, 2, 9)",
48 overwrite=True,
49 )
50 self.runModule(
51 "r.mapcalc",
52 expression="map_float = float(300) + row() + col()",
53 overwrite=True,
54 )
55 self.runModule(
56 "r.mapcalc",
57 expression="map_double = double(400) + row() + col()",
58 overwrite=True,
59 )
60 self.runModule(
61 "r.mapcalc",
62 expression="map_negative = -double(10) - row() - col()",
63 overwrite=True,
64 )
65
66 def test_1(self):
67 # Output of r.univar
68 univar_string = """n=8100
69 null_cells=0
70 cells=8100
71 min=102
72 max=280
73 range=178
74 mean=191
75 mean_of_abs=191
76 sum=1547100"""
77
78 self.assertRasterFitsUnivar(
79 raster="map_a", reference=univar_string, precision=1e-10
80 )
81 self.assertModuleKeyValue(
82 module="r.univar",
83 map="map_a",
84 flags="g",
85 nprocs=4,
86 reference=univar_string,
87 precision=1e-10,
88 sep="=",
89 )
90 self.assertModuleKeyValue(
91 module="r.univar",
92 map="map_a",
93 format="shell",
94 nprocs=4,
95 reference=univar_string,
96 precision=1e-10,
97 sep="=",
98 )
99
100 def test_2(self):
101 # Output of r.univar
102 univar_string = """n=81
103 null_cells=0
104 cells=81
105 min=112
106 max=272
107 range=160
108 mean=192
109 mean_of_abs=192
110 sum=15552"""
111
112 self.runModule("g.region", res=10)
113 self.assertRasterFitsUnivar(
114 raster="map_a", reference=univar_string, precision=1e-10
115 )
116 self.assertModuleKeyValue(
117 module="r.univar",
118 map="map_a",
119 flags="g",
120 nprocs=4,
121 reference=univar_string,
122 precision=1e-10,
123 sep="=",
124 )
125 self.assertModuleKeyValue(
126 module="r.univar",
127 map="map_a",
128 format="shell",
129 nprocs=4,
130 reference=univar_string,
131 precision=1e-10,
132 sep="=",
133 )
134
135 def test_3(self):
136 """
137 Check the -r flag
138 :return:
139 """
140
141 univar_string = """n=8100
142 null_cells=0
143 cells=8100
144 min=102
145 max=280
146 range=178
147 mean=191
148 mean_of_abs=191
149 sum=1547100"""
150
151 self.runModule("g.region", res=10)
152 self.assertModuleKeyValue(
153 module="r.univar",
154 map="map_a",
155 flags="rg",
156 reference=univar_string,
157 precision=1e-10,
158 sep="=",
159 )
160 self.assertModuleKeyValue(
161 module="r.univar",
162 map="map_a",
163 flags="rg",
164 nprocs=4,
165 reference=univar_string,
166 precision=1e-10,
167 sep="=",
168 )
169
170 def test_extended(self):
171 univar_string_float = """
172 n=8100
173 null_cells=0
174 cells=8100
175 min=302
176 max=480
177 range=178
178 mean=391
179 mean_of_abs=391
180 stddev=36.7400780256838
181 variance=1349.83333333333
182 coeff_var=9.396439392758
183 sum=3167100
184 first_quartile=365
185 median=391
186 third_quartile=417
187 percentile_90=441"""
188
189 self.assertModuleKeyValue(
190 module="r.univar",
191 map="map_float",
192 flags="ge",
193 nprocs=1,
194 reference=univar_string_float,
195 precision=1e-10,
196 sep="=",
197 )
198
199 univar_string_double = """
200 n=8100
201 null_cells=0
202 cells=8100
203 min=402
204 max=580
205 range=178
206 mean=491
207 mean_of_abs=491
208 stddev=36.7400780256838
209 variance=1349.83333333333
210 coeff_var=7.48270428221666
211 sum=3977100
212 first_quartile=465
213 median=491
214 third_quartile=517
215 percentile_90=541"""
216
217 self.assertModuleKeyValue(
218 module="r.univar",
219 map="map_double",
220 flags="ge",
221 nprocs=1,
222 reference=univar_string_double,
223 precision=1e-10,
224 sep="=",
225 )
226
227 def test_multiple_1(self):
228 # Output of r.univar
229 univar_string = """n=16200
230 null_cells=0
231 cells=16200
232 min=102
233 max=380
234 range=278
235 mean=241
236 mean_of_abs=241
237 sum=3904200"""
238
239 self.assertModuleKeyValue(
240 module="r.univar",
241 map=["map_a", "map_b"],
242 flags="rg",
243 reference=univar_string,
244 precision=1e-10,
245 sep="=",
246 )
247 self.assertModuleKeyValue(
248 module="r.univar",
249 map=["map_a", "map_b"],
250 flags="rg",
251 nprocs=4,
252 reference=univar_string,
253 precision=1e-10,
254 sep="=",
255 )
256
257 def test_multiple_2(self):
258 # Output of r.univar
259 univar_string = """n=162
260 null_cells=0
261 cells=162
262 min=112
263 max=372
264 range=260
265 mean=242
266 mean_of_abs=242
267 sum=39204"""
268
269 self.runModule("g.region", res=10)
270 self.assertModuleKeyValue(
271 module="r.univar",
272 map=["map_a", "map_b"],
273 flags="g",
274 reference=univar_string,
275 precision=1e-10,
276 sep="=",
277 )
278 self.assertModuleKeyValue(
279 module="r.univar",
280 map=["map_a", "map_b"],
281 format="shell",
282 reference=univar_string,
283 precision=1e-10,
284 sep="=",
285 )
286 self.assertModuleKeyValue(
287 module="r.univar",
288 map=["map_a", "map_b"],
289 flags="g",
290 nprocs=4,
291 reference=univar_string,
292 precision=1e-10,
293 sep="=",
294 )
295
296 def test_multiple_3(self):
297 """
298 Check the -r flag
299 :return:
300 """
301
302 # Output of r.univar
303 univar_string = """n=16200
304 null_cells=0
305 cells=16200
306 min=102
307 max=380
308 range=278
309 mean=241
310 mean_of_abs=241
311 sum=3904200"""
312
313 self.runModule("g.region", res=10)
314 self.assertModuleKeyValue(
315 module="r.univar",
316 map=["map_a", "map_b"],
317 flags="rg",
318 reference=univar_string,
319 precision=1e-10,
320 sep="=",
321 )
322 self.assertModuleKeyValue(
323 module="r.univar",
324 map=["map_a", "map_b"],
325 flags="rg",
326 nprocs=4,
327 reference=univar_string,
328 precision=1e-10,
329 sep="=",
330 )
331
332 def test_negative(self):
333 """
334 check map with only negative values
335 :return:
336 """
337
338 univar_string = """n=8100
339 null_cells=0
340 cells=8100
341 min=-190
342 max=-12
343 range=178
344 mean=-101
345 mean_of_abs=101
346 stddev=36.7400780256838
347 variance=1349.83333333333
348 coeff_var=-36.3763148769146
349 sum=-818100"""
350
351 self.runModule("g.region", res=10)
352 self.assertModuleKeyValue(
353 module="r.univar",
354 map="map_negative",
355 flags="rg",
356 reference=univar_string,
357 precision=1e-10,
358 sep="=",
359 )
360 self.assertModuleKeyValue(
361 module="r.univar",
362 map="map_negative",
363 flags="rg",
364 nprocs=4,
365 reference=univar_string,
366 precision=1e-10,
367 sep="=",
368 )
369
370 def test_1_zone(self):
371 """
372 one map and zone
373 :return:
374 """
375
376 # Output of r.univar
377 univar_string = """zone=1;
378 n=1710
379 null_cells=0
380 cells=1710
381 min=102
382 max=209
383 range=107
384 mean=155.5
385 mean_of_abs=155.5
386 sum=265905
387 zone=2;
388 n=6390
389 null_cells=0
390 cells=6390
391 min=121
392 max=280
393 range=159
394 mean=200.5
395 mean_of_abs=200.5
396 sum=1281195"""
397
398 self.runModule("g.region", res=1)
399 self.assertModuleKeyValue(
400 module="r.univar",
401 map=["map_a"],
402 zones="zone_map",
403 flags="g",
404 reference=univar_string,
405 precision=1e-10,
406 sep="=",
407 )
408 self.assertModuleKeyValue(
409 module="r.univar",
410 map=["map_a"],
411 zones="zone_map",
412 flags="g",
413 nprocs=4,
414 reference=univar_string,
415 precision=1e-10,
416 sep="=",
417 )
418 self.assertModuleKeyValue(
419 module="r.univar",
420 map=["map_a"],
421 zones="zone_map",
422 format="shell",
423 nprocs=4,
424 reference=univar_string,
425 precision=1e-10,
426 sep="=",
427 )
428
429 def test_2_zone(self):
430 """
431 multiple maps and zone
432 :return:
433 """
434
435 # Output of r.univar
436 univar_string = """zone=1;
437 n=3420
438 null_cells=0
439 cells=3420
440 min=102
441 max=309
442 range=207
443 mean=205.5
444 mean_of_abs=205.5
445 stddev=56.6119834192962
446 variance=3204.91666666667
447 coeff_var=27.5484104230152
448 sum=702810
449 zone=2;
450 n=12780
451 null_cells=0
452 cells=12780
453 min=121
454 max=380
455 range=259
456 mean=250.5
457 mean_of_abs=250.5
458 stddev=59.9576239244574
459 variance=3594.91666666667
460 coeff_var=23.9351792113602
461 sum=3201390"""
462
463 self.assertModuleKeyValue(
464 module="r.univar",
465 map=["map_a", "map_b"],
466 zones="zone_map",
467 flags="g",
468 reference=univar_string,
469 precision=1e-10,
470 sep="=",
471 )
472 self.assertModuleKeyValue(
473 module="r.univar",
474 map=["map_a", "map_b"],
475 zones="zone_map",
476 flags="g",
477 nprocs=4,
478 reference=univar_string,
479 precision=1e-10,
480 sep="=",
481 )
482
483 def test_3_zone(self):
484 """
485 multiple maps and zone
486 :return:
487 """
488
489 # Output of r.univar
490 univar_string = """zone=1;
491 n=3420
492 null_cells=0
493 cells=3420
494 min=102
495 max=309
496 range=207
497 mean=205.5
498 mean_of_abs=205.5
499 stddev=56.6119834192962
500 variance=3204.91666666667
501 coeff_var=27.5484104230152
502 sum=702810
503 first_quartile=155
504 median=205.5
505 third_quartile=255
506 percentile_90=282
507 zone=2;
508 n=12780
509 null_cells=0
510 cells=12780
511 min=121
512 max=380
513 range=259
514 mean=250.5
515 mean_of_abs=250.5
516 stddev=59.9576239244574
517 variance=3594.91666666667
518 coeff_var=23.9351792113602
519 sum=3201390
520 first_quartile=200
521 median=250.5
522 third_quartile=300
523 percentile_90=330"""
524
525 self.assertModuleKeyValue(
526 module="r.univar",
527 map=["map_a", "map_b"],
528 zones="zone_map",
529 flags="ge",
530 reference=univar_string,
531 precision=1e-10,
532 sep="=",
533 )
534 self.assertModuleKeyValue(
535 module="r.univar",
536 map=["map_a", "map_b"],
537 zones="zone_map",
538 flags="ge",
539 nprocs=4,
540 reference=univar_string,
541 precision=1e-10,
542 sep="=",
543 )
544
545 def test_zone_with_gap_in_cats(self):
546 """
547 test if gaps in categories in a map are not included in the output
548 :return:
549 """
550
551 # Output of r.univar
552 univar_string = """zone=2;
553 n=12600
554 null_cells=0
555 cells=12600
556 min=122
557 max=380
558 range=258
559 mean=251
560 mean_of_abs=251
561 stddev=59.8595578555895
562 variance=3583.16666666667
563 coeff_var=23.8484294245376
564 sum=3162600
565 first_quartile=201
566 median=251
567 third_quartile=301
568 percentile_90=331
569 zone=9;
570 n=3600
571 null_cells=0
572 cells=3600
573 min=102
574 max=310
575 range=208
576 mean=206
577 mean_of_abs=206
578 stddev=56.6406803160649
579 variance=3208.16666666667
580 coeff_var=27.4954758815849
581 sum=741600
582 first_quartile=156
583 median=206
584 third_quartile=256
585 percentile_90=283"""
586
587 self.assertModuleKeyValue(
588 module="r.univar",
589 map=["map_a", "map_b"],
590 zones="zone_map_with_gap",
591 flags="ge",
592 reference=univar_string,
593 precision=1e-10,
594 sep="=",
595 )
596 self.assertModuleKeyValue(
597 module="r.univar",
598 map=["map_a", "map_b"],
599 zones="zone_map_with_gap",
600 flags="ge",
601 nprocs=4,
602 reference=univar_string,
603 precision=1e-10,
604 sep="=",
605 )
606
607 def test_t_flag(self):
608 reference = """
609 zone|label|non_null_cells|null_cells|min|max|range|mean|mean_of_abs|stddev|variance|coeff_var|sum|sum_abs
610 1||1710|0|102|209|107|155.5|155.5|26.5502667908755|704.916666666667|17.0741265536177|265905|265905
611 2||6390|0|121|280|159|200.5|200.5|33.0895250293302|1094.91666666667|16.5035037552769|1281195|1281195
612 """
613 module = SimpleModule(
614 "r.univar",
615 map=["map_a"],
616 zones="zone_map",
617 flags="t",
618 )
619 self.runModule(module)
620 self.assertEqual(module.outputs.stdout.strip(), reference.strip())
621
622 # CSV takes precedence over the t flag
623 reference = """
624 zone,label,non_null_cells,null_cells,min,max,range,mean,mean_of_abs,stddev,variance,coeff_var,sum,sum_abs
625 1,,1710,0,102,209,107,155.5,155.5,26.5502667908755,704.916666666667,17.0741265536177,265905,265905
626 2,,6390,0,121,280,159,200.5,200.5,33.0895250293302,1094.91666666667,16.5035037552769,1281195,1281195
627 """
628 module = SimpleModule(
629 "r.univar", map=["map_a"], zones="zone_map", flags="t", format="csv"
630 )
631 self.runModule(module)
632 self.assertEqual(module.outputs.stdout.strip(), reference.strip())
633
634 module = SimpleModule("r.univar", map=["map_a"], zones="zone_map", format="csv")
635 self.runModule(module)
636 self.assertEqual(module.outputs.stdout.strip(), reference.strip())
637
638 def test_json(self):
639 reference = {
640 "n": 16200,
641 "null_cells": 0,
642 "cells": 16200,
643 "min": 102,
644 "max": 380,
645 "range": 278,
646 "mean": 241,
647 "mean_of_abs": 241,
648 "stddev": 62.04702517714555,
649 "variance": 3849.8333333333335,
650 "coeff_var": 25.745653600475332,
651 "sum": 3904200,
652 "first_quartile": 191,
653 "median": 241,
654 "third_quartile": 291,
655 "percentiles": [{"percentile": 90, "value": 324}],
656 }
657
658 module = SimpleModule(
659 "r.univar",
660 map=["map_a", "map_b"],
661 flags="e",
662 format="json",
663 )
664 self.runModule(module)
665 output = json.loads(module.outputs.stdout)
666
667 for ref_key, ref_val in reference.items():
668 if isinstance(ref_val, float):
669 self.assertAlmostEqual(ref_val, output[ref_key], places=6)
670 else:
671 self.assertEqual(ref_val, output[ref_key])
672
673 def test_json_zone(self):
674 reference = [
675 {
676 "zone": 1,
677 "zone_label": "",
678 "n": 3420,
679 "null_cells": 0,
680 "cells": 3420,
681 "min": 102,
682 "max": 309,
683 "range": 207,
684 "mean": 205.5,
685 "mean_of_abs": 205.5,
686 "stddev": 56.611983419296187,
687 "variance": 3204.9166666666665,
688 "coeff_var": 27.548410423015174,
689 "sum": 702810,
690 "first_quartile": 155,
691 "median": 205.5,
692 "percentiles": [{"percentile": 90, "value": 282}],
693 "third_quartile": 255,
694 },
695 {
696 "zone": 2,
697 "zone_label": "",
698 "n": 12780,
699 "null_cells": 0,
700 "cells": 12780,
701 "min": 121,
702 "max": 380,
703 "range": 259,
704 "mean": 250.5,
705 "mean_of_abs": 250.5,
706 "stddev": 59.957623924457401,
707 "variance": 3594.9166666666665,
708 "coeff_var": 23.935179211360243,
709 "sum": 3201390,
710 "first_quartile": 200,
711 "median": 250.5,
712 "percentiles": [{"percentile": 90, "value": 330}],
713 "third_quartile": 300,
714 },
715 ]
716
717 module = SimpleModule(
718 "r.univar",
719 map=["map_a", "map_b"],
720 zones="zone_map",
721 flags="e",
722 format="json",
723 )
724 self.runModule(module)
725 output = json.loads(module.outputs.stdout)
726 for expected, received in zip_longest(reference, output):
727 self.assertCountEqual(list(expected.keys()), list(received.keys()))
728 for key in expected:
729 if isinstance(expected[key], float):
730 self.assertAlmostEqual(expected[key], received[key], places=6)
731 else:
732 self.assertEqual(expected[key], received[key])
733
734
735 if __name__ == "__main__":
736 from grass.gunittest.main import test
737
738 test()