test_integer_rounding.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: test_integer_rounding
  5. # AUTHOR: Vaclav Petras
  6. # PURPOSE: Fast test of rounding integers based on a small example
  7. # COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. from grass.gunittest.case import TestCase
  15. from grass.gunittest.main import test
  16. from grass.script import list_strings
  17. # generated by
  18. # g.region n=12 s=9 e=21 w=18 t=8 b=4 res=1 res3=1 -p3
  19. # r3.mapcalc "x = rand(0,5.)" seed=100 && r3.out.ascii x prec=2
  20. INPUT = """\
  21. version: grass7
  22. order: nsbt
  23. north: 12.000000
  24. south: 9.000000
  25. east: 21.000000
  26. west: 18.000000
  27. top: 8.000000
  28. bottom: 4.000000
  29. rows: 3
  30. cols: 3
  31. levels: 4
  32. 1.26 1.04 4.70
  33. 2.11 1.98 1.91
  34. 1.16 2.68 2.67
  35. 4.31 4.81 1.58
  36. 3.68 3.27 0.25
  37. 2.27 4.03 3.28
  38. 4.06 4.15 0.40
  39. 1.02 3.40 3.26
  40. 4.88 0.43 1.02
  41. 1.16 0.07 0.78
  42. 0.07 3.52 0.45
  43. 2.86 4.79 1.43
  44. """
  45. # created from the above and template from
  46. # r.mapcalc "x = rand(0,10)" seed=100 && r.out.ascii x prec=0
  47. OUTPUTS = [
  48. """\
  49. north: 12
  50. south: 9
  51. east: 21
  52. west: 18
  53. rows: 3
  54. cols: 3
  55. 1 1 5
  56. 2 2 2
  57. 1 3 3
  58. """,
  59. """\
  60. north: 12
  61. south: 9
  62. east: 21
  63. west: 18
  64. rows: 3
  65. cols: 3
  66. 4 5 2
  67. 4 3 0
  68. 2 4 3
  69. """,
  70. """\
  71. north: 12
  72. south: 9
  73. east: 21
  74. west: 18
  75. rows: 3
  76. cols: 3
  77. 4 4 0
  78. 1 3 3
  79. 5 0 1
  80. """,
  81. """\
  82. north: 12
  83. south: 9
  84. east: 21
  85. west: 18
  86. rows: 3
  87. cols: 3
  88. 1 0 1
  89. 0 4 0
  90. 3 5 1
  91. """,
  92. ]
  93. class TestR3ToRastIntegerRounding(TestCase):
  94. # TODO: replace by unified handing of maps
  95. # mixing class and object attributes
  96. to_remove_3d = []
  97. to_remove_2d = []
  98. rast3d = "r3_to_rast_test_int_round"
  99. rast2d = "r3_to_rast_test_int_round"
  100. rast2d_ref = "r3_to_rast_test_int_round_ref"
  101. rast2d_refs = []
  102. def setUp(self):
  103. self.use_temp_region()
  104. self.runModule("r3.in.ascii", input="-", stdin_=INPUT, output=self.rast3d)
  105. self.to_remove_3d.append(self.rast3d)
  106. self.runModule("g.region", raster_3d=self.rast3d)
  107. for i, data in enumerate(OUTPUTS):
  108. rast = "%s_%d" % (self.rast2d_ref, i)
  109. self.runModule(
  110. "r.in.ascii", input="-", stdin_=data, output=rast, type="CELL"
  111. )
  112. self.to_remove_2d.append(rast)
  113. self.rast2d_refs.append(rast)
  114. def tearDown(self):
  115. if self.to_remove_3d:
  116. self.runModule(
  117. "g.remove",
  118. flags="f",
  119. type="raster_3d",
  120. name=",".join(self.to_remove_3d),
  121. verbose=True,
  122. )
  123. if self.to_remove_2d:
  124. self.runModule(
  125. "g.remove",
  126. flags="f",
  127. type="raster",
  128. name=",".join(self.to_remove_2d),
  129. verbose=True,
  130. )
  131. self.del_temp_region()
  132. def test_rounding(self):
  133. self.assertModule(
  134. "r3.to.rast", input=self.rast3d, output=self.rast2d, type="CELL", add=0.5
  135. )
  136. rasts = list_strings(
  137. "raster",
  138. mapset=".",
  139. pattern="%s_*" % self.rast2d,
  140. exclude="%s_*" % self.rast2d_ref,
  141. )
  142. self.assertEquals(
  143. len(rasts), 4, msg="Wrong number of 2D rasters present" " in the mapset"
  144. )
  145. ref_info = dict(cells=9)
  146. ref_univar = dict(cells=9, null_cells=0)
  147. for rast in rasts:
  148. self.assertRasterExists(rast)
  149. # the following doesn't make much sense because we just listed them
  150. self.to_remove_2d.append(rast)
  151. self.assertRasterFitsInfo(raster=rast, reference=ref_info, precision=0)
  152. self.assertRasterFitsUnivar(raster=rast, reference=ref_univar, precision=0)
  153. # check the actual values
  154. for rast_ref, rast in zip(self.rast2d_refs, rasts):
  155. self.assertRastersNoDifference(
  156. actual=rast, reference=rast_ref, precision=0.1
  157. )
  158. if __name__ == "__main__":
  159. test()