<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> from collections import defaultdict <EOL> from . VBLearnAlg import VBLearnAlg <EOL> from . . util import EPS , discrete_single_draw <EOL> import logging <EOL> Log = logging . getLogger ( '<STR_LIT>' ) <EOL> Log . setLevel ( logging . DEBUG ) <EOL> class BirthProposalError ( ValueError ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( type ( self ) , self ) . __init__ ( * args , ** kwargs ) <EOL> def subsample_data ( DataObj , LP , targetCompID , targetProbThr = <NUM_LIT:0.1> , <EOL> maxTargetObs = <NUM_LIT:100> , maxTargetSize = <NUM_LIT:100> , randstate = np . random , <EOL> subsampleroutine = '<STR_LIT>' , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in LP : <EOL> if subsampleroutine == '<STR_LIT>' : <EOL> mask = LP [ '<STR_LIT>' ] [ : , targetCompID ] > targetProbThr <EOL> TargetData = DataObj . select_subset_by_mask ( wordMask = np . flatnonzero ( mask ) , <EOL> doTrackFullSize = False ) <EOL> elif subsampleroutine == '<STR_LIT>' : <EOL> if '<STR_LIT>' in LP : <EOL> Ndk = LP [ '<STR_LIT>' ] <EOL> else : <EOL> Ndk = LP [ '<STR_LIT>' ] . copy ( ) <EOL> Ndk /= np . sum ( Ndk , axis = <NUM_LIT:1> ) [ : , np . newaxis ] <EOL> docmask = Ndk [ : , targetCompID ] > targetProbThr <EOL> docIDs = np . flatnonzero ( docmask ) <EOL> if docIDs . size == <NUM_LIT:0> : <EOL> return None <EOL> randstate . shuffle ( docIDs ) <EOL> docIDs = docIDs [ : maxTargetSize ] <EOL> TargetData = DataObj . select_subset_by_mask ( docMask = docIDs , <EOL> doTrackFullSize = False ) <EOL> else : <EOL> mask = LP [ '<STR_LIT>' ] [ : , targetCompID ] > targetProbThr <EOL> objIDs = np . flatnonzero ( mask ) <EOL> randstate . shuffle ( objIDs ) <EOL> targetObjIDs = objIDs [ : maxTargetObs ] <EOL> TargetData = DataObj . select_subset_by_mask ( targetObjIDs , <EOL> doTrackFullSize = False ) <EOL> return TargetData <EOL> def run_birth_move ( curModel , targetData , SS , randstate = np . random , <EOL> ktarget = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if SS is None : <EOL> msg = "<STR_LIT>" <EOL> raise BirthProposalError ( msg ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> assert hasattr ( targetData , '<STR_LIT>' ) <EOL> import BirthMoveTopicModel <EOL> freshSS = BirthMoveTopicModel . create_expanded_suff_stats ( <EOL> targetData , curModel , SS , <EOL> randstate = randstate , ** kwargs ) <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> else : <EOL> freshModel = curModel . copy ( ) <EOL> freshSS = learn_fresh_model ( freshModel , targetData , <EOL> randstate = randstate , curModel = curModel , ** kwargs ) <EOL> Kfresh = freshSS . K <EOL> Kold = curModel . obsModel . K <EOL> assert Kold == SS . K <EOL> if hasattr ( SS , '<STR_LIT>' ) : <EOL> freshSS . setField ( '<STR_LIT>' , np . zeros ( freshSS . K ) , dims = '<STR_LIT>' ) <EOL> newSS = SS . copy ( ) <EOL> if kwargs [ '<STR_LIT>' ] and kwargs [ '<STR_LIT>' ] : <EOL> newSS . insertEmptyComps ( Kfresh - Kold ) <EOL> newSS += freshSS <EOL> birthCompIDs = range ( Kold , Kfresh ) <EOL> modifiedCompIDs = range ( Kfresh ) <EOL> else : <EOL> newSS . insertComps ( freshSS ) <EOL> birthCompIDs = range ( Kold , Kold + Kfresh ) <EOL> modifiedCompIDs = range ( Kold , Kold + Kfresh ) <EOL> newModel = curModel . copy ( ) <EOL> newModel . update_global_params ( newSS ) <EOL> MoveInfo = dict ( didAddNew = True , <EOL> msg = '<STR_LIT>' % ( len ( birthCompIDs ) ) , <EOL> modifiedCompIDs = modifiedCompIDs , <EOL> birthCompIDs = birthCompIDs , <EOL> extraSS = freshSS ) <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] : <EOL> viz_birth_proposal_2D ( curModel , newModel , ktarget , birthCompIDs ) <EOL> return newModel , newSS , MoveInfo <EOL> except BirthProposalError , e : <EOL> MoveInfo = dict ( didAddNew = False , msg = str ( e ) , <EOL> birthCompIDs = [ ] , modifiedCompIDs = [ ] ) <EOL> return curModel , SS , MoveInfo <EOL> def learn_fresh_model ( freshModel , targetData , Kmax = <NUM_LIT> , Kfresh = <NUM_LIT:10> , <EOL> freshInitName = '<STR_LIT>' , freshAlgName = '<STR_LIT>' , <EOL> nFreshLap = <NUM_LIT:50> , randstate = np . random , <EOL> doRemoveRedundant = True , <EOL> doSimpleThrTest = False , curModel = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> Kfresh = np . minimum ( Kfresh , Kmax - freshModel . obsModel . K ) <EOL> if Kfresh < <NUM_LIT:2> : <EOL> raise BirthProposalError ( '<STR_LIT>' % ( Kmax ) ) <EOL> seed = randstate . choice ( xrange ( <NUM_LIT> ) ) <EOL> freshModel . init_global_params ( targetData , K = Kfresh , <EOL> seed = seed , initname = freshInitName ) <EOL> LearnAlgConstructor = dict ( ) <EOL> LearnAlgConstructor [ '<STR_LIT>' ] = VBLearnAlg <EOL> algP = dict ( nLap = nFreshLap , convergeSigFig = <NUM_LIT:6> , startLap = <NUM_LIT:0> ) <EOL> outP = dict ( saveEvery = - <NUM_LIT:1> , traceEvery = - <NUM_LIT:1> , printEvery = - <NUM_LIT:1> ) <EOL> learnAlg = LearnAlgConstructor [ freshAlgName ] ( savedir = None , <EOL> algParams = algP , outputParams = outP , seed = seed ) <EOL> targetLP , evBound = learnAlg . fit ( freshModel , targetData ) <EOL> targetSS = freshModel . get_global_suff_stats ( targetData , targetLP ) <EOL> if doSimpleThrTest : <EOL> Nthr = np . maximum ( <NUM_LIT:100> , <NUM_LIT> * targetData . nObs ) <EOL> rejectIDs = np . flatnonzero ( targetSS . N < Nthr ) <EOL> rejectIDs = np . sort ( rejectIDs ) [ : : - <NUM_LIT:1> ] <EOL> for kreject in rejectIDs : <EOL> targetSS . removeComp ( kreject ) <EOL> elif curModel is not None : <EOL> targetSS = clean_up_fresh_model ( targetData , curModel , freshModel , <EOL> randstate = randstate , ** kwargs ) <EOL> if targetSS . K < <NUM_LIT:2> : <EOL> msg = '<STR_LIT>' <EOL> raise BirthProposalError ( msg % ( targetData . nObs ) ) <EOL> if doRemoveRedundant : <EOL> targetSS = clean_up_expanded_suff_stats ( targetData , curModel , targetSS , <EOL> randstate = randstate , ** kwargs ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> didCreateNewComps = ( targetSS . K - curModel . obsModel . K ) >= <NUM_LIT:1> <EOL> else : <EOL> didCreateNewComps = targetSS . K >= <NUM_LIT:1> <EOL> if not didCreateNewComps : <EOL> msg = '<STR_LIT>' <EOL> raise BirthProposalError ( msg ) <EOL> return targetSS <EOL> def clean_up_expanded_suff_stats ( targetData , curModel , targetSS , <EOL> randstate = np . random , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> import MergeMove <EOL> Korig = curModel . allocModel . K <EOL> origLP = curModel . calc_local_params ( targetData ) <EOL> expandSS = curModel . get_global_suff_stats ( targetData , origLP ) <EOL> expandSS . insertComps ( targetSS ) <EOL> expandModel = curModel . copy ( ) <EOL> expandModel . update_global_params ( expandSS ) <EOL> expandLP = expandModel . calc_local_params ( targetData ) <EOL> expandSS = expandModel . get_global_suff_stats ( targetData , expandLP , <EOL> doPrecompEntropy = True , doPrecompMergeEntropy = True ) <EOL> Kexpand = expandSS . K <EOL> mPairIDs = MergeMove . preselect_all_merge_candidates ( <EOL> expandModel , expandSS , randstate = np . random , <EOL> preselectroutine = kwargs [ '<STR_LIT>' ] , <EOL> mergePerLap = kwargs [ '<STR_LIT>' ] * ( Kexpand - Korig ) , <EOL> compIDs = range ( Korig , Kexpand ) ) <EOL> mPairIDsOrig = [ x for x in mPairIDs ] <EOL> xModel , xSS , xEv , MTracker = MergeMove . run_many_merge_moves ( <EOL> expandModel , targetData , expandSS , <EOL> nMergeTrials = expandSS . K ** <NUM_LIT:2> , <EOL> mPairIDs = mPairIDs , <EOL> randstate = randstate , ** kwargs ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> viz_birth_proposal_2D ( expandModel , xModel , None , None , <EOL> title1 = '<STR_LIT>' , <EOL> title2 = '<STR_LIT>' ) <EOL> for x in MTracker . acceptedOrigIDs : <EOL> assert x in mPairIDsOrig <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> targetSS = xSS <EOL> targetSS . setELBOFieldsToZero ( ) <EOL> targetSS . setMergeFieldsToZero ( ) <EOL> else : <EOL> kBList = [ kB for kA , kB in MTracker . acceptedOrigIDs ] <EOL> if len ( kBList ) == targetSS . K : <EOL> msg = '<STR_LIT>' <EOL> raise BirthProposalError ( msg ) <EOL> for kB in reversed ( sorted ( kBList ) ) : <EOL> ktarget = kB - Korig <EOL> if ktarget >= <NUM_LIT:0> : <EOL> targetSS . removeComp ( ktarget ) <EOL> return targetSS <EOL> def clean_up_fresh_model ( targetData , curModel , freshModel , <EOL> randstate = np . random , ** mergeKwArgs ) : <EOL> '''<STR_LIT>''' <EOL> import MergeMove <EOL> for trial in xrange ( <NUM_LIT:10> ) : <EOL> targetLP = freshModel . calc_local_params ( targetData ) <EOL> targetSS = freshModel . get_global_suff_stats ( targetData , targetLP , <EOL> doPrecompEntropy = True , doPrecompMergeEntropy = True ) <EOL> prevK = targetSS . K <EOL> freshModel , targetSS , freshEvBound , MTracker = MergeMove . run_many_merge_moves ( <EOL> freshModel , targetData , targetSS , <EOL> nMergeTrials = targetSS . K ** <NUM_LIT:2> , <EOL> randstate = randstate , <EOL> ** mergeKwArgs ) <EOL> if targetSS . K == prevK : <EOL> break <EOL> if targetSS . K < <NUM_LIT:2> : <EOL> return targetSS <EOL> singleModel = curModel . copy ( ) <EOL> singleSS = targetSS . getComp ( <NUM_LIT:0> , doCollapseK1 = False ) <EOL> singleModel . update_global_params ( singleSS ) <EOL> singleLP = singleModel . calc_local_params ( targetData ) <EOL> singleSS = singleModel . get_global_suff_stats ( targetData , singleLP , <EOL> doPrecompEntropy = True ) <EOL> singleModel . update_global_params ( singleSS ) <EOL> singleEvBound = singleModel . calc_evidence ( SS = singleSS ) <EOL> improveEvBound = freshEvBound - singleEvBound <EOL> if improveEvBound <= <NUM_LIT:0> or improveEvBound < <NUM_LIT> * abs ( singleEvBound ) : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" % ( targetSS . K , freshEvBound ) <EOL> msg += "<STR_LIT>" % ( singleSS . K , singleEvBound ) <EOL> raise BirthProposalError ( msg ) <EOL> curLP = curModel . calc_local_params ( targetData ) <EOL> curSS = curModel . get_global_suff_stats ( targetData , curLP , doPrecompEntropy = True ) <EOL> curEvBound = curModel . calc_evidence ( SS = curSS ) <EOL> improveEvBound = freshEvBound - curEvBound <EOL> if improveEvBound <= <NUM_LIT:0> or improveEvBound < <NUM_LIT> * abs ( curEvBound ) : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" % ( targetSS . K , freshEvBound ) <EOL> msg += "<STR_LIT>" % ( curSS . K , curEvBound ) <EOL> raise BirthProposalError ( msg ) <EOL> return targetSS <EOL> def select_birth_component ( SS , K = None , targetSelectName = '<STR_LIT>' , <EOL> randstate = np . random , emptyTHR = <NUM_LIT:100> , <EOL> lapsSinceLastBirth = defaultdict ( int ) , <EOL> excludeList = list ( ) , doVerbose = False , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if SS is None : <EOL> targetSelectName = '<STR_LIT>' <EOL> assert K is not None <EOL> elif K is None : <EOL> K = SS . K <EOL> else : <EOL> assert K == SS . K <EOL> if len ( excludeList ) >= K : <EOL> raise BirthProposalError ( '<STR_LIT>' % ( K ) ) <EOL> ps = np . zeros ( K ) <EOL> if targetSelectName == '<STR_LIT>' : <EOL> ps = np . ones ( K ) <EOL> elif targetSelectName == '<STR_LIT>' : <EOL> if hasattr ( SS , '<STR_LIT>' ) : <EOL> ps = SS . Nmajor . copy ( ) <EOL> else : <EOL> ps = SS . N . copy ( ) <EOL> ps [ SS . N < emptyTHR ] = <NUM_LIT:0> <EOL> elif targetSelectName == '<STR_LIT>' : <EOL> lapDist = np . asarray ( [ lapsSinceLastBirth [ kk ] for kk in range ( K ) ] ) <EOL> ps = np . maximum ( lapDist + <NUM_LIT> , <NUM_LIT:0> ) <EOL> ps = ps * ps <EOL> elif targetSelectName == '<STR_LIT>' : <EOL> lapDist = np . asarray ( [ lapsSinceLastBirth [ kk ] for kk in range ( K ) ] ) <EOL> ps = np . maximum ( lapDist + <NUM_LIT> , <NUM_LIT:0> ) <EOL> if hasattr ( SS , '<STR_LIT>' ) : <EOL> ps = ps * SS . Nmajor <EOL> else : <EOL> ps = ps * SS . N <EOL> ps [ SS . N < emptyTHR ] = <NUM_LIT:0> <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' + targetSelectName ) <EOL> ps [ excludeList ] = <NUM_LIT:0> <EOL> if np . sum ( ps ) < EPS : <EOL> raise BirthProposalError ( '<STR_LIT>' ) <EOL> sortIDs = np . argsort ( ps ) [ : : - <NUM_LIT:1> ] <EOL> if doVerbose : <EOL> for kk in sortIDs [ : <NUM_LIT:6> ] : <EOL> print "<STR_LIT>" % ( kk , ps [ kk ] / sum ( ps ) , lapsSinceLastBirth [ kk ] , SS . N [ kk ] ) <EOL> kbirth = discrete_single_draw ( ps , randstate ) <EOL> return kbirth <EOL> def viz_birth_proposal_2D ( curModel , newModel , ktarget , freshCompIDs , <EOL> title1 = '<STR_LIT>' , <EOL> title2 = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> from . . viz import GaussViz , BarsViz <EOL> from matplotlib import pylab <EOL> fig = pylab . figure ( ) <EOL> h1 = pylab . subplot ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> if curModel . obsModel . __class__ . __name__ . count ( '<STR_LIT>' ) : <EOL> GaussViz . plotGauss2DFromHModel ( curModel , compsToHighlight = ktarget ) <EOL> else : <EOL> BarsViz . plotBarsFromHModel ( curModel , compsToHighlight = ktarget , figH = h1 ) <EOL> pylab . title ( title1 ) <EOL> h2 = pylab . subplot ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> if curModel . obsModel . __class__ . __name__ . count ( '<STR_LIT>' ) : <EOL> GaussViz . plotGauss2DFromHModel ( newModel , compsToHighlight = freshCompIDs ) <EOL> else : <EOL> BarsViz . plotBarsFromHModel ( newModel , compsToHighlight = freshCompIDs , figH = h2 ) <EOL> pylab . title ( title2 ) <EOL> pylab . show ( block = False ) <EOL> try : <EOL> x = raw_input ( '<STR_LIT>' ) <EOL> except KeyboardInterrupt : <EOL> import sys <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> pylab . close ( ) </s>
