<s> """<STR_LIT>""" <EOL> import sys <EOL> try : <EOL> import sqlite3 <EOL> except ImportError : <EOL> import logging <EOL> logging . error ( '<STR_LIT>' ) <EOL> import time <EOL> from cPickle import dumps , loads , HIGHEST_PROTOCOL , UnpicklingError <EOL> from optparse import OptionParser <EOL> from traits . trait_handlers import TraitListObject , TraitDictObject <EOL> from openmdao . main . interfaces import implements , ICaseRecorder , ICaseIterator <EOL> from openmdao . main . case import Case <EOL> from openmdao . lib . casehandlers . util import driver_map <EOL> _casetable_attrs = set ( [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> _vartable_attrs = set ( [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:value>' ] ) <EOL> def _query_split ( query ) : <EOL> """<STR_LIT>""" <EOL> for op in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:<>' , '<STR_LIT:>>' , '<STR_LIT:=>' ] : <EOL> parts = query . split ( op , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> return ( parts [ <NUM_LIT:0> ] . strip ( ) , op , parts [ <NUM_LIT:1> ] . strip ( ) ) <EOL> raise ValueError ( "<STR_LIT>" % query ) <EOL> class DBCaseIterator ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ICaseIterator ) <EOL> def __init__ ( self , dbfile = '<STR_LIT>' , selectors = None , connection = None ) : <EOL> if connection is not None : <EOL> self . _dbfile = dbfile <EOL> self . _connection = connection <EOL> else : <EOL> self . _connection = None <EOL> self . dbfile = dbfile <EOL> self . selectors = selectors <EOL> self . _connection . text_factory = sqlite3 . OptimizedUnicode <EOL> @ property <EOL> def dbfile ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dbfile <EOL> @ dbfile . setter <EOL> def dbfile ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _dbfile = value <EOL> if self . _connection : <EOL> self . _connection . close ( ) <EOL> self . _connection = sqlite3 . connect ( value ) <EOL> def __iter__ ( self ) : <EOL> return self . _next_case ( ) <EOL> def _next_case ( self ) : <EOL> """<STR_LIT>""" <EOL> sql = [ "<STR_LIT>" ] <EOL> if self . selectors is not None : <EOL> for sel in self . selectors : <EOL> rhs , rel , lhs = _query_split ( sel ) <EOL> if rhs in _casetable_attrs : <EOL> if len ( sql ) == <NUM_LIT:1> : <EOL> sql . append ( "<STR_LIT>" % ( rhs , rel , lhs ) ) <EOL> else : <EOL> sql . append ( "<STR_LIT>" % ( rhs , rel , lhs ) ) <EOL> casecur = self . _connection . cursor ( ) <EOL> casecur . execute ( '<STR_LIT:U+0020>' . join ( sql ) ) <EOL> sql = [ '<STR_LIT>' ] <EOL> if self . selectors is not None : <EOL> for sel in self . selectors : <EOL> rhs , rel , lhs = _query_split ( sel ) <EOL> if rhs in _vartable_attrs : <EOL> sql . append ( "<STR_LIT>" % ( rhs , rel , lhs ) ) <EOL> combined = '<STR_LIT:U+0020>' . join ( sql ) <EOL> varcur = self . _connection . cursor ( ) <EOL> for cid , text_id , parent , msg , model_id , timeEnter in casecur : <EOL> varcur . execute ( combined % cid ) <EOL> inputs = [ ] <EOL> outputs = [ ] <EOL> for var_id , vname , case_id , sense , value in varcur : <EOL> if isinstance ( value , ( float , int , str ) ) : <EOL> pass <EOL> elif value is None : <EOL> value = float ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> value = loads ( str ( value ) ) <EOL> except UnpicklingError as err : <EOL> print '<STR_LIT:value>' , type ( value ) , repr ( value ) <EOL> raise UnpicklingError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( vname , text_id , str ( err ) ) ) <EOL> if sense == '<STR_LIT:i>' : <EOL> inputs . append ( ( vname , value ) ) <EOL> elif sense == '<STR_LIT:o>' : <EOL> outputs . append ( ( vname , value ) ) <EOL> if len ( inputs ) > <NUM_LIT:0> or len ( outputs ) > <NUM_LIT:0> : <EOL> exc = Exception ( msg ) if msg else None <EOL> yield Case ( inputs = inputs , outputs = outputs , exc = exc , <EOL> case_uuid = text_id , parent_uuid = parent ) <EOL> class DBCaseRecorder ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ICaseRecorder ) <EOL> def __init__ ( self , dbfile = '<STR_LIT>' , model_id = '<STR_LIT>' , append = False ) : <EOL> self . dbfile = dbfile <EOL> self . model_id = model_id <EOL> self . _cfg_map = { } <EOL> if append : <EOL> exstr = '<STR_LIT>' <EOL> else : <EOL> exstr = '<STR_LIT>' <EOL> self . _connection . execute ( """<STR_LIT>""" % exstr ) <EOL> self . _connection . execute ( """<STR_LIT>""" % exstr ) <EOL> @ property <EOL> def dbfile ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dbfile <EOL> @ dbfile . setter <EOL> def dbfile ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _dbfile = value <EOL> self . _connection = sqlite3 . connect ( value ) <EOL> self . _iter_conn = sqlite3 . connect ( value ) <EOL> def startup ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def register ( self , driver , inputs , outputs ) : <EOL> """<STR_LIT>""" <EOL> self . _cfg_map [ driver ] = driver_map ( driver , inputs , outputs ) <EOL> def record_constants ( self , constants ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def record ( self , driver , inputs , outputs , exc , case_uuid , parent_uuid ) : <EOL> """<STR_LIT>""" <EOL> if self . _connection is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> cur = self . _connection . cursor ( ) <EOL> msg = '<STR_LIT>' if exc is None else str ( exc ) <EOL> cur . execute ( """<STR_LIT>""" , <EOL> ( None , case_uuid , parent_uuid , msg , self . model_id ) ) <EOL> case_id = cur . lastrowid <EOL> v = ( None , '<STR_LIT>' , case_id , None , time . time ( ) ) <EOL> cur . execute ( "<STR_LIT>" , <EOL> v ) <EOL> in_names , out_names = self . _cfg_map [ driver ] <EOL> for name , value in zip ( in_names , inputs ) : <EOL> if isinstance ( value , ( float , int , str ) ) : <EOL> v = ( None , name , case_id , '<STR_LIT:i>' , value ) <EOL> else : <EOL> if isinstance ( value , TraitDictObject ) : <EOL> value = dict ( value ) <EOL> elif isinstance ( value , TraitListObject ) : <EOL> value = list ( value ) <EOL> v = ( None , name , case_id , '<STR_LIT:i>' , <EOL> sqlite3 . Binary ( dumps ( value , HIGHEST_PROTOCOL ) ) ) <EOL> cur . execute ( "<STR_LIT>" , <EOL> v ) <EOL> for name , value in zip ( out_names , outputs ) : <EOL> if isinstance ( value , ( float , int , str ) ) : <EOL> v = ( None , name , case_id , '<STR_LIT:o>' , value ) <EOL> else : <EOL> if isinstance ( value , TraitDictObject ) : <EOL> value = dict ( value ) <EOL> elif isinstance ( value , TraitListObject ) : <EOL> value = list ( value ) <EOL> v = ( None , name , case_id , '<STR_LIT:o>' , <EOL> sqlite3 . Binary ( dumps ( value , HIGHEST_PROTOCOL ) ) ) <EOL> cur . execute ( "<STR_LIT>" , <EOL> v ) <EOL> self . _connection . commit ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _connection is not None and self . _dbfile != '<STR_LIT>' : <EOL> self . _connection . commit ( ) <EOL> self . _connection . close ( ) <EOL> self . _connection = None <EOL> def get_iterator ( self ) : <EOL> """<STR_LIT>""" <EOL> return DBCaseIterator ( dbfile = self . _dbfile , connection = self . _connection ) <EOL> """<STR_LIT>""" <EOL> def list_db_vars ( dbname ) : <EOL> """<STR_LIT>""" <EOL> connection = sqlite3 . connect ( dbname ) <EOL> varcur = connection . cursor ( ) <EOL> varcur . execute ( "<STR_LIT>" ) <EOL> varnames = set ( [ v for v in varcur ] ) <EOL> return varnames <EOL> def case_db_to_dict ( dbname , varnames , case_sql = '<STR_LIT>' , var_sql = '<STR_LIT>' , include_errors = False ) : <EOL> """<STR_LIT>""" <EOL> connection = sqlite3 . connect ( dbname ) <EOL> vardict = dict ( [ ( name , [ ] ) for name in varnames ] ) <EOL> sql = [ "<STR_LIT>" ] <EOL> qlist = [ ] <EOL> if case_sql : <EOL> qlist . append ( case_sql ) <EOL> if not include_errors : <EOL> qlist . append ( "<STR_LIT>" ) <EOL> if qlist : <EOL> sql . append ( "<STR_LIT>" % '<STR_LIT>' . join ( qlist ) ) <EOL> casecur = connection . cursor ( ) <EOL> casecur . execute ( '<STR_LIT:U+0020>' . join ( sql ) ) <EOL> sql = [ "<STR_LIT>" ] <EOL> vars_added = False <EOL> for i , name in enumerate ( vardict . keys ( ) ) : <EOL> if i == <NUM_LIT:0> : <EOL> sql . append ( "<STR_LIT>" ) <EOL> else : <EOL> sql . append ( "<STR_LIT>" ) <EOL> sql . append ( "<STR_LIT>" % name ) <EOL> vars_added = True <EOL> if vars_added : <EOL> sql . append ( "<STR_LIT:)>" ) <EOL> if var_sql : <EOL> sql . append ( "<STR_LIT>" % var_sql ) <EOL> combined = '<STR_LIT:U+0020>' . join ( sql ) <EOL> varcur = connection . cursor ( ) <EOL> for case_id in casecur : <EOL> casedict = { } <EOL> varcur . execute ( combined % case_id ) <EOL> for vname , value in varcur : <EOL> if not isinstance ( value , ( float , int , str ) ) : <EOL> try : <EOL> value = loads ( str ( value ) ) <EOL> except UnpicklingError as err : <EOL> raise UnpicklingError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( vname , str ( err ) ) ) <EOL> casedict [ vname ] = value <EOL> if len ( casedict ) != len ( vardict ) : <EOL> continue <EOL> for name , value in casedict . items ( ) : <EOL> vardict [ name ] . append ( value ) <EOL> return vardict <EOL> def _get_lines ( dbname , xnames , ynames , case_sql = None , var_sql = None ) : <EOL> """<STR_LIT>""" <EOL> vardict = case_db_to_dict ( dbname , xnames + ynames , case_sql , var_sql ) <EOL> lines = [ ] <EOL> yvals = [ ] <EOL> xvals = [ ] <EOL> for i , name in enumerate ( ynames ) : <EOL> yvals . append ( vardict [ name ] ) <EOL> if len ( xnames ) == <NUM_LIT:0> : <EOL> xvals . append ( range ( len ( vardict [ name ] ) ) ) <EOL> elif len ( xnames ) == <NUM_LIT:1> : <EOL> xvals . append ( vardict [ xnames [ <NUM_LIT:0> ] ] ) <EOL> else : <EOL> xvals . append ( vardict [ xnames [ i ] ] ) <EOL> for xdata , ydata in zip ( xvals , yvals ) : <EOL> lines . append ( ( xdata , ydata ) ) <EOL> return lines <EOL> def displayXY ( dbname , xnames , ynames , case_sql = None , var_sql = None , <EOL> title = '<STR_LIT>' , grid = False , xlabel = '<STR_LIT>' , ylabel = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if '<STR_LIT>' not in sys . modules : <EOL> import matplotlib <EOL> if sys . platform == '<STR_LIT>' : <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> import wx <EOL> except ImportError : <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> else : <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> import matplotlib . pyplot as plt <EOL> except ImportError : <EOL> print '<STR_LIT>' <EOL> return <EOL> fig = plt . figure ( ) <EOL> fig . add_subplot ( <NUM_LIT> ) <EOL> for i , line in enumerate ( _get_lines ( dbname , xnames , ynames , case_sql , var_sql ) ) : <EOL> args = [ ] <EOL> kwargs = { } <EOL> args . append ( line [ <NUM_LIT:0> ] ) <EOL> args . append ( line [ <NUM_LIT:1> ] ) <EOL> kwargs [ '<STR_LIT:label>' ] = '<STR_LIT:%s>' % ynames [ i ] <EOL> plt . plot ( * args , ** kwargs ) <EOL> if grid : <EOL> plt . grid ( True ) <EOL> if xlabel : <EOL> plt . xlabel ( xlabel ) <EOL> if ylabel : <EOL> plt . ylabel ( ylabel ) <EOL> if title : <EOL> plt . title ( title ) <EOL> plt . legend ( ) <EOL> plt . show ( ) <EOL> def cmdlineXYplot ( ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT:title>" , <EOL> help = "<STR_LIT>" , ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if options . listvars : <EOL> print <EOL> for name in sorted ( list_db_vars ( options . dbname ) ) : <EOL> print name <EOL> print <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if len ( args ) > <NUM_LIT:0> or not options . ynames or not options . dbname : <EOL> parser . print_help ( ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> if options . xnames : <EOL> xs = options . xnames . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> xs = [ ] <EOL> ys = options . ynames . split ( '<STR_LIT:U+002C>' ) <EOL> if len ( xs ) > <NUM_LIT:1> and len ( xs ) != len ( ys ) : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> displayXY ( options . dbname , xs , ys , options . case_sql , options . var_sql , <EOL> title = options . title , grid = options . grid , xlabel = options . xlabel , <EOL> ylabel = options . ylabel ) </s>
