statistics.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. try:
  18. from grass.lib.imagery import *
  19. except ImportError, e:
  20. sys.stderr.write(_("Loading imagery lib failed"))
  21. class Statistics:
  22. """! Statistis conected to one class (category).
  23. It is Python counterpart of similar C structure.
  24. But it adds some attributes or features used in wxIClass.
  25. It is not interface to C structure (it copies values).
  26. """
  27. def __init__(self):
  28. self.category = -1
  29. self.name = ""
  30. self.rasterName = ""
  31. self.color = "0:0:0"
  32. self.nbands = 0
  33. self.ncells = 0
  34. self.nstd = 1.5
  35. self.bands = []
  36. self.ready = False
  37. def SetReady(self, ready = True):
  38. self.ready = ready
  39. def IsReady(self):
  40. return self.ready
  41. def SetBaseStatistics(self, cat, name, color):
  42. """! Sets basic (non-statistical) values.
  43. @todo Later self.name is changed but self.rasterName is not.
  44. self.rasterName should not be set by user. It can remains the same.
  45. But it should be done more explicitly.
  46. Currently it looks like unintentional feature or bug.
  47. """
  48. self.category = cat
  49. self.name = name
  50. self.color = color
  51. rasterPath = grass.tempfile(create = False)
  52. name = name.replace(' ', '_')
  53. self.rasterName = name + '_' + os.path.basename(rasterPath)
  54. def SetStatistics(self, cStatistics):
  55. """! Sets all statistical values.
  56. Copies all statistic values from \a cStattistics.
  57. @param cStatistics pointer to C statistics structure
  58. """
  59. cat = c_int()
  60. I_iclass_statistics_get_cat(cStatistics, byref(cat))
  61. self.category = cat.value
  62. name = c_char_p()
  63. I_iclass_statistics_get_name(cStatistics, byref(name))
  64. self.name = name.value
  65. color = c_char_p()
  66. I_iclass_statistics_get_color(cStatistics, byref(color))
  67. self.color = color.value
  68. nbands = c_int()
  69. I_iclass_statistics_get_nbands(cStatistics, byref(nbands))
  70. self.nbands = nbands.value
  71. ncells = c_int()
  72. I_iclass_statistics_get_ncells(cStatistics, byref(ncells))
  73. self.ncells = ncells.value
  74. nstd = c_float()
  75. I_iclass_statistics_get_nstd(cStatistics, byref(nstd))
  76. self.nstd = nstd.value
  77. self.SetBandStatistics(cStatistics)
  78. def SetBandStatistics(self, cStatistics):
  79. """! Sets all band statistics.
  80. @param cStatistics pointer to C statistics structure
  81. """
  82. self.bands = []
  83. for i in range(self.nbands):
  84. band = BandStatistics()
  85. band.SetStatistics(cStatistics, index = i)
  86. self.bands.append(band)
  87. class BandStatistics:
  88. """! Statistis conected to one band within class (category).
  89. @see Statistics
  90. """
  91. def __init__(self):
  92. self.min = self.max = None
  93. self.rangeMin = self.rangeMax = None
  94. self.mean = None
  95. self.stddev = None
  96. self.histo = [0] * 256 # max categories
  97. def SetStatistics(self, cStatistics, index):
  98. """! Sets statistics for one band by given index.
  99. @param cStatistics pointer to C statistics structure
  100. @param index index of band in C statistics structure
  101. """
  102. min, max = c_int(), c_int()
  103. I_iclass_statistics_get_min(cStatistics, index, byref(min))
  104. I_iclass_statistics_get_max(cStatistics, index, byref(max))
  105. self.min, self.max = min.value, max.value
  106. rangeMin, rangeMax = c_int(), c_int()
  107. I_iclass_statistics_get_range_min(cStatistics, index, byref(rangeMin))
  108. I_iclass_statistics_get_range_max(cStatistics, index, byref(rangeMax))
  109. self.rangeMin, self.rangeMax = rangeMin.value, rangeMax.value
  110. mean, stddev = c_float(), c_float()
  111. I_iclass_statistics_get_mean(cStatistics, index, byref(mean))
  112. I_iclass_statistics_get_stddev(cStatistics, index, byref(stddev))
  113. self.mean, self.stddev = mean.value, stddev.value
  114. histo = c_int()
  115. for i in range(len(self.histo)):
  116. I_iclass_statistics_get_histo(cStatistics, index, i, byref(histo))
  117. self.histo[i] = histo.value