statistics.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """
  2. @package iclass.statistics
  3. @brief wxIClass classes for storing statistics about cells in training areas.
  4. Classes:
  5. - statistics::StatisticsData
  6. - statistics::Statistics
  7. - statistics::BandStatistics
  8. (C) 2006-2011, 2013 by the GRASS Development Team
  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. @author Vaclav Petras <wenzeslaus gmail.com>
  13. @author Anna Kratochvilova <kratochanna gmail.com>
  14. """
  15. import os
  16. import six
  17. from ctypes import *
  18. import grass.script as grass
  19. from core.utils import _
  20. try:
  21. from grass.lib.imagery import *
  22. except ImportError as e:
  23. sys.stderr.write(_("Loading imagery lib failed"))
  24. from grass.pydispatch.signal import Signal
  25. class StatisticsData:
  26. """Stores all statistics.
  27. """
  28. def __init__(self):
  29. self.statisticsDict = {}
  30. self.statisticsList = []
  31. self.statisticsAdded = Signal("StatisticsData.statisticsAdded")
  32. self.statisticsDeleted = Signal("StatisticsData.statisticsDeleted")
  33. self.allStatisticsDeleted = Signal(
  34. "StatisticsData.allStatisticsDeleted")
  35. self.statisticsSet = Signal("StatisticsData.statisticsSet")
  36. def GetStatistics(self, cat):
  37. return self.statisticsDict[cat]
  38. def AddStatistics(self, cat, name, color):
  39. st = Statistics()
  40. st.SetBaseStatistics(cat=cat, name=name, color=color)
  41. st.statisticsSet.connect(
  42. lambda stats: self.statisticsSet.emit(
  43. cat=cat, stats=stats))
  44. self.statisticsDict[cat] = st
  45. self.statisticsList.append(cat)
  46. self.statisticsAdded.emit(cat=cat, name=name, color=color)
  47. def DeleteStatistics(self, cat):
  48. del self.statisticsDict[cat]
  49. self.statisticsList.remove(cat)
  50. self.statisticsDeleted.emit(cat=cat)
  51. def GetCategories(self):
  52. return self.statisticsList[:]
  53. def DeleteAllStatistics(self):
  54. self.statisticsDict.clear()
  55. del self.statisticsList[:] # not ...=[] !
  56. self.allStatisticsDeleted.emit()
  57. class Statistics:
  58. """Statistis conected to one class (category).
  59. It is Python counterpart of similar C structure.
  60. But it adds some attributes or features used in wxIClass.
  61. It is not interface to C structure (it copies values).
  62. """
  63. def __init__(self):
  64. self.category = -1
  65. self.name = ""
  66. self.rasterName = ""
  67. self.color = "0:0:0"
  68. self.nbands = 0
  69. self.ncells = 0
  70. self.nstd = 1.5
  71. self.bands = []
  72. self.ready = False
  73. self.statisticsSet = Signal("Statistics.statisticsSet")
  74. def SetReady(self, ready=True):
  75. self.ready = ready
  76. def IsReady(self):
  77. return self.ready
  78. def SetBaseStatistics(self, cat, name, color):
  79. """Sets basic (non-statistical) values.
  80. .. todo::
  81. Later self.name is changed but self.rasterName is not.
  82. self.rasterName should not be set by user. It can remains
  83. the same. But it should be done more explicitly. Currently
  84. it looks like unintentional feature or bug.
  85. """
  86. self.category = cat
  87. self.name = name
  88. self.color = color
  89. rasterPath = grass.tempfile(create=False)
  90. name = name.replace(' ', '_')
  91. self.rasterName = name + '_' + os.path.basename(rasterPath)
  92. def SetFromcStatistics(self, cStatistics):
  93. """Sets all statistical values.
  94. Copies all statistic values from \a cStattistics.
  95. :param cStatistics: pointer to C statistics structure
  96. """
  97. cat = c_int()
  98. set_stats = {}
  99. I_iclass_statistics_get_cat(cStatistics, byref(cat))
  100. if self.category != cat.value:
  101. set_stats["category"] = cat.value
  102. name = c_char_p()
  103. I_iclass_statistics_get_name(cStatistics, byref(name))
  104. if self.name != name.value:
  105. set_stats["name"] = name.value
  106. color = c_char_p()
  107. I_iclass_statistics_get_color(cStatistics, byref(color))
  108. if self.color != color.value:
  109. set_stats["color"] = color.value
  110. nbands = c_int()
  111. I_iclass_statistics_get_nbands(cStatistics, byref(nbands))
  112. if self.nbands != nbands.value:
  113. set_stats["nbands"] = nbands.value
  114. ncells = c_int()
  115. I_iclass_statistics_get_ncells(cStatistics, byref(ncells))
  116. if self.ncells != ncells.value:
  117. set_stats["ncells"] = ncells.value
  118. nstd = c_float()
  119. I_iclass_statistics_get_nstd(cStatistics, byref(nstd))
  120. if self.nstd != nstd.value:
  121. set_stats["nstd"] = nstd.value
  122. self.SetStatistics(set_stats)
  123. self.SetBandStatistics(cStatistics)
  124. def SetBandStatistics(self, cStatistics):
  125. """Sets all band statistics.
  126. :param cStatistics: pointer to C statistics structure
  127. """
  128. self.bands = []
  129. for i in range(self.nbands):
  130. band = BandStatistics()
  131. band.SetFromcStatistics(cStatistics, index=i)
  132. self.bands.append(band)
  133. def SetStatistics(self, stats):
  134. for st, val in six.iteritems(stats):
  135. setattr(self, st, val)
  136. self.statisticsSet.emit(stats=stats)
  137. class BandStatistics:
  138. """Statistis conected to one band within class (category).
  139. :class:`Statistics`
  140. """
  141. def __init__(self):
  142. self.min = self.max = None
  143. self.rangeMin = self.rangeMax = None
  144. self.mean = None
  145. self.stddev = None
  146. self.histo = [0] * 256 # max categories
  147. def SetFromcStatistics(self, cStatistics, index):
  148. """Sets statistics for one band by given index.
  149. :param cStatistics: pointer to C statistics structure
  150. :param index: index of band in C statistics structure
  151. """
  152. min, max = c_int(), c_int()
  153. I_iclass_statistics_get_min(cStatistics, index, byref(min))
  154. I_iclass_statistics_get_max(cStatistics, index, byref(max))
  155. self.min, self.max = min.value, max.value
  156. rangeMin, rangeMax = c_int(), c_int()
  157. I_iclass_statistics_get_range_min(cStatistics, index, byref(rangeMin))
  158. I_iclass_statistics_get_range_max(cStatistics, index, byref(rangeMax))
  159. self.rangeMin, self.rangeMax = rangeMin.value, rangeMax.value
  160. mean, stddev = c_float(), c_float()
  161. I_iclass_statistics_get_mean(cStatistics, index, byref(mean))
  162. I_iclass_statistics_get_stddev(cStatistics, index, byref(stddev))
  163. self.mean, self.stddev = mean.value, stddev.value
  164. histo = c_int()
  165. for i in range(len(self.histo)):
  166. I_iclass_statistics_get_histo(cStatistics, index, i, byref(histo))
  167. self.histo[i] = histo.value