<s> """<STR_LIT>""" <EOL> from __future__ import with_statement , print_function <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> import sys <EOL> import os <EOL> import re <EOL> import warnings <EOL> import traceback <EOL> import shutil <EOL> try : <EOL> import dill as pickle <EOL> except ImportError : <EOL> import pickle <EOL> import numpy as np <EOL> from . import fitters <EOL> from . fitters import FitDriver , StepMonitor , ConsoleMonitor , nllf_scale <EOL> from . mapper import MPMapper , AMQPMapper , MPIMapper , SerialMapper <EOL> from . formatnum import format_uncertainty <EOL> from . import util <EOL> from . import initpop <EOL> from . import __version__ <EOL> from . import plugin <EOL> from . import options <EOL> from . util import pushdir <EOL> def install_plugin ( p ) : <EOL> """<STR_LIT>""" <EOL> for symbol in plugin . __all__ : <EOL> if hasattr ( p , symbol ) : <EOL> setattr ( plugin , symbol , getattr ( p , symbol ) ) <EOL> def load_model ( path , model_options = None ) : <EOL> """<STR_LIT>""" <EOL> from . fitproblem import load_problem <EOL> directory , filename = os . path . split ( path ) <EOL> with pushdir ( directory ) : <EOL> problem = plugin . load_model ( filename ) <EOL> if problem is None : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> problem = pickle . load ( open ( filename , '<STR_LIT:rb>' ) ) <EOL> else : <EOL> problem = load_problem ( filename , options = model_options ) <EOL> problem . model_reset ( ) <EOL> problem . path = os . path . abspath ( path ) <EOL> if not hasattr ( problem , '<STR_LIT:title>' ) : <EOL> problem . title = filename <EOL> problem . name , _ = os . path . splitext ( filename ) <EOL> problem . options = model_options <EOL> return problem <EOL> def preview ( problem ) : <EOL> """<STR_LIT>""" <EOL> import pylab <EOL> problem . show ( ) <EOL> problem . plot ( ) <EOL> pylab . show ( ) <EOL> def save_best ( fitdriver , problem , best ) : <EOL> """<STR_LIT>""" <EOL> problem . setp ( best ) <EOL> pardata = "<STR_LIT>" . join ( "<STR_LIT>" % ( name , value ) <EOL> for name , value in zip ( problem . labels ( ) , problem . getp ( ) ) ) <EOL> open ( problem . output_path + "<STR_LIT>" , '<STR_LIT>' ) . write ( pardata ) <EOL> fitdriver . save ( problem . output_path ) <EOL> with util . redirect_console ( problem . output_path + "<STR_LIT>" ) : <EOL> fitdriver . show ( ) <EOL> fitdriver . plot ( problem . output_path ) <EOL> fitdriver . show ( ) <EOL> PARS_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> def load_best ( problem , path ) : <EOL> """<STR_LIT>""" <EOL> targets = dict ( ( name , np . NaN ) for name in problem . labels ( ) ) <EOL> with open ( path , '<STR_LIT>' ) as fid : <EOL> for line in fid : <EOL> m = PARS_PATTERN . match ( line ) <EOL> label , value = m . group ( '<STR_LIT:label>' ) , float ( m . group ( '<STR_LIT:value>' ) ) <EOL> if label in targets : <EOL> targets [ label ] = value <EOL> values = [ targets [ label ] for label in problem . labels ( ) ] <EOL> problem . setp ( np . asarray ( values ) ) <EOL> recall_best = load_best <EOL> def store_overwrite_query_gui ( path ) : <EOL> """<STR_LIT>""" <EOL> import wx <EOL> msg_dlg = wx . MessageDialog ( None , path + "<STR_LIT>" , '<STR_LIT>' , <EOL> wx . YES_NO | wx . ICON_QUESTION ) <EOL> retCode = msg_dlg . ShowModal ( ) <EOL> msg_dlg . Destroy ( ) <EOL> if retCode != wx . ID_YES : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def store_overwrite_query ( path ) : <EOL> """<STR_LIT>""" <EOL> print ( path , "<STR_LIT>" ) <EOL> print ( <EOL> "<STR_LIT>" ) <EOL> ans = input ( "<STR_LIT>" ) <EOL> if ans not in ( "<STR_LIT:y>" , "<STR_LIT:Y>" , "<STR_LIT:yes>" ) : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def make_store ( problem , opts , exists_handler ) : <EOL> """<STR_LIT>""" <EOL> if opts . store : <EOL> problem . store = opts . store <EOL> problem . output_path = os . path . join ( problem . store , problem . name ) <EOL> if not opts . overwrite and os . path . exists ( problem . output_path + '<STR_LIT>' ) : <EOL> if opts . batch : <EOL> print ( <EOL> problem . store + "<STR_LIT>" , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> exists_handler ( problem . output_path ) <EOL> if not os . path . exists ( problem . store ) : <EOL> os . mkdir ( problem . store ) <EOL> shutil . copy2 ( problem . path , problem . store ) <EOL> if opts . batch : <EOL> sys . stdout = open ( problem . output_path + "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> def run_profiler ( problem , steps ) : <EOL> """<STR_LIT>""" <EOL> from . util import profile <EOL> p = initpop . random_init ( int ( steps ) , None , problem ) <EOL> profile ( lambda * args : list ( map ( * args ) ) , problem . nllf , p ) <EOL> def run_timer ( mapper , problem , steps ) : <EOL> """<STR_LIT>""" <EOL> import time <EOL> T0 = time . time ( ) <EOL> p = initpop . random_init ( int ( steps ) , None , problem ) <EOL> mapper ( p ) <EOL> print ( "<STR_LIT>" % ( <NUM_LIT:1000> * ( time . time ( ) - T0 ) / steps , ) ) <EOL> def start_remote_fit ( problem , options , queue , notify ) : <EOL> """<STR_LIT>""" <EOL> from jobqueue . client import connect <EOL> data = dict ( package = '<STR_LIT>' , <EOL> version = __version__ , <EOL> problem = pickle . dumps ( problem ) , <EOL> options = pickle . dumps ( options ) ) <EOL> request = dict ( service = '<STR_LIT>' , <EOL> version = __version__ , <EOL> notify = notify , <EOL> name = problem . title , <EOL> data = data ) <EOL> server = connect ( queue ) <EOL> job = server . submit ( request ) <EOL> return job <EOL> def initial_model ( opts ) : <EOL> """<STR_LIT>""" <EOL> if opts . seed is not None : <EOL> np . random . seed ( opts . seed ) <EOL> if opts . args : <EOL> problem = load_model ( opts . args [ <NUM_LIT:0> ] , opts . args [ <NUM_LIT:1> : ] ) <EOL> if opts . pars is not None : <EOL> load_best ( problem , opts . pars ) <EOL> if opts . simrandom : <EOL> problem . randomize ( ) <EOL> if opts . simulate or opts . simrandom : <EOL> noise = None if opts . noise == "<STR_LIT:data>" else float ( opts . noise ) <EOL> problem . simulate_data ( noise = noise ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( problem . summarize ( ) ) <EOL> print ( "<STR_LIT>" , problem . chisq ( ) ) <EOL> if opts . shake : <EOL> problem . randomize ( ) <EOL> else : <EOL> problem = None <EOL> return problem <EOL> def resynth ( fitdriver , problem , mapper , opts ) : <EOL> """<STR_LIT>""" <EOL> make_store ( problem , opts , exists_handler = store_overwrite_query ) <EOL> fid = open ( problem . output_path + "<STR_LIT>" , '<STR_LIT>' ) <EOL> fitdriver . mapper = mapper . start_mapper ( problem , opts . args ) <EOL> for i in range ( opts . resynth ) : <EOL> problem . resynth_data ( ) <EOL> best , fbest = fitdriver . fit ( ) <EOL> scale , err = nllf_scale ( problem ) <EOL> print ( "<STR_LIT>" % ( i , scale * fbest ) ) <EOL> fid . write ( '<STR_LIT>' % ( scale * fbest ) ) <EOL> fid . write ( '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % v for v in best ) ) <EOL> fid . write ( '<STR_LIT:\n>' ) <EOL> problem . restore_data ( ) <EOL> fid . close ( ) <EOL> def set_mplconfig ( appdatadir ) : <EOL> r"""<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> if hasattr ( sys , '<STR_LIT>' ) : <EOL> mplconfigdir = os . path . join ( <EOL> os . environ [ '<STR_LIT>' ] , appdatadir , '<STR_LIT>' ) <EOL> mplconfigdir = os . environ . setdefault ( '<STR_LIT>' , mplconfigdir ) <EOL> if not os . path . exists ( mplconfigdir ) : <EOL> os . makedirs ( mplconfigdir ) <EOL> def config_matplotlib ( backend = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( sys , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in os . environ : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" ) <EOL> if backend is None : <EOL> backend = '<STR_LIT>' <EOL> import matplotlib <EOL> if backend is not None : <EOL> matplotlib . use ( backend ) <EOL> matplotlib . interactive ( False ) <EOL> def beep ( ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> import winsound <EOL> winsound . MessageBeep ( winsound . MB_ICONEXCLAMATION ) <EOL> else : <EOL> print ( "<STR_LIT>" , file = sys . __stdout__ ) <EOL> def run_command ( c ) : <EOL> """<STR_LIT>""" <EOL> exec ( c , globals ( ) ) <EOL> def setup_logging ( ) : <EOL> import logging <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> def warn_with_traceback ( message , category , filename , lineno , file = None , line = None ) : <EOL> traceback . print_stack ( ) <EOL> log = file if hasattr ( file , '<STR_LIT>' ) else sys . stderr <EOL> log . write ( warnings . formatwarning ( message , category , filename , lineno , line ) ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> sys . argv . append ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> import runpy <EOL> sys . argv = sys . argv [ <NUM_LIT:2> : ] <EOL> runpy . run_module ( sys . argv [ <NUM_LIT:0> ] , run_name = "<STR_LIT:__main__>" ) <EOL> sys . exit ( ) <EOL> elif sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> import runpy <EOL> sys . argv = sys . argv [ <NUM_LIT:2> : ] <EOL> runpy . run_path ( sys . argv [ <NUM_LIT:0> ] , run_name = "<STR_LIT:__main__>" ) <EOL> sys . exit ( ) <EOL> elif sys . argv [ <NUM_LIT:1> ] == '<STR_LIT:-c>' : <EOL> run_command ( sys . argv [ <NUM_LIT:2> ] ) <EOL> sys . exit ( ) <EOL> elif sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> sys . argv = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> from IPython import start_ipython <EOL> sys . exit ( start_ipython ( ) ) <EOL> opts = options . getopts ( ) <EOL> setup_logging ( ) <EOL> if opts . edit : <EOL> from . gui . gui_app import main as gui <EOL> gui ( ) <EOL> return <EOL> if opts . batch or opts . remote or opts . noshow : <EOL> config_matplotlib ( backend = '<STR_LIT>' ) <EOL> else : <EOL> config_matplotlib ( ) <EOL> problem = initial_model ( opts ) <EOL> if opts . mpi : <EOL> MPIMapper . start_worker ( problem ) <EOL> mapper = MPIMapper <EOL> elif opts . parallel or opts . worker : <EOL> if opts . transport == '<STR_LIT>' : <EOL> mapper = AMQPMapper <EOL> elif opts . transport == '<STR_LIT>' : <EOL> mapper = MPMapper <EOL> elif opts . transport == '<STR_LIT>' : <EOL> mapper = CeleryMapper <EOL> else : <EOL> mapper = SerialMapper <EOL> if opts . worker : <EOL> mapper . start_worker ( problem ) <EOL> return <EOL> if np . isfinite ( float ( opts . time ) ) : <EOL> import time <EOL> start_time = time . time ( ) <EOL> stop_time = start_time + float ( opts . time ) * <NUM_LIT> <EOL> abort_test = lambda : time . time ( ) >= stop_time <EOL> else : <EOL> abort_test = lambda : False <EOL> fitdriver = FitDriver ( <EOL> opts . fit_config . selected_fitter , problem = problem , abort_test = abort_test , <EOL> ** opts . fit_config . selected_values ) <EOL> if opts . time_model : <EOL> run_timer ( mapper . start_mapper ( problem , opts . args ) , <EOL> problem , steps = int ( opts . steps ) ) <EOL> elif opts . profile : <EOL> run_profiler ( problem , steps = int ( opts . steps ) ) <EOL> elif opts . chisq : <EOL> if opts . cov : <EOL> print ( problem . cov ( ) ) <EOL> print ( "<STR_LIT>" , problem . chisq_str ( ) ) <EOL> elif opts . preview : <EOL> if opts . cov : <EOL> print ( problem . cov ( ) ) <EOL> preview ( problem ) <EOL> elif opts . resynth > <NUM_LIT:0> : <EOL> resynth ( fitdriver , problem , mapper , opts ) <EOL> elif opts . remote : <EOL> chisq = problem ( ) <EOL> print ( "<STR_LIT>" , chisq ) <EOL> job = start_remote_fit ( problem , opts , <EOL> queue = opts . queue , notify = opts . notify ) <EOL> print ( "<STR_LIT>" , job [ '<STR_LIT:id>' ] ) <EOL> else : <EOL> if opts . resume : <EOL> resume_path = os . path . join ( opts . resume , problem . name ) <EOL> else : <EOL> resume_path = None <EOL> make_store ( problem , opts , exists_handler = store_overwrite_query ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT:U+0020>" . join ( sys . argv ) ) <EOL> problem . show ( ) <EOL> if opts . stepmon : <EOL> fid = open ( problem . output_path + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fitdriver . monitors = [ ConsoleMonitor ( problem ) , <EOL> StepMonitor ( problem , fid , fields = [ '<STR_LIT>' , '<STR_LIT:value>' ] ) ] <EOL> fitdriver . mapper = mapper . start_mapper ( problem , opts . args ) <EOL> best , fbest = fitdriver . fit ( resume = resume_path ) <EOL> save_best ( fitdriver , problem , best ) <EOL> if opts . err or opts . cov : <EOL> fitdriver . show_err ( ) <EOL> if opts . cov : <EOL> np . set_printoptions ( linewidth = <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( problem . cov ( ) ) <EOL> print ( "<STR_LIT>" ) <EOL> if opts . entropy : <EOL> print ( "<STR_LIT>" ) <EOL> S , dS = fitdriver . entropy ( ) <EOL> print ( "<STR_LIT>" % format_uncertainty ( S , dS ) ) <EOL> mapper . stop_mapper ( fitdriver . mapper ) <EOL> if not opts . batch and not opts . mpi and not opts . noshow : <EOL> beep ( ) <EOL> import pylab <EOL> pylab . show ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
