statistics.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """!
  2. @package iclass.statistics
  3. @brief wxIClass classes for storing statistics about cells in training areas.
  4. Classes:
  5. - statistics::Statistics
  6. - statistics::BandStatistics
  7. (C) 2006-2011 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. @author Vaclav Petras <wenzeslaus gmail.com>
  12. @author Anna Kratochvilova <kratochanna gmail.com>
  13. """
  14. import os
  15. from ctypes import *
  16. import grass.script as grass
  17. from core.utils import _
  18. try:
  19. from grass.lib.imagery import *
  20. except ImportError, e:
  21. sys.stderr.write(_("Loading imagery lib failed"))
  22. class Statistics:
  23. """! Statistis conected to one class (category).
  24. It is Python counterpart of similar C structure.
  25. But it adds some attributes or features used in wxIClass.
  26. It is not interface to C structure (it copies values).
  27. """
  28. def __init__(self):
  29. self.category = -1
  30. self.name = ""
  31. self.rasterName = ""
  32. self.color = "0:0:0"
  33. self.nbands = 0
  34. self.ncells = 0
  35. self.nstd = 1.5
  36. self.bands = []
  37. self.ready = False
  38. def SetReady(self, ready = True):
  39. self.ready = ready
  40. def IsReady(self):
  41. return self.ready
  42. def SetBaseStatistics(self, cat, name, color):
  43. """! Sets basic (non-statistical) values.
  44. @todo Later self.name is changed but self.rasterName is not.
  45. self.rasterName should not be set by user. It can remains the same.
  46. But it should be done more explicitly.
  47. Currently it looks like unintentional feature or bug.
  48. """
  49. self.category = cat
  50. self.name = name
  51. self.color = color
  52. rasterPath = grass.tempfile(create = False)
  53. name = name.replace(' ', '_')
  54. self.rasterName = name + '_' + os.path.basename(rasterPath)
  55. def SetStatistics(self, cStatistics):
  56. """! Sets all statistical values.
  57. Copies all statistic values from \a cStattistics.
  58. @param cStatistics pointer to C statistics structure
  59. """
  60. cat = c_int()
  61. I_iclass_statistics_get_cat(cStatistics, byref(cat))
  62. self.category = cat.value
  63. name = c_char_p()
  64. I_iclass_statistics_get_name(cStatistics, byref(name))
  65. self.name = name.value
  66. color = c_char_p()
  67. I_iclass_statistics_get_color(cStatistics, byref(color))
  68. self.color = color.value
  69. nbands = c_int()
  70. I_iclass_statistics_get_nbands(cStatistics, byref(nbands))
  71. self.nbands = nbands.value
  72. ncells = c_int()
  73. I_iclass_statistics_get_ncells(cStatistics, byref(ncells))
  74. self.ncells = ncells.value
  75. nstd = c_float()
  76. I_iclass_statistics_get_nstd(cStatistics, byref(nstd))
  77. self.nstd = nstd.value
  78. self.SetBandStatistics(cStatistics)
  79. def SetBandStatistics(self, cStatistics):
  80. """! Sets all band statistics.
  81. @param cStatistics pointer to C statistics structure
  82. """
  83. self.bands = []
  84. for i in range(self.nbands):
  85. band = BandStatistics()
  86. band.SetStatistics(cStatistics, index = i)
  87. self.bands.append(band)
  88. class BandStatistics:
  89. """! Statistis conected to one band within class (category).
  90. @see Statistics
  91. """
  92. def __init__(self):
  93. self.min = self.max = None
  94. self.rangeMin = self.rangeMax = None
  95. self.mean = None
  96. self.stddev = None
  97. self.histo = [0] * 256 # max categories
  98. def SetStatistics(self, cStatistics, index):
  99. """! Sets statistics for one band by given index.
  100. @param cStatistics pointer to C statistics structure
  101. @param index index of band in C statistics structure
  102. """
  103. min, max = c_int(), c_int()
  104. I_iclass_statistics_get_min(cStatistics, index, byref(min))
  105. I_iclass_statistics_get_max(cStatistics, index, byref(max))
  106. self.min, self.max = min.value, max.value
  107. rangeMin, rangeMax = c_int(), c_int()
  108. I_iclass_statistics_get_range_min(cStatistics, index, byref(rangeMin))
  109. I_iclass_statistics_get_range_max(cStatistics, index, byref(rangeMax))
  110. self.rangeMin, self.rangeMax = rangeMin.value, rangeMax.value
  111. mean, stddev = c_float(), c_float()
  112. I_iclass_statistics_get_mean(cStatistics, index, byref(mean))
  113. I_iclass_statistics_get_stddev(cStatistics, index, byref(stddev))
  114. self.mean, self.stddev = mean.value, stddev.value
  115. histo = c_int()
  116. for i in range(len(self.histo)):
  117. I_iclass_statistics_get_histo(cStatistics, index, i, byref(histo))
  118. self.histo[i] = histo.value