plots.py 9.7 KB

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