<s> from dbconnect import DBConnect , UniqueImageClause , image_key_columns <EOL> from multiclasssql import filter_table_prefix <EOL> from properties import Properties <EOL> import datamodel <EOL> import guiutils as ui <EOL> from wx . combo import OwnerDrawnComboBox as ComboBox <EOL> import sqltools as sql <EOL> import imagetools <EOL> import logging <EOL> import numpy as np <EOL> import os <EOL> import sys <EOL> import re <EOL> import wx <EOL> import seaborn as sns <EOL> from matplotlib . figure import Figure <EOL> from matplotlib . backends . backend_wxagg import FigureCanvasWxAgg <EOL> from matplotlib . backends . backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar <EOL> from cpatool import CPATool <EOL> p = Properties . getInstance ( ) <EOL> db = DBConnect . getInstance ( ) <EOL> NO_GROUP = "<STR_LIT>" <EOL> ID_EXIT = wx . NewId ( ) <EOL> SELECT_MULTIPLE = '<STR_LIT>' <EOL> class DataSourcePanel ( wx . Panel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , figpanel , ** kwargs ) : <EOL> wx . Panel . __init__ ( self , parent , ** kwargs ) <EOL> self . SetBackgroundColour ( '<STR_LIT>' ) <EOL> self . figpanel = figpanel <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> self . x_columns = [ ] <EOL> self . table_choice = ui . TableComboBox ( self , - <NUM_LIT:1> , style = wx . CB_READONLY ) <EOL> self . x_choice = ComboBox ( self , - <NUM_LIT:1> , size = ( <NUM_LIT:200> , - <NUM_LIT:1> ) ) <EOL> self . x_multiple = wx . Button ( self , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . group_choice = ComboBox ( self , - <NUM_LIT:1> , choices = [ NO_GROUP ] + p . _groups_ordered , style = wx . CB_READONLY ) <EOL> self . group_choice . Select ( <NUM_LIT:0> ) <EOL> self . filter_choice = ui . FilterComboBox ( self , style = wx . CB_READONLY ) <EOL> self . filter_choice . Select ( <NUM_LIT:0> ) <EOL> self . update_chart_btn = wx . Button ( self , - <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . update_column_fields ( ) <EOL> sz = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> sz . Add ( wx . StaticText ( self , - <NUM_LIT:1> , "<STR_LIT>" ) , <NUM_LIT:0> , wx . TOP , <NUM_LIT:4> ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( self . table_choice , <NUM_LIT:1> , wx . EXPAND ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( wx . StaticText ( self , - <NUM_LIT:1> , "<STR_LIT>" ) , <NUM_LIT:0> , wx . TOP , <NUM_LIT:4> ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( self . x_choice , <NUM_LIT:2> , wx . EXPAND ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( self . x_multiple , <NUM_LIT:0> , wx . EXPAND | wx . TOP , <NUM_LIT:2> ) <EOL> sizer . Add ( sz , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . AddSpacer ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> sz = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> sz . Add ( wx . StaticText ( self , - <NUM_LIT:1> , "<STR_LIT>" ) , <NUM_LIT:0> , wx . TOP , <NUM_LIT:4> ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( self . group_choice , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . Add ( sz , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . AddSpacer ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> sz = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> sz . Add ( wx . StaticText ( self , - <NUM_LIT:1> , "<STR_LIT>" ) , <NUM_LIT:0> , wx . TOP , <NUM_LIT:4> ) <EOL> sz . AddSpacer ( ( <NUM_LIT:3> , - <NUM_LIT:1> ) ) <EOL> sz . Add ( self . filter_choice , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . Add ( sz , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . AddSpacer ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> sizer . Add ( self . update_chart_btn ) <EOL> wx . EVT_BUTTON ( self . x_multiple , - <NUM_LIT:1> , self . on_select_multiple ) <EOL> wx . EVT_COMBOBOX ( self . table_choice , - <NUM_LIT:1> , self . on_table_selected ) <EOL> wx . EVT_COMBOBOX ( self . x_choice , - <NUM_LIT:1> , self . on_column_selected ) <EOL> wx . EVT_BUTTON ( self . update_chart_btn , - <NUM_LIT:1> , self . update_figpanel ) <EOL> self . SetSizer ( sizer ) <EOL> self . Show ( <NUM_LIT:1> ) <EOL> def on_select_multiple ( self , evt ) : <EOL> tablename = self . table_choice . GetString ( self . table_choice . GetSelection ( ) ) <EOL> column_names = self . get_numeric_columns_from_table ( tablename ) <EOL> dlg = wx . MultiChoiceDialog ( self , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , column_names ) <EOL> dlg . SetSelections ( [ column_names . index ( v ) for v in self . x_columns ] ) <EOL> if ( dlg . ShowModal ( ) == wx . ID_OK ) : <EOL> self . x_choice . SetValue ( SELECT_MULTIPLE ) <EOL> self . x_columns = [ column_names [ i ] for i in dlg . GetSelections ( ) ] <EOL> self . group_choice . Disable ( ) <EOL> self . group_choice . SetStringSelection ( NO_GROUP ) <EOL> def on_table_selected ( self , evt ) : <EOL> table = self . table_choice . Value <EOL> if table == ui . TableComboBox . OTHER_TABLE : <EOL> t = ui . get_other_table_from_user ( self ) <EOL> if t is not None : <EOL> self . table_choice . Items = self . table_choice . Items [ : - <NUM_LIT:1> ] + [ t ] + self . table_choice . Items [ - <NUM_LIT:1> : ] <EOL> self . table_choice . Select ( self . table_choice . Items . index ( t ) ) <EOL> else : <EOL> self . table_choice . Select ( <NUM_LIT:0> ) <EOL> return <EOL> self . group_choice . Enable ( ) <EOL> self . x_columns = [ ] <EOL> self . update_column_fields ( ) <EOL> def on_column_selected ( self , evt ) : <EOL> self . group_choice . Enable ( ) <EOL> def update_column_fields ( self ) : <EOL> tablename = self . table_choice . GetString ( self . table_choice . GetSelection ( ) ) <EOL> fieldnames = self . get_numeric_columns_from_table ( tablename ) <EOL> self . x_choice . Clear ( ) <EOL> self . x_choice . AppendItems ( fieldnames ) <EOL> self . x_choice . SetSelection ( <NUM_LIT:0> ) <EOL> def get_numeric_columns_from_table ( self , table ) : <EOL> '''<STR_LIT>''' <EOL> measurements = db . GetColumnNames ( table ) <EOL> types = db . GetColumnTypes ( table ) <EOL> return [ m for m , t in zip ( measurements , types ) if t in [ float , int , long ] ] <EOL> def update_figpanel ( self , evt = None ) : <EOL> table = self . table_choice . Value <EOL> fltr = self . filter_choice . get_filter_or_none ( ) <EOL> grouping = self . group_choice . Value <EOL> if self . x_choice . Value == SELECT_MULTIPLE : <EOL> points_dict = { } <EOL> for col in self . x_columns : <EOL> pts = self . loadpoints ( table , col , fltr , NO_GROUP ) <EOL> for k in pts . keys ( ) : assert k not in points_dict . keys ( ) <EOL> points_dict . update ( pts ) <EOL> else : <EOL> col = self . x_choice . Value <EOL> points_dict = self . loadpoints ( table , col , fltr , grouping ) <EOL> if <NUM_LIT:100> >= len ( points_dict ) > <NUM_LIT> : <EOL> res = wx . MessageDialog ( self , '<STR_LIT>' <EOL> '<STR_LIT>' % ( len ( points_dict ) ) , <EOL> '<STR_LIT>' , style = wx . YES_NO | wx . NO_DEFAULT <EOL> ) . ShowModal ( ) <EOL> if res != wx . ID_YES : <EOL> return <EOL> elif len ( points_dict ) > <NUM_LIT:100> : <EOL> wx . MessageBox ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( len ( points_dict ) ) , '<STR_LIT>' ) <EOL> return <EOL> self . figpanel . setpoints ( points_dict ) <EOL> if self . group_choice . Value != NO_GROUP : <EOL> self . figpanel . set_x_axis_label ( grouping ) <EOL> self . figpanel . set_y_axis_label ( self . x_choice . Value ) <EOL> self . figpanel . draw ( ) <EOL> def loadpoints ( self , tablename , col , fltr = None , grouping = NO_GROUP ) : <EOL> '''<STR_LIT>''' <EOL> q = sql . QueryBuilder ( ) <EOL> select = [ sql . Column ( tablename , col ) ] <EOL> if grouping != NO_GROUP : <EOL> dm = datamodel . DataModel . getInstance ( ) <EOL> group_cols = dm . GetGroupColumnNames ( grouping , include_table_name = True ) <EOL> select += [ sql . Column ( * col . split ( '<STR_LIT:.>' ) ) for col in group_cols ] <EOL> q . set_select_clause ( select ) <EOL> if fltr is not None : <EOL> q . add_filter ( fltr ) <EOL> res = db . execute ( str ( q ) ) <EOL> res = np . array ( res , dtype = object ) <EOL> for row in res : <EOL> if row [ <NUM_LIT:0> ] is None : <EOL> row [ <NUM_LIT:0> ] = np . nan <EOL> points_dict = { } <EOL> if self . group_choice . Value != NO_GROUP : <EOL> for row in res : <EOL> groupkey = tuple ( row [ <NUM_LIT:1> : ] ) <EOL> points_dict [ groupkey ] = points_dict . get ( groupkey , [ ] ) + [ row [ <NUM_LIT:0> ] ] <EOL> else : <EOL> points_dict = { col : [ r [ <NUM_LIT:0> ] for r in res ] } <EOL> return points_dict <EOL> def save_settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . x_choice . Value == SELECT_MULTIPLE : <EOL> cols = self . x_columns <EOL> else : <EOL> cols = [ self . x_choice . GetString ( self . x_choice . GetSelection ( ) ) ] <EOL> return { '<STR_LIT>' : self . table_choice . Value , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( cols ) , <EOL> '<STR_LIT>' : self . filter_choice . Value , <EOL> '<STR_LIT>' : self . figpanel . subplot . get_xlim ( ) , <EOL> '<STR_LIT>' : self . figpanel . subplot . get_ylim ( ) , <EOL> } <EOL> def load_settings ( self , settings ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in settings : <EOL> self . table_choice . SetStringSelection ( settings [ '<STR_LIT>' ] ) <EOL> self . update_column_fields ( ) <EOL> if '<STR_LIT>' in settings : <EOL> cols = map ( str . strip , settings [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> if len ( cols ) == <NUM_LIT:1> : <EOL> self . x_choice . SetStringSelection ( cols [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . x_choice . SetValue ( SELECT_MULTIPLE ) <EOL> self . x_columns = cols <EOL> if '<STR_LIT>' in settings : <EOL> self . filter_choice . SetStringSelection ( settings [ '<STR_LIT>' ] ) <EOL> self . update_figpanel ( ) <EOL> if '<STR_LIT>' in settings : <EOL> self . figpanel . subplot . set_xlim ( eval ( settings [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in settings : <EOL> self . figpanel . subplot . set_ylim ( eval ( settings [ '<STR_LIT>' ] ) ) <EOL> self . figpanel . draw ( ) <EOL> class BoxPlotPanel ( FigureCanvasWxAgg ) : <EOL> def __init__ ( self , parent , points , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> self . figure = Figure ( ) <EOL> FigureCanvasWxAgg . __init__ ( self , parent , - <NUM_LIT:1> , self . figure , ** kwargs ) <EOL> self . canvas = self . figure . canvas <EOL> self . SetMinSize ( ( <NUM_LIT:100> , <NUM_LIT:100> ) ) <EOL> self . figure . set_facecolor ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . figure . set_edgecolor ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . canvas . SetBackgroundColour ( '<STR_LIT>' ) <EOL> self . navtoolbar = None <EOL> self . setpoints ( points ) <EOL> def setpoints ( self , points ) : <EOL> '''<STR_LIT>''' <EOL> self . xlabels = [ ] <EOL> self . points = [ ] <EOL> ignored = <NUM_LIT:0> <EOL> for label , values in sorted ( points . items ( ) ) : <EOL> if type ( label ) in [ tuple , list ] : <EOL> self . xlabels += [ '<STR_LIT:U+002C>' . join ( [ str ( l ) for l in label ] ) ] <EOL> else : <EOL> self . xlabels += [ label ] <EOL> self . points += [ np . array ( values ) . astype ( '<STR_LIT:f>' ) [ ~ np . isnan ( values ) ] ] <EOL> ignored += len ( np . array ( values ) [ np . isnan ( values ) ] ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . subplot = self . figure . add_subplot ( <NUM_LIT> ) <EOL> self . subplot . clear ( ) <EOL> if len ( self . points ) == <NUM_LIT:0> : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> return <EOL> self . subplot . boxplot ( self . points , sym = '<STR_LIT>' ) <EOL> if len ( self . points ) > <NUM_LIT:1> : <EOL> self . figure . autofmt_xdate ( ) <EOL> self . subplot . set_xticklabels ( self . xlabels ) <EOL> self . reset_toolbar ( ) <EOL> if ignored == <NUM_LIT:0> : <EOL> logging . info ( '<STR_LIT>' % ( sum ( map ( len , self . points ) ) ) ) <EOL> else : <EOL> logging . warn ( '<STR_LIT>' <EOL> % ( sum ( map ( len , self . points ) ) , ignored ) ) <EOL> def set_x_axis_label ( self , label ) : <EOL> self . subplot . set_xlabel ( label ) <EOL> def set_y_axis_label ( self , label ) : <EOL> self . subplot . set_ylabel ( label ) <EOL> def get_point_lists ( self ) : <EOL> return self . points <EOL> def get_xlabels ( self ) : <EOL> return self . xlabels <EOL> def get_toolbar ( self ) : <EOL> if not self . navtoolbar : <EOL> self . navtoolbar = NavigationToolbar ( self . canvas ) <EOL> self . navtoolbar . DeleteToolByPos ( <NUM_LIT:6> ) <EOL> return self . navtoolbar <EOL> def reset_toolbar ( self ) : <EOL> if self . navtoolbar : <EOL> self . navtoolbar . _views . clear ( ) <EOL> self . navtoolbar . _positions . clear ( ) <EOL> self . navtoolbar . push_current ( ) <EOL> class BoxPlot ( wx . Frame , CPATool ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , size = ( <NUM_LIT> , <NUM_LIT> ) , ** kwargs ) : <EOL> wx . Frame . __init__ ( self , parent , - <NUM_LIT:1> , size = size , title = '<STR_LIT>' , ** kwargs ) <EOL> CPATool . __init__ ( self ) <EOL> self . SetName ( self . tool_name ) <EOL> self . SetBackgroundColour ( "<STR_LIT>" ) <EOL> points = { } <EOL> figpanel = BoxPlotPanel ( self , points ) <EOL> configpanel = DataSourcePanel ( self , figpanel ) <EOL> self . SetToolBar ( figpanel . get_toolbar ( ) ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sizer . Add ( figpanel , <NUM_LIT:1> , wx . EXPAND ) <EOL> sizer . Add ( configpanel , <NUM_LIT:0> , wx . EXPAND | wx . ALL , <NUM_LIT:5> ) <EOL> self . SetSizer ( sizer ) <EOL> self . save_settings = configpanel . save_settings <EOL> self . load_settings = configpanel . load_settings <EOL> def SetToolBar ( self , toolbar ) : <EOL> from matplotlib . backends . backend_wx import _load_bitmap <EOL> toolbar . Hide ( ) <EOL> tb = self . CreateToolBar ( ( wx . TB_HORIZONTAL | wx . TB_TEXT ) ) <EOL> _NTB2_HOME = wx . NewId ( ) <EOL> _NTB2_BACK = wx . NewId ( ) <EOL> _NTB2_FORWARD = wx . NewId ( ) <EOL> _NTB2_PAN = wx . NewId ( ) <EOL> _NTB2_ZOOM = wx . NewId ( ) <EOL> _NTB2_SAVE = wx . NewId ( ) <EOL> _NTB2_SUBPLOT = wx . NewId ( ) <EOL> tb . AddSimpleTool ( _NTB2_HOME , _load_bitmap ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tb . AddSimpleTool ( _NTB2_BACK , _load_bitmap ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tb . AddSimpleTool ( _NTB2_FORWARD , _load_bitmap ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tb . AddCheckTool ( _NTB2_PAN , _load_bitmap ( '<STR_LIT>' ) , shortHelp = '<STR_LIT>' , longHelp = '<STR_LIT>' ) <EOL> tb . AddCheckTool ( _NTB2_ZOOM , _load_bitmap ( '<STR_LIT>' ) , shortHelp = '<STR_LIT>' , longHelp = '<STR_LIT>' ) <EOL> tb . AddSeparator ( ) <EOL> tb . AddSimpleTool ( _NTB2_SUBPLOT , _load_bitmap ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tb . AddSimpleTool ( _NTB2_SAVE , _load_bitmap ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . home , id = _NTB2_HOME ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . forward , id = _NTB2_FORWARD ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . back , id = _NTB2_BACK ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . zoom , id = _NTB2_ZOOM ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . pan , id = _NTB2_PAN ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . configure_subplots , id = _NTB2_SUBPLOT ) <EOL> self . Bind ( wx . EVT_TOOL , toolbar . save_figure , id = _NTB2_SAVE ) <EOL> tb . Realize ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> app = wx . PySimpleApp ( ) <EOL> logging . basicConfig ( level = logging . DEBUG , ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> p = Properties . getInstance ( ) <EOL> p . LoadFile ( sys . argv [ <NUM_LIT:1> ] ) <EOL> elif not p . show_load_dialog ( ) : <EOL> print '<STR_LIT>' <EOL> wx . GetApp ( ) . Exit ( ) <EOL> sys . exit ( ) <EOL> afraser <EOL> boxplot = BoxPlot ( None ) <EOL> boxplot . Show ( ) <EOL> app . MainLoop ( ) <EOL> try : <EOL> import javabridge <EOL> javabridge . kill_vm ( ) <EOL> except : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> print "<STR_LIT>" </s>
