
    ը	f,                     j    d Z ddlZddlmc mZ ddlmc mZ ddlm	Z	  G d dej                        Zy)a  
@package iclass.plots

@brief wxIClass plots (histograms, coincidence plots).

Classes:
 - plots::PlotPanel

(C) 2006-2011,2013 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.

@author Vaclav Petras <wenzeslaus gmail.com>
@author Anna Kratochvilova <kratochanna gmail.com>
    N)GErrorc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
dd	Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)	PlotPanela/  Panel for drawing multiple plots.

    There are three types of plots: histograms, coincidence plots and scatter plots.
    Histograms show frequency of cell category values in training areas
    for each band and for one category. Coincidence plots show min max range
    of classes for each band.
    c                    t         j                  j                  | |       | j                  dd       || _        || _        g | _        g | _        || _        d | _	        t        j                  t        j                        | _        | j                          | j                          | j!                          | j#                  | j                         | j                  j%                  |        | j'                          y )NFTscroll_xscroll_y)scrolledScrolledPanel__init__SetupScrolling_gifaceparent
canvasListbandList
stats_data
currentCatwxBoxSizerVERTICAL	mainSizer_createControlPanel_createPlotPanel_createScatterPlotPanelSetSizerFitLayout)selfr   gifacer   s       -/usr/lib/grass83/gui/wxpython/iclass/plots.pyr   zPlotPanel.__init__"   s    ''f5UT:$R[[1  "$$&dnn%4     c                 R    | j                   r| j                   j                          y y N)iscatt_panelCloseWindowr   s    r    r%   zPlotPanel.CloseWindow7   s"    ))+ r!   c                 B   t        j                  |       | _        | j                  j	                  | j                  dt         j
                  d       t        j                  t         j                        | _        | j                  j                  | j                         y )N)r      r   
proportionflagborder)
r   PanelcanvasPanelr   AddEXPANDr   r   canvasSizerr   r&   s    r    r   zPlotPanel._createPlotPanel;   sh    88404++		RST;;r{{3!!$"2"23r!   c           	         t        j                  | t         j                  t        d      t        d      t        d      g      | _        | j
                  j                  | j                  dt         j                  t         j                  z  d       | j                  j                  t         j                  | j                         y )N
HistogramszCoincident plotszScatter plots)idchoicesr      r)   )r   ChoiceID_ANY_
plotSwitchr   r/   r0   ALLBind
EVT_CHOICEOnPlotTypeSelectedr&   s    r    r   zPlotPanel._createControlPanelA   s    ))yy|_a(:&;Q=OP

 	OO		BFF0B1 	 	
 	R]]D,C,CDr!   c                    	 ddl m}  || | j                  | j                  j	                               | _        | j                  j                  | j
                  dt        j                  d       | j
                  j                          y	# t        $ rK}t        d|z        | _        t        j                  t        | j                  d|        d	| _        Y d	}~y	d	}~ww xY w)
z"Init interactive scatter plot toolr   )IClassIScattPanel)r   r   iclass_mapwinr(   r)   zfScatter plot functionality is disabled.

Reason: Unable to import packages needed for scatter plot.
%sF)showTracebackr   N)iscatt.framer@   r   r   GetFirstWindowr$   r   r/   r   r0   HideImportErrorr9   scatt_error	CallAfterr   )r   r@   es      r    r   z!PlotPanel._createScatterPlotPanelL   s    	%6 1||"kk88:!D
 NN!!abii   ""$ 	% IKLM D LL!1!1tT $D	%s   B	B 	C ACC c                 t   | j                   j                         dv rd| j                  dd       | j                  r| j                  j	                          | j
                  j                          | j                          n| j                   j                         dk(  ry| j                  dd       | j                  r| j                  j                          nt        | j                         | j
                  j	                          | j                          | j                  y| j                   j                         dk(  rX| j                  j                  | j                        }|j                         s| j                          y| j                  |       n| j!                          | j                          y)zPlot type selected)r   r(   FTr      Nr   )r:   GetSelectionr   r$   rE   r.   Showr   r   rG   r   r   GetStatisticsIsReady
