plots.py 9.6 KB

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