test_integer_rounding.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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,
  105. output=self.rast3d)
  106. self.to_remove_3d.append(self.rast3d)
  107. self.runModule('g.region', raster_3d=self.rast3d)
  108. for i, data in enumerate(OUTPUTS):
  109. rast = "%s_%d" % (self.rast2d_ref, i)
  110. self.runModule('r.in.ascii', input='-', stdin_=data,
  111. output=rast, type='CELL')
  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('g.remove', flags='f', type='raster_3d',
  117. name=','.join(self.to_remove_3d), verbose=True)
  118. if self.to_remove_2d:
  119. self.runModule('g.remove', flags='f', type='raster',
  120. name=','.join(self.to_remove_2d), verbose=True)
  121. self.del_temp_region()
  122. def test_rounding(self):
  123. self.assertModule('r3.to.rast', input=self.rast3d,
  124. output=self.rast2d, type='CELL', add=0.5)
  125. rasts = list_strings('raster', mapset=".",
  126. pattern="%s_*" % self.rast2d,
  127. exclude="%s_*" % self.rast2d_ref)
  128. self.assertEquals(len(rasts), 4,
  129. msg="Wrong number of 2D rasters present"
  130. " in the mapset")
  131. ref_info = dict(cells=9)
  132. ref_univar = dict(cells=9, null_cells=0)
  133. for rast in rasts:
  134. self.assertRasterExists(rast)
  135. # the following doesn't make much sense because we just listed them
  136. self.to_remove_2d.append(rast)
  137. self.assertRasterFitsInfo(raster=rast, reference=ref_info,
  138. precision=0)
  139. self.assertRasterFitsUnivar(raster=rast, reference=ref_univar,
  140. precision=0)
  141. # check the actual values
  142. for rast_ref, rast in zip(self.rast2d_refs, rasts):
  143. self.assertRastersNoDifference(actual=rast,
  144. reference=rast_ref,
  145. precision=0.1)
  146. if __name__ == '__main__':
  147. test()