statistics.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. self.rasterName = name + '_' + os.path.basename(rasterPath)
  53. def SetStatistics(self, cStatistics):
  54. """! Sets all statistical values.
  55. Copies all statistic values from \a cStattistics.
  56. @param cStatistics pointer to C statistics structure
  57. """
  58. cat = c_int()
  59. I_iclass_statistics_get_cat(cStatistics, byref(cat))
  60. self.category = cat.value
  61. name = c_char_p()
  62. I_iclass_statistics_get_name(cStatistics, byref(name))
  63. self.name = name.value
  64. color = c_char_p()
  65. I_iclass_statistics_get_color(cStatistics, byref(color))
  66. self.color = color.value
  67. nbands = c_int()
  68. I_iclass_statistics_get_nbands(cStatistics, byref(nbands))
  69. self.nbands = nbands.value
  70. ncells = c_int()
  71. I_iclass_statistics_get_ncells(cStatistics, byref(ncells))
  72. self.ncells = ncells.value
  73. nstd = c_float()
  74. I_iclass_statistics_get_nstd(cStatistics, byref(nstd))
  75. self.nstd = nstd.value
  76. self.SetBandStatistics(cStatistics)
  77. def SetBandStatistics(self, cStatistics):
  78. """! Sets all band statistics.
  79. @param cStatistics pointer to C statistics structure
  80. """
  81. self.bands = []
  82. for i in range(self.nbands):
  83. band = BandStatistics()
  84. band.SetStatistics(cStatistics, index = i)
  85. self.bands.append(band)
  86. class BandStatistics:
  87. """! Statistis conected to one band within class (category).
  88. @see Statistics
  89. """
  90. def __init__(self):
  91. self.min = self.max = None
  92. self.rangeMin = self.rangeMax = None
  93. self.mean = None
  94. self.stddev = None
  95. self.histo = [0] * 256 # max categories
  96. def SetStatistics(self, cStatistics, index):
  97. """! Sets statistics for one band by given index.
  98. @param cStatistics pointer to C statistics structure
  99. @param index index of band in C statistics structure
  100. """
  101. min, max = c_int(), c_int()
  102. I_iclass_statistics_get_min(cStatistics, index, byref(min))
  103. I_iclass_statistics_get_max(cStatistics, index, byref(max))
  104. self.min, self.max = min.value, max.value
  105. rangeMin, rangeMax = c_int(), c_int()
  106. I_iclass_statistics_get_range_min(cStatistics, index, byref(rangeMin))
  107. I_iclass_statistics_get_range_max(cStatistics, index, byref(rangeMax))
  108. self.rangeMin, self.rangeMax = rangeMin.value, rangeMax.value
  109. mean, stddev = c_float(), c_float()
  110. I_iclass_statistics_get_mean(cStatistics, index, byref(mean))
  111. I_iclass_statistics_get_stddev(cStatistics, index, byref(stddev))
  112. self.mean, self.stddev = mean.value, stddev.value
  113. histo = c_int()
  114. for i in range(len(self.histo)):
  115. I_iclass_statistics_get_histo(cStatistics, index, i, byref(histo))
  116. self.histo[i] = histo.value