plots.py 9.7 KB

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