plots.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. """!
  2. @package iclass.plots
  3. @brief wxIClass plots (histograms, coincidence plots).
  4. Classes:
  5. - plots::PlotPanel
  6. (C) 2006-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. @author Vaclav Petras <wenzeslaus gmail.com>
  11. @author Anna Kratochvilova <kratochanna gmail.com>
  12. """
  13. import wx
  14. import wx.lib.plot as plot
  15. import wx.lib.scrolledpanel as scrolled
  16. from core.utils import _
  17. class PlotPanel(scrolled.ScrolledPanel):
  18. """!Panel for drawing multiple plots.
  19. There are two types of plots: histograms and coincidence plots.
  20. Histograms show frequency of cell category values in training areas
  21. for each band and for one category. Coincidence plots show min max range
  22. of classes for each band.
  23. """
  24. def __init__(self, parent, giface, stats_data):
  25. scrolled.ScrolledPanel.__init__(self, parent)
  26. self.SetupScrolling(scroll_x = False, scroll_y = True)
  27. self._giface = giface
  28. self.parent = parent
  29. self.canvasList = []
  30. self.bandList = []
  31. self.stats_data = stats_data
  32. self.currentCat = None
  33. self.mainSizer = wx.BoxSizer(wx.VERTICAL)
  34. self._createControlPanel()
  35. self.SetSizer(self.mainSizer)
  36. self.mainSizer.Fit(self)
  37. self.Layout()
  38. def _createControlPanel(self):
  39. self.plotSwitch = wx.Choice(self, id = wx.ID_ANY,
  40. choices = [_("Histograms"),
  41. _("Coincident plots")])
  42. self.mainSizer.Add(self.plotSwitch, proportion = 0, flag = wx.EXPAND|wx.ALL, border = 5)
  43. self.plotSwitch.Bind(wx.EVT_CHOICE, self.OnPlotTypeSelected)
  44. def OnPlotTypeSelected(self, event):
  45. """!Plot type selected"""
  46. if self.currentCat is None:
  47. return
  48. if self.plotSwitch.GetSelection() == 0:
  49. stat = self.stats_data.GetStatistics(self.currentCat)
  50. if not stat.IsReady():
  51. self.ClearPlots()
  52. return
  53. self.DrawHistograms(stat)
  54. else:
  55. self.DrawCoincidencePlots()
  56. def StddevChanged(self):
  57. """!Standard deviation multiplier changed, redraw histograms"""
  58. if self.plotSwitch.GetSelection() == 0:
  59. stat = self.stats_data.GetStatistics(self.currentCat)
  60. self.UpdateRanges(stat)
  61. def EnableZoom(self, type, enable = True):
  62. for canvas in self.canvasList:
  63. canvas.SetEnableZoom(enable)
  64. #canvas.zoom = type
  65. def EnablePan(self, enable = True):
  66. for canvas in self.canvasList:
  67. canvas.SetEnableDrag(enable)
  68. def DestroyPlots(self):
  69. """!Destroy all plot canvases"""
  70. for panel in self.canvasList:
  71. panel.Destroy()
  72. self.canvasList = []
  73. def ClearPlots(self):
  74. """!Clears plot canvases"""
  75. for bandIdx in range(len(self.bandList)):
  76. self.canvasList[bandIdx].Clear()
  77. def Reset(self):
  78. """!Reset plots (when new map imported)"""
  79. self.currentCat = None
  80. self.ClearPlots()
  81. # bands are still the same
  82. def CreatePlotCanvases(self):
  83. """!Create plot canvases according to the number of bands"""
  84. for band in self.bandList:
  85. canvas = plot.PlotCanvas(self)
  86. canvas.SetMinSize((-1, 140))
  87. canvas.SetFontSizeTitle(10)
  88. canvas.SetFontSizeAxis(8)
  89. self.canvasList.append(canvas)
  90. self.mainSizer.Add(item = canvas, proportion = 1, flag = wx.EXPAND, border = 0)
  91. self.SetVirtualSize(self.GetBestVirtualSize())
  92. self.Layout()
  93. def UpdatePlots(self, group, subgroup, currentCat, stats_data):
  94. """!Update plots after new analysis
  95. @param group imagery group
  96. @param subgroup imagery group
  97. @param currentCat currently selected category (class)
  98. @param stats_data StatisticsData instance (defined in statistics.py)
  99. """
  100. self.stats_data = stats_data
  101. self.currentCat = currentCat
  102. self.bandList = self.parent.GetGroupLayers(group, subgroup)
  103. graphType = self.plotSwitch.GetSelection()
  104. stat = self.stats_data.GetStatistics(currentCat)
  105. if not stat.IsReady() and graphType == 0:
  106. return
  107. self.DestroyPlots()
  108. self.CreatePlotCanvases()
  109. self.OnPlotTypeSelected(None)
  110. def UpdateCategory(self, cat):
  111. self.currentCat = cat
  112. def DrawCoincidencePlots(self):
  113. """!Draw coincidence plots"""
  114. for bandIdx in range(len(self.bandList)):
  115. self.canvasList[bandIdx].SetYSpec(type = 'none')
  116. lines = []
  117. level = 0.5
  118. lines.append(self.DrawInvisibleLine(level))
  119. cats = self.stats_data.GetCategories()
  120. for i, cat in enumerate(cats):
  121. stat = self.stats_data.GetStatistics(cat)
  122. if not stat.IsReady():
  123. continue
  124. color = stat.color
  125. level = i + 1
  126. line = self.DrawCoincidenceLine(level, color, stat.bands[bandIdx])
  127. lines.append(line)
  128. # invisible
  129. level += 0.5
  130. lines.append(self.DrawInvisibleLine(level))
  131. plotGraph = plot.PlotGraphics(lines, title = self.bandList[bandIdx])
  132. self.canvasList[bandIdx].Draw(plotGraph)
  133. def DrawCoincidenceLine(self, level, color, bandValues):
  134. """!Draw line between band min and max values
  135. @param level y coordinate of line
  136. @param color class color
  137. @param bandValues BandStatistics instance
  138. """
  139. minim = bandValues.min
  140. maxim = bandValues.max
  141. points = [(minim, level), (maxim, level)]
  142. color = wx.Colour(*map(int, color.split(':')))
  143. return plot.PolyLine(points, colour = color, width = 4)
  144. def DrawInvisibleLine(self, level):
  145. """!Draw white line to achieve better margins"""
  146. points = [(100, level), (101, level)]
  147. return plot.PolyLine(points, colour = wx.WHITE, width = 1)
  148. def DrawHistograms(self, statistics):
  149. """!Draw histograms for one class
  150. @param statistics statistics for one class
  151. """
  152. self.histogramLines = []
  153. for bandIdx in range(len(self.bandList)):
  154. self.canvasList[bandIdx].Clear()
  155. self.canvasList[bandIdx].SetYSpec(type = 'auto')
  156. histgramLine = self.CreateHistogramLine(bandValues = statistics.bands[bandIdx])
  157. meanLine = self.CreateMean(bandValues = statistics.bands[bandIdx])
  158. minLine = self.CreateMin(bandValues = statistics.bands[bandIdx])
  159. maxLine = self.CreateMax(bandValues = statistics.bands[bandIdx])
  160. self.histogramLines.append([histgramLine, meanLine, minLine, maxLine])
  161. maxRangeLine = self.CreateMaxRange(bandValues = statistics.bands[bandIdx])
  162. minRangeLine = self.CreateMinRange(bandValues = statistics.bands[bandIdx])
  163. plotGraph = plot.PlotGraphics(self.histogramLines[bandIdx] + [minRangeLine, maxRangeLine],
  164. title = self.bandList[bandIdx])
  165. self.canvasList[bandIdx].Draw(plotGraph)
  166. def CreateMinRange(self, bandValues):
  167. maxVal = max(bandValues.histo)
  168. rMin = bandValues.rangeMin
  169. points = [(rMin, 0), (rMin, maxVal)]
  170. return plot.PolyLine(points, colour = wx.RED, width = 1)
  171. def CreateMaxRange(self, bandValues):
  172. maxVal = max(bandValues.histo)
  173. rMax = bandValues.rangeMax
  174. points = [(rMax, 0), (rMax, maxVal)]
  175. return plot.PolyLine(points, colour = wx.RED, width = 1)
  176. def CreateMean(self, bandValues):
  177. maxVal = max(bandValues.histo)
  178. mean = bandValues.mean
  179. points = [(mean, 0), (mean, maxVal)]
  180. return plot.PolyLine(points, colour = wx.BLUE, width = 1)
  181. def CreateMin(self, bandValues):
  182. maxVal = max(bandValues.histo)
  183. minim = bandValues.min
  184. points = [(minim, 0), (minim, maxVal)]
  185. return plot.PolyLine(points, colour = wx.Colour(200, 200, 200), width = 1)
  186. def CreateMax(self, bandValues):
  187. maxVal = max(bandValues.histo)
  188. maxim = bandValues.max
  189. points = [(maxim, 0), (maxim, maxVal)]
  190. return plot.PolyLine(points, colour = wx.Colour(200, 200, 200), width = 1)
  191. def CreateHistogramLine(self, bandValues):
  192. points = []
  193. for cellCat, count in enumerate(bandValues.histo):
  194. if cellCat < bandValues.min - 5:
  195. continue
  196. if cellCat > bandValues.max + 5:
  197. break
  198. points.append((cellCat, count))
  199. return plot.PolyLine(points, colour = wx.BLACK, width = 1)
  200. def UpdateRanges(self, statistics):
  201. """!Redraw ranges lines in histograms when std dev multiplier changes
  202. @param statistics python Statistics instance
  203. """
  204. for bandIdx in range(len(self.bandList)):
  205. self.canvasList[bandIdx].Clear()
  206. maxRangeLine = self.CreateMaxRange(bandValues = statistics.bands[bandIdx])
  207. minRangeLine = self.CreateMinRange(bandValues = statistics.bands[bandIdx])
  208. plotGraph = plot.PlotGraphics(self.histogramLines[bandIdx] + [minRangeLine, maxRangeLine],
  209. title = self.bandList[bandIdx])
  210. self.canvasList[bandIdx].Draw(plotGraph)