<s> '''<STR_LIT>''' <EOL> from bnpy . ioutil import ModelWriter <EOL> from bnpy . util import closeAtMSigFigs , isEvenlyDivisibleFloat <EOL> import numpy as np <EOL> import time <EOL> import os <EOL> import logging <EOL> import scipy . io <EOL> Log = logging . getLogger ( '<STR_LIT>' ) <EOL> Log . setLevel ( logging . DEBUG ) <EOL> class LearnAlg ( object ) : <EOL> def __init__ ( self , savedir = None , seed = <NUM_LIT:0> , <EOL> algParams = dict ( ) , outputParams = dict ( ) , <EOL> onLapCompleteFunc = lambda : None , onFinishFunc = lambda : None , <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> if type ( savedir ) == str : <EOL> self . savedir = os . path . splitext ( savedir ) [ <NUM_LIT:0> ] <EOL> else : <EOL> self . savedir = None <EOL> self . seed = int ( seed ) <EOL> self . PRNG = np . random . RandomState ( self . seed ) <EOL> self . algParams = algParams <EOL> self . outputParams = outputParams <EOL> self . TraceLaps = set ( ) <EOL> self . evTrace = list ( ) <EOL> self . SavedIters = set ( ) <EOL> self . PrintIters = set ( ) <EOL> self . nObsProcessed = <NUM_LIT:0> <EOL> self . algParamsLP = dict ( ) <EOL> for k , v in algParams . items ( ) : <EOL> if k . count ( '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> self . algParamsLP [ k ] = v <EOL> def fit ( self , hmodel , Data ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def set_random_seed_at_lap ( self , lap ) : <EOL> '''<STR_LIT>''' <EOL> if isEvenlyDivisibleFloat ( lap , <NUM_LIT:1.0> ) : <EOL> self . PRNG = np . random . RandomState ( self . seed + int ( lap ) ) <EOL> def set_start_time_now ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . start_time = time . time ( ) <EOL> def add_nObs ( self , nObs ) : <EOL> '''<STR_LIT>''' <EOL> self . nObsProcessed += nObs <EOL> def get_elapsed_time ( self ) : <EOL> '''<STR_LIT>''' <EOL> return time . time ( ) - self . start_time <EOL> def buildRunInfo ( self , evBound , status , nLap = None ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( evBound = evBound , status = status , nLap = nLap , <EOL> evTrace = self . evTrace , lapTrace = self . TraceLaps ) <EOL> def hasMove ( self , moveName ) : <EOL> if moveName in self . algParams : <EOL> return True <EOL> return False <EOL> def verify_evidence ( self , evBound = <NUM_LIT> , prevBound = <NUM_LIT:0> , lapFrac = None ) : <EOL> '''<STR_LIT>''' <EOL> if np . isnan ( evBound ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if np . isinf ( prevBound ) : <EOL> return False <EOL> isIncreasing = prevBound <= evBound <EOL> M = self . algParams [ '<STR_LIT>' ] <EOL> isWithinTHR = closeAtMSigFigs ( prevBound , evBound , M = M ) <EOL> mLPkey = '<STR_LIT>' <EOL> if not isIncreasing and not isWithinTHR : <EOL> serious = True <EOL> if self . hasMove ( '<STR_LIT>' ) and ( len ( self . BirthCompIDs ) > <NUM_LIT:0> or len ( self . ModifiedCompIDs ) > <NUM_LIT:0> ) : <EOL> warnMsg = '<STR_LIT>' <EOL> warnMsg += '<STR_LIT>' <EOL> serious = False <EOL> elif mLPkey in self . algParams and not self . algParams [ mLPkey ] : <EOL> warnMsg = '<STR_LIT>' <EOL> warnMsg += '<STR_LIT>' <EOL> serious = False <EOL> else : <EOL> warnMsg = '<STR_LIT>' <EOL> warnMsg += '<STR_LIT>' % ( prevBound ) <EOL> warnMsg += '<STR_LIT>' % ( evBound ) <EOL> if lapFrac is None : <EOL> prefix = "<STR_LIT>" <EOL> else : <EOL> prefix = "<STR_LIT>" % ( lapFrac ) <EOL> if serious or not self . algParams [ '<STR_LIT>' ] : <EOL> Log . error ( prefix + warnMsg ) <EOL> return isWithinTHR <EOL> def save_state ( self , hmodel , iterid , lap , evBound , doFinal = False ) : <EOL> '''<STR_LIT>''' <EOL> traceEvery = self . outputParams [ '<STR_LIT>' ] <EOL> if traceEvery <= <NUM_LIT:0> : <EOL> traceEvery = - <NUM_LIT:1> <EOL> doTrace = isEvenlyDivisibleFloat ( lap , traceEvery ) or iterid < <NUM_LIT:3> <EOL> if traceEvery > <NUM_LIT:0> and ( doFinal or doTrace ) and lap not in self . TraceLaps : <EOL> self . evTrace . append ( evBound ) <EOL> self . TraceLaps . add ( lap ) <EOL> if self . savedir is None : <EOL> return <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( lap ) ) <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( evBound ) ) <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( self . nObsProcessed ) ) <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( self . get_elapsed_time ( ) ) ) <EOL> if self . hasMove ( '<STR_LIT>' ) or self . hasMove ( '<STR_LIT>' ) : <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( hmodel . obsModel . K ) ) <EOL> saveEvery = self . outputParams [ '<STR_LIT>' ] <EOL> if saveEvery <= <NUM_LIT:0> or self . savedir is None : <EOL> return <EOL> doSave = isEvenlyDivisibleFloat ( lap , saveEvery ) or iterid < <NUM_LIT:3> <EOL> if ( doFinal or doSave ) and iterid not in self . SavedIters : <EOL> self . SavedIters . add ( iterid ) <EOL> with open ( self . mkfile ( '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( lap ) ) <EOL> prefix = ModelWriter . makePrefixForLap ( lap ) <EOL> ModelWriter . save_model ( hmodel , self . savedir , prefix , <EOL> doSavePriorInfo = ( iterid < <NUM_LIT:1> ) , doLinkBest = True ) <EOL> def mkfile ( self , fname ) : <EOL> return os . path . join ( self . savedir , fname ) <EOL> def getFileWriteMode ( self ) : <EOL> if self . savedir is None : <EOL> return None <EOL> return '<STR_LIT:a>' <EOL> def plot_results ( self , hmodel , Data , LP ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def print_state ( self , hmodel , iterid , lap , evBound , doFinal = False , status = '<STR_LIT>' , rho = None ) : <EOL> printEvery = self . outputParams [ '<STR_LIT>' ] <EOL> if printEvery <= <NUM_LIT:0> : <EOL> return None <EOL> doPrint = iterid < <NUM_LIT:3> or isEvenlyDivisibleFloat ( lap , printEvery ) <EOL> if rho is None : <EOL> rhoStr = '<STR_LIT>' <EOL> else : <EOL> rhoStr = '<STR_LIT>' % ( rho ) <EOL> if iterid == lap : <EOL> lapStr = '<STR_LIT>' % ( lap ) <EOL> else : <EOL> lapStr = '<STR_LIT>' % ( lap ) <EOL> maxLapStr = '<STR_LIT>' % ( self . algParams [ '<STR_LIT>' ] + self . algParams [ '<STR_LIT>' ] ) <EOL> logmsg = '<STR_LIT>' <EOL> if self . __class__ . __name__ . count ( '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> if lap < self . algParams [ '<STR_LIT>' ] + <NUM_LIT:1.0> : <EOL> logmsg = '<STR_LIT>' <EOL> logmsg = logmsg % ( lapStr , <EOL> maxLapStr , <EOL> self . get_elapsed_time ( ) , <EOL> hmodel . allocModel . K , <EOL> evBound , <EOL> rhoStr ) <EOL> if ( doFinal or doPrint ) and iterid not in self . PrintIters : <EOL> self . PrintIters . add ( iterid ) <EOL> Log . info ( logmsg ) <EOL> if doFinal : <EOL> Log . info ( '<STR_LIT>' % ( status ) ) <EOL> def print_msg ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> Log . info ( msg ) <EOL> def isFirstBatch ( self , lapFrac ) : <EOL> '''<STR_LIT>''' <EOL> if self . lapFracInc == <NUM_LIT:1.0> : <EOL> isFirstBatch = True <EOL> else : <EOL> isFirstBatch = np . allclose ( lapFrac - np . floor ( lapFrac ) , self . lapFracInc ) <EOL> return isFirstBatch <EOL> def isLastBatch ( self , lapFrac ) : <EOL> '''<STR_LIT>''' <EOL> return lapFrac % <NUM_LIT:1> == <NUM_LIT:0> <EOL> def do_birth_at_lap ( self , lapFrac ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in self . algParams : <EOL> return False <EOL> nLapTotal = self . algParams [ '<STR_LIT>' ] <EOL> frac = self . algParams [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if lapFrac > nLapTotal : <EOL> return False <EOL> return ( nLapTotal <= <NUM_LIT:5> ) or ( lapFrac <= np . ceil ( frac * nLapTotal ) ) <EOL> def eval_custom_func ( self , hmodel , iterid , lapFrac ) : <EOL> '''<STR_LIT>''' <EOL> import ast <EOL> customFuncPath = self . algParams [ '<STR_LIT>' ] <EOL> customFuncArgs = self . algParams [ '<STR_LIT>' ] <EOL> nLapTotal = self . algParams [ '<STR_LIT>' ] <EOL> percentDone = lapFrac / nLapTotal <EOL> if customFuncPath is not None and customFuncPath != '<STR_LIT:None>' : <EOL> import sys <EOL> sys . path . append ( customFuncPath ) <EOL> import customFunc <EOL> if lapFrac % <NUM_LIT:1> != <NUM_LIT:0> : <EOL> customFunc . onBatchComplete ( hmodel , percentDone , customFuncArgs ) <EOL> elif lapFrac % <NUM_LIT:1> == <NUM_LIT:0> and lapFrac < nLapTotal : <EOL> customFunc . onLapComplete ( hmodel , percentDone , customFuncArgs ) <EOL> else : <EOL> customFunc . onAlgorithmComplete ( hmodel , percentDone , customFuncArgs ) </s>
