<s> from ConfigParser import ConfigParser <EOL> from multiprocessing import Process , Pool , cpu_count <EOL> from numpy import * <EOL> import os , sys , time , itertools , re , optparse , types <EOL> def mp_runrep ( args ) : <EOL> """<STR_LIT>""" <EOL> return PyExperimentSuite . run_rep ( * args ) <EOL> def progress ( params , rep ) : <EOL> """<STR_LIT>""" <EOL> name = params [ '<STR_LIT:name>' ] <EOL> fullpath = os . path . join ( params [ '<STR_LIT:path>' ] , params [ '<STR_LIT:name>' ] ) <EOL> logname = os . path . join ( fullpath , '<STR_LIT>' % rep ) <EOL> if os . path . exists ( logname ) : <EOL> logfile = open ( logname , '<STR_LIT:r>' ) <EOL> lines = logfile . readlines ( ) <EOL> logfile . close ( ) <EOL> return int ( <NUM_LIT:100> * len ( lines ) / params [ '<STR_LIT>' ] ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def convert_param_to_dirname ( param ) : <EOL> """<STR_LIT>""" <EOL> if type ( param ) == types . StringType : <EOL> return param <EOL> else : <EOL> return re . sub ( "<STR_LIT>" , '<STR_LIT:0>' , '<STR_LIT>' % param ) <EOL> class PyExperimentSuite ( object ) : <EOL> restore_supported = False <EOL> def __init__ ( self ) : <EOL> self . parse_opt ( ) <EOL> self . parse_cfg ( ) <EOL> self . key_warning_issued = [ ] <EOL> def parse_opt ( self ) : <EOL> """<STR_LIT>""" <EOL> optparser = optparse . OptionParser ( ) <EOL> optparser . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , dest = '<STR_LIT>' , type = '<STR_LIT:string>' , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , dest = '<STR_LIT>' , type = '<STR_LIT:int>' , default = cpu_count ( ) , <EOL> help = "<STR_LIT>" % cpu_count ( ) ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , dest = '<STR_LIT>' , type = '<STR_LIT:string>' , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = "<STR_LIT>" ) <EOL> options , args = optparser . parse_args ( ) <EOL> self . options = options <EOL> return options , args <EOL> def parse_cfg ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cfgparser = ConfigParser ( ) <EOL> if not self . cfgparser . read ( self . options . config ) : <EOL> raise SystemExit ( '<STR_LIT>' % self . options . config ) <EOL> def mkdir ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> def get_exps ( self , path = '<STR_LIT:.>' ) : <EOL> """<STR_LIT>""" <EOL> exps = [ ] <EOL> for dp , dn , fn in os . walk ( path ) : <EOL> if '<STR_LIT>' in fn : <EOL> subdirs = [ os . path . join ( dp , d ) for d in os . listdir ( dp ) if os . path . isdir ( os . path . join ( dp , d ) ) ] <EOL> if all ( map ( lambda s : self . get_exps ( s ) == [ ] , subdirs ) ) : <EOL> exps . append ( dp ) <EOL> return exps <EOL> def items_to_params ( self , items ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> for t , v in items : <EOL> try : <EOL> if v in [ '<STR_LIT>' , '<STR_LIT:list>' ] : <EOL> params [ t ] = v <EOL> else : <EOL> params [ t ] = eval ( v ) <EOL> if isinstance ( params [ t ] , ndarray ) : <EOL> params [ t ] = params [ t ] . tolist ( ) <EOL> except ( NameError , SyntaxError ) : <EOL> params [ t ] = v <EOL> return params <EOL> def get_params ( self , exp , cfgname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cfgp = ConfigParser ( ) <EOL> cfgp . read ( os . path . join ( exp , cfgname ) ) <EOL> section = cfgp . sections ( ) [ <NUM_LIT:0> ] <EOL> params = self . items_to_params ( cfgp . items ( section ) ) <EOL> params [ '<STR_LIT:name>' ] = section <EOL> return params <EOL> def get_exp ( self , name , path = '<STR_LIT:.>' ) : <EOL> """<STR_LIT>""" <EOL> exps = [ ] <EOL> for dp , dn , df in os . walk ( path ) : <EOL> if '<STR_LIT>' in df : <EOL> cfgp = ConfigParser ( ) <EOL> cfgp . read ( os . path . join ( dp , '<STR_LIT>' ) ) <EOL> if name in cfgp . sections ( ) : <EOL> exps . append ( dp ) <EOL> return exps <EOL> def write_config_file ( self , params , path ) : <EOL> """<STR_LIT>""" <EOL> cfgp = ConfigParser ( ) <EOL> cfgp . add_section ( params [ '<STR_LIT:name>' ] ) <EOL> for p in params : <EOL> if p == '<STR_LIT:name>' : <EOL> continue <EOL> cfgp . set ( params [ '<STR_LIT:name>' ] , p , params [ p ] ) <EOL> f = open ( os . path . join ( path , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> cfgp . write ( f ) <EOL> f . close ( ) <EOL> def get_history ( self , exp , rep , tags ) : <EOL> """<STR_LIT>""" <EOL> params = self . get_params ( exp ) <EOL> if params == None : <EOL> raise SystemExit ( '<STR_LIT>' % exp ) <EOL> if tags != '<STR_LIT:all>' and not hasattr ( tags , '<STR_LIT>' ) : <EOL> tags = [ tags ] <EOL> results = { } <EOL> logfile = os . path . join ( exp , '<STR_LIT>' % rep ) <EOL> try : <EOL> f = open ( logfile ) <EOL> except IOError : <EOL> if len ( tags ) == <NUM_LIT:1> : <EOL> return [ ] <EOL> else : <EOL> return { } <EOL> for line in f : <EOL> pairs = line . split ( ) <EOL> for pair in pairs : <EOL> tag , val = pair . split ( '<STR_LIT::>' ) <EOL> if tags == '<STR_LIT:all>' or tag in tags : <EOL> if not tag in results : <EOL> try : <EOL> results [ tag ] = [ eval ( val ) ] <EOL> except ( NameError , SyntaxError ) : <EOL> results [ tag ] = [ val ] <EOL> else : <EOL> try : <EOL> results [ tag ] . append ( eval ( val ) ) <EOL> except ( NameError , SyntaxError ) : <EOL> results [ tag ] . append ( val ) <EOL> f . close ( ) <EOL> if len ( results ) == <NUM_LIT:0> : <EOL> if len ( tags ) == <NUM_LIT:1> : <EOL> return [ ] <EOL> else : <EOL> return { } <EOL> if len ( tags ) == <NUM_LIT:1> : <EOL> return results [ results . keys ( ) [ <NUM_LIT:0> ] ] <EOL> else : <EOL> return results <EOL> def get_history_tags ( self , exp , rep = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> history = self . get_history ( exp , rep , '<STR_LIT:all>' ) <EOL> return history . keys ( ) <EOL> def get_value ( self , exp , rep , tags , which = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> history = self . get_history ( exp , rep , tags ) <EOL> if len ( history ) == <NUM_LIT:0> : <EOL> return None <EOL> if type ( history ) == dict : <EOL> for h in history : <EOL> if which == '<STR_LIT>' : <EOL> history [ h ] = history [ h ] [ - <NUM_LIT:1> ] <EOL> if which == '<STR_LIT>' : <EOL> history [ h ] = min ( history [ h ] ) <EOL> if which == '<STR_LIT>' : <EOL> history [ h ] = max ( history [ h ] ) <EOL> if type ( which ) == int : <EOL> history [ h ] = history [ h ] [ which ] <EOL> return history <EOL> else : <EOL> if which == '<STR_LIT>' : <EOL> return history [ - <NUM_LIT:1> ] <EOL> if which == '<STR_LIT>' : <EOL> return min ( history ) <EOL> if which == '<STR_LIT>' : <EOL> return max ( history ) <EOL> if type ( which ) == int : <EOL> return history [ which ] <EOL> else : <EOL> return None <EOL> def get_values_fix_params ( self , exp , rep , tag , which = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> subexps = self . get_exps ( exp ) <EOL> tagvalues = [ '<STR_LIT>' % ( k , convert_param_to_dirname ( kwargs [ k ] ) ) for k in kwargs ] <EOL> values = [ self . get_value ( se , rep , tag , which ) for se in subexps if all ( map ( lambda tv : tv in se , tagvalues ) ) ] <EOL> params = [ self . get_params ( se ) for se in subexps if all ( map ( lambda tv : tv in se , tagvalues ) ) ] <EOL> return values , params <EOL> def get_histories_fix_params ( self , exp , rep , tag , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> subexps = self . get_exps ( exp ) <EOL> tagvalues = [ re . sub ( "<STR_LIT>" , '<STR_LIT:0>' , '<STR_LIT>' % ( k , kwargs [ k ] ) ) for k in kwargs ] <EOL> histories = [ self . get_history ( se , rep , tag ) for se in subexps if all ( map ( lambda tv : tv in se , tagvalues ) ) ] <EOL> params = [ self . get_params ( se ) for se in subexps if all ( map ( lambda tv : tv in se , tagvalues ) ) ] <EOL> return histories , params <EOL> def get_histories_over_repetitions ( self , exp , tags , aggregate ) : <EOL> """<STR_LIT>""" <EOL> params = self . get_params ( exp ) <EOL> if tags == '<STR_LIT:all>' : <EOL> tags = self . get_history ( exp , <NUM_LIT:0> , '<STR_LIT:all>' ) . keys ( ) <EOL> if not hasattr ( tags , '<STR_LIT>' ) : <EOL> tags = [ tags ] <EOL> results = { } <EOL> for tag in tags : <EOL> histories = zeros ( ( params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] ) ) <EOL> skipped = [ ] <EOL> for i in range ( params [ '<STR_LIT>' ] ) : <EOL> try : <EOL> histories [ i , : ] = self . get_history ( exp , i , tag ) <EOL> except ValueError : <EOL> h = self . get_history ( exp , i , tag ) <EOL> if len ( h ) == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' % ( i , params [ '<STR_LIT>' ] ) ) <EOL> skipped . append ( i ) <EOL> elif len ( h ) > params [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT>' % ( i , len ( h ) , params [ '<STR_LIT>' ] ) ) <EOL> h = h [ : params [ '<STR_LIT>' ] ] <EOL> histories [ i , : ] = h <EOL> elif len ( h ) < params [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT>' % ( i , len ( h ) , params [ '<STR_LIT>' ] ) ) <EOL> params [ '<STR_LIT>' ] = len ( h ) <EOL> histories = histories [ : , : params [ '<STR_LIT>' ] ] <EOL> histories [ i , : ] = h <EOL> histories = delete ( histories , skipped , axis = <NUM_LIT:0> ) <EOL> params [ '<STR_LIT>' ] -= len ( skipped ) <EOL> aggregated = zeros ( params [ '<STR_LIT>' ] ) <EOL> for i in range ( params [ '<STR_LIT>' ] ) : <EOL> aggregated [ i ] = aggregate ( histories [ : , i ] ) <EOL> if len ( tags ) == <NUM_LIT:1> : <EOL> return aggregated <EOL> else : <EOL> results [ tag ] = aggregated <EOL> return results <EOL> def browse ( self ) : <EOL> """<STR_LIT>""" <EOL> for d in self . get_exps ( '<STR_LIT:.>' ) : <EOL> params = self . get_params ( d ) <EOL> name = params [ '<STR_LIT:name>' ] <EOL> basename = name . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> if self . options . experiments and basename not in self . options . experiments : <EOL> continue <EOL> fullpath = os . path . join ( params [ '<STR_LIT:path>' ] , name ) <EOL> prog = <NUM_LIT:0> <EOL> for i in range ( params [ '<STR_LIT>' ] ) : <EOL> prog += progress ( params , i ) <EOL> prog /= params [ '<STR_LIT>' ] <EOL> if self . options . progress : <EOL> bar = "<STR_LIT:[>" <EOL> bar += "<STR_LIT:=>" * int ( prog / <NUM_LIT:4> ) <EOL> bar += "<STR_LIT:U+0020>" * int ( <NUM_LIT> - prog / <NUM_LIT:4> ) <EOL> bar += "<STR_LIT:]>" <EOL> print '<STR_LIT>' % ( prog , bar , d ) <EOL> continue <EOL> print '<STR_LIT>' % ( '<STR_LIT>' , d ) <EOL> try : <EOL> minfile = min ( <EOL> ( os . path . join ( dirname , filename ) <EOL> for dirname , dirnames , filenames in os . walk ( fullpath ) <EOL> for filename in filenames <EOL> if filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) , <EOL> key = lambda fn : os . stat ( fn ) . st_mtime ) <EOL> maxfile = max ( <EOL> ( os . path . join ( dirname , filename ) <EOL> for dirname , dirnames , filenames in os . walk ( fullpath ) <EOL> for filename in filenames <EOL> if filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) , <EOL> key = lambda fn : os . stat ( fn ) . st_mtime ) <EOL> except ValueError : <EOL> print '<STR_LIT>' % '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' % time . strftime ( '<STR_LIT>' , time . localtime ( os . stat ( minfile ) . st_mtime ) ) <EOL> print '<STR_LIT>' % time . strftime ( '<STR_LIT>' , time . localtime ( os . stat ( maxfile ) . st_mtime ) ) <EOL> for k in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> print '<STR_LIT>' % ( k , params [ k ] ) <EOL> print '<STR_LIT>' % ( '<STR_LIT>' , prog ) <EOL> if self . options . browse_big : <EOL> for p in [ p for p in params if p not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT:name>' ) ] : <EOL> print '<STR_LIT>' % ( p , params [ p ] ) <EOL> print <EOL> def expand_param_list ( self , paramlist ) : <EOL> """<STR_LIT>""" <EOL> if type ( paramlist ) == types . DictType : <EOL> paramlist = [ paramlist ] <EOL> iparamlist = [ ] <EOL> for params in paramlist : <EOL> if ( '<STR_LIT>' in params and params [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> iparamlist . append ( params ) <EOL> else : <EOL> iterparams = [ p for p in params if hasattr ( params [ p ] , '<STR_LIT>' ) ] <EOL> if len ( iterparams ) > <NUM_LIT:0> : <EOL> self . mkdir ( os . path . join ( params [ '<STR_LIT:path>' ] , params [ '<STR_LIT:name>' ] ) ) <EOL> self . write_config_file ( params , os . path . join ( params [ '<STR_LIT:path>' ] , params [ '<STR_LIT:name>' ] ) ) <EOL> if '<STR_LIT>' in params and params [ '<STR_LIT>' ] == '<STR_LIT:list>' : <EOL> iterfunc = itertools . izip <EOL> elif ( '<STR_LIT>' not in params ) or ( '<STR_LIT>' in params and params [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> iterfunc = itertools . product <EOL> else : <EOL> raise SystemExit ( "<STR_LIT>" % params [ '<STR_LIT>' ] ) <EOL> for il in iterfunc ( * [ params [ p ] for p in iterparams ] ) : <EOL> par = params . copy ( ) <EOL> converted = str ( zip ( iterparams , map ( convert_param_to_dirname , il ) ) ) <EOL> par [ '<STR_LIT:name>' ] = par [ '<STR_LIT:name>' ] + '<STR_LIT:/>' + re . sub ( "<STR_LIT>" , '<STR_LIT>' , converted ) <EOL> for i , ip in enumerate ( iterparams ) : <EOL> par [ ip ] = il [ i ] <EOL> iparamlist . append ( par ) <EOL> else : <EOL> iparamlist . append ( params ) <EOL> return iparamlist <EOL> def create_dir ( self , params , delete = False ) : <EOL> """<STR_LIT>""" <EOL> fullpath = os . path . join ( params [ '<STR_LIT:path>' ] , params [ '<STR_LIT:name>' ] ) <EOL> self . mkdir ( fullpath ) <EOL> if delete : <EOL> os . system ( '<STR_LIT>' % fullpath ) <EOL> self . write_config_file ( params , fullpath ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . options . browse or self . options . browse_big or self . options . progress : <EOL> self . browse ( ) <EOL> raise SystemExit <EOL> paramlist = [ ] <EOL> for exp in self . cfgparser . sections ( ) : <EOL> if not self . options . experiments or exp in self . options . experiments : <EOL> params = self . items_to_params ( self . cfgparser . items ( exp ) ) <EOL> params [ '<STR_LIT:name>' ] = exp <EOL> paramlist . append ( params ) <EOL> self . do_experiment ( paramlist ) <EOL> def do_experiment ( self , params ) : <EOL> """<STR_LIT>""" <EOL> paramlist = self . expand_param_list ( params ) <EOL> for pl in paramlist : <EOL> if ( '<STR_LIT:name>' in pl ) and ( '<STR_LIT>' in pl ) and ( '<STR_LIT>' in pl ) and ( '<STR_LIT:path>' in pl ) : <EOL> self . create_dir ( pl , self . options . delete ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> return False <EOL> explist = [ ] <EOL> for p in paramlist : <EOL> explist . extend ( zip ( [ self ] * p [ '<STR_LIT>' ] , [ p ] * p [ '<STR_LIT>' ] , xrange ( p [ '<STR_LIT>' ] ) ) ) <EOL> if self . options . ncores == <NUM_LIT:1> : <EOL> for e in explist : <EOL> mp_runrep ( e ) <EOL> else : <EOL> pool = Pool ( processes = self . options . ncores ) <EOL> pool . map ( mp_runrep , explist ) <EOL> return True <EOL> def run_rep ( self , params , rep ) : <EOL> """<STR_LIT>""" <EOL> name = params [ '<STR_LIT:name>' ] <EOL> fullpath = os . path . join ( params [ '<STR_LIT:path>' ] , params [ '<STR_LIT:name>' ] ) <EOL> logname = os . path . join ( fullpath , '<STR_LIT>' % rep ) <EOL> restore = <NUM_LIT:0> <EOL> if os . path . exists ( logname ) : <EOL> logfile = open ( logname , '<STR_LIT:r>' ) <EOL> lines = logfile . readlines ( ) <EOL> logfile . close ( ) <EOL> if '<STR_LIT>' in params and len ( lines ) == params [ '<STR_LIT>' ] : <EOL> return False <EOL> if not self . restore_supported : <EOL> os . remove ( logname ) <EOL> restore = <NUM_LIT:0> <EOL> else : <EOL> restore = len ( lines ) <EOL> self . reset ( params , rep ) <EOL> if restore : <EOL> logfile = open ( logname , '<STR_LIT:a>' ) <EOL> self . restore_state ( params , rep , restore ) <EOL> else : <EOL> logfile = open ( logname , '<STR_LIT:w>' ) <EOL> for it in xrange ( restore , params [ '<STR_LIT>' ] ) : <EOL> dic = self . iterate ( params , rep , it ) <EOL> if self . restore_supported : <EOL> self . save_state ( params , rep , it ) <EOL> for k in dic : <EOL> if '<STR_LIT:U+0020>' in k : <EOL> newk = k . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> dic [ newk ] = dic [ k ] <EOL> del dic [ k ] <EOL> if k not in self . key_warning_issued : <EOL> print "<STR_LIT>" % ( k , newk ) <EOL> self . key_warning_issued . append ( k ) <EOL> outstr = '<STR_LIT:U+0020>' . join ( map ( lambda x : '<STR_LIT>' % ( x [ <NUM_LIT:0> ] , str ( x [ <NUM_LIT:1> ] ) ) , dic . items ( ) ) ) <EOL> logfile . write ( outstr + '<STR_LIT:\n>' ) <EOL> logfile . flush ( ) <EOL> logfile . close ( ) <EOL> self . finalize ( params , rep ) <EOL> def reset ( self , params , rep ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def iterate ( self , params , rep , n ) : <EOL> """<STR_LIT>""" <EOL> ret = { '<STR_LIT>' : n , '<STR_LIT>' : rep } <EOL> return ret <EOL> def finalize ( self , params , rep ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def save_state ( self , params , rep , n ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def restore_state ( self , params , rep , n ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