ClearPlotsDrawHistogramsDrawCoincidencePlots)r   eventstats      r    r>   zPlotPanel.OnPlotTypeSelectedb   sA    ??'')V3>  !!&&(!!#KKM__))+q0?  !!&&(t''(!!#KKM??"??'')Q.??00AD<<>!%%%'r!   c                     | j                   j                         dk(  r7| j                  j                  | j                        }| j                  |       yy)z8Standard deviation multiplier changed, redraw histogramsr   N)r:   rL   r   rN   r   UpdateRanges)r   rT   s     r    StddevChangedzPlotPanel.StddevChanged   sB    ??'')Q.??00ADd# /r!   c                 4    | j                   D ]	  }||_         y r#   )r   
enableZoom)r   typeenablecanvass       r    
EnableZoomzPlotPanel.EnableZoom   s    oo 	'F &F	'r!   c                 H    | j                   D ]  }|j                  |        y r#   )r   SetEnableDrag)r   r[   r\   s      r    	EnablePanzPlotPanel.EnablePan   s#    oo 	)F  (	)r!   c                 T    | j                   D ]  }|j                           g | _         y)zDestroy all plot canvasesN)r   Destroy)r   panels     r    DestroyPlotszPlotPanel.DestroyPlots   s'    __ 	EMMO	 r!   c                     t        t        | j                              D ]  }| j                  |   j	                          ! y)zClears plot canvasesN)rangelenr   r   Clear)r   bandIdxs     r    rP   zPlotPanel.ClearPlots   s4    S/0 	-GOOG$**,	-r!   c                 2    d| _         | j                          y)z#Reset plots (when new map imported)N)r   rP   r&   s    r    ResetzPlotPanel.Reset   s    r!   c                    | j                   D ]  }t        j                  | j                        }|j	                  d       d|_        d|_        | j                  j                  |       | j                  j                  |dt        j                  d        | j                  | j                                | j                          y)z5Create plot canvases according to the number of bands)   
      r(   r   r)   N)r   plot
PlotCanvasr.   
SetMinSizefontSizeTitlefontSizeAxisr   appendr1   r/   r   r0   SetVirtualSizeGetBestVirtualSizer   )r   bandr\   s      r    CreatePlotCanvaseszPlotPanel.CreatePlotCanvases   s    MM 	QD__T%5%56Fi(#%F "#FOO""6*  ABIIa P	Q 	D3356r!   c                 Z   || _         || _        | j                  j                  ||      | _        | j
                  j                         }| j                   j                  |      }|j                         s|dk(  ry| j                          | j                          | j                  d       y)a  Update plots after new analysis

        :param group: imagery group
        :param subgroup: imagery group
        :param currentCat: currently selected category (class)
        :param stats_data: StatisticsData instance (defined in statistics.py)
        r   N)r   r   r   GetGroupLayersr   r:   rL   rN   rO   rd   rz   r>   )r   groupsubgroupr   r   	graphTyperT   s          r    UpdatePlotszPlotPanel.UpdatePlots   s     %$225(COO002	,,Z8||~)q.!%r!   c                     || _         y r#   )r   )r   cats     r    UpdateCategoryzPlotPanel.UpdateCategory   s	    r!   c                    t        t        | j                              D ]=  }d| j                  |   _        g }d}|j                  | j                  |             | j                  j                         }t        |      D ]s  \  }}| j                  j                  |      }|j                         s2|j                  }|dz   }| j                  |||j                  |         }	|j                  |	       u |dz  }|j                  | j                  |             t        j                   || j                  |         }
| j                  |   j#                  |
       @ y)zDraw coincidence plotsnoneg      ?r(   titleN)rf   rg   r   r   ySpecrv   DrawInvisibleLiner   GetCategories	enumeraterN   rO   colorDrawCoincidenceLinebandsrq   PlotGraphicsDraw)r   ri   lineslevelcatsir   rT   r   line	plotGraphs              r    rR   zPlotPanel.DrawCoincidencePlots   s)   S/0 	5G-3DOOG$*EELL//67??002D#D/ #344S9||~

A//udjj>QRT"# SLELL//67))%t}}W7MNIOOG$)))4+	5r!   c                     |j                   }|j                  }||f||fg}t        j                  t	        t
        |j                  d             }t        j                  ||d      S )zDraw line between band min and max values

        :param level: y coordinate of line
        :param color: class color
        :param bandValues: BandStatistics instance
        :   colourwidth)	minmaxr   Colourmapintsplitrq   PolyLine)r   r   r   
