statistics.py 6.8 KB

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