test_nulls.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: test_small_data
  5. # AUTHOR: Vaclav Petras
  6. # PURPOSE: Fast test of nulls values based on the small test
  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. # copied from the small test
  18. # generated by
  19. # g.region n=12 s=9 e=21 w=18 t=8 b=4 res=1 res3=1 -p3
  20. # r3.mapcalc "x = rand(0,10)" seed=100 && r3.out.ascii x prec=0
  21. # added nulls in the way that each raster has two null cells
  22. # so we can easily test it for all rasters together
  23. INPUT = """\
  24. version: grass7
  25. order: nsbt
  26. north: 12
  27. south: 9
  28. east: 21
  29. west: 18
  30. top: 8
  31. bottom: 4
  32. rows: 3
  33. cols: 3
  34. levels: 4
  35. 6 5 1
  36. 0 * 5
  37. 1 7 *
  38. 8 2 *
  39. * 4 2
  40. 8 5 6
  41. 1 2 8
  42. 1 5 *
  43. 1 1 *
  44. 1 8 3
  45. 6 * *
  46. 5 1 7
  47. """
  48. # created from the above data above
  49. OUTPUTS = [
  50. """\
  51. north: 12
  52. south: 9
  53. east: 21
  54. west: 18
  55. rows: 3
  56. cols: 3
  57. 6 5 1
  58. 0 * 5
  59. 1 7 *
  60. """,
  61. """\
  62. north: 12
  63. south: 9
  64. east: 21
  65. west: 18
  66. rows: 3
  67. cols: 3
  68. 8 2 *
  69. * 4 2
  70. 8 5 6
  71. """,
  72. """\
  73. north: 12
  74. south: 9
  75. east: 21
  76. west: 18
  77. rows: 3
  78. cols: 3
  79. 1 2 8
  80. 1 5 *
  81. 1 1 *
  82. """,
  83. """\
  84. north: 12
  85. south: 9
  86. east: 21
  87. west: 18
  88. rows: 3
  89. cols: 3
  90. 1 8 3
  91. 6 * *
  92. 5 1 7
  93. """,
  94. ]
  95. # basically copy of the class from the small test
  96. class TestR3ToRastNulls(TestCase):
  97. # TODO: replace by unified handing of maps
  98. # mixing class and object attributes
  99. to_remove_3d = []
  100. to_remove_2d = []
  101. rast3d = "r3_to_rast_test_nulls"
  102. rast2d = "r3_to_rast_test_nulls"
  103. rast2d_ref = "r3_to_rast_test_nulls_ref"
  104. rast2d_refs = []
  105. def setUp(self):
  106. self.use_temp_region()
  107. self.runModule("r3.in.ascii", input="-", stdin_=INPUT, output=self.rast3d)
  108. self.to_remove_3d.append(self.rast3d)
  109. self.runModule("g.region", raster_3d=self.rast3d)
  110. for i, data in enumerate(OUTPUTS):
  111. rast = "%s_%d" % (self.rast2d_ref, i)
  112. self.runModule("r.in.ascii", input="-", stdin_=data, output=rast)
  113. self.to_remove_2d.append(rast)
  114. self.rast2d_refs.append(rast)
  115. def tearDown(self):
  116. if self.to_remove_3d:
  117. self.runModule(
  118. "g.remove",
  119. flags="f",
  120. type="raster_3d",
  121. name=",".join(self.to_remove_3d),
  122. verbose=True,
  123. )
  124. if self.to_remove_2d:
  125. self.runModule(
  126. "g.remove",
  127. flags="f",
  128. type="raster",
  129. name=",".join(self.to_remove_2d),
  130. verbose=True,
  131. )
  132. self.del_temp_region()
  133. def test_b(self):
  134. self.assertModule("r3.to.rast", input=self.rast3d, output=self.rast2d)
  135. rasts = list_strings(
  136. "raster",
  137. mapset=".",
  138. pattern="%s_*" % self.rast2d,
  139. exclude="%s_*" % self.rast2d_ref,
  140. )
  141. self.assertEquals(
  142. len(rasts), 4, msg="Wrong number of 2D rasters present" " in the mapset"
  143. )
  144. ref_info = dict(cells=9)
  145. # only this tests the presence of nulls
  146. ref_univar = dict(cells=9, null_cells=2)
  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. # TODO: this does not check the position of nulls
  155. # (it ignores nulls)
  156. for rast_ref, rast in zip(self.rast2d_refs, rasts):
  157. self.assertRastersNoDifference(
  158. actual=rast, reference=rast_ref, precision=0.1
  159. )
  160. if __name__ == "__main__":
  161. test()