bandValuesminimmaximpointss          r    r   zPlotPanel.DrawCoincidenceLine   sY     %.5%.1		3sEKK$456}}VE;;r!   c                 ^    d|fd|fg}t        j                  |t        j                  d      S )z)Draw white line to achieve better marginsd   e   r(   r   )rq   r   r   WHITE)r   r   r   s      r    r   zPlotPanel.DrawInvisibleLine   s+    ,e-}}VBHHA>>r!   c                    g | _         t        t        | j                              D ]a  }| j                  |   j                          d| j                  |   _        | j                  |j                  |         }| j                  |j                  |         }| j                  |j                  |         }| j                  |j                  |         }| j                   j                  ||||g       | j                  |j                  |         }| j                  |j                  |         }t        j                   | j                   |   ||gz   | j                  |         }	| j                  |   j#                  |	       d y)z[Draw histograms for one class

        :param statistics: statistics for one class
        autor   r   N)histogramLinesrf   rg   r   r   rh   r   CreateHistogramLiner   
CreateMean	CreateMin	CreateMaxrv   CreateMaxRangeCreateMinRangerq   r   r   )
r   
statisticsri   histgramLinemeanLineminLinemaxLinemaxRangeLineminRangeLiner   s
             r    rQ   zPlotPanel.DrawHistograms   sk   
 !S/0 	5GOOG$**,-3DOOG$*33%++G4 4 L *2B2B72KLHnn
0@0@0InJGnn
0@0@0InJG&&h'QR..*:J:J7:S.TL..*:J:J7:S.TL))##G,l/KKmmG,I OOG$)))4-	5r!   c                     t        |j                        }|j                  }|df||fg}t        j                  |t
        j                  d      S Nr   r(   r   )r   historangeMinrq   r   r   RED)r   r   maxValrMinr   s        r    r   zPlotPanel.CreateMinRange  sF    Z%%&"")dF^,}}VBFF!<<r!   c                     t        |j                        }|j                  }|df||fg}t        j                  |t
        j                  d      S r   )r   r   rangeMaxrq   r   r   r   )r   r   r   rMaxr   s        r    r   zPlotPanel.CreateMaxRange  sF    Z%%&"")dF^,}}VBFF!<<r!   c                     t        |j                        }|j                  }|df||fg}t        j                  |t
        j                  d      S r   )r   r   meanrq   r   r   BLUE)r   r   r   r   r   s        r    r   zPlotPanel.CreateMean$  sD    Z%%&)dF^,}}VBGG1==r!   c                     t        |j                        }|j                  }|df||fg}t        j                  |t        j                  ddd      d      S Nr      r(   r   )r   r   r   rq   r   r   r   )r   r   r   r   r   s        r    r   zPlotPanel.CreateMin+  M    Z%%&!*ufo.}}VBIIc3,DANNr!   c                     t        |j                        }|j                   }|df||fg}t        j                  |t	        j
                  ddd      d      S r   )r   r   rq   r   r   r   )r   r   r   r   r   s        r    r   zPlotPanel.CreateMax2  r   r!   c                     g }t        |j                        D ]?  \  }}||j                  dz
  k  r||j                  dz   kD  r n|j	                  ||f       A t        j                  |t        j                  d      S )Nr6   r(   r   )	r   r   r   r   rv   rq   r   r   BLACK)r   r   r   cellCatcounts        r    r   zPlotPanel.CreateHistogramLine9  sw    '
(8(89 	,NGU!++!++MM7E*+	, }}VBHHA>>r!   c                    t        t        | j                              D ]  }| j                  |   j	                          | j                  |j                  |         }| j                  |j                  |         }t        j                  | j                  |   ||gz   | j                  |         }| j                  |   j                  |        y)zRedraw ranges lines in histograms when std dev multiplier changes

        :param statistics: python Statistics instance
        r   r   N)rf   rg   r   r   rh   r   r   r   rq   r   r   r   )r   r   ri   r   r   r   s         r    rV   zPlotPanel.UpdateRangesD  s    
 S/0 		5GOOG$**,..*:J:J7:S.TL..*:J:J7:S.TL))##G,l/KKmmG,I OOG$)))4		5r!   N)T)__name__
__module____qualname____doc__r   r%   r   r   r   r>   rW   r]   r`   rd   rP   rk   rz   r   r   rR   r   r   rQ   r   r   r   r   r   r   rV    r!   r    r   r      s    *,4	E%,B$')-
&,52<?
5<==>OO	?5r!   r   )r   r   wx.lib.plotlibrq   wx.lib.scrolledpanelscrolledpanelr
   	core.gcmdr   r   r   r   r!   r    <module>r      s2   " 
   ' ' y5&& y5r!   